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