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