Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

Classes

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

Interfaces

  • IReflection
  • IRow
  • IRowContainer
  • ISupplementalDriver
  • 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: 
 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 PDO */
 44:     private $pdo;
 45: 
 46: 
 47:     public function __construct($dsn, $user = NULL, $password = NULL, array $options = NULL)
 48:     {
 49:         if (func_num_args() > 4) { // compatibility
 50:             $options['driverClass'] = func_get_arg(4);
 51:         }
 52:         $this->params = array($dsn, $user, $password);
 53:         $this->options = (array) $options;
 54: 
 55:         if (empty($options['lazy'])) {
 56:             $this->connect();
 57:         }
 58:     }
 59: 
 60: 
 61:     public function connect()
 62:     {
 63:         if ($this->pdo) {
 64:             return;
 65:         }
 66:         $this->pdo = new PDO($this->params[0], $this->params[1], $this->params[2], $this->options);
 67:         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 68: 
 69:         $class = empty($this->options['driverClass'])
 70:             ? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
 71:             : $this->options['driverClass'];
 72:         $this->driver = new $class($this, $this->options);
 73:         $this->preprocessor = new SqlPreprocessor($this);
 74:         $this->onConnect($this);
 75:     }
 76: 
 77: 
 78:     /** @return string */
 79:     public function getDsn()
 80:     {
 81:         return $this->params[0];
 82:     }
 83: 
 84: 
 85:     /** @return PDO */
 86:     public function getPdo()
 87:     {
 88:         $this->connect();
 89:         return $this->pdo;
 90:     }
 91: 
 92: 
 93:     /** @return ISupplementalDriver */
 94:     public function getSupplementalDriver()
 95:     {
 96:         $this->connect();
 97:         return $this->driver;
 98:     }
 99: 
100: 
101:     /**
102:      * @param  string  sequence object
103:      * @return string
104:      */
105:     public function getInsertId($name = NULL)
106:     {
107:         return $this->getPdo()->lastInsertId($name);
108:     }
109: 
110: 
111:     /**
112:      * @param  string  string to be quoted
113:      * @param  int     data type hint
114:      * @return string
115:      */
116:     public function quote($string, $type = PDO::PARAM_STR)
117:     {
118:         return $this->getPdo()->quote($string, $type);
119:     }
120: 
121: 
122:     /** @deprecated */
123:     function beginTransaction()
124:     {
125:         $this->queryArgs('::beginTransaction', array());
126:     }
127: 
128: 
129:     /** @deprecated */
130:     function commit()
131:     {
132:         $this->queryArgs('::commit', array());
133:     }
134: 
135: 
136:     /** @deprecated */
137:     public function rollBack()
138:     {
139:         $this->queryArgs('::rollBack', array());
140:     }
141: 
142: 
143:     /** @deprecated */
144:     public function query($statement)
145:     {
146:         $args = func_get_args();
147:         return $this->queryArgs(array_shift($args), $args);
148:     }
149: 
150: 
151:     /** @deprecated */
152:     function queryArgs($statement, array $params)
153:     {
154:         $this->connect();
155:         if ($params) {
156:             array_unshift($params, $statement);
157:             list($statement, $params) = $this->preprocessor->process($params);
158:         }
159: 
160:         try {
161:             $result = new ResultSet($this, $statement, $params);
162:         } catch (\PDOException $e) {
163:             $e->queryString = $statement;
164:             $this->onQuery($this, $e);
165:             throw $e;
166:         }
167:         $this->onQuery($this, $result);
168:         return $result;
169:     }
170: 
171: 
172:     /********************* shortcuts ****************d*g**/
173: 
174: 
175:     /** @deprecated */
176:     function fetch($args)
177:     {
178:         $args = func_get_args();
179:         return $this->queryArgs(array_shift($args), $args)->fetch();
180:     }
181: 
182: 
183:     /** @deprecated */
184:     function fetchField($args)
185:     {
186:         $args = func_get_args();
187:         return $this->queryArgs(array_shift($args), $args)->fetchField();
188:     }
189: 
190: 
191:     /** @deprecated */
192:     function fetchPairs($args)
193:     {
194:         $args = func_get_args();
195:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
196:     }
197: 
198: 
199:     /** @deprecated */
200:     function fetchAll($args)
201:     {
202:         $args = func_get_args();
203:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
204:     }
205: 
206: 
207:     /** @deprecated */
208:     static function literal($value)
209:     {
210:         $args = func_get_args();
211:         return new SqlLiteral(array_shift($args), $args);
212:     }
213: 
214: }
215: 
Nette 2.2.6 API API documentation generated by ApiGen 2.8.0