1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Database;
9:
10: use Nette;
11: use PDO;
12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Connection extends Nette\Object
24: {
25:
26: public $onConnect;
27:
28:
29: public $onQuery;
30:
31:
32: private $params;
33:
34:
35: private $options;
36:
37:
38: private $driver;
39:
40:
41: private $preprocessor;
42:
43:
44: private $context;
45:
46:
47: private $pdo;
48:
49:
50: public function __construct($dsn, $user = NULL, $password = NULL, array $options = NULL)
51: {
52: if (func_num_args() > 4) {
53: $options['driverClass'] = func_get_arg(4);
54: }
55: $this->params = array($dsn, $user, $password);
56: $this->options = (array) $options;
57:
58: if (empty($options['lazy'])) {
59: $this->connect();
60: }
61: }
62:
63:
64: public function connect()
65: {
66: if ($this->pdo) {
67: return;
68: }
69: $this->pdo = new PDO($this->params[0], $this->params[1], $this->params[2], $this->options);
70: $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
71:
72: $class = empty($this->options['driverClass'])
73: ? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
74: : $this->options['driverClass'];
75: $this->driver = new $class($this, $this->options);
76: $this->preprocessor = new SqlPreprocessor($this);
77: $this->onConnect($this);
78: }
79:
80:
81:
82: public function getDsn()
83: {
84: return $this->params[0];
85: }
86:
87:
88:
89: public function getPdo()
90: {
91: $this->connect();
92: return $this->pdo;
93: }
94:
95:
96:
97: public function getSupplementalDriver()
98: {
99: $this->connect();
100: return $this->driver;
101: }
102:
103:
104: 105: 106: 107:
108: public function getInsertId($name = NULL)
109: {
110: return $this->getPdo()->lastInsertId($name);
111: }
112:
113:
114: 115: 116: 117: 118:
119: public function quote($string, $type = PDO::PARAM_STR)
120: {
121: return $this->getPdo()->quote($string, $type);
122: }
123:
124:
125:
126: function beginTransaction()
127: {
128: $this->queryArgs('::beginTransaction', array());
129: }
130:
131:
132:
133: function commit()
134: {
135: $this->queryArgs('::commit', array());
136: }
137:
138:
139:
140: public function rollBack()
141: {
142: $this->queryArgs('::rollBack', array());
143: }
144:
145:
146:
147: public function query($statement)
148: {
149: $args = func_get_args();
150: return $this->queryArgs(array_shift($args), $args);
151: }
152:
153:
154:
155: function queryArgs($statement, array $params)
156: {
157: $this->connect();
158: if ($params) {
159: array_unshift($params, $statement);
160: list($statement, $params) = $this->preprocessor->process($params);
161: }
162:
163: try {
164: $result = new ResultSet($this, $statement, $params);
165: } catch (\PDOException $e) {
166: $e->queryString = $statement;
167: $this->onQuery($this, $e);
168: throw $e;
169: }
170: $this->onQuery($this, $result);
171: return $result;
172: }
173:
174:
175:
176:
177:
178:
179: function fetch($args)
180: {
181: $args = func_get_args();
182: return $this->queryArgs(array_shift($args), $args)->fetch();
183: }
184:
185:
186:
187: function fetchField($args)
188: {
189: $args = func_get_args();
190: return $this->queryArgs(array_shift($args), $args)->fetchField();
191: }
192:
193:
194:
195: function fetchPairs($args)
196: {
197: $args = func_get_args();
198: return $this->queryArgs(array_shift($args), $args)->fetchPairs();
199: }
200:
201:
202:
203: function fetchAll($args)
204: {
205: $args = func_get_args();
206: return $this->queryArgs(array_shift($args), $args)->fetchAll();
207: }
208:
209:
210:
211: static function literal($value)
212: {
213: $args = func_get_args();
214: return new SqlLiteral(array_shift($args), $args);
215: }
216:
217:
218:
219:
220:
221:
222: function table($table)
223: {
224: trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context::table() instead.', E_USER_DEPRECATED);
225: if (!$this->context) {
226: $this->context = new Context($this);
227: }
228: return $this->context->table($table);
229: }
230:
231:
232:
233: function setContext(Context $context)
234: {
235: $this->context = $context;
236: return $this;
237: }
238:
239:
240:
241: function getContext()
242: {
243: return $this->context;
244: }
245:
246:
247:
248: function setDatabaseReflection()
249: {
250: trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context instead.', E_USER_DEPRECATED);
251: return $this;
252: }
253:
254:
255:
256: function setCacheStorage()
257: {
258: trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context instead.', E_USER_DEPRECATED);
259: }
260:
261:
262:
263: function lastInsertId($name = NULL)
264: {
265: trigger_error(__METHOD__ . '() is deprecated; use getInsertId() instead.', E_USER_DEPRECATED);
266: return $this->getInsertId($name);
267: }
268:
269:
270:
271: function exec($statement)
272: {
273: trigger_error(__METHOD__ . '() is deprecated; use Nette\Database\Context::query()->getRowCount() instead.', E_USER_DEPRECATED);
274: $args = func_get_args();
275: return $this->queryArgs(array_shift($args), $args)->getRowCount();
276: }
277:
278: }
279: