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