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