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