Packages

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

Classes

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