1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30:
31: namespace LoggedPDO;
32:
33: 34: 35: 36:
37: class PDOStatement extends \PDOStatement {
38:
39: 40: 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: 55: 56: 57: 58: 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: 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: 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: