Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

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 (https://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:     /** @return Nette\Database\Table\Selection */
114:     public function table($table)
115:     {
116:         return new Table\Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
117:     }
118: 
119: 
120:     /** @return Connection */
121:     public function getConnection()
122:     {
123:         return $this->connection;
124:     }
125: 
126: 
127:     /** @return IReflection */
128:     public function getDatabaseReflection()
129:     {
130:         return $this->reflection;
131:     }
132: 
133: 
134:     /********************* shortcuts ****************d*g**/
135: 
136: 
137:     /**
138:      * Shortcut for query()->fetch()
139:      * @param  string  statement
140:      * @param  mixed   [parameters, ...]
141:      * @return Row
142:      */
143:     public function fetch($args)
144:     {
145:         $args = func_get_args();
146:         return $this->queryArgs(array_shift($args), $args)->fetch();
147:     }
148: 
149: 
150:     /**
151:      * Shortcut for query()->fetchField()
152:      * @param  string  statement
153:      * @param  mixed   [parameters, ...]
154:      * @return mixed
155:      */
156:     public function fetchField($args)
157:     {
158:         $args = func_get_args();
159:         return $this->queryArgs(array_shift($args), $args)->fetchField();
160:     }
161: 
162: 
163:     /**
164:      * Shortcut for query()->fetchPairs()
165:      * @param  string  statement
166:      * @param  mixed   [parameters, ...]
167:      * @return array
168:      */
169:     public function fetchPairs($args)
170:     {
171:         $args = func_get_args();
172:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
173:     }
174: 
175: 
176:     /**
177:      * Shortcut for query()->fetchAll()
178:      * @param  string  statement
179:      * @param  mixed   [parameters, ...]
180:      * @return array
181:      */
182:     public function fetchAll($args)
183:     {
184:         $args = func_get_args();
185:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
186:     }
187: 
188: 
189:     /**
190:      * @return SqlLiteral
191:      */
192:     public static function literal($value)
193:     {
194:         $args = func_get_args();
195:         return new SqlLiteral(array_shift($args), $args);
196:     }
197: 
198: }
199: 
Nette 2.1 API documentation generated by ApiGen 2.8.0