Namespaces

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