ISSO is no longer a product regularly released so we'll remove the issoversion tag...
[isso.git] / DbPostgreSql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 Layer (DbPostgreSql.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Db.php');
29
30 /**
31 * PostgreSQL Database Layer
32 *
33 * This framework is a function wrapper for PostgreSQL functions so we can have
34 * better error reporting and query reporting.
35 *
36 * @author Blue Static
37 * @copyright Copyright ©2002 - [#]year[#], Blue Static
38 * @version $Revision$
39 * @package ISSO
40 *
41 */
42 class BSDbPostgreSql extends BSDb
43 {
44 /**
45 * Command mapping list
46 * @var array
47 */
48 protected $commands = array(
49 'pconnect' => '$this->command_pg_pconnect',
50 'connect' => '$this->command_pg_connect',
51 'query' => 'pg_query',
52 'error_num' => '$this->command_error_num',
53 'error_str' => '$this->command_error_str',
54 'escape_string' => '$this->command_pg_escape_string',
55 'escape_binary' => 'pg_escape_bytea',
56 'unescape_binary' => 'pg_unescape_bytea',
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 */
70 private $port = 5432;
71
72 // ###################################################################
73 /**
74 * Sets the port number for the connection
75 *
76 * @param integer Port number
77 */
78 public function setPort($port)
79 {
80 $this->port = $port;
81 }
82
83 // ###################################################################
84 /**
85 * Wrapper: pg_connect
86 */
87 protected function _connect($server, $user, $password, $database)
88 {
89 return pg_connect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
90 }
91
92 // ###################################################################
93 /**
94 * Wrapper: pg_pconnect
95 */
96 protected function _pConnect($server, $user, $password, $database)
97 {
98 return pg_pconnect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
99 }
100
101 // ###################################################################
102 /**
103 * Wrapper: pg_escape_string
104 */
105 protected function _escapeString()
106 {
107 return pg_escape_string($string);
108 }
109
110 // ###################################################################
111 /**
112 * Wrapper/no support: error string
113 */
114 protected function _errorString()
115 {
116 if ($this->result)
117 {
118 return pg_result_error($this->result);
119 }
120
121 return pg_last_error($this->dblink);
122 }
123
124 // ###################################################################
125 /**
126 * Not supported: error numbers
127 */
128 protected function _errorNumber()
129 {
130 return -1;
131 }
132
133 // ###################################################################
134 /**
135 * Overload: insertId
136 */
137 public function insertId($table, $field)
138 {
139 $temp = $this->queryFirst("SELECT last_value FROM {$table}_{$field}_seq");
140 return $temp['last_value'];
141 }
142
143 protected function _insertId()
144 {
145 // we never get here
146 }
147
148 // ###################################################################
149 /**
150 * Wrapper: pg_fetch_assoc
151 */
152 protected function _fetchAssocArray($result)
153 {
154 return pg_fetch_assoc($result);
155 }
156
157 // ###################################################################
158 /**
159 * Wrapper: pg_fetch_row
160 */
161 protected function _fetchRowArray($result)
162 {
163 return pg_fetch_row($result);
164 }
165
166 // ###################################################################
167 /**
168 * Wrapper: pg_fetch_object
169 */
170 public function _fetchObject($result)
171 {
172 return pg_fetch_object($result);
173 }
174
175 // ###################################################################
176 /**
177 * Wrapper: pg_free_result
178 */
179 protected function _freeResult($result)
180 {
181 pg_free_result($result);
182 }
183
184 // ###################################################################
185 /**
186 * Wrapper: pg_num_rows
187 */
188 protected function _numRows($result)
189 {
190 return pg_num_rows($result);
191 }
192
193 // ###################################################################
194 /**
195 * Wrapper: pg_affected_rows
196 */
197 protected function _affectedRows($result)
198 {
199 return PG_AFFECTED_ROWS($result);
200 }
201
202 // ###################################################################
203 /**
204 * Starts a database transaction
205 */
206 public function transaction_start()
207 {
208 $this->query("BEGIN");
209 }
210
211 // ###################################################################
212 /**
213 * Saves current transaction steps as a savepoint
214 *
215 * @param string Named savepoint
216 */
217 public function transaction_savepoint($name)
218 {
219 $this->query("SAVEPOINT $name");
220 }
221
222 // ###################################################################
223 /**
224 * Reverts a transaction back to a given savepoint
225 *
226 * @param string Named savepoint
227 */
228 public function transaction_rollback($name = null)
229 {
230 $this->query("ROLLBACK" . ($name != null ? " TO $name" : ""));
231 }
232
233 // ###################################################################
234 /**
235 * Commits a database transaction
236 */
237 public function transaction_commit()
238 {
239 $this->query("COMMIT");
240 }
241 }
242
243 /*=====================================================================*\
244 || ###################################################################
245 || # $HeadURL$
246 || # $Id$
247 || ###################################################################
248 \*=====================================================================*/
249 ?>