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