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