Overview

Namespaces

  • cli_db
    • propel
      • map
      • om
  • cli_import
  • LoggedPDO
  • None
  • PHP
  • webservices
    • cart
    • combisearch
    • details
      • annotations
        • feature
    • graphs
      • barplot
      • genome
    • listing
    • queue

Classes

  • PDO
  • PDOStatement
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * @author Lorenz Weber <mail@phryneas.de>
  5:  * @copyright (c) 2013, Lorenz Weber
  6:  * @package loggedPDO
  7:  * 
  8:  * The MIT License (MIT)
  9:  * 
 10:  * @copyright (c) 2013, Lorenz Weber
 11:  * 
 12:  * Permission is hereby granted, free of charge, to any person obtaining a copy
 13:  * of this software and associated documentation files (the "Software"), to deal
 14:  * in the Software without restriction, including without limitation the rights
 15:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 16:  * copies of the Software, and to permit persons to whom the Software is
 17:  * furnished to do so, subject to the following conditions:
 18:  *
 19:  * The above copyright notice and this permission notice shall be included in
 20:  * all copies or substantial portions of the Software.
 21:  *
 22:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 23:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 24:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 25:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 26:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 27:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 28:  * THE SOFTWARE.
 29:  */
 30: 
 31: namespace LoggedPDO;
 32: 
 33: /**
 34:  * extended PDOStatement that logs query, parameters and run time on execution.
 35:  * @see \LoggedPDO\PDO
 36:  */
 37: class PDOStatement extends \PDOStatement {
 38: 
 39:     /**
 40:      * The respective \LoggedPDO\PDO instance that created this statement. Will be set by \LoggedPDO\PDO on creation.
 41:      */
 42:     public $pdo;
 43:     private $boundParams = array();
 44: 
 45:     private function __construct() {
 46:         
 47:     }
 48: 
 49:     private static $PDO_PLACEHOLDER_NONE = 0;
 50:     private static $PDO_PLACEHOLDER_NAMED = 1;
 51:     private static $PDO_PLACEHOLDER_POSITIONAL = 2;
 52: 
 53:     /**
 54:      * {@inheritdoc}
 55:      * When execute is called record the time it takes and then log the query
 56:      * Parameters will be replaced and logged, but if your query is really weird, this might fail.
 57:      * in this case
 58:      * @see \LoggedPDO\PDO::$log_replace_params
 59:      */
 60:     public function execute($bound_input_params = null) {
 61:         $query = $this->queryString;
 62: 
 63:         if ($bound_input_params == null) {
 64:             $params = $this->boundParams;
 65:         }
 66:         else {
 67:             $params = $bound_input_params;
 68:         }
 69: 
 70:         if ($this->pdo->log_replace_params) {
 71:             $query_type = self::$PDO_PLACEHOLDER_NONE;
 72:             if (preg_match('/[^:?][?][^:?]/', $query) || preg_match('/[^:?][?]/', $query)) {
 73:                 $query_type |= self::$PDO_PLACEHOLDER_POSITIONAL;
 74:             }
 75: 
 76:             if (preg_match('/[^:?][:]([0-9A-Za-z]+)/', $query)) {
 77:                 $query_type |= self::$PDO_PLACEHOLDER_NAMED;
 78:             }
 79:             
 80: 
 81:             if ($query_type == (self::$PDO_PLACEHOLDER_NAMED | self::$PDO_PLACEHOLDER_POSITIONAL)) {
 82:                 throw new \PDOException('mixed named and positional parameters');
 83:             }
 84: 
 85:             foreach ($params as $pname => $pvalue) {
 86:                 if ($query_type == self::$PDO_PLACEHOLDER_POSITIONAL)
 87:                     $query = preg_replace("/\?/", $this->pdo->quote($pvalue), $query, 1);
 88:                 else if ($query_type == self::$PDO_PLACEHOLDER_NAMED)
 89:                     $query = preg_replace("/($pname)/", $this->pdo->quote($pvalue), $query, 1);
 90:             }
 91:         }
 92: 
 93:         $start = microtime(true);
 94:         $ex = null;
 95:         try {
 96:             $result = parent::execute($bound_input_params);
 97:         } catch (\PDOException $e) {
 98:             $ex = $e;
 99:         }
100:         $time = microtime(true) - $start;
101: 
102:         $this->pdo->log($query, round($time * 1000, 3), $params);
103: 
104: 
105:         if ($ex != null)
106:             throw $ex;
107:         return $result;
108:     }
109: 
110:     /**
111:      * {@inheritdoc}
112:      */
113:     public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null) {
114:         if (is_string($parameter) && strpos($parameter, ':') === false)
115:             $this->boundParams[':' . $parameter] = &$variable;
116:         else
117:             $this->boundParams[$parameter] = &$variable;
118: 
119:         return parent::bindParam($parameter, $variable, $data_type, $length, $driver_options);
120:     }
121: 
122:     /**
123:      * {@inheritdoc}
124:      */
125:     public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) {
126:         if (is_string($parameter) && strpos($parameter, ':') === false)
127:             $this->boundParams[':' . $parameter] = $value;
128:         else
129:             $this->boundParams[$parameter] = $value;
130: 
131:         return parent::bindValue($parameter, $value, $data_type);
132:     }
133: 
134: }
135: 
136: ?>
137: 
tbro API documentation generated by ApiGen 2.8.0