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: 
 12: 
 13: /**
 14:  * Database context.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class Context extends Nette\Object
 19: {
 20:     /** @var Connection */
 21:     private $connection;
 22: 
 23:     /** @var IReflection */
 24:     private $reflection;
 25: 
 26:     /** @var Nette\Caching\IStorage */
 27:     private $cacheStorage;
 28: 
 29:     /** @var SqlPreprocessor */
 30:     private $preprocessor;
 31: 
 32: 
 33:     public function __construct(Connection $connection, IReflection $reflection = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
 34:     {
 35:         $this->connection = $connection;
 36:         $this->reflection = $reflection ?: new Reflection\ConventionalReflection;
 37:         $this->cacheStorage = $cacheStorage;
 38:     }
 39: 
 40: 
 41:     /** @return void */
 42:     public function beginTransaction()
 43:     {
 44:         $this->queryArgs('::beginTransaction', array());
 45:     }
 46: 
 47: 
 48:     /** @return void */
 49:     public function commit()
 50:     {
 51:         $this->queryArgs('::commit', array());
 52:     }
 53: 
 54: 
 55:     /** @return void */
 56:     public function rollBack()
 57:     {
 58:         $this->queryArgs('::rollBack', array());
 59:     }
 60: 
 61: 
 62:     /**
 63:      * @param  string  sequence object
 64:      * @return string
 65:      */
 66:     public function getInsertId($name = NULL)
 67:     {
 68:         return $this->connection->getInsertId($name);
 69:     }
 70: 
 71: 
 72:     /**
 73:      * Generates and executes SQL query.
 74:      * @param  string  statement
 75:      * @param  mixed   [parameters, ...]
 76:      * @return ResultSet
 77:      */
 78:     public function query($statement)
 79:     {
 80:         $args = func_get_args();
 81:         return $this->queryArgs(array_shift($args), $args);
 82:     }
 83: 
 84: 
 85:     /**
 86:      * @param  string  statement
 87:      * @param  array
 88:      * @return ResultSet
 89:      */
 90:     public function queryArgs($statement, array $params)
 91:     {
 92:         $this->connection->connect();
 93:         if ($params) {
 94:             if (!$this->preprocessor) {
 95:                 $this->preprocessor = new SqlPreprocessor($this->connection);
 96:             }
 97:             array_unshift($params, $statement);
 98:             list($statement, $params) = $this->preprocessor->process($params);
 99:         }
100: 
101:         try {
102:             $result = new ResultSet($this->connection, $statement, $params);
103:         } catch (\PDOException $e) {
104:             $e->queryString = $statement;
105:             $this->connection->onQuery($this->connection, $e);
106:             throw $e;
107:         }
108:         $this->connection->onQuery($this->connection, $result);
109:         return $result;
110:     }
111: 
112: 
113:     /**
114:      * @param  string
115:      * @return Nette\Database\Table\Selection
116:      */
117:     public function table($table)
118:     {
119:         return new Table\Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
120:     }
121: 
122: 
123:     /** @return Connection */
124:     public function getConnection()
125:     {
126:         return $this->connection;
127:     }
128: 
129: 
130:     /** @return IReflection */
131:     public function getDatabaseReflection()
132:     {
133:         return $this->reflection;
134:     }
135: 
136: 
137:     /********************* shortcuts ****************d*g**/
138: 
139: 
140:     /**
141:      * Shortcut for query()->fetch()
142:      * @param  string  statement
143:      * @param  mixed   [parameters, ...]
144:      * @return Row
145:      */
146:     public function fetch($args)
147:     {
148:         $args = func_get_args();
149:         return $this->queryArgs(array_shift($args), $args)->fetch();
150:     }
151: 
152: 
153:     /**
154:      * Shortcut for query()->fetchField()
155:      * @param  string  statement
156:      * @param  mixed   [parameters, ...]
157:      * @return mixed
158:      */
159:     public function fetchField($args)
160:     {
161:         $args = func_get_args();
162:         return $this->queryArgs(array_shift($args), $args)->fetchField();
163:     }
164: 
165: 
166:     /**
167:      * Shortcut for query()->fetchPairs()
168:      * @param  string  statement
169:      * @param  mixed   [parameters, ...]
170:      * @return array
171:      */
172:     public function fetchPairs($args)
173:     {
174:         $args = func_get_args();
175:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
176:     }
177: 
178: 
179:     /**
180:      * Shortcut for query()->fetchAll()
181:      * @param  string  statement
182:      * @param  mixed   [parameters, ...]
183:      * @return array
184:      */
185:     public function fetchAll($args)
186:     {
187:         $args = func_get_args();
188:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
189:     }
190: 
191: 
192:     /**
193:      * @return SqlLiteral
194:      */
195:     public static function literal($value)
196:     {
197:         $args = func_get_args();
198:         return new SqlLiteral(array_shift($args), $args);
199:     }
200: 
201: }
202: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0