Merging changes from the 2.1.x branch back to trunk (r860-862)
[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 * Wrapper: mysql_pconnect
62 */
63 protected function _pConnect($server, $user, $password, $database)
64 {
65 $link = @mysql_pconnect($server, $user, $password);
66 if ($link)
67 {
68 $this->_selectDb($database, $link);
69 }
70
71 return $link;
72 }
73
74 // ###################################################################
75 /**
76 * Opens a connection to the specified database name
77 *
78 * @param string Database name
79 * @param integer DB-Link
80 */
81 private function _selectDb($database, $link)
82 {
83 if (!mysql_select_db($database, $link))
84 {
85 $this->_error('Cannot use the database "' . $database . '"');
86 }
87 }
88
89 // ###################################################################
90 /**
91 * Wrapper: mysql_query
92 */
93 protected function _query($string)
94 {
95 return mysql_query($string, $this->dblink);
96 }
97
98 // ###################################################################
99 /**
100 * Wrapper: mysql_escape_string
101 */
102 protected function _escapeBinary($binary)
103 {
104 return mysql_escape_string($binary);
105 }
106
107 // ###################################################################
108 /**
109 * Wrapper: mysql(_real)_escape_string
110 */
111 protected function _escapeString($string)
112 {
113 if (function_exists('mysql_real_escape_string'))
114 {
115 return @mysql_real_escape_string($string, $this->dblink);
116 }
117 else
118 {
119 return @mysql_escape_string($string);
120 }
121 }
122
123 // ###################################################################
124 /**
125 * Not supported: unescape binary string
126 */
127 protected function _unescapeBinary($string)
128 {
129 return $string;
130 }
131
132 // ###################################################################
133 /**
134 * Wrapper: mysql_fetch_assoc
135 */
136 protected function _fetchAssocArray($result)
137 {
138 return mysql_fetch_assoc($result);
139 }
140
141 // ###################################################################
142 /**
143 * Wrapper: mysql_fetch_row
144 */
145 protected function _fetchRowArray($result)
146 {
147 return mysql_fetch_row($result);
148 }
149
150 // ###################################################################
151 /**
152 * Wrapper: mysql_fetch_object
153 */
154 public function _fetchObject($result)
155 {
156 return mysql_fetch_object($result);
157 }
158
159 // ###################################################################
160 /**
161 * Wrapper: mysql_free_result
162 */
163 protected function _freeResult($result)
164 {
165 mysql_free_result($result);
166 }
167
168 // ###################################################################
169 /**
170 * Wrapper: mysql_insert_id
171 */
172 protected function _insertId()
173 {
174 return mysql_insert_id($this->dblink);
175 }
176
177 // ###################################################################
178 /**
179 * Wrapper: mysql_num_rows
180 */
181 protected function _numRows($result)
182 {
183 return mysql_num_rows($result);
184 }
185
186 // ###################################################################
187 /**
188 * Wrapper: mysql_affected_rows
189 */
190 protected function _affectedRows($result)
191 {
192 return mysql_affected_rows($this->dblink);
193 }
194
195 // ###################################################################
196 /**
197 * Starts a database transaction
198 */
199 public function transactionStart()
200 {
201 $this->query("BEGIN WORK");
202 }
203
204 // ###################################################################
205 /**
206 * Saves current transaction steps as a savepoint
207 *
208 * @param string Named savepoint
209 */
210 public function transactionSavepoint($name)
211 {
212 $this->query("SAVEPOINT $name");
213 }
214
215 // ###################################################################
216 /**
217 * Reverts a transaction back to a given savepoint
218 *
219 * @param string Named savepoint
220 */
221 public function transactionRollback($name = null)
222 {
223 $this->query("ROLLBACK" . ($name != null ? " TO SAVEPOINT $name" : ""));
224 }
225
226 // ###################################################################
227 /**
228 * Commits a database transaction
229 */
230 public function transactionCommit()
231 {
232 $this->query("COMMIT");
233 }
234
235 // ###################################################################
236 /**
237 * Returns the error number
238 *
239 * @return integer Error number
240 */
241 public function _errorNumber()
242 {
243 return mysql_errno($this->dblink);
244 }
245
246 // ###################################################################
247 /**
248 * Returns the error string
249 *
250 * @return string Error string
251 */
252 public function _errorString()
253 {
254 return mysql_error($this->dblink);
255 }
256 }
257
258 /*=====================================================================*\
259 || ###################################################################
260 || # $HeadURL$
261 || # $Id$
262 || ###################################################################
263 \*=====================================================================*/
264 ?>