-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdo-dbi.php
137 lines (115 loc) · 3.78 KB
/
pdo-dbi.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
<?php
/* Copyright 2005-2008 Andrew A. Bakun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# simple wrapper class to make PHP's PDO be somewhat more Perl DBI-ish
# and increase database backend portability
#
# sample connection strings
# $dbh = new PDODBI('odbc:NAME', 'user', 'pass');
# $dbh = new PDODBI('mysql:host=x;dbname=y', 'user', 'pass');
# $dbh = new PDODBI('sqlite:/opt/databases/mydb.sq3');
# $dbh = new PDODBI('sqlite::memory:');
# $dbh = new PDODBI('sqlite2:/opt/databases/mydb.sq2');
# $dbh = new PDODBI('sqlite2::memory:');
#
# error handling could use some review/improvement
#
# this code is experimental
class PDODBI extends PDO {
public function prepare($s, $do=array()) {
#$x = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
#if ($x === 'mysql') { $do[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = true; }
#$s = preg_replace("/\n+/", '', $s);
#print "<pre>"; print_r($s); print "</pre>";
$x = parent::prepare($s, $do);
if (!$x) {
#$tb = debug_backtrace();
#$where = sprintf('%s:%d', $tb[0]['file'], $tb[0]['line']);
$ex = join("\n", $this->errorInfo());
throw new Exception($ex);
}
$x = & new PDODBIstmt($x);
return $x;
}
public function insert_id() {
return $this->lastInsertId();
}
}
class PDODBIstmt {
private $sth;
public function __construct($stmthandle) {
$this->sth = $stmthandle;
}
public function __destruct() {
$this->finish();
}
public function __call($n, $a) {
$callback = array($this->sth, $n);
return call_user_func_array($callback, $a);
}
public function execute() {
$a = func_get_args();
return $this->sth->execute($a);
}
public function execute_array($a) {
return $this->sth->execute($a);
}
public function fetchrow_arrayref() {
return $this->fetchrow_array();
}
public function fetchrow_array() {
return $this->sth->fetch(PDO::FETCH_NUM);
}
public function fetchrow_hashref() {
return $this->fetchrow_hash();
}
# some drivers (mysql) return column names with the case specified
# other drivers (interbase/firebird) return column names forcefully capitalized
# thus, making string hash indexes non-portable, so force everything to lowercase
public function fetchrow_hash() {
$r = $this->sth->fetch(PDO::FETCH_ASSOC);
if (!$r) { return $r; }
$n = array();
foreach ($r as $k=>$v) {
$n[strtolower($k)] = $v;
}
return $n;
}
# same as above
public function fetchrow_object() {
$r = $this->sth->fetch(PDO::FETCH_OBJ);
if (!$r) { return $r; }
$c = get_class($r);
$n = & new $c();
foreach (get_object_vars($r) as $k=>$v) {
$k = strtolower($k);
@ $n->$k = $v;
}
return $n;
}
public function affected_rows() {
return $this->sth->rowCount();
}
public function num_rows() {
return NULL;
}
public function finish() {
$this->sth->closeCursor();
# do nothing, nothing to be done; compatiblity only
}
public function _stmt() {
return $this->sth->queryString;
}
}