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: class NStatement extends PDOStatement
25: {
26:
27: private $connection;
28:
29:
30: private $time;
31:
32:
33: private $types;
34:
35:
36:
37: protected function __construct(NConnection $connection)
38: {
39: $this->connection = $connection;
40: $this->setFetchMode(PDO::FETCH_CLASS, 'NRow', array($this));
41: }
42:
43:
44:
45: 46: 47:
48: public function getConnection()
49: {
50: return $this->connection;
51: }
52:
53:
54:
55: 56: 57: 58: 59:
60: public function execute($params = array())
61: {
62: static $types = array('boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,
63: 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL);
64:
65: foreach ($params as $key => $value) {
66: $type = gettype($value);
67: $this->bindValue(is_int($key) ? $key + 1 : $key, $value, isset($types[$type]) ? $types[$type] : PDO::PARAM_STR);
68: }
69:
70: $time = microtime(TRUE);
71: try {
72: parent::execute();
73: } catch (PDOException $e) {
74: $e->queryString = $this->queryString;
75: throw $e;
76: }
77: $this->time = microtime(TRUE) - $time;
78: $this->connection->__call('onQuery', array($this, $params));
79:
80: return $this;
81: }
82:
83:
84:
85: 86: 87: 88:
89: public function fetchPairs()
90: {
91: return $this->fetchAll(PDO::FETCH_KEY_PAIR);
92: }
93:
94:
95:
96: 97: 98: 99: 100:
101: public function normalizeRow($row)
102: {
103: foreach ($this->detectColumnTypes() as $key => $type) {
104: $value = $row[$key];
105: if ($value === NULL || $value === FALSE || $type === IReflection::FIELD_TEXT) {
106:
107: } elseif ($type === IReflection::FIELD_INTEGER) {
108: $row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
109:
110: } elseif ($type === IReflection::FIELD_FLOAT) {
111: $value = strpos($value, '.') === FALSE ? $value : rtrim(rtrim($value, '0'), '.');
112: $float = (float) $value;
113: $row[$key] = (string) $float === $value ? $float : $value;
114:
115: } elseif ($type === IReflection::FIELD_BOOL) {
116: $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
117:
118: } elseif ($type === IReflection::FIELD_DATETIME || $type === IReflection::FIELD_DATE || $type === IReflection::FIELD_TIME) {
119: $row[$key] = new NDateTime53($value);
120:
121: }
122: }
123:
124: return $this->connection->getSupplementalDriver()->normalizeRow($row, $this);
125: }
126:
127:
128:
129: private function detectColumnTypes()
130: {
131: if ($this->types === NULL) {
132: $this->types = array();
133: if ($this->connection->getSupplementalDriver()->isSupported(ISupplementalDriver::SUPPORT_COLUMNS_META)) {
134: $col = 0;
135: while ($meta = $this->getColumnMeta($col++)) {
136: if (isset($meta['native_type'])) {
137: $this->types[$meta['name']] = NDatabaseHelpers::detectType($meta['native_type']);
138: }
139: }
140: }
141: }
142: return $this->types;
143: }
144:
145:
146:
147: 148: 149:
150: public function getTime()
151: {
152: return $this->time;
153: }
154:
155:
156:
157:
158:
159:
160:
161: 162: 163: 164:
165: public function dump()
166: {
167: NDatabaseHelpers::dumpResult($this);
168: }
169:
170:
171:
172:
173:
174:
175:
176: 177: 178:
179: public function getReflection()
180: {
181: return new NClassReflection($this);
182: }
183:
184:
185:
186: public function __call($name, $args)
187: {
188: return NObjectMixin::call($this, $name, $args);
189: }
190:
191:
192:
193: public function &__get($name)
194: {
195: return NObjectMixin::get($this, $name);
196: }
197:
198:
199:
200: public function __set($name, $value)
201: {
202: return NObjectMixin::set($this, $name, $value);
203: }
204:
205:
206:
207: public function __isset($name)
208: {
209: return NObjectMixin::has($this, $name);
210: }
211:
212:
213:
214: public function __unset($name)
215: {
216: NObjectMixin::remove($this, $name);
217: }
218:
219: }
220: