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