Change the language call
[isso.git] / db_postgresql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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 Blue Static
38 * @copyright Copyright ©2002 - [#]year[#], Blue Static
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' => 'pg_escape_bytea',
58 'unescape_binary' => 'pg_unescape_bytea',
59 'fetch_assoc' => 'pg_fetch_assoc',
60 'fetch_row' => 'pg_fetch_row',
61 'fetch_object' => 'pg_fetch_object',
62 'free_result' => 'pg_free_result',
63 'insert_id' => '%link', // how do we support this...?
64 'num_rows' => 'pg_num_rows',
65 'affected_rows' => 'pg_affected_rows'
66 );
67
68 /**
69 * Port number to connect to
70 * @var integer
71 * @access private
72 */
73 var $port = 5432;
74
75 // ###################################################################
76 /**
77 * Constructor
78 */
79 function __construct(&$registry)
80 {
81 parent::__construct($registry);
82 }
83
84 // ###################################################################
85 /**
86 * (PHP 4) Constructor
87 */
88 function DB_PostgreSQL(&$registry)
89 {
90 $this->__construct($registry);
91 }
92
93 // ###################################################################
94 /**
95 * Sets the PGSQL port number
96 *
97 * @access public
98 *
99 * @param integer The port number
100 */
101 function setPort($port)
102 {
103 $this->port = $port;
104 }
105
106 // ###################################################################
107 /**
108 * Gets the currently-set port number
109 *
110 * @access public
111 *
112 * @return integer The port number
113 */
114 function getPort()
115 {
116 return $this->port;
117 }
118
119 // ###################################################################
120 /**
121 * Wrapper: pg_connect
122 *
123 * @access protected
124 *
125 * @param string Server name
126 * @param string User name
127 * @param string Password
128 * @param string Database
129 *
130 * @return integer DB-Link
131 */
132 function command_pg_connect($server, $user, $password, $database)
133 {
134 $this->registry->check_isso_fields(get_class($this));
135 return pg_connect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
136 }
137
138 // ###################################################################
139 /**
140 * Wrapper: pg_pconnect
141 *
142 * @access protected
143 *
144 * @param string Server name
145 * @param string User name
146 * @param string Password
147 * @param string Database
148 *
149 * @return integer DB-Link
150 */
151 function command_pg_pconnect($server, $user, $password, $database)
152 {
153 $this->registry->check_isso_fields(get_class($this));
154 return pg_pconnect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
155 }
156
157 // ###################################################################
158 /**
159 * Wrapper: pg_escape_string
160 *
161 * @access protected
162 *
163 * @param integer DB-Link (unused)
164 * @param string Raw string
165 *
166 * @return string Escaped string
167 */
168 function command_pg_escape_string($link, $string)
169 {
170 return pg_escape_string($string);
171 }
172
173 // ###################################################################
174 /**
175 * Wrapper/no support: error string
176 *
177 * @access protected
178 *
179 * @param integer DB-Link
180 *
181 * @return string Error string
182 */
183 function command_error_str($link)
184 {
185 if ($this->result)
186 {
187 return pg_result_error($this->result);
188 }
189
190 return pg_last_error($link);
191 }
192
193 // ###################################################################
194 /**
195 * Not supported: error numbers
196 *
197 * @access protected
198 *
199 * @param integer DB-Link
200 *
201 * @return integer Returns -1 always
202 */
203 function command_error_num($link)
204 {
205 return -1;
206 }
207
208 // ###################################################################
209 /**
210 * Overload: insert_id
211 *
212 * @access public
213 *
214 * @param string Table name
215 * @param string Auto-up field
216 *
217 * @return integer Insert ID
218 */
219 function insert_id($table, $field)
220 {
221 $temp = $this->query_first("SELECT last_value FROM {$table}_{$field}_seq");
222 return $temp['last_value'];
223 }
224
225 // ###################################################################
226 /**
227 * Starts a database transaction
228 *
229 * @access public
230 */
231 function transaction_start()
232 {
233 $this->query("BEGIN");
234 }
235
236 // ###################################################################
237 /**
238 * Saves current transaction steps as a savepoint
239 *
240 * @access public
241 *
242 * @param string Named savepoint
243 */
244 function transaction_savepoint($name)
245 {
246 $this->query("SAVEPOINT $name");
247 }
248
249 // ###################################################################
250 /**
251 * Reverts a transaction back to a given savepoint
252 *
253 * @access public
254 *
255 * @param string Named savepoint
256 */
257 function transaction_rollback($name = null)
258 {
259 $this->query("ROLLBACK" . ($name != null ? " TO $name" : ""));
260 }
261
262 // ###################################################################
263 /**
264 * Commits a database transaction
265 *
266 * @access public
267 */
268 function transaction_commit()
269 {
270 $this->query("COMMIT");
271 }
272 }
273
274 /*=====================================================================*\
275 || ###################################################################
276 || # $HeadURL$
277 || # $Id$
278 || ###################################################################
279 \*=====================================================================*/
280 ?>