1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Database;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Context extends Nette\Object
19: {
20:
21: private $connection;
22:
23:
24: private $reflection;
25:
26:
27: private $cacheStorage;
28:
29:
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:
42: public function beginTransaction()
43: {
44: $this->queryArgs('::beginTransaction', array());
45: }
46:
47:
48:
49: public function commit()
50: {
51: $this->queryArgs('::commit', array());
52: }
53:
54:
55:
56: public function rollBack()
57: {
58: $this->queryArgs('::rollBack', array());
59: }
60:
61:
62: 63: 64: 65:
66: public function getInsertId($name = NULL)
67: {
68: return $this->connection->getInsertId($name);
69: }
70:
71:
72: 73: 74: 75: 76: 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: 87: 88: 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: 115: 116:
117: public function table($table)
118: {
119: return new Table\Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
120: }
121:
122:
123:
124: public function getConnection()
125: {
126: return $this->connection;
127: }
128:
129:
130:
131: public function getDatabaseReflection()
132: {
133: return $this->reflection;
134: }
135:
136:
137:
138:
139:
140: 141: 142: 143: 144: 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: 155: 156: 157: 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: 168: 169: 170: 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: 181: 182: 183: 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: 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: