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 $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->query('::beginTransaction');
126: }
127:
128:
129:
130: function commit()
131: {
132: $this->query('::commit');
133: }
134:
135:
136:
137: public function rollBack()
138: {
139: $this->query('::rollBack');
140: }
141:
142:
143: 144: 145: 146: 147: 148:
149: public function query($statement)
150: {
151: $this->connect();
152:
153: $args = is_array($statement) ? $statement : func_get_args();
154: list($statement, $params) = count($args) > 1
155: ? $this->preprocessor->process($args)
156: : array($args[0], array());
157:
158: try {
159: $result = new ResultSet($this, $statement, $params);
160: } catch (\PDOException $e) {
161: $e->queryString = $statement;
162: $this->onQuery($this, $e);
163: throw $e;
164: }
165: $this->onQuery($this, $result);
166: return $result;
167: }
168:
169:
170: 171: 172: 173: 174:
175: public function queryArgs($statement, array $params)
176: {
177: array_unshift($params, $statement);
178: return $this->query($params);
179: }
180:
181:
182:
183:
184:
185: 186: 187: 188: 189: 190:
191: public function fetch($args)
192: {
193: return $this->query(func_get_args())->fetch();
194: }
195:
196:
197: 198: 199: 200: 201: 202:
203: public function fetchField($args)
204: {
205: return $this->query(func_get_args())->fetchField();
206: }
207:
208:
209: 210: 211: 212: 213: 214:
215: public function fetchPairs($args)
216: {
217: return $this->query(func_get_args())->fetchPairs();
218: }
219:
220:
221: 222: 223: 224: 225: 226:
227: public function fetchAll($args)
228: {
229: return $this->query(func_get_args())->fetchAll();
230: }
231:
232:
233: 234: 235:
236: public static function literal($value)
237: {
238: $args = func_get_args();
239: return new SqlLiteral(array_shift($args), $args);
240: }
241:
242: }
243: