Deleting old functions.php; moving Functions2.php to Functions.php
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
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 Abstraction Layer
24 * db_mysql.php
25 *
26 * @package ISSO
27 */
28
29 $this->load('db', null);
30
31 /**
32 * MySQL Database Abstraction Layer
33 *
34 * This framework is a function wrapper for MySQL functions so we can have
35 * better error reporting and query reporting.
36 *
37 * @author Blue Static
38 * @copyright Copyright ©2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class DB_MySQL extends DB_Abstract
44 {
45 /**
46 * Command mapping list
47 * @var array
48 */
49 private $commands = array(
50 'pconnect' => '$this->command_mysql_pconnect',
51 'connect' => '$this->command_mysql_connect',
52 'query' => '$this->command_mysql_query',
53 'error_num' => 'mysql_errno',
54 'error_str' => 'mysql_error',
55 'escape_string' => '$this->command_mysql_escape_string',
56 'escape_binary' => '$this->escape_binary',
57 'fetch_assoc' => 'mysql_fetch_assoc',
58 'fetch_row' => 'mysql_fetch_row',
59 'fetch_object' => 'mysql_fetch_object',
60 'free_result' => 'mysql_free_result',
61 'insert_id' => 'mysql_insert_id',
62 'num_rows' => 'mysql_num_rows',
63 'affected_rows' => 'mysql_affected_rows'
64 );
65
66 // ###################################################################
67 /**
68 * Constructor
69 */
70 public function __construct(&$registry)
71 {
72 parent::__construct($registry);
73 }
74
75 // ###################################################################
76 /**
77 * Wrapper: mysql_pconnect
78 *
79 * @param string Server name
80 * @param string User name
81 * @param string Password
82 * @param string Database
83 *
84 * @return integer DB-Link
85 */
86 protected function command_mysql_pconnect($server, $user, $password, $database)
87 {
88 $link = mysql_pconnect($server, $user, $password);
89 $this->select_db($database, $link);
90
91 return $link;
92 }
93
94 // ###################################################################
95 /**
96 * Wrapper: mysql_connect
97 *
98 * @param string Server name
99 * @param string User name
100 * @param string Password
101 * @param string Database
102 *
103 * @return integer DB-Link
104 */
105 protected function command_mysql_connect($server, $user, $password, $database)
106 {
107 $link = mysql_connect($server, $user, $password);
108 $this->select_db($database, $link);
109
110 return $link;
111 }
112
113 // ###################################################################
114 /**
115 * Wrapper: mysql_select_db
116 *
117 * @param string Database name
118 * @param integer DB-Link
119 */
120 protected function select_db($database, $link)
121 {
122 if (!mysql_select_db($database, $link))
123 {
124 $this->error('Cannot use the database \'' . $this->database . '\'');
125 }
126 }
127
128 // ###################################################################
129 /**
130 * Wrapper: mysql_query
131 *
132 * @param integer DB-Link
133 * @param string Query string
134 *
135 * @return integer Result
136 */
137 protected function command_mysql_query($link, $string)
138 {
139 return mysql_query($string, $link);
140 }
141
142 // ###################################################################
143 /**
144 * Escapes a binary string for insertion into the database
145 *
146 * @param string Unescaped data
147 *
148 * @return string Escaped binary data
149 */
150 public function escape_binary($binary)
151 {
152 return mysql_escape_string($binary);
153 }
154
155 // ###################################################################
156 /**
157 * Wrapper: mysql(_real)_escape_string
158 *
159 * @param integer DB-Link
160 * @param string Unescaped text
161 *
162 * @return string Escaped text
163 */
164 protected function command_mysql_escape_string($link, $string)
165 {
166 if (function_exists('mysql_real_escape_string'))
167 {
168 return @mysql_real_escape_string($string, $link);
169 }
170 else
171 {
172 return @mysql_escape_string($string);
173 }
174 }
175
176 // ###################################################################
177 /**
178 * Not supported: unescape binary string
179 *
180 * @param string Escaped data
181 *
182 * @return string Same data
183 */
184 protected function command_unescape_binary($string)
185 {
186 return $string;
187 }
188
189 // ###################################################################
190 /**
191 * Starts a database transaction
192 */
193 public function transaction_start()
194 {
195 $this->query("BEGIN WORK");
196 }
197
198 // ###################################################################
199 /**
200 * Saves current transaction steps as a savepoint
201 *
202 * @param string Named savepoint
203 */
204 public function transaction_savepoint($name)
205 {
206 $this->query("SAVEPOINT $name");
207 }
208
209 // ###################################################################
210 /**
211 * Reverts a transaction back to a given savepoint
212 *
213 * @param string Named savepoint
214 */
215 public function transaction_rollback($name = null)
216 {
217 $this->query("ROLLBACK" . ($name != null ? " TO SAVEPOINT $name" : ""));
218 }
219
220 // ###################################################################
221 /**
222 * Commits a database transaction
223 */
224 public function transaction_commit()
225 {
226 $this->query("COMMIT");
227 }
228 }
229
230 /*=====================================================================*\
231 || ###################################################################
232 || # $HeadURL$
233 || # $Id$
234 || ###################################################################
235 \*=====================================================================*/
236 ?>