Same change as the last commit
[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 public function __construct(&$registry)
86 {
87 parent::__construct($registry);
88 }
89
90 // ###################################################################
91 /**
92 * Sets an ISSO field
93 *
94 * @param string Field name
95 * @param mixed Value of the field
96 */
97 public function set($name, $value)
98 {
99 $this->registry->do_set($name, $value, 'db_postgresql');
100 }
101
102 // ###################################################################
103 /**
104 * Gets an ISSO field
105 *
106 * @param string Field name
107 *
108 * @return mixed Value of the field
109 */
110 public function get($fieldname)
111 {
112 return $this->registry->do_get($fieldname, 'db_postgresql');
113 }
114
115
116 // ###################################################################
117 /**
118 * Wrapper: pg_connect
119 *
120 * @param string Server name
121 * @param string User name
122 * @param string Password
123 * @param string Database
124 *
125 * @return integer DB-Link
126 */
127 protected function command_pg_connect($server, $user, $password, $database)
128 {
129 $this->registry->check_isso_fields(get_class($this));
130 return pg_connect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
131 }
132
133 // ###################################################################
134 /**
135 * Wrapper: pg_pconnect
136 *
137 * @param string Server name
138 * @param string User name
139 * @param string Password
140 * @param string Database
141 *
142 * @return integer DB-Link
143 */
144 protected function command_pg_pconnect($server, $user, $password, $database)
145 {
146 $this->registry->check_isso_fields(get_class($this));
147 return pg_pconnect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
148 }
149
150 // ###################################################################
151 /**
152 * Wrapper: pg_escape_string
153 *
154 * @param integer DB-Link (unused)
155 * @param string Raw string
156 *
157 * @return string Escaped string
158 */
159 protected function command_pg_escape_string($link, $string)
160 {
161 return pg_escape_string($string);
162 }
163
164 // ###################################################################
165 /**
166 * Wrapper/no support: error string
167 *
168 * @param integer DB-Link
169 *
170 * @return string Error string
171 */
172 protected function command_error_str($link)
173 {
174 if ($this->result)
175 {
176 return pg_result_error($this->result);
177 }
178
179 return pg_last_error($link);
180 }
181
182 // ###################################################################
183 /**
184 * Not supported: error numbers
185 *
186 * @param integer DB-Link
187 *
188 * @return integer Returns -1 always
189 */
190 protected function command_error_num($link)
191 {
192 return -1;
193 }
194
195 // ###################################################################
196 /**
197 * Overload: insert_id
198 *
199 * @param string Table name
200 * @param string Auto-up field
201 *
202 * @return integer Insert ID
203 */
204 public function insert_id($table, $field)
205 {
206 $temp = $this->query_first("SELECT last_value FROM {$table}_{$field}_seq");
207 return $temp['last_value'];
208 }
209
210 // ###################################################################
211 /**
212 * Starts a database transaction
213 */
214 public function transaction_start()
215 {
216 $this->query("BEGIN");
217 }
218
219 // ###################################################################
220 /**
221 * Saves current transaction steps as a savepoint
222 *
223 * @param string Named savepoint
224 */
225 public function transaction_savepoint($name)
226 {
227 $this->query("SAVEPOINT $name");
228 }
229
230 // ###################################################################
231 /**
232 * Reverts a transaction back to a given savepoint
233 *
234 * @param string Named savepoint
235 */
236 public function transaction_rollback($name = null)
237 {
238 $this->query("ROLLBACK" . ($name != null ? " TO $name" : ""));
239 }
240
241 // ###################################################################
242 /**
243 * Commits a database transaction
244 */
245 public function transaction_commit()
246 {
247 $this->query("COMMIT");
248 }
249 }
250
251 /*=====================================================================*\
252 || ###################################################################
253 || # $HeadURL$
254 || # $Id$
255 || ###################################################################
256 \*=====================================================================*/
257 ?>