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)
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: $class = ucfirst(str_replace('sql', 'Sql', $this->getAttribute(PDO::ATTR_DRIVER_NAME)));
58: $class = 'Nette\Database\Drivers\\' . $class . 'Driver';
59: if (class_exists($class)) {
60: $this->driver = new $class($this, (array) $options);
61: }
62:
63: $this->preprocessor = new SqlPreprocessor($this);
64: if (func_num_args() > 4) {
65: trigger_error('Set database reflection via setDatabaseReflection().', E_USER_WARNING);
66: $this->setDatabaseReflection(func_get_arg(5));
67: }
68: }
69:
70:
71:
72: public function getDsn()
73: {
74: return $this->dsn;
75: }
76:
77:
78:
79:
80: public function getSupplementalDriver()
81: {
82: return $this->driver;
83: }
84:
85:
86:
87: 88: 89: 90: 91:
92: public function setDatabaseReflection(IReflection $databaseReflection)
93: {
94: $databaseReflection->setConnection($this);
95: $this->databaseReflection = $databaseReflection;
96: return $this;
97: }
98:
99:
100:
101:
102: public function getDatabaseReflection()
103: {
104: if (!$this->databaseReflection) {
105: $this->setDatabaseReflection(new Reflection\ConventionalReflection);
106: }
107: return $this->databaseReflection;
108: }
109:
110:
111:
112: 113: 114: 115: 116:
117: public function setCacheStorage(Nette\Caching\IStorage $storage = NULL)
118: {
119: $this->cache = $storage ? new Nette\Caching\Cache($storage, 'Nette.Database.' . md5($this->dsn)) : NULL;
120: return $this;
121: }
122:
123:
124:
125: public function getCache()
126: {
127: return $this->cache;
128: }
129:
130:
131:
132: 133: 134: 135: 136: 137:
138: public function query($statement)
139: {
140: $args = func_get_args();
141: return $this->queryArgs(array_shift($args), $args);
142: }
143:
144:
145:
146: 147: 148: 149: 150: 151:
152: public function exec($statement)
153: {
154: $args = func_get_args();
155: return $this->queryArgs(array_shift($args), $args)->rowCount();
156: }
157:
158:
159:
160: 161: 162: 163: 164:
165: public function queryArgs($statement, $params)
166: {
167: foreach ($params as $value) {
168: if (is_array($value) || is_object($value)) {
169: $need = TRUE; break;
170: }
171: }
172: if (isset($need) && $this->preprocessor !== NULL) {
173: list($statement, $params) = $this->preprocessor->process($statement, $params);
174: }
175:
176: return $this->prepare($statement)->execute($params);
177: }
178:
179:
180:
181:
182:
183:
184:
185: 186: 187: 188: 189: 190:
191: public function fetch($args)
192: {
193: $args = func_get_args();
194: return $this->queryArgs(array_shift($args), $args)->fetch();
195: }
196:
197:
198:
199: 200: 201: 202: 203: 204:
205: public function fetchColumn($args)
206: {
207: $args = func_get_args();
208: return $this->queryArgs(array_shift($args), $args)->fetchColumn();
209: }
210:
211:
212:
213: 214: 215: 216: 217: 218:
219: public function fetchPairs($args)
220: {
221: $args = func_get_args();
222: return $this->queryArgs(array_shift($args), $args)->fetchPairs();
223: }
224:
225:
226:
227: 228: 229: 230: 231: 232:
233: public function fetchAll($args)
234: {
235: $args = func_get_args();
236: return $this->queryArgs(array_shift($args), $args)->fetchAll();
237: }
238:
239:
240:
241:
242:
243:
244:
245: 246: 247: 248: 249:
250: public function table($table)
251: {
252: return new Table\Selection($table, $this);
253: }
254:
255:
256:
257:
258:
259:
260:
261: 262: 263:
264: public static function getReflection()
265: {
266: return new Nette\Reflection\ClassType(get_called_class());
267: }
268:
269:
270:
271: public function __call($name, $args)
272: {
273: return ObjectMixin::call($this, $name, $args);
274: }
275:
276:
277:
278: public function &__get($name)
279: {
280: return ObjectMixin::get($this, $name);
281: }
282:
283:
284:
285: public function __set($name, $value)
286: {
287: return ObjectMixin::set($this, $name, $value);
288: }
289:
290:
291:
292: public function __isset($name)
293: {
294: return ObjectMixin::has($this, $name);
295: }
296:
297:
298:
299: public function __unset($name)
300: {
301: ObjectMixin::remove($this, $name);
302: }
303:
304: }
305: