1 |
dpavlin |
1.1 |
<?PHP // -*-Mode: C++;-*- |
2 |
|
|
/* |
3 |
|
|
Joseph Artsimovich (joseph_a<at>mail<dot>ru) |
4 |
|
|
DBD::Pg -- The PostgreSQL Driver. |
5 |
|
|
See: http://evil.inetarena.com/php/DBI.php for more info. |
6 |
|
|
|
7 |
|
|
Licence: LGPL (http://www.gnu.org/). |
8 |
|
|
|
9 |
|
|
This software is provided "AS IS" with no warrenty either implicit, |
10 |
|
|
explicit or otherwise. Use at your own risk. |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
Notes: |
14 |
|
|
|
15 |
|
|
o insert_id is not implemented |
16 |
|
|
*/ |
17 |
|
|
|
18 |
|
|
class DBD_Pg { |
19 |
|
|
|
20 |
|
|
var $is_persisent; |
21 |
|
|
var $DBI; |
22 |
|
|
var $exit_on_fail = 0; |
23 |
|
|
var $dbh; |
24 |
|
|
var $last_oid; |
25 |
|
|
|
26 |
|
|
function DBD_connect( $db_name, $db_host = '', |
27 |
|
|
$db_user = '', $db_passwd = '', |
28 |
|
|
$is_persistent = 0){ |
29 |
|
|
|
30 |
|
|
//Track the $is_persistant just for fun... |
31 |
|
|
$is_persistent = $is_persistent ? $is_persistent : 0; |
32 |
|
|
$this->is_persistent = $is_persistent; |
33 |
|
|
|
34 |
|
|
//DEBUG... |
35 |
|
|
//echo "DBD::Pg: [$db_name][$db_host][$db_user][$is_persistent]<br>\n"; |
36 |
|
|
|
37 |
|
|
$str = ''; |
38 |
|
|
if ($db_host) $str .= "host=$db_host "; |
39 |
|
|
if ($db_name) $str .= "dbname=$db_name "; |
40 |
|
|
if ($db_user) $str .= "user=$db_user "; |
41 |
|
|
if ($db_passwd) $str .= "password=$db_passwd "; |
42 |
|
|
|
43 |
|
|
$str = chop($str); |
44 |
|
|
|
45 |
|
|
if( $is_persistent ){ |
46 |
|
|
$conn = pg_pconnect( $str ); |
47 |
|
|
} else { |
48 |
|
|
$conn = pg_connect( $str ); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if( !$conn ){ |
52 |
|
|
$this->errstr = "DBD::Pg Error: Could not connect to the database."; |
53 |
|
|
return; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
$this->dbh = $conn; |
57 |
|
|
return( $this ); |
58 |
|
|
}//end connect function |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
function disconnect( ){ |
63 |
|
|
pg_close( $this->dbh ); |
64 |
|
|
$this->dbh = undef; |
65 |
|
|
return( 1 ); |
66 |
|
|
}//end disconnect function |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
function prepare( $query ){ |
70 |
|
|
if( ! $this->dbh ){ return; } |
71 |
|
|
//Does nothing but save the query... |
72 |
|
|
$sth = new STH_Pg; |
73 |
|
|
$sth->DBH( $this ); |
74 |
|
|
$sth->prepare( $query ); |
75 |
|
|
return( $sth ); |
76 |
|
|
}//end dbh prepare fn |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
//do is a reserved word so I have to name this something else. |
80 |
|
|
function dbh_do( $query ){ |
81 |
|
|
//run the query and return the number of affected rows. |
82 |
|
|
$result = pg_exec( $this->dbh, $query ); |
83 |
|
|
if( ! $result ){ |
84 |
|
|
$this->errstr = "DBI Error: 'dbh_do' failed: ". |
85 |
|
|
pg_errormessage( $this->dbh ); |
86 |
|
|
|
87 |
|
|
if( $this->exit_on_fail ){ |
88 |
|
|
echo $this->errstr."<br>\n"; |
89 |
|
|
$this->disconnect( ); |
90 |
|
|
exit( ); } |
91 |
|
|
|
92 |
|
|
return; |
93 |
|
|
} |
94 |
|
|
$this->last_oid=pg_GetLastOid( $result ); |
95 |
|
|
$rows = pg_cmdtuples( $result ); |
96 |
|
|
if( $rows == 0 ){ $rows = '0E0'; } |
97 |
|
|
return( $rows ); |
98 |
|
|
}//end fn do |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
function insert_id( ){ |
102 |
|
|
//Get the last serial number from an insert. |
103 |
|
|
$this->errstr = "DBI Error: 'insertid' unimplemented."; |
104 |
|
|
return( -1 ); |
105 |
|
|
}//end fn do |
106 |
|
|
|
107 |
|
|
function last_oid( ){ |
108 |
|
|
return( $this->last_oid ); |
109 |
|
|
}//end fn do |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
function quote( $string ){ |
113 |
|
|
if( $string ){ |
114 |
|
|
return( "'".addslashes( $string )."'" ); |
115 |
|
|
} else { |
116 |
|
|
return( 'NULL' ); |
117 |
|
|
} |
118 |
|
|
}//end fn quote |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
}//end DBI class |
122 |
|
|
|
123 |
|
|
|
124 |
|
|
//=========================================================== |
125 |
|
|
|
126 |
|
|
class STH_Pg { |
127 |
|
|
|
128 |
|
|
var $query; |
129 |
|
|
var $DBH; |
130 |
|
|
var $dbh; |
131 |
|
|
var $result; |
132 |
|
|
var $row = 0; |
133 |
|
|
|
134 |
|
|
function STH( $dbh = '' ){ |
135 |
|
|
if( $dbh ){ |
136 |
|
|
$this->dbh = $dbh; |
137 |
|
|
} |
138 |
|
|
return( $this ); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
function DBH( $dbh ){ |
143 |
|
|
$this->DBH = $dbh; //watch out, this copies the object not the reference. |
144 |
|
|
$this->dbh = $dbh->dbh; |
145 |
|
|
return( $dbh ); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
function prepare( $query ){ |
150 |
|
|
if( ! $this->dbh ){ return; } |
151 |
|
|
$this->query = $query; |
152 |
|
|
return( $query ); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
function execute( ){ |
157 |
|
|
if( ! $this->dbh ){ return; } |
158 |
|
|
//echo "Running query $this->query<br>\n"; |
159 |
|
|
$this->result = pg_exec( $this->dbh, $this->query ); |
160 |
|
|
|
161 |
|
|
if( ! $this->result ){ |
162 |
|
|
$this->errstr = "STH Execute failed: ".pg_errormessage( $this->dbh ); |
163 |
|
|
return; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
return( 1 ); |
167 |
|
|
}//end execute fn |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
function finish( ){ |
171 |
|
|
if( ! $this->result ){ return; } |
172 |
|
|
pg_freeresult( $this->result ); |
173 |
|
|
$this->result = undef; |
174 |
|
|
return( 1 ); |
175 |
|
|
}//end execute fn |
176 |
|
|
|
177 |
|
|
|
178 |
|
|
function rows( ){ |
179 |
|
|
return( pg_numrows( $this->result ) ); |
180 |
|
|
} //end rows fn |
181 |
|
|
|
182 |
|
|
function fetchrow_array( ){ |
183 |
|
|
if( ! $this->result ){ |
184 |
|
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
185 |
|
|
return; |
186 |
|
|
} |
187 |
|
|
if ($this->row >= pg_numrows($this->result)) return 0; |
188 |
|
|
return( pg_fetch_row( $this->result, $this->row++ )); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
function fetchrow_hash( ){ |
193 |
|
|
if( ! $this->result ){ |
194 |
|
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
195 |
|
|
return; |
196 |
|
|
} |
197 |
|
|
if ($this->row >= pg_numrows($this->result)) return 0; |
198 |
|
|
return( pg_fetch_array( $this->result, $this->row++ )); |
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
|
203 |
|
|
$GLOBALS[classDBDPg_read] = 1; |