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