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