Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Connection
  • Context
  • Helpers
  • ResultSet
  • Row
  • SqlLiteral
  • SqlPreprocessor

Interfaces

  • IReflection
  • IRow
  • IRowContainer
  • 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 (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Database;
  9: 
 10: use Nette;
 11: use PDO;
 12: 
 13: 
 14: /**
 15:  * Represents a connection between PHP and a database server.
 16:  *
 17:  * @author     David Grudl
 18:  *
 19:  * @property-read  ISupplementalDriver  $supplementalDriver
 20:  * @property-read  string               $dsn
 21:  * @property-read  PDO                  $pdo
 22:  */
 23: class Connection extends Nette\Object
 24: {
 25:     /** @var array of function (Connection $connection); Occurs after connection is established */
 26:     public $onConnect;
 27: 
 28:     /** @var array of function (Connection $connection, ResultSet|Exception $result); Occurs after query is executed */
 29:     public $onQuery;
 30: 
 31:     /** @var array */
 32:     private $params;
 33: 
 34:     /** @var array */
 35:     private $options;
 36: 
 37:     /** @var ISupplementalDriver */
 38:     private $driver;
 39: 
 40:     /** @var SqlPreprocessor */
 41:     private $preprocessor;
 42: 
 43:     /** @var Context */
 44:     private $context;
 45: 
 46:     /** @var PDO */
 47:     private $pdo;
 48: 
 49: 
 50:     public function __construct($dsn, $user = NULL, $password = NULL, array $options = NULL)
 51:     {
 52:         if (func_num_args() > 4) { // compatibility
 53:             $options['driverClass'] = func_get_arg(4);
 54:         }
 55:         $this->params = array($dsn, $user, $password);
 56:         $this->options = (array) $options;
 57: 
 58:         if (empty($options['lazy'])) {
 59:             $this->connect();
 60:         }
 61:     }
 62: 
 63: 
 64:     public function connect()
 65:     {
 66:         if ($this->pdo) {
 67:             return;
 68:         }
 69:         $this->pdo = new PDO($this->params[0], $this->params[1], $this->params[2], $this->options);
 70:         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 71: 
 72:         $class = empty($this->options['driverClass'])
 73:             ? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
 74:             : $this->options['driverClass'];
 75:         $this->driver = new $class($this, $this->options);
 76:         $this->preprocessor = new SqlPreprocessor($this);
 77:         $this->onConnect($this);
 78:     }
 79: 
 80: 
 81:     /** @return string */
 82:     public function getDsn()
 83:     {
 84:         return $this->params[0];
 85:     }
 86: 
 87: 
 88:     /** @return PDO */
 89:     public function getPdo()
 90:     {
 91:         $this->connect();
 92:         return $this->pdo;
 93:     }
 94: 
 95: 
 96:     /** @return ISupplementalDriver */
 97:     public function getSupplementalDriver()
 98:     {
 99:         $this->connect();
100:         return $this->driver;
101:     }
102: 
103: 
104:     /**
105:      * @param  string  sequence object
106:      * @return string
107:      */
108:     public function getInsertId($name = NULL)
109:     {
110:         return $this->getPdo()->lastInsertId($name);
111:     }
112: 
113: 
114:     /**
115:      * @param  string  string to be quoted
116:      * @param  int     data type hint
117:      * @return string
118:      */
119:     public function quote($string, $type = PDO::PARAM_STR)
120:     {
121:         return $this->getPdo()->quote($string, $type);
122:     }
123: 
124: 
125:     /** @deprecated */
126:     function beginTransaction()
127:     {
128:         $this->queryArgs('::beginTransaction', array());
129:     }
130: 
131: 
132:     /** @deprecated */
133:     function commit()
134:     {
135:         $this->queryArgs('::commit', array());
136:     }
137: 
138: 
139:     /** @deprecated */
140:     public function rollBack()
141:     {
142:         $this->queryArgs('::rollBack', array());
143:     }
144: 
145: 
146:     /** @deprecated */
147:     public function query($statement)
148:     {
149:         $args = func_get_args();
150:         return $this->queryArgs(array_shift($args), $args);
151:     }
152: 
153: 
154:     /** @deprecated */
155:     function queryArgs($statement, array $params)
156:     {
157:         $this->connect();
158:         if ($params) {
159:             array_unshift($params, $statement);
160:             list($statement, $params) = $this->preprocessor->process($params);
161:         }
162: 
163:         try {
164:             $result = new ResultSet($this, $statement, $params);
165:         } catch (\PDOException $e) {
166:             $e->queryString = $statement;
167:             $this->onQuery($this, $e);
168:             throw $e;
169:         }
170:         $this->onQuery($this, $result);
171:         return $result;
172:     }
173: 
174: 
175:     /********************* shortcuts ****************d*g**/
176: 
177: 
178:     /** @deprecated */
179:     function fetch($args)
180:     {
181:         $args = func_get_args();
182:         return $this->queryArgs(array_shift($args), $args)->fetch();
183:     }
184: 
185: 
186:     /** @deprecated */
187:     function fetchField($args)
188:     {
189:         $args = func_get_args();
190:         return $this->queryArgs(array_shift($args), $args)->fetchField();
191:     }
192: 
193: 
194:     /** @deprecated */
195:     function fetchPairs($args)
196:     {
197:         $args = func_get_args();
198:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
199:     }
200: 
201: 
202:     /** @deprecated */
203:     function fetchAll($args)
204:     {
205:         $args = func_get_args();
206:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
207:     }
208: 
209: 
210:     /** @deprecated */
211:     static function literal($value)
212:     {
213:         $args = func_get_args();
214:         return new SqlLiteral(array_shift($args), $args);
215:     }
216: 
217: 
218:     /********************* Selection ****************d*g**/
219: 
220: 
221:     /** @deprecated */
222:     function table($table)
223:     {
224:         trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context::table() instead.', E_USER_DEPRECATED);
225:         if (!$this->context) {
226:             $this->context = new Context($this);
227:         }
228:         return $this->context->table($table);
229:     }
230: 
231: 
232:     /** @deprecated */
233:     function setContext(Context $context)
234:     {
235:         $this->context = $context;
236:         return $this;
237:     }
238: 
239: 
240:     /** @deprecated */
241:     function getContext()
242:     {
243:         return $this->context;
244:     }
245: 
246: 
247:     /** @deprecated */
248:     function setDatabaseReflection()
249:     {
250:         trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context instead.', E_USER_DEPRECATED);
251:         return $this;
252:     }
253: 
254: 
255:     /** @deprecated */
256:     function setCacheStorage()
257:     {
258:         trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context instead.', E_USER_DEPRECATED);
259:     }
260: 
261: 
262:     /** @deprecated */
263:     function lastInsertId($name = NULL)
264:     {
265:         trigger_error(__METHOD__ . '() is deprecated; use getInsertId() instead.', E_USER_DEPRECATED);
266:         return $this->getInsertId($name);
267:     }
268: 
269: 
270:     /** @deprecated */
271:     function exec($statement)
272:     {
273:         trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context::query()->getRowCount() instead.', E_USER_DEPRECATED);
274:         $args = func_get_args();
275:         return $this->queryArgs(array_shift($args), $args)->getRowCount();
276:     }
277: 
278: }
279: 
Nette 2.1 API documentation generated by ApiGen 2.8.0