/
Connection.php
270 lines (232 loc) · 6.42 KB
/
Connection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
namespace Cake\Model\Datasource\Database;
use PDOException,
Cake\Model\Datasource\Database\Exception\MissingDriverException,
Cake\Model\Datasource\Database\Exception\MissingExtensionException,
Cake\Model\Datasource\Database\Exception\MissingConnectionException;
/**
* Represents a conection with a database server
*
**/
class Connection {
/**
* Contains the configuration params for this connection
*
* @var array
**/
protected $_config;
/**
* Driver object, responsible for creating the real connection
* and provide specific SQL dialact
*
* @var Cake\Model\Datasource\Database\Driver
**/
protected $_driver;
/**
* Whether connection was established or not
*
* @var boolean
**/
protected $_connected = false;
/**
* Constructor
*
* @param array $config configuration for conencting to database
* @throws \Cake\Model\Datasource\Database\Exception\MissingDriverException if driver class can not be found
* @throws \Cake\Model\Datasource\Database\Exception\MissingExtensionException if driver cannot be used
* @return void
**/
public function __construct($config) {
$this->_config = $config;
if (!class_exists($config['datasource'])) {
throw new MissingDriverException(array('driver' => $config['datasource']));
}
$this->_driver = new $config['datasource'];
if (!$this->_driver->enabled()) {
throw new MissingExtensionException(array('driver' => get_class($this->_driver)));
}
}
/**
* Connects to the configured databatase
*
* @throws \Cake\Model\Datasource\Database\Exception\MissingConnectionException if credentials are invalid
* @return boolean true on success or false if already connected
**/
public function connect() {
if ($this->_connected) {
return false;
}
try {
return $this->_connected = $this->_driver->connect($this->_config);
} catch(\Exception $e) {
throw new MissingConnectionException(array('reason' => $e->getMessage()));
}
}
/**
* Disconnects from database server
*
* @return void
**/
public function disconnect() {
$this->_driver->disconnect();
$this->_connected = false;
}
/**
* Returns wheter connection to database server was already stablished
*
* @return boolean
**/
public function isConnected() {
return $this->_connected;
}
/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Model\Datasource\Database\Statement
**/
public function prepare($sql) {
$this->connect();
return $this->_driver->prepare($sql);
}
/**
* Executes a query using $params for interpolating values and $types as a hint for each
* those params
*
* @param string $query SQL to be executed and interpolated with $params
* @param array $params list or associative array of params to be interpolated in $query as values
* @param array $types list or associative array of types to be used for casting values in query
* @return Cake\Model\Datasource\Database\Statement executed statament
**/
public function execute($query, array $params = array(), array $types = array()) {
$this->connect();
if ($params) {
$statement = $this->prepare($query);
$this->_bindValues($statement, $params, $types);
$result = $statement->execute();
} else {
$statement = $this->query($query);
}
return $statement;
}
/**
* Executes a SQL statament and returns the Statement object as result
*
* @return Cake\Model\Datasource\Database\Statement
**/
public function query($sql) {
$this->connect();
$statement = $this->prepare($sql);
$statement->execute();
return $statement;
}
/**
* Executes an INSERT query on the specified table
*
* @param string $table the table to update values in
* @param array $data values to be inserted
* @params array $types list of associative array containing the types to be used for casting
* @return Cake\Model\Datasource\Database\Statement
**/
public function insert($table, array $data, array $types = array()) {
}
/**
* Executes an UPDATE statement on the specified table
*
* @param string $table the table to delete rows from
* @param array $data values to be updated
* @param array $conditions conditions to be set for update statement
* @param array $types list of associative array containing the types to be used for casting
* @return Cake\Model\Datasource\Database\Statement
**/
public function update($table, array $data, array $conditions = array(), $types = array()) {
}
/**
* Executes a DELETE statement on the specified table
*
* @param string $table the table to delete rows from
* @param array $conditions conditions to be set for delete statement
* @return Cake\Model\Datasource\Database\Statement
**/
public function delete($table, $conditions = array()) {
}
/**
* Starts a new transaction
*
* @return void
**/
public function begin() {
}
/**
* Commits current transaction
*
* @return void
**/
public function commit() {
}
/**
* Rollsabck current transaction
*
* @return void
**/
public function rollback() {
}
/**
* Quotes value to be used safely in database query
*
* @param mixed $value
* @param Type to be used for determining kind of quoting to perform
* @return mixed queted value
**/
public function quote($value, $type = null) {
}
/**
* Quotes a database identifier (a column name, table name, etc..) to
* be used safely in queries without the risk of using reserver words
*
* @param string $identifier
* @return string
**/
public function quoteIdentifier($identifier) {
}
/**
* Returns last id generated for a table or sequence in database
*
* @param strin $table table name or sequence to get last insert value from
* @return string|integer
**/
public function lastInsertId($table) {
}
/**
* Sets database charset for this connection
*
* @param string $collection the charset name
**/
public function charset($collation) {
}
/**
* Binds values to statement object with corresponding type
*
* @param \Cake\Model\Datasource\Database\Statement The statement objet to bind values to
* @param array $params list of values to be bound
* @param array $types list of types to be used, kesy should match those in $params
* @return void
**/
protected function _bindValues($statement, $params, $types) {
if (empty($params)) {
return;
}
$annonymousParams = is_int(key($params)) ? true : false;
$offset = 1;
foreach ($params as $index => $value) {
$type = null;
if (isset($types[$index])) {
$type = $types[$index];
}
if ($annonymousParams) {
$index += $offset;
}
$statement->bindValue($index, $value, $type);
}
}
}