[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace DebugBar\DataCollector\PDO; 4 5 /** 6 * Holds information about a statement 7 */ 8 class TracedStatement 9 { 10 protected $sql; 11 12 protected $rowCount; 13 14 protected $parameters; 15 16 protected $startTime; 17 18 protected $endTime; 19 20 protected $duration; 21 22 protected $startMemory; 23 24 protected $endMemory; 25 26 protected $memoryDelta; 27 28 protected $exception; 29 30 /** 31 * @param string $sql 32 * @param array $params 33 * @param string $preparedId 34 */ 35 public function __construct($sql, array $params = [], $preparedId = null) 36 { 37 $this->sql = $sql; 38 $this->parameters = $this->checkParameters($params); 39 $this->preparedId = $preparedId; 40 } 41 42 /** 43 * @param null $startTime 44 * @param null $startMemory 45 */ 46 public function start($startTime = null, $startMemory = null) 47 { 48 $this->startTime = $startTime ?: microtime(true); 49 $this->startMemory = $startMemory ?: memory_get_usage(false); 50 } 51 52 /** 53 * @param \Exception|null $exception 54 * @param int $rowCount 55 * @param float $endTime 56 * @param int $endMemory 57 */ 58 public function end(\Exception $exception = null, $rowCount = 0, $endTime = null, $endMemory = null) 59 { 60 $this->endTime = $endTime ?: microtime(true); 61 $this->duration = $this->endTime - $this->startTime; 62 $this->endMemory = $endMemory ?: memory_get_usage(false); 63 $this->memoryDelta = $this->endMemory - $this->startMemory; 64 $this->exception = $exception; 65 $this->rowCount = $rowCount; 66 } 67 68 /** 69 * Check parameters for illegal (non UTF-8) strings, like Binary data. 70 * 71 * @param array $params 72 * @return array 73 */ 74 public function checkParameters($params) 75 { 76 foreach ($params as &$param) { 77 if (!mb_check_encoding($param, 'UTF-8')) { 78 $param = '[BINARY DATA]'; 79 } 80 } 81 return $params; 82 } 83 84 /** 85 * Returns the SQL string used for the query, without filled parameters 86 * 87 * @return string 88 */ 89 public function getSql() 90 { 91 return $this->sql; 92 } 93 94 /** 95 * Returns the SQL string with any parameters used embedded 96 * 97 * @param string $quotationChar 98 * @return string 99 */ 100 public function getSqlWithParams($quotationChar = '<>') 101 { 102 if (($l = strlen($quotationChar)) > 1) { 103 $quoteLeft = substr($quotationChar, 0, $l / 2); 104 $quoteRight = substr($quotationChar, $l / 2); 105 } else { 106 $quoteLeft = $quoteRight = $quotationChar; 107 } 108 109 $sql = $this->sql; 110 111 $cleanBackRefCharMap = ['%' => '%%', '$' => '$%', '\\' => '\\%']; 112 113 foreach ($this->parameters as $k => $v) { 114 115 $backRefSafeV = strtr($v, $cleanBackRefCharMap); 116 117 $v = "$quoteLeft$backRefSafeV$quoteRight"; 118 119 if (is_numeric($k)) { 120 $marker = "\?"; 121 } else { 122 $marker = (preg_match("/^:/", $k)) ? $k : ":" . $k; 123 } 124 125 $matchRule = "/({$marker}(?!\w))(?=(?:[^$quotationChar]|[$quotationChar][^$quotationChar]*[$quotationChar])*$)/"; 126 for ($i = 0; $i <= mb_substr_count($sql, $k); $i++) { 127 $sql = preg_replace($matchRule, $v, $sql, 1); 128 } 129 } 130 131 $sql = strtr($sql, array_flip($cleanBackRefCharMap)); 132 133 return $sql; 134 } 135 136 /** 137 * Returns the number of rows affected/returned 138 * 139 * @return int 140 */ 141 public function getRowCount() 142 { 143 return $this->rowCount; 144 } 145 146 /** 147 * Returns an array of parameters used with the query 148 * 149 * @return array 150 */ 151 public function getParameters() 152 { 153 $params = []; 154 foreach ($this->parameters as $name => $param) { 155 $params[$name] = htmlentities($param, ENT_QUOTES, 'UTF-8', false); 156 } 157 return $params; 158 } 159 160 /** 161 * Returns the prepared statement id 162 * 163 * @return string 164 */ 165 public function getPreparedId() 166 { 167 return $this->preparedId; 168 } 169 170 /** 171 * Checks if this is a prepared statement 172 * 173 * @return boolean 174 */ 175 public function isPrepared() 176 { 177 return $this->preparedId !== null; 178 } 179 180 /** 181 * @return float 182 */ 183 public function getStartTime() 184 { 185 return $this->startTime; 186 } 187 188 /** 189 * @return float 190 */ 191 public function getEndTime() 192 { 193 return $this->endTime; 194 } 195 196 /** 197 * Returns the duration in seconds + microseconds of the execution 198 * 199 * @return float 200 */ 201 public function getDuration() 202 { 203 return $this->duration; 204 } 205 206 /** 207 * @return int 208 */ 209 public function getStartMemory() 210 { 211 return $this->startMemory; 212 } 213 214 /** 215 * @return int 216 */ 217 public function getEndMemory() 218 { 219 return $this->endMemory; 220 } 221 222 /** 223 * Returns the memory usage during the execution 224 * 225 * @return int 226 */ 227 public function getMemoryUsage() 228 { 229 return $this->memoryDelta; 230 } 231 232 /** 233 * Checks if the statement was successful 234 * 235 * @return boolean 236 */ 237 public function isSuccess() 238 { 239 return $this->exception === null; 240 } 241 242 /** 243 * Returns the exception triggered 244 * 245 * @return \Exception 246 */ 247 public function getException() 248 { 249 return $this->exception; 250 } 251 252 /** 253 * Returns the exception's code 254 * 255 * @return int|string 256 */ 257 public function getErrorCode() 258 { 259 return $this->exception !== null ? $this->exception->getCode() : 0; 260 } 261 262 /** 263 * Returns the exception's message 264 * 265 * @return string 266 */ 267 public function getErrorMessage() 268 { 269 return $this->exception !== null ? $this->exception->getMessage() : ''; 270 } 271 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |