Packages

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

Classes

  • NConnection
  • NRow
  • NSqlLiteral
  • NSqlPreprocessor
  • NStatement

Interfaces

  • 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, 2011 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:  * @package Nette\Database
 20:  */
 21: class NStatement extends PDOStatement
 22: {
 23:     /** @var NConnection */
 24:     private $connection;
 25: 
 26:     /** @var float */
 27:     public $time;
 28: 
 29:     /** @var array */
 30:     private $types;
 31: 
 32: 
 33: 
 34:     protected function __construct(NConnection $connection)
 35:     {
 36:         $this->connection = $connection;
 37:         $this->setFetchMode(PDO::FETCH_CLASS, 'NRow', array($this));
 38:     }
 39: 
 40: 
 41: 
 42:     /**
 43:      * @return NConnection
 44:      */
 45:     public function getConnection()
 46:     {
 47:         return $this->connection;
 48:     }
 49: 
 50: 
 51: 
 52:     /**
 53:      * Executes statement.
 54:      * @param  array
 55:      * @return NStatement  provides a fluent interface
 56:      */
 57:     public function execute($params = array())
 58:     {
 59:         static $types = array('boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,
 60:             'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL);
 61: 
 62:         foreach ($params as $key => $value) {
 63:             $type = gettype($value);
 64:             $this->bindValue(is_int($key) ? $key + 1 : $key, $value, isset($types[$type]) ? $types[$type] : PDO::PARAM_STR);
 65:         }
 66: 
 67:         $time = microtime(TRUE);
 68:         try {
 69:             parent::execute();
 70:         } catch (PDOException $e) {
 71:             $e->queryString = $this->queryString;
 72:             throw $e;
 73:         }
 74:         $this->time = microtime(TRUE) - $time;
 75:         $this->connection->__call('onQuery', array($this, $params)); // $this->connection->onQuery() in PHP 5.3
 76: 
 77:         return $this;
 78:     }
 79: 
 80: 
 81: 
 82:     /**
 83:      * Fetches into an array where the 1st column is a key and all subsequent columns are values.
 84:      * @return array
 85:      */
 86:     public function fetchPairs()
 87:     {
 88:         return $this->fetchAll(PDO::FETCH_KEY_PAIR); // since PHP 5.2.3
 89:     }
 90: 
 91: 
 92: 
 93:     /**
 94:      * Normalizes result row.
 95:      * @param  array
 96:      * @return array
 97:      */
 98:     public function normalizeRow($row)
 99:     {
100:         if ($this->types === NULL) {
101:             $this->types = array();
102:             if ($this->connection->getSupplementalDriver()->supports['meta']) { // workaround for PHP bugs #53782, #54695
103:                 $col = 0;
104:                 foreach ($row as $key => $foo) {
105:                     $type = $this->getColumnMeta($col++);
106:                     if (isset($type['native_type'])) {
107:                         $this->types[$key] = NDatabaseReflection::detectType($type['native_type']);
108:                     }
109:                 }
110:             }
111:         }
112: 
113:         foreach ($this->types as $key => $type) {
114:             $value = $row[$key];
115:             if ($value === NULL || $value === FALSE || $type === NDatabaseReflection::FIELD_TEXT) {
116: 
117:             } elseif ($type === NDatabaseReflection::FIELD_INTEGER) {
118:                 $row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
119: 
120:             } elseif ($type === NDatabaseReflection::FIELD_FLOAT) {
121:                 $row[$key] = (string) ($tmp = (float) $value) === $value ? $tmp : $value;
122: 
123:             } elseif ($type === NDatabaseReflection::FIELD_BOOL) {
124:                 $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
125:             }
126:         }
127: 
128:         return $this->connection->getSupplementalDriver()->normalizeRow($row, $this);
129:     }
130: 
131: 
132: 
133:     /********************* misc tools ****************d*g**/
134: 
135: 
136: 
137:     /**
138:      * Displays complete result set as HTML table for debug purposes.
139:      * @return void
140:      */
141:     public function dump()
142:     {
143:         echo "\n<table class=\"dump\">\n<caption>" . htmlSpecialChars($this->queryString) . "</caption>\n";
144:         if (!$this->columnCount()) {
145:             echo "\t<tr>\n\t\t<th>Affected rows:</th>\n\t\t<td>", $this->rowCount(), "</td>\n\t</tr>\n</table>\n";
146:             return;
147:         }
148:         $i = 0;
149:         foreach ($this as $row) {
150:             if ($i === 0) {
151:                 echo "<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
152:                 foreach ($row as $col => $foo) {
153:                     echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
154:                 }
155:                 echo "\t</tr>\n</thead>\n<tbody>\n";
156:             }
157:             echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
158:             foreach ($row as $col) {
159:                 //if (is_object($col)) $col = $col->__toString();
160:                 echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
161:             }
162:             echo "\t</tr>\n";
163:             $i++;
164:         }
165: 
166:         if ($i === 0) {
167:             echo "\t<tr>\n\t\t<td><em>empty result set</em></td>\n\t</tr>\n</table>\n";
168:         } else {
169:             echo "</tbody>\n</table>\n";
170:         }
171:     }
172: 
173: 
174: 
175:     /********************* NObject behaviour ****************d*g**/
176: 
177: 
178: 
179:     /**
180:      * @return NClassReflection
181:      */
182:     public function getReflection()
183:     {
184:         return new NClassReflection($this);
185:     }
186: 
187: 
188: 
189:     public function __call($name, $args)
190:     {
191:         return NObjectMixin::call($this, $name, $args);
192:     }
193: 
194: 
195: 
196:     public function &__get($name)
197:     {
198:         return NObjectMixin::get($this, $name);
199:     }
200: 
201: 
202: 
203:     public function __set($name, $value)
204:     {
205:         return NObjectMixin::set($this, $name, $value);
206:     }
207: 
208: 
209: 
210:     public function __isset($name)
211:     {
212:         return NObjectMixin::has($this, $name);
213:     }
214: 
215: 
216: 
217:     public function __unset($name)
218:     {
219:         NObjectMixin::remove($this, $name);
220:     }
221: 
222: }
223: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0