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 connection between PHP and a database server.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property       IReflection          $databaseReflection
 21:  * @property-read  ISupplementalDriver  $supplementalDriver
 22:  * @property-read  string               $dsn
 23:  * @package Nette\Database
 24:  */
 25: class NConnection extends PDO
 26: {
 27:     /** @var string */
 28:     private $dsn;
 29: 
 30:     /** @var ISupplementalDriver */
 31:     private $driver;
 32: 
 33:     /** @var NSqlPreprocessor */
 34:     private $preprocessor;
 35: 
 36:     /** @var IReflection */
 37:     private $databaseReflection;
 38: 
 39:     /** @var NCache */
 40:     private $cache;
 41: 
 42:     /** @var array of function(Statement $result, $params); Occurs after query is executed */
 43:     public $onQuery;
 44: 
 45: 
 46:     public function __construct($dsn, $username = NULL, $password = NULL, array $options = NULL, $driverClass = NULL)
 47:     {
 48:         parent::__construct($this->dsn = $dsn, $username, $password, $options);
 49:         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 50:         $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('NStatement', array($this)));
 51: 
 52:         $driverClass = ($tmp=$driverClass) ? $tmp : 'N' . ucfirst(str_replace('sql', 'Sql', $this->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver';
 53:         $this->driver = new $driverClass($this, (array) $options);
 54:         $this->preprocessor = new NSqlPreprocessor($this);
 55:     }
 56: 
 57: 
 58:     public function getDsn()
 59:     {
 60:         return $this->dsn;
 61:     }
 62: 
 63: 
 64:     /** @return ISupplementalDriver */
 65:     public function getSupplementalDriver()
 66:     {
 67:         return $this->driver;
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Sets database reflection.
 73:      * @return self
 74:      */
 75:     public function setDatabaseReflection(IReflection $databaseReflection)
 76:     {
 77:         $databaseReflection->setConnection($this);
 78:         $this->databaseReflection = $databaseReflection;
 79:         return $this;
 80:     }
 81: 
 82: 
 83:     /** @return IReflection */
 84:     public function getDatabaseReflection()
 85:     {
 86:         if (!$this->databaseReflection) {
 87:             $this->setDatabaseReflection(new NConventionalReflection);
 88:         }
 89:         return $this->databaseReflection;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Sets cache storage engine.
 95:      * @return self
 96:      */
 97:     public function setCacheStorage(ICacheStorage $storage = NULL)
 98:     {
 99:         $this->cache = $storage ? new NCache($storage, 'Nette.Database.' . md5($this->dsn)) : NULL;
100:         return $this;
101:     }
102: 
103: 
104:     public function getCache()
105:     {
106:         return $this->cache;
107:     }
108: 
109: 
110:     /**
111:      * Generates and executes SQL query.
112:      * @param  string  statement
113:      * @param  mixed   [parameters, ...]
114:      * @return NStatement
115:      */
116:     public function query($statement)
117:     {
118:         $args = func_get_args();
119:         return $this->queryArgs(array_shift($args), $args);
120:     }
121: 
122: 
123:     /**
124:      * Generates and executes SQL query.
125:      * @param  string  statement
126:      * @param  mixed   [parameters, ...]
127:      * @return int     number of affected rows
128:      */
129:     public function exec($statement)
130:     {
131:         $args = func_get_args();
132:         return $this->queryArgs(array_shift($args), $args)->rowCount();
133:     }
134: 
135: 
136:     /**
137:      * @param  string  statement
138:      * @param  array
139:      * @return NStatement
140:      */
141:     public function queryArgs($statement, $params)
142:     {
143:         foreach ($params as $value) {
144:             if (is_array($value) || is_object($value)) {
145:                 $need = TRUE; break;
146:             }
147:         }
148:         if (isset($need) && $this->preprocessor !== NULL) {
149:             list($statement, $params) = $this->preprocessor->process($statement, $params);
150:         }
151: 
152:         return $this->prepare($statement)->execute($params);
153:     }
154: 
155: 
156:     /********************* shortcuts ****************d*g**/
157: 
158: 
159:     /**
160:      * Shortcut for query()->fetch()
161:      * @param  string  statement
162:      * @param  mixed   [parameters, ...]
163:      * @return NRow
164:      */
165:     public function fetch($args)
166:     {
167:         $args = func_get_args();
168:         return $this->queryArgs(array_shift($args), $args)->fetch();
169:     }
170: 
171: 
172:     /**
173:      * Shortcut for query()->fetchField()
174:      * @param  string  statement
175:      * @param  mixed   [parameters, ...]
176:      * @return mixed
177:      */
178:     public function fetchField($args)
179:     {
180:         $args = func_get_args();
181:         return $this->queryArgs(array_shift($args), $args)->fetchField();
182:     }
183: 
184: 
185:     /**
186:      * Shortcut for query()->fetchColumn()
187:      * @param  string  statement
188:      * @param  mixed   [parameters, ...]
189:      * @return mixed
190:      */
191:     public function fetchColumn($args)
192:     {
193:         $args = func_get_args();
194:         return $this->queryArgs(array_shift($args), $args)->fetchColumn();
195:     }
196: 
197: 
198:     /**
199:      * Shortcut for query()->fetchPairs()
200:      * @param  string  statement
201:      * @param  mixed   [parameters, ...]
202:      * @return array
203:      */
204:     public function fetchPairs($args)
205:     {
206:         $args = func_get_args();
207:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
208:     }
209: 
210: 
211:     /**
212:      * Shortcut for query()->fetchAll()
213:      * @param  string  statement
214:      * @param  mixed   [parameters, ...]
215:      * @return array
216:      */
217:     public function fetchAll($args)
218:     {
219:         $args = func_get_args();
220:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
221:     }
222: 
223: 
224:     /********************* selector ****************d*g**/
225: 
226: 
227:     /**
228:      * Creates selector for table.
229:      * @param  string
230:      * @return NTableSelection
231:      */
232:     public function table($table)
233:     {
234:         return new NTableSelection($table, $this);
235:     }
236: 
237: 
238:     /********************* NObject behaviour ****************d*g**/
239: 
240: 
241:     /**
242:      * @return NClassReflection
243:      */
244:     public function getReflection()
245:     {
246:         return new NClassReflection($this);
247:     }
248: 
249: 
250:     public function __call($name, $args)
251:     {
252:         return NObjectMixin::call($this, $name, $args);
253:     }
254: 
255: 
256:     public function &__get($name)
257:     {
258:         return NObjectMixin::get($this, $name);
259:     }
260: 
261: 
262:     public function __set($name, $value)
263:     {
264:         return NObjectMixin::set($this, $name, $value);
265:     }
266: 
267: 
268:     public function __isset($name)
269:     {
270:         return NObjectMixin::has($this, $name);
271:     }
272: 
273: 
274:     public function __unset($name)
275:     {
276:         NObjectMixin::remove($this, $name);
277:     }
278: 
279: }
280: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0