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