Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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

Interfaces

  • IConventions
  • IReflection
  • IRow
  • IRowContainer
  • IStructure
  • ISupplementalDriver

Exceptions

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