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