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