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

  • Connection
  • DatabaseHelpers
  • Row
  • SqlLiteral
  • SqlPreprocessor
  • Statement

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 Connection $connection
 21:  * @property-write $fetchMode
 22:  * @package Nette\Database
 23:  */
 24: class Statement extends PDOStatement
 25: {
 26:     /** @var Connection */
 27:     private $connection;
 28: 
 29:     /** @var float */
 30:     private $time;
 31: 
 32:     /** @var array */
 33:     private $types;
 34: 
 35: 
 36: 
 37:     protected function __construct(Connection $connection)
 38:     {
 39:         $this->connection = $connection;
 40:         $this->setFetchMode(PDO::FETCH_CLASS, 'Row', array($this));
 41:     }
 42: 
 43: 
 44: 
 45:     /**
 46:      * @return Connection
 47:      */
 48:     public function getConnection()
 49:     {
 50:         return $this->connection;
 51:     }
 52: 
 53: 
 54: 
 55:     /**
 56:      * Executes statement.
 57:      * @param  array
 58:      * @return Statement  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:                 $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 DateTime53($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)) { // workaround for PHP bugs #53782, #54695
134:                 $col = 0;
135:                 while ($meta = $this->getColumnMeta($col++)) {
136:                     if (isset($meta['native_type'])) {
137:                         $this->types[$meta['name']] = DatabaseHelpers::detectType($meta['native_type']);
138:                     }
139:                 }
140:             }
141:         }
142:         return $this->types;
143:     }
144: 
145: 
146: 
147:     /**
148:      * @return float
149:      */
150:     public function getTime()
151:     {
152:         return $this->time;
153:     }
154: 
155: 
156: 
157:     /********************* misc tools ****************d*g**/
158: 
159: 
160: 
161:     /**
162:      * Displays complete result set as HTML table for debug purposes.
163:      * @return void
164:      */
165:     public function dump()
166:     {
167:         DatabaseHelpers::dumpResult($this);
168:     }
169: 
170: 
171: 
172:     /********************* Object behaviour ****************d*g**/
173: 
174: 
175: 
176:     /**
177:      * @return ClassReflection
178:      */
179:     public function getReflection()
180:     {
181:         return new ClassReflection($this);
182:     }
183: 
184: 
185: 
186:     public function __call($name, $args)
187:     {
188:         return ObjectMixin::call($this, $name, $args);
189:     }
190: 
191: 
192: 
193:     public function &__get($name)
194:     {
195:         return ObjectMixin::get($this, $name);
196:     }
197: 
198: 
199: 
200:     public function __set($name, $value)
201:     {
202:         return ObjectMixin::set($this, $name, $value);
203:     }
204: 
205: 
206: 
207:     public function __isset($name)
208:     {
209:         return ObjectMixin::has($this, $name);
210:     }
211: 
212: 
213: 
214:     public function __unset($name)
215:     {
216:         ObjectMixin::remove($this, $name);
217:     }
218: 
219: }
220: 
Nette Framework 2.0.7 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0