Added new binary escape methods
[isso.git] / db_postgresql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * PostgreSQL Database Abstraction Layer
24 * db_postgresql.php
25 *
26 * @package ISSO
27 */
28
29 $this->load('db', null);
30
31 /**
32 * PostgreSQL Database Abstraction Layer
33 *
34 * This framework is a function wrapper for PostgresQL functions so we can have
35 * better error reporting and query reporting.
36 *
37 * @author Iris Studios, Inc.
38 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class DB_PostgreSQL extends DB_Abstract
44 {
45 /**
46 * Command mapping list
47 * @var array
48 * @access private
49 */
50 var $commands = array(
51 'pconnect' => '$this->command_pg_pconnect',
52 'connect' => '$this->command_pg_connect',
53 'query' => 'pg_query',
54 'error_num' => '$this->command_error_num',
55 'error_str' => '$this->command_error_str',
56 'escape_string' => '$this->command_pg_escape_string',
57 'escape_binary' => 'mysqli_real_escape_string',
58 'unescape_binary' => '$this->command_unescape_binary',
59 'escape_binary' => 'pg_escape_bytea',
60 'unescape_binary' => 'pg_unescape_bytea',
61 'fetch_assoc' => 'pg_fetch_assoc',
62 'fetch_row' => 'pg_fetch_row',
63 'fetch_object' => 'pg_fetch_object',
64 'free_result' => 'pg_free_result',
65 'insert_id' => '%link', // how do we support this...?
66 'num_rows' => 'pg_num_rows',
67 'affected_rows' => 'pg_affected_rows'
68 );
69
70 /**
71 * Port number to connect to
72 * @var integer
73 * @access private
74 */
75 var $port = 5432;
76
77 /**
78 * Fields array that is used in this module
79 * @var array
80 * @access private
81 */
82 var $fields = array(
83 'port' => array(REQ_NO, null, true)
84 );
85
86 // ###################################################################
87 /**
88 * Constructor
89 */
90 function __construct(&$registry)
91 {
92 parent::__construct($registry);
93 }
94
95 // ###################################################################
96 /**
97 * (PHP 4) Constructor
98 */
99 function DB_PostgreSQL(&$registry)
100 {
101 $this->__construct($registry);
102 }
103
104 // ###################################################################
105 /**
106 * Sets an ISSO field
107 *
108 * @access public
109 *
110 * @param string Field name
111 * @param mixed Value of the field
112 */
113 function set($name, $value)
114 {
115 $this->registry->do_set($name, $value, 'db_postgresql');
116 }
117
118 // ###################################################################
119 /**
120 * Gets an ISSO field
121 *
122 * @access public
123 *
124 * @param string Field name
125 *
126 * @return mixed Value of the field
127 */
128 function get($fieldname)
129 {
130 return $this->registry->do_get($fieldname, 'db_postgresql');
131 }
132
133
134 // ###################################################################
135 /**
136 * Wrapper: pg_connect
137 *
138 * @access protected
139 *
140 * @param string Server name
141 * @param string User name
142 * @param string Password
143 * @param string Database
144 *
145 * @return integer DB-Link
146 */
147 function command_pg_connect($server, $user, $password, $database)
148 {
149 $this->registry->check_isso_fields(get_class($this));
150 return pg_connect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
151 }
152
153 // ###################################################################
154 /**
155 * Wrapper: pg_pconnect
156 *
157 * @access protected
158 *
159 * @param string Server name
160 * @param string User name
161 * @param string Password
162 * @param string Database
163 *
164 * @return integer DB-Link
165 */
166 function command_pg_pconnect($server, $user, $password, $database)
167 {
168 $this->registry->check_isso_fields(get_class($this));
169 return pg_pconnect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
170 }
171
172 // ###################################################################
173 /**
174 * Wrapper: pg_escape_string
175 *
176 * @access protected
177 *
178 * @param integer DB-Link (unused)
179 * @param string Raw string
180 *
181 * @return string Escaped string
182 */
183 function command_pg_escape_string($link, $string)
184 {
185 return pg_escape_string($string);
186 }
187
188 // ###################################################################
189 /**
190 * Wrapper/no support: error string
191 *
192 * @access protected
193 *
194 * @param integer DB-Link
195 *
196 * @return string Error string
197 */
198 function command_error_str($link)
199 {
200 if ($this->result)
201 {
202 return pg_result_error($this->result);
203 }
204
205 return pg_last_error($link);
206 }
207
208 // ###################################################################
209 /**
210 * Not supported: error numbers
211 *
212 * @access protected
213 *
214 * @param integer DB-Link
215 *
216 * @return integer Returns -1 always
217 */
218 function command_error_num($link)
219 {
220 return -1;
221 }
222
223 // ###################################################################
224 /**
225 * Overload: insert_id
226 *
227 * @access public
228 *
229 * @param string Table name
230 * @param string Auto-up field
231 *
232 * @return integer Insert ID
233 */
234 function insert_id($table, $field)
235 {
236 $temp = $this->query_first("SELECT last_value FROM {$table}_{$field}_seq");
237 return $temp['last_value'];
238 }
239
240 // ###################################################################
241 /**
242 * Starts a database transaction
243 *
244 * @access public
245 */
246 function transaction_start()
247 {
248 $this->query("BEGIN");
249 }
250
251 // ###################################################################
252 /**
253 * Saves current transaction steps as a savepoint
254 *
255 * @access public
256 *
257 * @param string Named savepoint
258 */
259 function transaction_savepoint($name)
260 {
261 $this->query("SAVEPOINT $name");
262 }
263
264 // ###################################################################
265 /**
266 * Reverts a transaction back to a given savepoint
267 *
268 * @access public
269 *
270 * @param string Named savepoint
271 */
272 function transaction_rollback($name = null)
273 {
274 $this->query("ROLLBACK" . ($name != null ? " TO $name" : ""));
275 }
276
277 // ###################################################################
278 /**
279 * Commits a database transaction
280 *
281 * @access public
282 */
283 function transaction_commit()
284 {
285 $this->query("COMMIT");
286 }
287 }
288
289 /*=====================================================================*\
290 || ###################################################################
291 || # $HeadURL$
292 || # $Id$
293 || ###################################################################
294 \*=====================================================================*/
295 ?>