1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Database;
9:
10: use Nette,
11: 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 $pdo;
45:
46:
47: public function __construct($dsn, $user = NULL, $password = NULL, array $options = NULL)
48: {
49: if (func_num_args() > 4) {
50: $options['driverClass'] = func_get_arg(4);
51: }
52: $this->params = array($dsn, $user, $password);
53: $this->options = (array) $options;
54:
55: if (empty($options['lazy'])) {
56: $this->connect();
57: }
58: }
59:
60:
61: public function connect()
62: {
63: if ($this->pdo) {
64: return;
65: }
66: $this->pdo = new PDO($this->params[0], $this->params[1], $this->params[2], $this->options);
67: $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
68:
69: $class = empty($this->options['driverClass'])
70: ? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
71: : $this->options['driverClass'];
72: $this->driver = new $class($this, $this->options);
73: $this->preprocessor = new SqlPreprocessor($this);
74: $this->onConnect($this);
75: }
76:
77:
78:
79: public function getDsn()
80: {
81: return $this->params[0];
82: }
83:
84:
85:
86: public function getPdo()
87: {
88: $this->connect();
89: return $this->pdo;
90: }
91:
92:
93:
94: public function getSupplementalDriver()
95: {
96: $this->connect();
97: return $this->driver;
98: }
99:
100:
101: 102: 103: 104:
105: public function getInsertId($name = NULL)
106: {
107: return $this->getPdo()->lastInsertId($name);
108: }
109:
110:
111: 112: 113: 114: 115:
116: public function quote($string, $type = PDO::PARAM_STR)
117: {
118: return $this->getPdo()->quote($string, $type);
119: }
120:
121:
122:
123: function beginTransaction()
124: {
125: $this->queryArgs('::beginTransaction', array());
126: }
127:
128:
129:
130: function commit()
131: {
132: $this->queryArgs('::commit', array());
133: }
134:
135:
136:
137: public function rollBack()
138: {
139: $this->queryArgs('::rollBack', array());
140: }
141:
142:
143:
144: public function query($statement)
145: {
146: $args = func_get_args();
147: return $this->queryArgs(array_shift($args), $args);
148: }
149:
150:
151:
152: function queryArgs($statement, array $params)
153: {
154: $this->connect();
155: if ($params) {
156: array_unshift($params, $statement);
157: list($statement, $params) = $this->preprocessor->process($params);
158: }
159:
160: try {
161: $result = new ResultSet($this, $statement, $params);
162: } catch (\PDOException $e) {
163: $e->queryString = $statement;
164: $this->onQuery($this, $e);
165: throw $e;
166: }
167: $this->onQuery($this, $result);
168: return $result;
169: }
170:
171:
172:
173:
174:
175:
176: function fetch($args)
177: {
178: $args = func_get_args();
179: return $this->queryArgs(array_shift($args), $args)->fetch();
180: }
181:
182:
183:
184: function fetchField($args)
185: {
186: $args = func_get_args();
187: return $this->queryArgs(array_shift($args), $args)->fetchField();
188: }
189:
190:
191:
192: function fetchPairs($args)
193: {
194: $args = func_get_args();
195: return $this->queryArgs(array_shift($args), $args)->fetchPairs();
196: }
197:
198:
199:
200: function fetchAll($args)
201: {
202: $args = func_get_args();
203: return $this->queryArgs(array_shift($args), $args)->fetchAll();
204: }
205:
206:
207:
208: static function literal($value)
209: {
210: $args = func_get_args();
211: return new SqlLiteral(array_shift($args), $args);
212: }
213:
214: }
215: