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:
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:
30: public function __construct(Connection $connection, IReflection $reflection = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
31: {
32: $this->connection = $connection;
33: $this->reflection = $reflection ?: new Reflection\ConventionalReflection;
34: $this->cacheStorage = $cacheStorage;
35: }
36:
37:
38: /** @return void */
39: public function beginTransaction()
40: {
41: $this->connection->beginTransaction();
42: }
43:
44:
45: /** @return void */
46: public function commit()
47: {
48: $this->connection->commit();
49: }
50:
51:
52: /** @return void */
53: public function rollBack()
54: {
55: $this->connection->rollBack();
56: }
57:
58:
59: /**
60: * @param string sequence object
61: * @return string
62: */
63: public function getInsertId($name = NULL)
64: {
65: return $this->connection->getInsertId($name);
66: }
67:
68:
69: /**
70: * Generates and executes SQL query.
71: * @param string statement
72: * @param mixed [parameters, ...]
73: * @return ResultSet
74: */
75: public function query($statement)
76: {
77: return $this->connection->query(func_get_args());
78: }
79:
80:
81: /**
82: * @param string statement
83: * @param array
84: * @return ResultSet
85: */
86: public function queryArgs($statement, array $params)
87: {
88: return $this->connection->queryArgs($statement, $params);
89: }
90:
91:
92: /**
93: * @param string
94: * @return Nette\Database\Table\Selection
95: */
96: public function table($table)
97: {
98: return new Table\Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
99: }
100:
101:
102: /** @return Connection */
103: public function getConnection()
104: {
105: return $this->connection;
106: }
107:
108:
109: /** @return IReflection */
110: public function getDatabaseReflection()
111: {
112: return $this->reflection;
113: }
114:
115:
116: /********************* shortcuts ****************d*g**/
117:
118:
119: /**
120: * Shortcut for query()->fetch()
121: * @param string statement
122: * @param mixed [parameters, ...]
123: * @return Row
124: */
125: public function fetch($args)
126: {
127: return $this->connection->query(func_get_args())->fetch();
128: }
129:
130:
131: /**
132: * Shortcut for query()->fetchField()
133: * @param string statement
134: * @param mixed [parameters, ...]
135: * @return mixed
136: */
137: public function fetchField($args)
138: {
139: return $this->connection->query(func_get_args())->fetchField();
140: }
141:
142:
143: /**
144: * Shortcut for query()->fetchPairs()
145: * @param string statement
146: * @param mixed [parameters, ...]
147: * @return array
148: */
149: public function fetchPairs($args)
150: {
151: return $this->connection->query(func_get_args())->fetchPairs();
152: }
153:
154:
155: /**
156: * Shortcut for query()->fetchAll()
157: * @param string statement
158: * @param mixed [parameters, ...]
159: * @return array
160: */
161: public function fetchAll($args)
162: {
163: return $this->connection->query(func_get_args())->fetchAll();
164: }
165:
166:
167: /**
168: * @return SqlLiteral
169: */
170: public static function literal($value)
171: {
172: $args = func_get_args();
173: return new SqlLiteral(array_shift($args), $args);
174: }
175:
176: }
177: