1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database;
13:
14: use Nette,
15: Nette\ObjectMixin,
16: PDO;
17:
18:
19:
20: 21: 22: 23: 24: 25: 26: 27: 28:
29: class Connection extends PDO
30: {
31:
32: private $dsn;
33:
34:
35: private $driver;
36:
37:
38: private $preprocessor;
39:
40:
41: private $databaseReflection;
42:
43:
44: private $cache;
45:
46:
47: public $onQuery;
48:
49:
50:
51: public function __construct($dsn, $username = NULL, $password = NULL, array $options = NULL, $driverClass = NULL)
52: {
53: parent::__construct($this->dsn = $dsn, $username, $password, $options);
54: $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
55: $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Nette\Database\Statement', array($this)));
56:
57: $driverClass = $driverClass ?: 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver';
58: $this->driver = new $driverClass($this, (array) $options);
59: $this->preprocessor = new SqlPreprocessor($this);
60: }
61:
62:
63:
64: public function getDsn()
65: {
66: return $this->dsn;
67: }
68:
69:
70:
71:
72: public function getSupplementalDriver()
73: {
74: return $this->driver;
75: }
76:
77:
78:
79: 80: 81: 82:
83: public function setDatabaseReflection(IReflection $databaseReflection)
84: {
85: $databaseReflection->setConnection($this);
86: $this->databaseReflection = $databaseReflection;
87: return $this;
88: }
89:
90:
91:
92:
93: public function getDatabaseReflection()
94: {
95: if (!$this->databaseReflection) {
96: $this->setDatabaseReflection(new Reflection\ConventionalReflection);
97: }
98: return $this->databaseReflection;
99: }
100:
101:
102:
103: 104: 105: 106:
107: public function setCacheStorage(Nette\Caching\IStorage $storage = NULL)
108: {
109: $this->cache = $storage ? new Nette\Caching\Cache($storage, 'Nette.Database.' . md5($this->dsn)) : NULL;
110: return $this;
111: }
112:
113:
114:
115: public function getCache()
116: {
117: return $this->cache;
118: }
119:
120:
121:
122: 123: 124: 125: 126: 127:
128: public function query($statement)
129: {
130: $args = func_get_args();
131: return $this->queryArgs(array_shift($args), $args);
132: }
133:
134:
135:
136: 137: 138: 139: 140: 141:
142: public function exec($statement)
143: {
144: $args = func_get_args();
145: return $this->queryArgs(array_shift($args), $args)->rowCount();
146: }
147:
148:
149:
150: 151: 152: 153: 154:
155: public function queryArgs($statement, $params)
156: {
157: foreach ($params as $value) {
158: if (is_array($value) || is_object($value)) {
159: $need = TRUE; break;
160: }
161: }
162: if (isset($need) && $this->preprocessor !== NULL) {
163: list($statement, $params) = $this->preprocessor->process($statement, $params);
164: }
165:
166: return $this->prepare($statement)->execute($params);
167: }
168:
169:
170:
171:
172:
173:
174:
175: 176: 177: 178: 179: 180:
181: public function fetch($args)
182: {
183: $args = func_get_args();
184: return $this->queryArgs(array_shift($args), $args)->fetch();
185: }
186:
187:
188:
189: 190: 191: 192: 193: 194:
195: public function fetchColumn($args)
196: {
197: $args = func_get_args();
198: return $this->queryArgs(array_shift($args), $args)->fetchColumn();
199: }
200:
201:
202:
203: 204: 205: 206: 207: 208:
209: public function fetchPairs($args)
210: {
211: $args = func_get_args();
212: return $this->queryArgs(array_shift($args), $args)->fetchPairs();
213: }
214:
215:
216:
217: 218: 219: 220: 221: 222:
223: public function fetchAll($args)
224: {
225: $args = func_get_args();
226: return $this->queryArgs(array_shift($args), $args)->fetchAll();
227: }
228:
229:
230:
231:
232:
233:
234:
235: 236: 237: 238: 239:
240: public function table($table)
241: {
242: return new Table\Selection($table, $this);
243: }
244:
245:
246:
247:
248:
249:
250:
251: 252: 253:
254: public static function getReflection()
255: {
256: return new Nette\Reflection\ClassType(get_called_class());
257: }
258:
259:
260:
261: public function __call($name, $args)
262: {
263: return ObjectMixin::call($this, $name, $args);
264: }
265:
266:
267:
268: public function &__get($name)
269: {
270: return ObjectMixin::get($this, $name);
271: }
272:
273:
274:
275: public function __set($name, $value)
276: {
277: return ObjectMixin::set($this, $name, $value);
278: }
279:
280:
281:
282: public function __isset($name)
283: {
284: return ObjectMixin::has($this, $name);
285: }
286:
287:
288:
289: public function __unset($name)
290: {
291: ObjectMixin::remove($this, $name);
292: }
293:
294: }
295: