Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • NConnection
  • NDatabaseHelpers
  • NRow
  • NSqlLiteral
  • NSqlPreprocessor
  • NStatement

Interfaces

  • IReflection
  • ISupplementalDriver
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Database
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Represents a prepared statement / result set.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read NConnection $connection
 21:  * @property-write $fetchMode
 22:  * @package Nette\Database
 23:  */
 24: class NStatement extends PDOStatement
 25: {
 26:     /** @var NConnection */
 27:     private $connection;
 28: 
 29:     /** @var float */
 30:     private $time;
 31: 
 32:     /** @var array */
 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:      * @return NConnection
 45:      */
 46:     public function getConnection()
 47:     {
 48:         return $this->connection;
 49:     }
 50: 
 51: 
 52:     /**
 53:      * @return string
 54:      */
 55:     public function getQueryString()
 56:     {
 57:         return $this->queryString;
 58:     }
 59: 
 60: 
 61:     /**
 62:      * @return int
 63:      */
 64:     public function getColumnCount()
 65:     {
 66:         return $this->columnCount();
 67:     }
 68: 
 69: 
 70:     /**
 71:      * @return int
 72:      */
 73:     public function getRowCount()
 74:     {
 75:         return $this->rowCount();
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Executes statement.
 81:      * @param  array
 82:      * @return self
 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)); // $this->connection->onQuery() in PHP 5.3
103: 
104:         return $this;
105:     }
106: 
107: 
108:     /**
109:      * Fetches into an array where the 1st column is a key and all subsequent columns are values.
110:      * @return array
111:      */
112:     public function fetchPairs()
113:     {
114:         return $this->fetchAll(PDO::FETCH_KEY_PAIR); // since PHP 5.2.3
115:     }
116: 
117: 
118:     /**
119:      * Fetches single field.
120:      * @return mixed|FALSE
121:      */
122:     public function fetchField($column = 0)
123:     {
124:         $row = $this->fetch();
125:         return $row ? $row[$column] : FALSE;
126:     }
127: 
128: 
129:     /**
130:      * Normalizes result row.
131:      * @param  array
132:      * @return array
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)) { // workaround for PHP bugs #53782, #54695
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:      * @return float
181:      */
182:     public function getTime()
183:     {
184:         return $this->time;
185:     }
186: 
187: 
188:     /********************* misc tools ****************d*g**/
189: 
190: 
191:     /**
192:      * Displays complete result set as HTML table for debug purposes.
193:      * @return void
194:      */
195:     public function dump()
196:     {
197:         NDatabaseHelpers::dumpResult($this);
198:     }
199: 
200: 
201:     /********************* NObject behaviour ****************d*g**/
202: 
203: 
204:     /**
205:      * @return NClassReflection
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: 
Nette Framework 2.0.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0