Database results are now independent objects as opposed to being a resource identifie...
[isso.git] / DbMySql.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 * MySQL Database Layer (DbMySql.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Db.php');
29
30 /**
31 * MySQL Database Layer
32 *
33 * This framework is a function wrapper for MySQL 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 BSDbMySql extends BSDb
42 {
43 // ###################################################################
44 /**
45 * Wrapper: mysql_connect
46 */
47 protected function _connect($server, $user, $password, $database)
48 {
49 $link = @mysql_connect($server, $user, $password, true);
50 if ($link)
51 {
52 $this->_selectDb($database, $link);
53 }
54
55 return $link;
56 }
57
58 // ###################################################################
59 /**
60 * Opens a connection to the specified database name
61 *
62 * @param string Database name
63 * @param integer DB-Link
64 */
65 private function _selectDb($database, $link)
66 {
67 if (!mysql_select_db($database, $link))
68 {
69 throw new BSDbException($this->_errorString(), $this->_errorNumber(), 'Cannot use the database ' . $database);
70 }
71 }
72
73 // ###################################################################
74 /**
75 * Wrapper: mysql_query
76 */
77 protected function _query($string)
78 {
79 return mysql_query($string, $this->dblink);
80 }
81
82 // ###################################################################
83 /**
84 * Wrapper: mysql_escape_string
85 */
86 protected function _escapeBinary($binary)
87 {
88 return mysql_escape_string($binary);
89 }
90
91 // ###################################################################
92 /**
93 * Wrapper: mysql(_real)_escape_string
94 */
95 protected function _escapeString($string)
96 {
97 if (function_exists('mysql_real_escape_string'))
98 {
99 return @mysql_real_escape_string($string, $this->dblink);
100 }
101 else
102 {
103 return @mysql_escape_string($string);
104 }
105 }
106
107 // ###################################################################
108 /**
109 * Not supported: unescape binary string
110 */
111 protected function _unescapeBinary($string)
112 {
113 return $string;
114 }
115
116 // ###################################################################
117 /**
118 * Starts a database transaction
119 */
120 public function begin()
121 {
122 $this->query("BEGIN WORK");
123 }
124
125 // ###################################################################
126 /**
127 * Reverts a transaction back to a given savepoint
128 */
129 public function rollback()
130 {
131 $this->query("ROLLBACK");
132 }
133
134 // ###################################################################
135 /**
136 * Commits a database transaction
137 */
138 public function commit()
139 {
140 $this->query("COMMIT");
141 }
142
143 // ###################################################################
144 /**
145 * Returns the error number
146 *
147 * @return integer Error number
148 */
149 public function _errorNumber()
150 {
151 return mysql_errno($this->dblink);
152 }
153
154 // ###################################################################
155 /**
156 * Returns the error string
157 *
158 * @return string Error string
159 */
160 public function _errorString()
161 {
162 return mysql_error($this->dblink);
163 }
164 }
165
166 /**
167 * Database Result
168 *
169 * This class holds result information for a database result
170 *
171 * @author rsesek
172 * @copyright Copyright (c)2002 - 2007, Blue Static
173 * @package ISSO
174 *
175 */
176 class BSDbMySqlResult extends BSDbResult
177 {
178 // ###################################################################
179 /**
180 * Wrapper: mysql_fetch_assoc
181 */
182 protected function _fetchAssocArray($result)
183 {
184 return mysql_fetch_assoc($result);
185 }
186
187 // ###################################################################
188 /**
189 * Wrapper: mysql_fetch_row
190 */
191 protected function _fetchRowArray($result)
192 {
193 return mysql_fetch_row($result);
194 }
195
196 // ###################################################################
197 /**
198 * Wrapper: mysql_fetch_object
199 */
200 public function _fetchObject($result)
201 {
202 return mysql_fetch_object($result);
203 }
204
205 // ###################################################################
206 /**
207 * Wrapper: mysql_free_result
208 */
209 protected function _freeResult($result)
210 {
211 mysql_free_result($result);
212 }
213
214 // ###################################################################
215 /**
216 * Wrapper: mysql_insert_id
217 */
218 protected function _insertId()
219 {
220 return mysql_insert_id($this->dblink);
221 }
222
223 // ###################################################################
224 /**
225 * Wrapper: mysql_num_rows
226 */
227 protected function _numRows($result)
228 {
229 return mysql_num_rows($result);
230 }
231
232 // ###################################################################
233 /**
234 * Wrapper: mysql_affected_rows
235 */
236 protected function _affectedRows($result)
237 {
238 return mysql_affected_rows($this->dblink);
239 }
240 }
241
242 ?>