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