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

Classes

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

Interfaces

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