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 |
|
|
25 |
|
function DBD_connect( $db_name, $db_host = '', |
26 |
|
$db_user = '', $db_passwd = '', |
27 |
|
$is_persistent = 0){ |
28 |
|
|
29 |
|
//Track the $is_persistant just for fun... |
30 |
|
$is_persistent = $is_persistent ? $is_persistent : 0; |
31 |
|
$this->is_persistent = $is_persistent; |
32 |
|
|
33 |
|
//DEBUG... |
34 |
|
//echo "DBD::Pg: [$db_name][$db_host][$db_user][$is_persistent]<br>\n"; |
35 |
|
|
36 |
|
$str = ''; |
37 |
|
if ($db_host) $str .= "host=$db_host "; |
38 |
|
if ($db_name) $str .= "dbname=$db_name "; |
39 |
|
if ($db_user) $str .= "user=$db_user "; |
40 |
|
if ($db_passwd) $str .= "password=$db_passwd "; |
41 |
|
|
42 |
|
$str = chop($str); |
43 |
|
|
44 |
|
if( $is_persistent ){ |
45 |
|
$conn = pg_pconnect( $str ); |
46 |
|
} else { |
47 |
|
$conn = pg_connect( $str ); |
48 |
|
} |
49 |
|
|
50 |
|
if( !$conn ){ |
51 |
|
$this->errstr = "DBD::Pg Error: Could not connect to the database."; |
52 |
|
return; |
53 |
|
} |
54 |
|
|
55 |
|
$this->dbh = $conn; |
56 |
|
return( $this ); |
57 |
|
}//end connect function |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
function disconnect( ){ |
62 |
|
pg_close( $this->dbh ); |
63 |
|
$this->dbh = undef; |
64 |
|
return( 1 ); |
65 |
|
}//end disconnect function |
66 |
|
|
67 |
|
|
68 |
|
function prepare( $query ){ |
69 |
|
if( ! $this->dbh ){ return; } |
70 |
|
//Does nothing but save the query... |
71 |
|
$sth = new STH_Pg; |
72 |
|
$sth->DBH( $this ); |
73 |
|
$sth->prepare( $query ); |
74 |
|
return( $sth ); |
75 |
|
}//end dbh prepare fn |
76 |
|
|
77 |
|
|
78 |
|
//do is a reserved word so I have to name this something else. |
79 |
|
function dbh_do( $query ){ |
80 |
|
//run the query and return the number of affected rows. |
81 |
|
$result = pg_exec( $this->dbh, $query ); |
82 |
|
if( ! $result ){ |
83 |
|
$this->errstr = "DBI Error: 'dbh_do' failed: ". |
84 |
|
pg_errormessage( $this->dbh ); |
85 |
|
|
86 |
|
if( $this->exit_on_fail ){ |
87 |
|
echo $this->errstr."<br>\n"; |
88 |
|
$this->disconnect( ); |
89 |
|
exit( ); } |
90 |
|
|
91 |
|
return; |
92 |
|
} |
93 |
|
$rows = pg_cmdtuples( $result ); |
94 |
|
if( $rows == 0 ){ $rows = '0E0'; } |
95 |
|
return( $rows ); |
96 |
|
}//end fn do |
97 |
|
|
98 |
|
|
99 |
|
function insert_id( ){ |
100 |
|
//Get the last serial number from an insert. |
101 |
|
$this->errstr = "DBI Error: 'insertid' unimplemented."; |
102 |
|
return( -1 ); |
103 |
|
}//end fn do |
104 |
|
|
105 |
|
|
106 |
|
function quote( $string ){ |
107 |
|
if( $string ){ |
108 |
|
return( "'".addslashes( $string )."'" ); |
109 |
|
} else { |
110 |
|
return( 'NULL' ); |
111 |
|
} |
112 |
|
}//end fn quote |
113 |
|
|
114 |
|
|
115 |
|
}//end DBI class |
116 |
|
|
117 |
|
|
118 |
|
//=========================================================== |
119 |
|
|
120 |
|
class STH_Pg { |
121 |
|
|
122 |
|
var $query; |
123 |
|
var $DBH; |
124 |
|
var $dbh; |
125 |
|
var $result; |
126 |
|
var $row = 0; |
127 |
|
|
128 |
|
function STH( $dbh = '' ){ |
129 |
|
if( $dbh ){ |
130 |
|
$this->dbh = $dbh; |
131 |
|
} |
132 |
|
return( $this ); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
function DBH( $dbh ){ |
137 |
|
$this->DBH = $dbh; //watch out, this copies the object not the reference. |
138 |
|
$this->dbh = $dbh->dbh; |
139 |
|
return( $dbh ); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
function prepare( $query ){ |
144 |
|
if( ! $this->dbh ){ return; } |
145 |
|
$this->query = $query; |
146 |
|
return( $query ); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
function execute( ){ |
151 |
|
if( ! $this->dbh ){ return; } |
152 |
|
//echo "Running query $this->query<br>\n"; |
153 |
|
$this->result = pg_exec( $this->dbh, $this->query ); |
154 |
|
|
155 |
|
if( ! $this->result ){ |
156 |
|
$this->errstr = "STH Execute failed: ".pg_errormessage( $this->dbh ); |
157 |
|
return; |
158 |
|
} |
159 |
|
|
160 |
|
return( 1 ); |
161 |
|
}//end execute fn |
162 |
|
|
163 |
|
|
164 |
|
function finish( ){ |
165 |
|
if( ! $this->result ){ return; } |
166 |
|
pg_freeresult( $this->result ); |
167 |
|
$this->result = undef; |
168 |
|
return( 1 ); |
169 |
|
}//end execute fn |
170 |
|
|
171 |
|
|
172 |
|
function rows( ){ |
173 |
|
return( pg_numrows( $this->result ) ); |
174 |
|
} //end rows fn |
175 |
|
|
176 |
|
function fetchrow_array( ){ |
177 |
|
if( ! $this->result ){ |
178 |
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
179 |
|
return; |
180 |
|
} |
181 |
|
if ($this->row >= pg_numrows($this->result)) return 0; |
182 |
|
return( pg_fetch_row( $this->result, $this->row++ )); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
function fetchrow_hash( ){ |
187 |
|
if( ! $this->result ){ |
188 |
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
189 |
|
return; |
190 |
|
} |
191 |
|
if ($this->row >= pg_numrows($this->result)) return 0; |
192 |
|
return( pg_fetch_array( $this->result, $this->row++ )); |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
|
$GLOBALS[classDBDPg_read] = 1; |