PDO.class.php
11.3 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/** File PDO.class.php *
* Porting of native PHP 5.1 PDO *
* object usable with PHP 4.X.X *
* and PHP 5.0.X version. *
* ------------------------------------ *
*(C) Andrea Giammarchi [2005/10/19] *
* ____________________________________ */
// check and preserve native PDO driver, for PHP4 or PHP 5.0 users
if(!class_exists('PDO')) {
// SUPPORTED STATIC ENVIROMENT VARIABLES
define('PDO_ATTR_SERVER_VERSION', 4); // server version
define('PDO_ATTR_CLIENT_VERSION', 5); // client version
define('PDO_ATTR_SERVER_INFO', 6); // server informations
define('PDO_ATTR_PERSISTENT', 12); // connection mode, persistent or normal
// SUPPORTED STATIC PDO FETCH MODE VARIABLES
define('PDO_FETCH_ASSOC', 2); // such mysql_fetch_assoc
define('PDO_FETCH_NUM', 3); // such mysql_fetch_row
define('PDO_FETCH_BOTH', 4); // such mysql_fetch_array
define('PDO_FETCH_OBJ', 5); // such mysql_fetch_object
// UNSUPPORTED STATIC PDO FETCH MODE VARIABLES
define('PDO_FETCH_LAZY', 1); // usable but not supported, default is PDO_FETCH_BOTH and will be used
define('PDO_FETCH_BOUND', 6); // usable but not supported, default is PDO_FETCH_BOTH and will be used
/**
* Class PDO
* PostgreSQL, SQLITE and MYSQL PDO support for PHP 4.X.X or PHP 5.0.X users, compatible with PHP 5.1.0 (RC1).
*
* DESCRIPTION [directly from http://us2.php.net/manual/en/ref.pdo.php]
* The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.
* Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.
* Note that you cannot perform any database functions using the PDO extension by itself;
* you must use a database-specific PDO driver to access a database server.
*
* HOW TO USE
* To know how to use PDO driver and all its methods visit php.net wonderful documentation.
* http://us2.php.net/manual/en/ref.pdo.php
* In this class some methods are not available and actually this porting is only for MySQL, SQLITE and PostgreSQL.
*
* LIMITS
* For some reasons ( time and php used version with this class ) some PDO methods are not availables and
* someother are not totally supported.
*
* PDO :: UNSUPPORTED METHODS:
* - beginTransaction [ mysql 3 has not transaction and manage them is possible only with a direct BEGIN
* or COMMIT query ]
* - commit
* - rollback
*
* PDO :: NOT TOTALLY SUPPORTED METHODS:
* - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
* - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
* - lastInsertId [ only fo PostgreSQL , returns only pg_last_oid ]
*
* - - - - - - - - - - - - - - - - - - - -
*
* PDOStatement :: UNSUPPORTED METHODS:
* - bindColumn [ is not possible to undeclare a variable and using global scope is not
* really a good idea ]
*
* PDOStatement :: NOT TOTALLY SUPPORTED METHODS:
* - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
* - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
* - setFetchMode [ supports only PDO_FETCH_NUM, PDO_FETCH_ASSOC, PDO_FETCH_OBJ and
* PDO_FETCH_BOTH database reading mode ]
* ---------------------------------------------
* @Compatibility >= PHP 4
* @Dependencies PDO_mysql.class.php
* PDO_sqlite.class.php
* PDOStatement_mysql.class.php
* PDOStatement_sqlite.class.php
* @Author Andrea Giammarchi
* @Site http://www.devpro.it/
* @Mail andrea [ at ] 3site [ dot ] it
* @Date 2005/10/13
* @LastModified 2005/12/01 21:40
* @Version 0.1b - tested, supports only PostgreSQL, MySQL or SQLITE databases
*/
class PDO {
/** Modified on 2005/12/01 to support new PDO constants on PHP 5.1.X */
const FETCH_ASSOC = PDO_FETCH_ASSOC;
const FETCH_NUM = PDO_FETCH_NUM;
const FETCH_BOTH = PDO_FETCH_BOTH;
const FETCH_OBJ = PDO_FETCH_OBJ;
const FETCH_LAZY = PDO_FETCH_LAZY;
const FETCH_BOUND = PDO_FETCH_BOUND;
const ATTR_SERVER_VERSION = PDO_ATTR_SERVER_VERSION;
const ATTR_CLIENT_VERSION = PDO_ATTR_CLIENT_VERSION;
const ATTR_SERVER_INFO = PDO_ATTR_SERVER_INFO;
const ATTR_PERSISTENT = PDO_ATTR_PERSISTENT;
/**
* 'Private' variables:
* __driver:PDO_* Dedicated PDO database class
*/
var $__driver;
/**
* Public constructor
* http://us2.php.net/manual/en/function.pdo-construct.php
*/
function PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
$con = &$this->__getDNS($string_dsn);
if($con['dbtype'] === 'mysql') {
require_once('PDO_mysql.class.php');
if(isset($con['port']))
$con['host'] .= ':'.$con['port'];
$this->__driver = new PDO_mysql(
$con['host'],
$con['dbname'],
$string_username,
$string_password
);
}
elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
require_once('PDO_sqlite.class.php');
$this->__driver = new PDO_sqlite($con['dbname']);
}
elseif($con['dbtype'] === 'pgsql') {
require_once('PDO_pgsql.class.php');
$string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
if(isset($con['port']))
$string_dsn .= " port={$con['port']}";
$this->__driver = new PDO_pgsql($string_dsn);
}
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-begintransaction.php
*/
function beginTransaction() {
$this->__driver->beginTransaction();
}
/** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER
* Public method
* Calls database_close function.
* this->close( Void ):Boolean
* @Return Boolean True on success, false otherwise
*/
function close() {
return $this->__driver->close();
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-commit.php
*/
function commit() {
$this->__driver->commit();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-exec.php
*/
function exec($query) {
return $this->__driver->exec($query);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-errorcode.php
*/
function errorCode() {
return $this->__driver->errorCode();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-errorinfo.php
*/
function errorInfo() {
return $this->__driver->errorInfo();
}
/** NOT TOTALLY UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-getattribute.php
*/
function getAttribute($attribute) {
return $this->__driver->getAttribute($attribute);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-lastinsertid.php
*/
function lastInsertId() {
return $this->__driver->lastInsertId();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-prepare.php
*/
function prepare($query, $array = Array()) {
return $this->__driver->prepare($query, $array = Array());
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-query.php
*/
function query($query) {
return $this->__driver->query($query);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-quote.php
*/
function quote($string) {
return $this->__driver->quote($string);
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-rollback.php
*/
function rollBack() {
$this->__driver->rollBack();
}
/** NOT TOTALLY UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-setattribute.php
*/
function setAttribute($attribute, $mixed) {
return $this->__driver->setAttribute($attribute, $mixed);
}
// PRIVATE METHOD [uncommented]
function __getDNS(&$string) {
$result = array();
$pos = strpos($string, ':');
$parameters = explode(';', substr($string, ($pos + 1)));
$result['dbtype'] = strtolower(substr($string, 0, $pos));
for($a = 0, $b = count($parameters); $a < $b; $a++) {
$tmp = explode('=', $parameters[$a]);
if(count($tmp) == 2)
$result[$tmp[0]] = $tmp[1];
else
$result['dbname'] = $parameters[$a];
}
return $result;
}
}
}
// If you have PHP 5.1 but want to test this class, declare PDO variables as _PDO variables
else {
/**
* Class _PDO
* (C) Andrea Giammarchi
* Please read PDO class comments to know more
*/
class _PDO {
const FETCH_ASSOC = PDO::FETCH_ASSOC;
const FETCH_NUM = PDO::FETCH_NUM;
const FETCH_BOTH = PDO::FETCH_BOTH;
const FETCH_OBJ = PDO::FETCH_OBJ;
const FETCH_LAZY = PDO::FETCH_LAZY;
const FETCH_BOUND = PDO::FETCH_BOUND;
const ATTR_SERVER_VERSION = PDO::ATTR_SERVER_VERSION;
const ATTR_CLIENT_VERSION = PDO::ATTR_CLIENT_VERSION;
const ATTR_SERVER_INFO = PDO::ATTR_SERVER_INFO;
const ATTR_PERSISTENT = PDO::ATTR_PERSISTENT;
var $__driver;
function _PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
$con = &$this->__getDNS($string_dsn);
if($con['dbtype'] === 'mysql') {
require_once('PDO_mysql.class.php');
if(isset($con['port']))
$con['host'] .= ':'.$con['port'];
$this->__driver = new PDO_mysql(
$con['host'],
$con['dbname'],
$string_username,
$string_password
);
}
elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
require_once('PDO_sqlite.class.php');
$this->__driver = new PDO_sqlite($con['dbname']);
}
elseif($con['dbtype'] === 'pgsql') {
require_once('PDO_pgsql.class.php');
$string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
if(isset($con['port']))
$string_dsn .= " port={$con['port']}";
$this->__driver = new PDO_pgsql($string_dsn);
}
}
function beginTransaction() {
$this->__driver->beginTransaction();
}
function close() {
return $this->__driver->close();
}
function commit() {
$this->__driver->commit();
}
function exec($query) {
return $this->__driver->exec($query);
}
function errorCode() {
return $this->__driver->errorCode();
}
function errorInfo() {
return $this->__driver->errorInfo();
}
function getAttribute($attribute) {
return $this->__driver->getAttribute($attribute);
}
function lastInsertId() {
return $this->__driver->lastInsertId();
}
function prepare($query, $array = Array()) {
return $this->__driver->prepare($query, $array = Array());
}
function query($query) {
return $this->__driver->query($query);
}
function quote($string) {
return $this->__driver->quote($string);
}
function rollBack() {
$this->__driver->rollBack();
}
function setAttribute($attribute, $mixed) {
return $this->__driver->setAttribute($attribute, $mixed);
}
function __getDNS(&$string) {
$result = array();
$pos = strpos($string, ':');
$parameters = explode(';', substr($string, ($pos + 1)));
$result['dbtype'] = strtolower(substr($string, 0, $pos));
for($a = 0, $b = count($parameters); $a < $b; $a++) {
$tmp = explode('=', $parameters[$a]);
if(count($tmp) == 2)
$result[$tmp[0]] = $tmp[1];
else
$result['dbname'] = $parameters[$a];
}
return $result;
}
}
}
?>