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