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
  • 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 (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Database;
  9: 
 10: use Nette;
 11: use 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:     /** @return void */
123:     function beginTransaction()
124:     {
125:         $this->query('::beginTransaction');
126:     }
127: 
128: 
129:     /** @return void */
130:     function commit()
131:     {
132:         $this->query('::commit');
133:     }
134: 
135: 
136:     /** @return void */
137:     public function rollBack()
138:     {
139:         $this->query('::rollBack');
140:     }
141: 
142: 
143:     /**
144:      * Generates and executes SQL query.
145:      * @param  string  statement
146:      * @param  mixed   [parameters, ...]
147:      * @return ResultSet
148:      */
149:     public function query($statement)
150:     {
151:         $this->connect();
152: 
153:         $args = is_array($statement) ? $statement : func_get_args(); // accepts arrays only internally
154:         list($statement, $params) = count($args) > 1
155:             ? $this->preprocessor->process($args)
156:             : array($args[0], array());
157: 
158:         try {
159:             $result = new ResultSet($this, $statement, $params);
160:         } catch (\PDOException $e) {
161:             $e->queryString = $statement;
162:             $this->onQuery($this, $e);
163:             throw $e;
164:         }
165:         $this->onQuery($this, $result);
166:         return $result;
167:     }
168: 
169: 
170:     /**
171:      * @param  string  statement
172:      * @param  array
173:      * @return ResultSet
174:      */
175:     public function queryArgs($statement, array $params)
176:     {
177:         array_unshift($params, $statement);
178:         return $this->query($params);
179:     }
180: 
181: 
182:     /********************* shortcuts ****************d*g**/
183: 
184: 
185:     /**
186:      * Shortcut for query()->fetch()
187:      * @param  string  statement
188:      * @param  mixed   [parameters, ...]
189:      * @return Row
190:      */
191:     public function fetch($args)
192:     {
193:         return $this->query(func_get_args())->fetch();
194:     }
195: 
196: 
197:     /**
198:      * Shortcut for query()->fetchField()
199:      * @param  string  statement
200:      * @param  mixed   [parameters, ...]
201:      * @return mixed
202:      */
203:     public function fetchField($args)
204:     {
205:         return $this->query(func_get_args())->fetchField();
206:     }
207: 
208: 
209:     /**
210:      * Shortcut for query()->fetchPairs()
211:      * @param  string  statement
212:      * @param  mixed   [parameters, ...]
213:      * @return array
214:      */
215:     public function fetchPairs($args)
216:     {
217:         return $this->query(func_get_args())->fetchPairs();
218:     }
219: 
220: 
221:     /**
222:      * Shortcut for query()->fetchAll()
223:      * @param  string  statement
224:      * @param  mixed   [parameters, ...]
225:      * @return array
226:      */
227:     public function fetchAll($args)
228:     {
229:         return $this->query(func_get_args())->fetchAll();
230:     }
231: 
232: 
233:     /**
234:      * @return SqlLiteral
235:      */
236:     public static function literal($value)
237:     {
238:         $args = func_get_args();
239:         return new SqlLiteral(array_shift($args), $args);
240:     }
241: 
242: }
243: 
Nette 2.2 API documentation generated by ApiGen 2.8.0