-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlPdo.php
More file actions
279 lines (187 loc) · 7.5 KB
/
pudlPdo.php
File metadata and controls
279 lines (187 loc) · 7.5 KB
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
271
272
273
274
275
276
277
278
279
<?php
if (!class_exists('pudl',false)) require_once(__DIR__.'/../pudl.php');
require_once(is_owner(__DIR__.'/pudlPdoResult.php'));
class pudlPdo
extends pudl {
////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __construct($options) {
// PRE-PROCESS OPTIONS
$options = self::_options($options);
// VALIDATE SERVER OPTION
if (empty($options['server'])) {
throw new pudlValueException(
$this,
'No DSN provided for PDO'
);
}
// DEFAULT OPTIONS
if (empty($options['options']) || !pudl_array($options['options'])) {
$options['options'] = [];
}
// DEFAULT TO ANSI STYLE IDENTIFIERS, BUT CAN BE OVERWRITTEN
$this->identifier = !empty($options['identifier'])
? $options['identifier']
: '"';
parent::__construct($options);
}
////////////////////////////////////////////////////////////////////////////
// DESTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __destruct() {
$this->disconnect();
parent::__destruct();
}
////////////////////////////////////////////////////////////////////////////
// CREATE A PDO CONNECTION TO THE DATABASE SERVER
////////////////////////////////////////////////////////////////////////////
public function connect() {
$auth = $this->auth();
pudl_require_extension('pdo');
//PERSISTENT CONNECTION
if (!empty($data['persistent'])) {
$data['options'][PDO::ATTR_PERSISTENT] = true;
}
try {
//USE EXISTING CONNECTION IF AVAILABLE
if ($auth['server'] instanceof PDO) {
$this->connection = $auth['server'];
//ATTEMPT A NEW CONNECTION OTHERWISE
} else {
$this->connection = new PDO(
$auth['server'],
$auth['username'],
$auth['password'],
$auth['options']
);
}
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
$this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
$this->connection->setAttribute(PDO::ATTR_TIMEOUT, $auth['timeout']);
} catch (PDOException $e) {
throw new pudlConnectionException($this,
'Unable to connect to DDO database ' .
'"' . $auth['server'] . '"' .
' with the username ' .
'"' . $auth['username'] . '"' .
"\nError: " . $e->getMessage()
);
}
// STORE WHICH SERVER WE'RE CONNECTED TO
if (!empty($auth['server'])) {
$this->connected = $auth['server'];
}
}
////////////////////////////////////////////////////////////////////////////
// DISCONNECT THE PDO CONNECTION FROM THE DATABASE SERVER
////////////////////////////////////////////////////////////////////////////
public function disconnect($trigger=true) {
parent::disconnect($trigger);
$this->connection = NULL;
}
////////////////////////////////////////////////////////////////////////////
// ESCAPE AN IDENTIFIER
////////////////////////////////////////////////////////////////////////////
public function identifier($identifier) {
if ($this->identifier === ']') {
return '[' . str_replace(']', ']]', $identifier) . ']';
}
return parent::identifier($identifier);
}
////////////////////////////////////////////////////////////////////////////
// ESCAPES SPECIAL CHARACTERS IN A STRING FOR USE IN A SQL STATEMENT
// http://php.net/manual/en/pdo.quote.php
////////////////////////////////////////////////////////////////////////////
public function escape($value) {
if (!$this->connection) return parent::escape($value);
return $this->connection->quote($value);
}
////////////////////////////////////////////////////////////////////////////
// PROCESS THE SQL QUERY
// http://php.net/manual/en/pdo.query.php
////////////////////////////////////////////////////////////////////////////
protected function process($query) {
if (!$this->connection) return new pudlPdoResult($this);
if (strtoupper(substr($query, 0, 7)) === 'SELECT ') {
$result = @$this->connection->query($query);
return new pudlPdoResult($this, $result);
}
$this->updated = @$this->connection->exec($query);
return new pudlPdoResult($this, true);
}
////////////////////////////////////////////////////////////////////////////
// GET THE MOST RECENT AUTO-INCREMENT ID
// http://php.net/manual/en/pdo.lastinsertid.php
////////////////////////////////////////////////////////////////////////////
public function insertId() {
if (!$this->connection) return 0;
return $this->connection->lastInsertId();
}
////////////////////////////////////////////////////////////////////////////
// GET THE NUMBER OF ROWS AFFECTED BY THE MOST RECENT SQL QUERY
////////////////////////////////////////////////////////////////////////////
public function updated() {
return $this->updated;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE ERROR CODE FOR THE MOST RECENT FUNCTION CALL
// http://php.net/manual/en/pdo.errorcode.php
////////////////////////////////////////////////////////////////////////////
public function errno() {
if (!$this->connection) return 0;
$error = $this->connection->errorCode();
$error = ltrim($error, '0');
return ($error === '') ? 0 : $error;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS A STRING DESCRIPTION OF THE LAST ERROR
// http://php.net/manual/en/pdo.errorinfo.php
////////////////////////////////////////////////////////////////////////////
public function error() {
if (!$this->connection) return false;
$error = $this->connection->errorInfo();
if (pudl_array($error)) $error = implode(' - ', $error);
$error = ltrim($error, '0');
$error = rtrim($error, ' -');
return ($error === '') ? false : $error;
}
////////////////////////////////////////////////////////////////////////////
// CHECK IF WE'RE CURRENTLY INSIDE OF A TRANSACTION
// http://php.net/manual/en/pdo.intransaction.php
////////////////////////////////////////////////////////////////////////////
public function inTransaction() {
if (!$this->connection) return false;
return $this->connection->inTransaction();
}
////////////////////////////////////////////////////////////////////////////
// BEGIN A NEW TRANSACTION
// http://php.net/manual/en/pdo.begintransaction.php
////////////////////////////////////////////////////////////////////////////
public function begin($snapshot=false) {
if ($this->connection) $this->connection->beginTransaction();
return $this;
}
////////////////////////////////////////////////////////////////////////////
// END THE CURRENT TRANSACTION BY COMMITTING IT
// http://php.net/manual/en/pdo.commit.php
////////////////////////////////////////////////////////////////////////////
public function commit($sync=false) {
if ($this->connection) $this->connection->commit();
if ($sync) $this->sync();
return $this;
}
////////////////////////////////////////////////////////////////////////////
// END THE CURRENT TRANSACTION BY ROLLING BACK THE CHANGES IT MADE
// http://php.net/manual/en/pdo.rollback.php
////////////////////////////////////////////////////////////////////////////
public function rollback($savepoint=NULL) {
if ($this->connection) $this->connection->rollBack();
return $this;
}
////////////////////////////////////////////////////////////////////////////
// MEMBER VARIABLES
////////////////////////////////////////////////////////////////////////////
private $updated = 0;
}