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
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

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
  • Other releases
  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:             $res = $this->getPdo()->lastInsertId($name);
128:             return $res === false ? '0' : $res;
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:     public function beginTransaction()
152:     {
153:         $this->query('::beginTransaction');
154:     }
155: 
156: 
157:     /** @return void */
158:     public 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:      * @return ResultSet
175:      */
176:     public function query($sql, ...$params)
177:     {
178:         list($sql, $params) = $this->preprocess($sql, ...$params);
179:         try {
180:             $result = new ResultSet($this, $sql, $params);
181:         } catch (PDOException $e) {
182:             $this->onQuery($this, $e);
183:             throw $e;
184:         }
185:         $this->onQuery($this, $result);
186:         return $result;
187:     }
188: 
189: 
190:     /**
191:      * @param  string
192:      * @return ResultSet
193:      */
194:     public function queryArgs($sql, array $params)
195:     {
196:         return $this->query($sql, ...$params);
197:     }
198: 
199: 
200:     /**
201:      * @return [string, array]
202:      */
203:     public function preprocess($sql, ...$params)
204:     {
205:         $this->connect();
206:         return $params
207:             ? $this->preprocessor->process(func_get_args())
208:             : [$sql, []];
209:     }
210: 
211: 
212:     /********************* shortcuts ****************d*g**/
213: 
214: 
215:     /**
216:      * Shortcut for query()->fetch()
217:      * @param  string
218:      * @return Row
219:      */
220:     public function fetch($sql, ...$params)
221:     {
222:         return $this->query($sql, ...$params)->fetch();
223:     }
224: 
225: 
226:     /**
227:      * Shortcut for query()->fetchField()
228:      * @param  string
229:      * @return mixed
230:      */
231:     public function fetchField($sql, ...$params)
232:     {
233:         return $this->query($sql, ...$params)->fetchField();
234:     }
235: 
236: 
237:     /**
238:      * Shortcut for query()->fetchPairs()
239:      * @param  string
240:      * @return array
241:      */
242:     public function fetchPairs($sql, ...$params)
243:     {
244:         return $this->query($sql, ...$params)->fetchPairs();
245:     }
246: 
247: 
248:     /**
249:      * Shortcut for query()->fetchAll()
250:      * @param  string
251:      * @return array
252:      */
253:     public function fetchAll($sql, ...$params)
254:     {
255:         return $this->query($sql, ...$params)->fetchAll();
256:     }
257: 
258: 
259:     /**
260:      * @return SqlLiteral
261:      */
262:     public static function literal($value, ...$params)
263:     {
264:         return new SqlLiteral($value, $params);
265:     }
266: }
267: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0