-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Driver.php
186 lines (158 loc) · 4.45 KB
/
Driver.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
<?php
namespace Cake\Model\Datasource\Database;
/**
* Represents a database diver containing all specificities for
* a database engine including its SQL dialect
*
**/
abstract class Driver {
/**
* String used to start a database identifier quoting to make it safe
*
* @var string
**/
public $startQuote = '"';
/**
* String used to end a database identifier quoting to make it safe
*
* @var string
**/
public $endQuote = '"';
/**
* Establishes a connection to the database server
*
* @param array $config configuration to be used for creating connection
* @return boolean true con success
**/
public abstract function connect(array $config);
/**
* Disconnects from database server
*
* @return void
**/
public abstract function disconnect();
/**
* Returns correct connection resource or object that is internally used
* If first argument is passed, it will set internal conenction object or
* result to the value passed
*
* @return mixed connection object used internally
**/
public abstract function connection($connection = null);
/**
* Returns whether php is able to use this driver for connecting to database
*
* @return boolean true if it is valid to use this driver
**/
public abstract function enabled();
/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Model\Datasource\Database\Statement
**/
public abstract function prepare($sql);
/**
* Starts a transaction
*
* @return boolean true on success, false otherwise
**/
public abstract function beginTransaction();
/**
* Commits a transaction
*
* @return boolean true on success, false otherwise
**/
public abstract function commitTransaction();
/**
* Rollsback a transaction
*
* @return boolean true on success, false otherwise
**/
public abstract function rollbackTransaction();
/**
* Returns whether this driver supports save points for nested transactions
*
* @return boolean true if save points are supported, false otherwise
**/
public function supportsSavePoints() {
return true;
}
/**
* Returns a SQL snippet for creating a new transaction savepoint
*
* @param string save point name
* @return string
**/
public function savePointSQL($name) {
return 'SAVEPOINT LEVEL' . $name;
}
/**
* Returns a SQL snippet for releasing a previously created save point
*
* @param string save point name
* @return string
**/
public function releaseSavePointSQL($name) {
return 'RELEASE SAVEPOINT LEVEL' . $name;
}
/**
* Returns a SQL snippet for rollbacking a previously created save point
*
* @param string save point name
* @return string
**/
public function rollbackSavePointSQL($name) {
return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
}
/**
* Returns a value in a safe representation to be used in a query string
*
* @return string
**/
public abstract function quote($value, $type);
/**
* 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) {
$identifier = trim($identifier);
if ($identifier === '*') {
return '*';
}
if (preg_match('/^[\w-]+(?:\.[^ \*]*)*$/', $identifier)) { // string, string.string
if (strpos($identifier, '.') === false) { // string
return $this->startQuote . $identifier . $this->endQuote;
}
$items = explode('.', $identifier);
return $this->startQuote . implode($this->endQuote . '.' . $this->startQuote, $items) . $this->endQuote;
}
if (preg_match('/^[\w-]+\.\*$/', $identifier)) { // string.*
return $this->startQuote . str_replace('.*', $this->endQuote . '.*', $identifier);
}
if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) { // Functions
return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
}
if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) {
return preg_replace(
'/\s{2,}/', ' ', $this->quoteIdentifier($matches[1]) . ' AS ' . $this->quoteIdentifier($matches[3])
);
}
if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) {
return $this->startQuote . $identifier . $this->endQuote;
}
return $identifier;
}
/**
* Returns last id generated for a table or sequence in database
*
* @param string $table table name or sequence to get last insert value from
* @return string|integer
**/
public function lastInsertId($table = null) {
return $this->_connection->lastInsertId($table);
}
}