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