-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.php
258 lines (226 loc) · 6.75 KB
/
postgresql.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
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
<?php
/*
PHP REST SQL: A HTTP REST interface to relational databases
written in PHP
postgresql.php :: PostgreSQL database adapter
Copyright (C) 2008 Guido De Rosa <[email protected]>
based on MySQL driver mysql.php by Paul James
Copyright (C) 2004 Paul James <[email protected]>
This program 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.
This program 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
*/
/* $id$ */
/**
* PHP REST PostgreSQL class
* PostgreSQL connection class.
*/
class postgresql {
/**
* @var int
*/
var $lastInsertPKeys;
/**
* @var resource
*/
var $lastQueryResultResource;
/**
* @var resource Database resource
*/
var $db;
/**
* Connect to the database.
* @param str[] config
*/
function connect($config) {
$connString = sprintf(
'host=%s dbname=%s user=%s password=%s',
$config['server'],
$config['database'],
$config['username'],
$config['password']
);
if ($this->db = pg_pconnect($connString)) {
return TRUE;
}
return FALSE;
}
/**
* Close the database connection.
*/
function close() {
pg_close($this->db);
}
/**
* Get the columns in a table.
* @param str table
* @return resource A resultset resource
*/
function getColumns($table) {
$qs = sprintf('SELECT * FROM information_schema.columns WHERE table_name =\'%s\'', $table);
return pg_query($qs, $this->db);
}
/**
* Get a row from a table.
* @param str table
* @param str where
* @return resource A resultset resource
*/
function getRow($table, $where) {
$result = pg_query(sprintf('SELECT * FROM %s WHERE %s', $table, $where));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the rows in a table.
* @param str primary The names of the primary columns to return
* @param str table
* @return resource A resultset resource
*/
function getTable($primary, $table) {
$result = pg_query(sprintf('SELECT %s FROM %s', $primary, $table));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the tables in a database.
* @return resource A resultset resource
*/
function getDatabase() {
return pg_query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\'');
}
/**
* Get the primary keys for the request table.
* @return str[] The primary key field names
*/
function getPrimaryKeys($table) {
$i = 0;
$primary = NULL;
do {
$query = sprintf('SELECT pg_attribute.attname
FROM pg_class, pg_attribute, pg_index
WHERE pg_class.oid = pg_attribute.attrelid AND
pg_class.oid = pg_index.indrelid AND
pg_index.indkey[%d] = pg_attribute.attnum AND
pg_index.indisprimary = \'t\'
and relname=\'%s\'',
$i,
$table
);
$result = pg_query($query);
$row = pg_fetch_assoc($result);
if ($row) {
$primary[] = $row['attname'];
}
$i++;
} while ($row);
return $primary;
}
/**
* Update a row.
* @param str table
* @param str values
* @param str where
* @return bool
*/
function updateRow($table, $values, $where) {
# translate from MySQL syntax :)
$values = preg_replace('/"/','\'',$values);
$values = preg_replace('/`/','"',$values);
$qs = sprintf('UPDATE %s SET %s WHERE %s', $table, $values, $where);
$result = pg_query($qs);
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Insert a new row.
* @param str table
* @param str names
* @param str values
* @return bool
*/
function insertRow($table, $names, $values) {
# translate from MySQL syntax
$names = preg_replace('/`/', '"', $names); #backticks r so MySQL-ish! ;)
$values = preg_replace('/"/', '\'', $values);
$pkeys = join(', ', $this->getPrimaryKeys($table));
$qs = sprintf(
'INSERT INTO $table ("%s") VALUES ("%s") RETURNING (%s)',
$names,
$values,
$pkeys
);
$result = pg_query($qs); #or die(pg_last_error());
$lastInsertPKeys = pg_fetch_row($result);
$this->lastInsertPKeys = $lastInsertPKeys;
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the columns in a table.
* @param str table
* @return resource A resultset resource
*/
function deleteRow($table, $where) {
$result = pg_query(sprintf('DELETE FROM %s WHERE %s', $table, $where));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Escape a string to be part of the database query.
* @param str string The string to escape
* @return str The escaped string
*/
function escape($string) {
return pg_escape_string($string);
}
/**
* Fetch a row from a query resultset.
* @param resource resource A resultset resource
* @return str[] An array of the fields and values from the next row in the resultset
*/
function row($resource) {
return pg_fetch_assoc($resource);
}
/**
* The number of rows in a resultset.
* @param resource resource A resultset resource
* @return int The number of rows
*/
function numRows($resource) {
return pg_num_rows($resource);
}
/**
* The number of rows affected by a query.
* @return int The number of rows
*/
function numAffected() {
return pg_affected_rows($this->lastQueryResultResource);
}
/**
* Get the ID of the last inserted record.
* @return int The last insert ID ('a/b' in case of multi-field primary key)
*/
function lastInsertId() {
return join('/', $this->lastInsertPKeys);
}
}
?>