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
  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: 
 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:      * @return NConnection
 47:      */
 48:     public function getConnection()
 49:     {
 50:         return $this->connection;
 51:     }
 52: 
 53: 
 54: 
 55:     /**
 56:      * Executes statement.
 57:      * @param  array
 58:      * @return NStatement  provides a fluent interface
 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)); // $this->connection->onQuery() in PHP 5.3
 79: 
 80:         return $this;
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Fetches into an array where the 1st column is a key and all subsequent columns are values.
 87:      * @return array
 88:      */
 89:     public function fetchPairs()
 90:     {
 91:         return $this->fetchAll(PDO::FETCH_KEY_PAIR); // since PHP 5.2.3
 92:     }
 93: 
 94: 
 95: 
 96:     /**
 97:      * Normalizes result row.
 98:      * @param  array
 99:      * @return array
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:                 $row[$key] = (string) ($tmp = (float) $value) === $value ? $tmp : $value;
112: 
113:             } elseif ($type === IReflection::FIELD_BOOL) {
114:                 $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
115: 
116:             } elseif ($type === IReflection::FIELD_DATETIME || $type === IReflection::FIELD_DATE || $type === IReflection::FIELD_TIME) {
117:                 $row[$key] = new NDateTime53($value);
118: 
119:             }
120:         }
121: 
122:         return $this->connection->getSupplementalDriver()->normalizeRow($row, $this);
123:     }
124: 
125: 
126: 
127:     private function detectColumnTypes()
128:     {
129:         if ($this->types === NULL) {
130:             $this->types = array();
131:             if ($this->connection->getSupplementalDriver()->isSupported(ISupplementalDriver::SUPPORT_COLUMNS_META)) { // workaround for PHP bugs #53782, #54695
132:                 $col = 0;
133:                 while ($meta = $this->getColumnMeta($col++)) {
134:                     if (isset($meta['native_type'])) {
135:                         $this->types[$meta['name']] = NDatabaseHelpers::detectType($meta['native_type']);
136:                     }
137:                 }
138:             }
139:         }
140:         return $this->types;
141:     }
142: 
143: 
144: 
145:     /**
146:      * @return float
147:      */
148:     public function getTime()
149:     {
150:         return $this->time;
151:     }
152: 
153: 
154: 
155:     /********************* misc tools ****************d*g**/
156: 
157: 
158: 
159:     /**
160:      * Displays complete result set as HTML table for debug purposes.
161:      * @return void
162:      */
163:     public function dump()
164:     {
165:         NDatabaseHelpers::dumpResult($this);
166:     }
167: 
168: 
169: 
170:     /********************* NObject behaviour ****************d*g**/
171: 
172: 
173: 
174:     /**
175:      * @return NClassReflection
176:      */
177:     public function getReflection()
178:     {
179:         return new NClassReflection($this);
180:     }
181: 
182: 
183: 
184:     public function __call($name, $args)
185:     {
186:         return NObjectMixin::call($this, $name, $args);
187:     }
188: 
189: 
190: 
191:     public function &__get($name)
192:     {
193:         return NObjectMixin::get($this, $name);
194:     }
195: 
196: 
197: 
198:     public function __set($name, $value)
199:     {
200:         return NObjectMixin::set($this, $name, $value);
201:     }
202: 
203: 
204: 
205:     public function __isset($name)
206:     {
207:         return NObjectMixin::has($this, $name);
208:     }
209: 
210: 
211: 
212:     public function __unset($name)
213:     {
214:         NObjectMixin::remove($this, $name);
215:     }
216: 
217: }
218: 
Nette Framework 2.0.6 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0