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