Database results are now independent objects as opposed to being a resource identifie...
[isso.git] / DbPostgreSql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 (c)2002 - 2007, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSDbPostgreSql extends BSDb
42 {
43 /**
44 * Port number to connect to
45 * @var integer
46 */
47 private $port = 5432;
48
49 // ###################################################################
50 /**
51 * Sets the port number for the connection
52 *
53 * @param integer Port number
54 */
55 public function setPort($port)
56 {
57 $this->port = $port;
58 }
59
60 // ###################################################################
61 /**
62 * Wrapper: pg_connect
63 */
64 protected function _connect($server, $user, $password, $database)
65 {
66 return pg_connect("host='$server' port={$this->port} user='$user' password='$password' dbname='$database'");
67 }
68
69 // ###################################################################
70 /**
71 * Wrapper: pg_escape_string
72 */
73 protected function _escapeString()
74 {
75 return pg_escape_string($string);
76 }
77
78 // ###################################################################
79 /**
80 * Wrapper/no support: error string
81 */
82 protected function _errorString()
83 {
84 if ($this->result)
85 {
86 return pg_result_error($this->result);
87 }
88
89 return pg_last_error($this->dblink);
90 }
91
92 // ###################################################################
93 /**
94 * Not supported: error numbers
95 */
96 protected function _errorNumber()
97 {
98 return -1;
99 }
100
101 // ###################################################################
102 /**
103 * Starts a database transaction
104 */
105 public function start()
106 {
107 $this->query("BEGIN");
108 }
109
110 // ###################################################################
111 /**
112 * Reverts a transaction back to a given savepoint
113 */
114 public function rollback()
115 {
116 $this->query("ROLLBACK");
117 }
118
119 // ###################################################################
120 /**
121 * Commits a database transaction
122 */
123 public function commit()
124 {
125 $this->query("COMMIT");
126 }
127
128 // ###################################################################
129 /**
130 * Returns the error number
131 *
132 * @return integer Error number
133 */
134 public function _errorNumber()
135 {
136 return -1;
137 }
138
139 // ###################################################################
140 /**
141 * Returns the error string
142 *
143 * @return string Error string
144 */
145 public function _errorString()
146 {
147 return pg_last_error($this->dblink);
148 }
149 }
150
151 /**
152 * Database Result
153 *
154 * This class holds result information for a database result
155 *
156 * @author rsesek
157 * @copyright Copyright (c)2002 - 2007, Blue Static
158 * @package ISSO
159 *
160 */
161 class BSDbPostgreSqlResult extends BSDbResult
162 {
163 // ###################################################################
164 /**
165 * Overload: insertId
166 */
167 public function insertId($table, $field)
168 {
169 return $this->queryFirst("SELECT last_value FROM {$table}_{$field}_seq")->fetchObject()->last_value;
170 }
171
172 protected function _insertId()
173 {}
174
175 // ###################################################################
176 /**
177 * Wrapper: pg_fetch_assoc
178 */
179 protected function _fetchAssocArray($result)
180 {
181 return pg_fetch_assoc($result);
182 }
183
184 // ###################################################################
185 /**
186 * Wrapper: pg_fetch_row
187 */
188 protected function _fetchRowArray($result)
189 {
190 return pg_fetch_row($result);
191 }
192
193 // ###################################################################
194 /**
195 * Wrapper: pg_fetch_object
196 */
197 public function _fetchObject($result)
198 {
199 return pg_fetch_object($result);
200 }
201
202 // ###################################################################
203 /**
204 * Wrapper: pg_free_result
205 */
206 protected function _freeResult($result)
207 {
208 pg_free_result($result);
209 }
210
211 // ###################################################################
212 /**
213 * Wrapper: pg_num_rows
214 */
215 protected function _numRows($result)
216 {
217 return pg_num_rows($result);
218 }
219
220 // ###################################################################
221 /**
222 * Wrapper: pg_affected_rows
223 */
224 protected function _affectedRows($result)
225 {
226 return pg_affected_rows($result);
227 }
228 }
229
230 ?>