[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/DataCollector/PDO/ -> TraceablePDO.php (source)

   1  <?php
   2  
   3  namespace DebugBar\DataCollector\PDO;
   4  
   5  use PDO;
   6  use PDOException;
   7  use DebugBar\DataCollector\PDO\TraceablePDOStatement;
   8  
   9  /**
  10   * A PDO proxy which traces statements
  11   */
  12  class TraceablePDO extends PDO
  13  {
  14      /** @var PDO */
  15      protected $pdo;
  16  
  17      /** @var TracedStatement[] */
  18      protected $executedStatements = [];
  19  
  20      public function __construct(PDO $pdo)
  21      {
  22          $this->pdo = $pdo;
  23          $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [TraceablePDOStatement::class, [$this]]);
  24      }
  25  
  26      /**
  27       * Initiates a transaction
  28           *
  29       * @link   http://php.net/manual/en/pdo.begintransaction.php
  30       * @return bool TRUE on success or FALSE on failure.
  31       */
  32      public function beginTransaction()
  33      {
  34          return $this->pdo->beginTransaction();
  35      }
  36  
  37      /**
  38       * Commits a transaction
  39       *
  40       * @link   http://php.net/manual/en/pdo.commit.php
  41       * @return bool TRUE on success or FALSE on failure.
  42       */
  43      public function commit()
  44      {
  45          return $this->pdo->commit();
  46      }
  47  
  48      /**
  49       * Fetch extended error information associated with the last operation on the database handle
  50       *
  51       * @link   http://php.net/manual/en/pdo.errorinfo.php
  52       * @return array PDO::errorInfo returns an array of error information
  53       */
  54      public function errorCode()
  55      {
  56          return $this->pdo->errorCode();
  57      }
  58  
  59      /**
  60       * Fetch extended error information associated with the last operation on the database handle
  61       *
  62       * @link   http://php.net/manual/en/pdo.errorinfo.php
  63       * @return array PDO::errorInfo returns an array of error information
  64       */
  65      public function errorInfo()
  66      {
  67          return $this->pdo->errorInfo();
  68      }
  69  
  70      /**
  71       * Execute an SQL statement and return the number of affected rows
  72       *
  73       * @link   http://php.net/manual/en/pdo.exec.php
  74       * @param  string   $statement
  75       * @return int|bool PDO::exec returns the number of rows that were modified or deleted by the
  76       * SQL statement you issued. If no rows were affected, PDO::exec returns 0. This function may
  77       * return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
  78       * Please read the section on Booleans for more information
  79       */
  80      public function exec($statement)
  81      {
  82          return $this->profileCall('exec', $statement, func_get_args());
  83      }
  84  
  85      /**
  86       * Retrieve a database connection attribute
  87       *
  88       * @link   http://php.net/manual/en/pdo.getattribute.php
  89       * @param  int   $attribute One of the PDO::ATTR_* constants
  90       * @return mixed A successful call returns the value of the requested PDO attribute.
  91       * An unsuccessful call returns null.
  92       */
  93      public function getAttribute($attribute)
  94      {
  95          return $this->pdo->getAttribute($attribute);
  96      }
  97  
  98      /**
  99       * Checks if inside a transaction
 100       *
 101       * @link   http://php.net/manual/en/pdo.intransaction.php
 102       * @return bool TRUE if a transaction is currently active, and FALSE if not.
 103       */
 104      public function inTransaction()
 105      {
 106          return $this->pdo->inTransaction();
 107      }
 108  
 109      /**
 110       * Returns the ID of the last inserted row or sequence value
 111       *
 112       * @link   http://php.net/manual/en/pdo.lastinsertid.php
 113       * @param  string $name [optional]
 114       * @return string If a sequence name was not specified for the name parameter, PDO::lastInsertId
 115       * returns a string representing the row ID of the last row that was inserted into the database.
 116       */
 117      public function lastInsertId($name = null)
 118      {
 119          return $this->pdo->lastInsertId($name);
 120      }
 121  
 122      /**
 123       * Prepares a statement for execution and returns a statement object
 124     *
 125       * @link   http://php.net/manual/en/pdo.prepare.php
 126       * @param  string $statement This must be a valid SQL statement template for the target DB server.
 127       * @param  array  $driver_options [optional] This array holds one or more key=&gt;value pairs to
 128       * set attribute values for the PDOStatement object that this method returns.
 129       * @return TraceablePDOStatement|bool If the database server successfully prepares the statement,
 130     * PDO::prepare returns a PDOStatement object. If the database server cannot successfully prepare
 131     * the statement, PDO::prepare returns FALSE or emits PDOException (depending on error handling).
 132       */
 133      public function prepare($statement, $driver_options = [])
 134      {
 135          return $this->pdo->prepare($statement, $driver_options);
 136      }
 137  
 138      /**
 139       * Executes an SQL statement, returning a result set as a PDOStatement object
 140     *
 141       * @link   http://php.net/manual/en/pdo.query.php
 142       * @param  string $statement
 143       * @param  int $fetchMode
 144       * @param  mixed ...$fetchModeArgs
 145       * @return TraceablePDOStatement|bool PDO::query returns a PDOStatement object, or FALSE on
 146     * failure.
 147     */
 148      public function query($statement, $fetchMode = null, ...$fetchModeArgs)
 149      {
 150          return $this->profileCall('query', $statement, func_get_args());
 151      }
 152  
 153      /**
 154       * Quotes a string for use in a query.
 155       *
 156       * @link   http://php.net/manual/en/pdo.quote.php
 157       * @param  string $string The string to be quoted.
 158       * @param  int    $parameter_type [optional] Provides a data type hint for drivers that have
 159       * alternate quoting styles.
 160       * @return string|bool A quoted string that is theoretically safe to pass into an SQL statement.
 161       * Returns FALSE if the driver does not support quoting in this way.
 162       */
 163      public function quote($string, $parameter_type = PDO::PARAM_STR)
 164      {
 165          return $this->pdo->quote($string, $parameter_type);
 166      }
 167  
 168      /**
 169       * Rolls back a transaction
 170       *
 171       * @link   http://php.net/manual/en/pdo.rollback.php
 172       * @return bool TRUE on success or FALSE on failure.
 173       */
 174      public function rollBack()
 175      {
 176          return $this->pdo->rollBack();
 177      }
 178  
 179      /**
 180       * Set an attribute
 181       *
 182       * @link   http://php.net/manual/en/pdo.setattribute.php
 183       * @param  int $attribute
 184       * @param  mixed $value
 185       * @return bool TRUE on success or FALSE on failure.
 186       */
 187      public function setAttribute($attribute, $value)
 188      {
 189          return $this->pdo->setAttribute($attribute, $value);
 190      }
 191  
 192      /**
 193       * Profiles a call to a PDO method
 194       *
 195       * @param  string $method
 196       * @param  string $sql
 197       * @param  array  $args
 198       * @return mixed  The result of the call
 199       */
 200      protected function profileCall($method, $sql, array $args)
 201      {
 202          $trace = new TracedStatement($sql);
 203          $trace->start();
 204  
 205          $ex = null;
 206          try {
 207              $result = $this->__call($method, $args);
 208          } catch (PDOException $e) {
 209              $ex = $e;
 210          }
 211  
 212          if ($this->pdo->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_EXCEPTION && $result === false) {
 213              $error = $this->pdo->errorInfo();
 214              $ex = new PDOException($error[2], $error[0]);
 215          }
 216  
 217          $trace->end($ex);
 218          $this->addExecutedStatement($trace);
 219  
 220          if ($this->pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_EXCEPTION && $ex !== null) {
 221              throw $ex;
 222          }
 223          return $result;
 224      }
 225  
 226      /**
 227       * Adds an executed TracedStatement
 228       *
 229       * @param TracedStatement $stmt
 230       */
 231      public function addExecutedStatement(TracedStatement $stmt)
 232      {
 233          $this->executedStatements[] = $stmt;
 234      }
 235  
 236      /**
 237       * Returns the accumulated execution time of statements
 238       *
 239       * @return int
 240       */
 241      public function getAccumulatedStatementsDuration()
 242      {
 243          return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); });
 244      }
 245  
 246      /**
 247       * Returns the peak memory usage while performing statements
 248       *
 249       * @return int
 250       */
 251      public function getMemoryUsage()
 252      {
 253          return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); });
 254      }
 255  
 256      /**
 257       * Returns the peak memory usage while performing statements
 258       *
 259       * @return int
 260       */
 261      public function getPeakMemoryUsage()
 262      {
 263          return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; });
 264      }
 265  
 266      /**
 267       * Returns the list of executed statements as TracedStatement objects
 268       *
 269       * @return TracedStatement[]
 270       */
 271      public function getExecutedStatements()
 272      {
 273          return $this->executedStatements;
 274      }
 275  
 276      /**
 277       * Returns the list of failed statements
 278       *
 279       * @return TracedStatement[]
 280       */
 281      public function getFailedExecutedStatements()
 282      {
 283          return array_filter($this->executedStatements, function ($s) { return !$s->isSuccess(); });
 284      }
 285  
 286      /**
 287       * @param $name
 288       * @return mixed
 289       */
 290      public function __get($name)
 291      {
 292          return $this->pdo->$name;
 293      }
 294  
 295      /**
 296       * @param $name
 297       * @param $value
 298       */
 299      public function __set($name, $value)
 300      {
 301          $this->pdo->$name = $value;
 302      }
 303  
 304      /**
 305       * @param $name
 306       * @param $args
 307       * @return mixed
 308       */
 309      public function __call($name, $args)
 310      {
 311          return call_user_func_array([$this->pdo, $name], $args);
 312      }
 313  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer