Newer
Older
Graham Cole
committed
<?php
/***********************************************************************
Graham Cole
committed
This file is part of PunBB.
PunBB is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
PunBB is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
************************************************************************/
// Make sure we have built in support for PostgreSQL
if (!function_exists('pg_connect'))
pun_exit('This PHP environment doesn\'t have PostgreSQL support built in. PostgreSQL support is required if you want to use a PostgreSQL database to run this forum. Consult the PHP documentation for further assistance.');
Graham Cole
committed
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
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $last_query_text = array();
var $in_transaction = 0;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($db_host != '')
{
if (strpos($db_host, ':') !== false)
{
list($db_host, $dbport) = explode(':', $db_host);
$connect_str[] = 'host='.$db_host.' port='.$dbport;
}
else
{
if ($db_host != 'localhost')
$connect_str[] = 'host='.$db_host;
}
}
if ($db_name)
$connect_str[] = 'dbname='.$db_name;
if ($db_username != '')
$connect_str[] = 'user='.$db_username;
if ($db_password != '')
$connect_str[] = 'password='.$db_password;
if ($p_connect)
$this->link_id = @pg_pconnect(implode(' ', $connect_str));
else
$this->link_id = @pg_connect(implode(' ', $connect_str));
if (!$this->link_id)
error('Unable to connect to PostgreSQL server', __FILE__, __LINE__);
else
return $this->link_id;
}
function start_transaction()
{
++$this->in_transaction;
return (@pg_query($this->link_id, 'BEGIN')) ? true : false;
}
function end_transaction()
{
--$this->in_transaction;
if (@pg_query($this->link_id, 'COMMIT'))
return true;
else
{
@pg_query($this->link_id, 'ROLLBACK');
return false;
}
}
function query($sql, $unbuffered = false) // $unbuffered is ignored since there is no pgsql_unbuffered_query()
{
if (strrpos($sql, 'LIMIT') !== false)
$sql = preg_replace('#LIMIT ([0-9]+),([ 0-9]+)#', 'LIMIT \\2 OFFSET \\1', $sql);
if (defined('PUN_SHOW_QUERIES'))
$q_start = get_microtime();
@pg_send_query($this->link_id, $sql);
$this->query_result = @pg_get_result($this->link_id);
if (pg_result_status($this->query_result) != PGSQL_FATAL_ERROR)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
++$this->num_queries;
$this->last_query_text[(int)$this->query_result] = $sql;
Graham Cole
committed
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
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_msg = @pg_result_error($this->query_result);
if ($this->in_transaction)
@pg_query($this->link_id, 'ROLLBACK');
--$this->in_transaction;
return false;
}
}
function result($query_id = 0, $row = 0)
{
return ($query_id) ? @pg_fetch_result($query_id, $row, 0) : false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @pg_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @pg_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @pg_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
}
function insert_id()
{
$query_id = $this->query_result;
if ($query_id && $this->last_query_text[$query_id] != '')
{
if (preg_match('/^INSERT INTO ([a-z0-9\_\-]+)/is', $this->last_query_text[$query_id], $table_name))
{
// Hack (don't ask)
if (substr($table_name[1], -6) == 'groups')
$table_name[1] .= '_g';
$temp_q_id = @pg_query($this->link_id, 'SELECT currval(\''.$table_name[1].'_id_seq\')');
return ($temp_q_id) ? intval(@pg_fetch_result($temp_q_id, 0)) : false;
}
}
return false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
if (!$query_id)
$query_id = $this->query_result;
return ($query_id) ? @pg_free_result($query_id) : false;
}
function escape($str)
{
return is_array($str) ? '' : pg_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = false;
/*
if (!empty($this->query_result))
{
$result['error_msg'] = trim(@pg_result_error($this->query_result));
if ($result['error_msg'] != '')
return $result;
}
$result['error_msg'] = (!empty($this->link_id)) ? trim(@pg_last_error($this->link_id)) : trim(@pg_last_error());
*/
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->in_transaction)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array('COMMIT', 0);
@pg_query($this->link_id, 'COMMIT');
}
if ($this->query_result)
@pg_free_result($this->query_result);
return @pg_close($this->link_id);
}
else
return false;
}
}