-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdodb.php
311 lines (257 loc) · 6.91 KB
/
pdodb.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* MysqliDb Class
*
* @category Database Access
* @package PdoDb
* @author Emmanuel O. Sobande <eosobande@gmail.com>
* @copyright Copyright (c) 2016
* @link http://github.com/eosobande/pdo-database-class
* @version 1.0
*/
trait PDOdbRef {
public function __db_instance_ref() {
global $db;
$this->db =& $db; // db reference
}
}
class PDOdb extends SQLBuilder {
/**
* Result fetch types
* @var int
*/
const _FETCH_ALL = 1;
const _FETCH_ONE = 2;
const _FETCH_ONE_FIELD = 3;
const _FETCH_FIRST_FROM_EACH_ROW = 4;
/**
* Database Server
* @var string
*/
private $server_name;
/**
* Database Name
* @var string
*/
private $db_name;
/**
* Database Username
* @var string
*/
private $db_user;
/**
* Database password
* @var string
*/
private $db_password;
/**
* Previously executed SQL query
* @var string
*/
private $last_sql;
/**
* Variable that holds the result of a SQL Query
* @var boolean
*/
private $result;
/**
* Variable that holds the number of rows returned|affected by SELECT/UPDATE/DELETE statements
* @var integer
*/
private $row_count;
/**
* The prepared statement object
* @var object
*/
private $stmt;
/**
* Instance of the PDO class
* @var boolean
*/
private $pdo;
/**
* The type of database we've connected to
* @var string
*/
private $database_type;
/**
* __construct method creates the database connection
*
* @param string $server_name
* @param string $db_name
* @param string $db_user
* @param string $db_pass
*/
public function __construct($server_name, $db_name=NULL, $db_user=NULL, $db_password=NULL) {
try {
if ($db_name && $db_user) {
$this->pdo = new PDO("mysql:host=$server_name;dbname=$db_name", $db_user, $db_password);
$this->database_type = 'MYSQL';
} else {
$this->pdo = new PDO("sqlite:$server_name");
$this->database_type = 'SQLITE';
}
$this->server_name = $server_name;
$this->db_name = $db_name;
$this->db_user = $db_user;
$this->db_password = $db_password;
// set the PDO error mode to exception
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// Connection failed
die($e->getMessage());
}
}
/**
* A method to close the PDO database connection
*
* @return void
*/
public function close() {
$this->db->pdo = NULL;
}
/**
* A method to instantiate a new PDO class if old instance times out
*
* @return void
*/
public function pdo_reconnect() {
$this->pdo = new PDOdb($this->server_name, $this->db_name, $this->db_user, $this->db_password);
}
/**
* Method returns $row_count
*
* @return int
*/
public function row_count() {
return $this->stmt->rowCount();
}
/**
* Method returns last sql statement
*
* @return string
*/
public function last_sql() {
return $this->last_sql;
}
/**
* A method of accessing the id of the last insert statement
*
* @uses PDO::lastInsertId() method
*
* @return int
*/
public function last_insert_id() {
return $this->pdo->lastInsertId();
}
/**
* A method of accessing error information from SQL execution
*
* @uses PDO::errorinfo() method
*
* @return string
*/
public function error_info() {
return $this->pdo->errorinfo();
}
/**
* Begin a Transaction (MULTIPLE QUERY)
*
* @uses PDO::beginTransaction()
* @return void
*/
public function begin_transaction() {
$this->pdo->beginTransaction();
}
/**
* Commit Transaction (MULTIPLE QUERY)
*
* @uses PDO::commit()
* @uses PDO::rollback()
*
* @return void
*/
public function commit() {
try {
$this->result = $this->pdo->commit();
} catch(PDOException $e) {
// roll back the transaction if something failed
$this->pdo->rollback();
echo $e->getMessage();
}
}
/**
* Method prepares an SQL statement
*
* @uses PDO::prepare($sql)
*
* @param array $options
*
* @return boolean
*/
public function prepare($options=[]) {
$this->stmt = $this->pdo->prepare($this->sql, $options);
// Set the last_sql variable
$this->last_sql = $this->sql;
return is_object($this->stmt);
}
/**
* Method executes a prepared statement
*
* @param array $args
* @return int
*/
public function execute($args=[], $prepared_stmt=FALSE) {
if (!$prepared_stmt && (empty($args) || is_array($args))) {
$this->prepare();
} elseif ($args && !is_array($args)) {
throw new Exception(__METHOD__.' expects parameter 1 to be array, '.strtoupper(gettype($args)).' given', 1);
}
try {
$this->result = $this->stmt->execute($args);
} catch (PDOException $e) {
if ($e->getCode() != 'HY000' || !stristr($e->getMessage(), 'server has gone away')) {
throw $e;
}
// Server has gone away, reconnect
$this->pdo_reconnect($prepared_stmt);
$this->result = $this->stmt->execute($args);
}
$this->unset_sql();
return $this->row_count();
}
/**
* Method returns a result set
*
* @param string $fetch_type
*
* @uses PDOStatement::setFetchMode(PDO::FETCH_ASSOC)
*
* @return array|string|int
*/
public function fetch($fetch_type=self::_FETCH_ALL, $exec=TRUE) {
if ($exec) {
$this->execute();
}
$this->result = $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
$rows = $fetch_type == self::_FETCH_ONE || $fetch_type == self::_FETCH_ONE ? $this->stmt->fetch() : $this->stmt->fetchAll();
if (!$rows) {
return NULL;
}
switch ($fetch_type) {
case self::_FETCH_ONE:
case self::_FETCH_ALL:
case NULL:
return $rows;
break;
case self::_FETCH_FIRST_FROM_EACH_ROW:
return array_map('array_shift', $rows);
break;
case self::_FETCH_ONE_FIELD:
$row = array_shift($rows);
return array_shift($row);
break;
}
}
}
$db = new PDOdb(DB_SERVER,DB_NAME,DB_USER,DB_PASS);