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