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