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: public function table($table)
115: {
116: return new Table\Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
117: }
118:
119:
120:
121: public function getConnection()
122: {
123: return $this->connection;
124: }
125:
126:
127:
128: public function getDatabaseReflection()
129: {
130: return $this->reflection;
131: }
132:
133:
134:
135:
136:
137: 138: 139: 140: 141: 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: 152: 153: 154: 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: 165: 166: 167: 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: 178: 179: 180: 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: 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: