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
      • 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
  • IRow
  • IRowContainer
  • IStructure
  • ISupplementalDriver

Exceptions

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