Namespaces

  • 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

  • Connection
  • Helpers
  • Row
  • SqlLiteral
  • SqlPreprocessor
  • Statement

Interfaces

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