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