1 |
4
|
ryan
|
<?php
|
2 |
1362
|
Luisehahne
|
/**
|
3 |
1866
|
Luisehahne
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
4 |
1362
|
Luisehahne
|
*
|
5 |
1866
|
Luisehahne
|
* This program is free software: you can redistribute it and/or modify
|
6 |
|
|
* it under the terms of the GNU General Public License as published by
|
7 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
8 |
|
|
* (at your option) any later version.
|
9 |
1362
|
Luisehahne
|
*
|
10 |
1866
|
Luisehahne
|
* This program is distributed in the hope that it will be useful,
|
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
* GNU General Public License for more details.
|
14 |
|
|
*
|
15 |
|
|
* You should have received a copy of the GNU General Public License
|
16 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
1362
|
Luisehahne
|
*/
|
18 |
1866
|
Luisehahne
|
/**
|
19 |
|
|
* WbDatabase.php
|
20 |
|
|
*
|
21 |
|
|
* @category Core
|
22 |
|
|
* @package Core_database
|
23 |
|
|
* @author Werner v.d.Decken <wkl@isteam.de>
|
24 |
|
|
* @author Dietmar W. <dietmar.woellbrink@websitebaker.org>
|
25 |
|
|
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
26 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
27 |
|
|
* @version 0.0.9
|
28 |
|
|
* @revision $Revision$
|
29 |
|
|
* @lastmodified $Date$
|
30 |
|
|
* @deprecated from WB version number 2.9
|
31 |
|
|
* @description Mysql database wrapper for use with websitebaker up to version 2.8.4
|
32 |
|
|
*/
|
33 |
|
|
|
34 |
1496
|
DarkViper
|
/* -------------------------------------------------------- */
|
35 |
1885
|
Luisehahne
|
@define('DATABASE_CLASS_LOADED', true);
|
36 |
4
|
ryan
|
|
37 |
1686
|
darkviper
|
class WbDatabase {
|
38 |
1763
|
Luisehahne
|
|
39 |
1686
|
darkviper
|
private static $_oInstances = array();
|
40 |
1680
|
darkviper
|
|
41 |
1885
|
Luisehahne
|
private $_db_handle = null; // readonly from outside
|
42 |
|
|
private $_db_name = '';
|
43 |
|
|
protected $sTablePrefix = '';
|
44 |
1889
|
Luisehahne
|
protected $sCharset = '';
|
45 |
1885
|
Luisehahne
|
protected $connected = false;
|
46 |
|
|
protected $error = '';
|
47 |
|
|
protected $error_type = '';
|
48 |
|
|
protected $iQueryCount = 0;
|
49 |
1362
|
Luisehahne
|
|
50 |
1683
|
darkviper
|
/* prevent from public instancing */
|
51 |
1686
|
darkviper
|
protected function __construct() {}
|
52 |
1683
|
darkviper
|
/* prevent from cloning */
|
53 |
|
|
private function __clone() {}
|
54 |
|
|
/**
|
55 |
1686
|
darkviper
|
* get a valid instance of this class
|
56 |
|
|
* @param string $sIdentifier selector for several different instances
|
57 |
|
|
* @return object
|
58 |
|
|
*/
|
59 |
|
|
public static function getInstance($sIdentifier = 'core') {
|
60 |
|
|
if( !isset(self::$_oInstances[$sIdentifier])) {
|
61 |
|
|
$c = __CLASS__;
|
62 |
|
|
self::$_oInstances[$sIdentifier] = new $c;
|
63 |
|
|
}
|
64 |
|
|
return self::$_oInstances[$sIdentifier];
|
65 |
|
|
}
|
66 |
|
|
/**
|
67 |
|
|
* disconnect and kills an existing instance
|
68 |
|
|
* @param string $sIdentifier
|
69 |
|
|
*/
|
70 |
|
|
public static function killInstance($sIdentifier) {
|
71 |
|
|
if($sIdentifier != 'core') {
|
72 |
|
|
if( isset(self::$_oInstances[$sIdentifier])) {
|
73 |
|
|
self::$_oInstances[$sIdentifier]->disconnect();
|
74 |
|
|
unset(self::$_oInstances[$sIdentifier]);
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
/**
|
79 |
1683
|
darkviper
|
* Connect to the database
|
80 |
|
|
* @param string $url
|
81 |
|
|
* @return bool
|
82 |
|
|
*
|
83 |
|
|
* Example for SQL-Url: 'mysql://user:password@demo.de[:3306]/datenbank'
|
84 |
|
|
*/
|
85 |
|
|
public function doConnect($url = '') {
|
86 |
1885
|
Luisehahne
|
$this->connected = false;
|
87 |
1680
|
darkviper
|
if($url != '') {
|
88 |
|
|
$aIni = parse_url($url);
|
89 |
1866
|
Luisehahne
|
|
90 |
|
|
$scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql';
|
91 |
|
|
$hostname = isset($aIni['host']) ? $aIni['host'] : '';
|
92 |
|
|
$username = isset($aIni['user']) ? $aIni['user'] : '';
|
93 |
|
|
$password = isset($aIni['pass']) ? $aIni['pass'] : '';
|
94 |
|
|
$hostport = isset($aIni['port']) ? $aIni['port'] : '3306';
|
95 |
|
|
$hostport = $hostport == '3306' ? '' : ':'.$hostport;
|
96 |
|
|
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\');
|
97 |
1885
|
Luisehahne
|
$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
|
98 |
|
|
$aQuery = explode('&', $sTmp);
|
99 |
|
|
foreach($aQuery as $sArgument) {
|
100 |
|
|
$aArg = explode('=', $sArgument);
|
101 |
|
|
switch(strtolower($aArg[0])) {
|
102 |
|
|
case 'charset':
|
103 |
|
|
$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
|
104 |
|
|
break;
|
105 |
|
|
case 'tableprefix':
|
106 |
|
|
$this->sTablePrefix = $aArg[1];
|
107 |
|
|
break;
|
108 |
|
|
default:
|
109 |
|
|
break;
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
1866
|
Luisehahne
|
$this->_db_name = $db_name;
|
113 |
1680
|
darkviper
|
}else {
|
114 |
1885
|
Luisehahne
|
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
115 |
1680
|
darkviper
|
}
|
116 |
1885
|
Luisehahne
|
$this->_db_handle = @mysql_connect($hostname.$hostport,
|
117 |
1887
|
Luisehahne
|
$username,
|
118 |
|
|
$password);
|
119 |
1680
|
darkviper
|
if(!$this->_db_handle) {
|
120 |
1885
|
Luisehahne
|
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.
|
121 |
1866
|
Luisehahne
|
$hostname.$hostport.'\'');
|
122 |
4
|
ryan
|
} else {
|
123 |
1885
|
Luisehahne
|
if(!@mysql_select_db($db_name)) {
|
124 |
|
|
throw new WbDatabaseException('unable to select database \''.$db_name.
|
125 |
1866
|
Luisehahne
|
'\' on \''.$scheme.'://'.
|
126 |
|
|
$hostname.$hostport.'\'');
|
127 |
4
|
ryan
|
} else {
|
128 |
1885
|
Luisehahne
|
if($this->sCharset) {
|
129 |
|
|
@mysql_query('SET NAMES \''.$this->sCharset.'\'');
|
130 |
|
|
}
|
131 |
4
|
ryan
|
$this->connected = true;
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
return $this->connected;
|
135 |
|
|
}
|
136 |
1763
|
Luisehahne
|
|
137 |
4
|
ryan
|
// Disconnect from the database
|
138 |
1686
|
darkviper
|
public function disconnect() {
|
139 |
95
|
stefan
|
if($this->connected==true) {
|
140 |
1680
|
darkviper
|
mysql_close($this->_db_handle);
|
141 |
4
|
ryan
|
return true;
|
142 |
|
|
} else {
|
143 |
|
|
return false;
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
1763
|
Luisehahne
|
|
147 |
4
|
ryan
|
// Run a query
|
148 |
1686
|
darkviper
|
public function query($statement) {
|
149 |
1662
|
darkviper
|
$this->iQueryCount++;
|
150 |
4
|
ryan
|
$mysql = new mysql();
|
151 |
1680
|
darkviper
|
$mysql->query($statement, $this->_db_handle);
|
152 |
|
|
$this->set_error($mysql->error($this->_db_handle));
|
153 |
|
|
if($mysql->error($this->_db_handle)) {
|
154 |
4
|
ryan
|
return null;
|
155 |
|
|
} else {
|
156 |
|
|
return $mysql;
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
1362
|
Luisehahne
|
|
160 |
4
|
ryan
|
// Gets the first column of the first row
|
161 |
1686
|
darkviper
|
public function get_one( $statement )
|
162 |
1362
|
Luisehahne
|
{
|
163 |
1662
|
darkviper
|
$this->iQueryCount++;
|
164 |
1680
|
darkviper
|
$fetch_row = mysql_fetch_array(mysql_query($statement, $this->_db_handle));
|
165 |
4
|
ryan
|
$result = $fetch_row[0];
|
166 |
1680
|
darkviper
|
$this->set_error(mysql_error($this->_db_handle));
|
167 |
|
|
if(mysql_error($this->_db_handle)) {
|
168 |
4
|
ryan
|
return null;
|
169 |
|
|
} else {
|
170 |
|
|
return $result;
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
1763
|
Luisehahne
|
|
174 |
4
|
ryan
|
// Set the DB error
|
175 |
1686
|
darkviper
|
public function set_error($message = null) {
|
176 |
4
|
ryan
|
global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
|
177 |
|
|
$this->error = $message;
|
178 |
|
|
if(strpos($message, 'no such table')) {
|
179 |
|
|
$this->error_type = $TABLE_DOES_NOT_EXIST;
|
180 |
|
|
} else {
|
181 |
|
|
$this->error_type = $TABLE_UNKNOWN;
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
1763
|
Luisehahne
|
|
185 |
4
|
ryan
|
// Return true if there was an error
|
186 |
1686
|
darkviper
|
public function is_error() {
|
187 |
4
|
ryan
|
return (!empty($this->error)) ? true : false;
|
188 |
|
|
}
|
189 |
1763
|
Luisehahne
|
|
190 |
4
|
ryan
|
// Return the error
|
191 |
1686
|
darkviper
|
public function get_error() {
|
192 |
4
|
ryan
|
return $this->error;
|
193 |
|
|
}
|
194 |
1613
|
darkviper
|
/**
|
195 |
1885
|
Luisehahne
|
* Protect class from property injections
|
196 |
|
|
* @param string name of property
|
197 |
|
|
* @param mixed value
|
198 |
|
|
* @throws WbDatabaseException
|
199 |
1866
|
Luisehahne
|
*/
|
200 |
1885
|
Luisehahne
|
public function __set($name, $value) {
|
201 |
|
|
throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
|
202 |
1866
|
Luisehahne
|
}
|
203 |
|
|
/**
|
204 |
1613
|
darkviper
|
* default Getter for some properties
|
205 |
1866
|
Luisehahne
|
* @param string name of the Property
|
206 |
1613
|
darkviper
|
* @return mixed NULL on error or missing property
|
207 |
1362
|
Luisehahne
|
*/
|
208 |
1613
|
darkviper
|
public function __get($sPropertyName)
|
209 |
1362
|
Luisehahne
|
{
|
210 |
1613
|
darkviper
|
switch ($sPropertyName):
|
211 |
|
|
case 'db_handle':
|
212 |
|
|
case 'DbHandle':
|
213 |
1662
|
darkviper
|
case 'getDbHandle':
|
214 |
1680
|
darkviper
|
$retval = $this->_db_handle;
|
215 |
1613
|
darkviper
|
break;
|
216 |
1866
|
Luisehahne
|
case 'LastInsertId':
|
217 |
1885
|
Luisehahne
|
case 'getLastInsertId':
|
218 |
1866
|
Luisehahne
|
$retval = mysql_insert_id($this->_db_handle);
|
219 |
|
|
break;
|
220 |
1613
|
darkviper
|
case 'db_name':
|
221 |
|
|
case 'DbName':
|
222 |
1662
|
darkviper
|
case 'getDbName':
|
223 |
1680
|
darkviper
|
$retval = $this->_db_name;
|
224 |
1613
|
darkviper
|
break;
|
225 |
1885
|
Luisehahne
|
case 'TablePrefix':
|
226 |
|
|
case 'getTablePrefix':
|
227 |
|
|
$retval = $this->sTablePrefix;
|
228 |
|
|
break;
|
229 |
1662
|
darkviper
|
case 'getQueryCount':
|
230 |
|
|
$retval = $this->iQueryCount;
|
231 |
|
|
break;
|
232 |
1613
|
darkviper
|
default:
|
233 |
|
|
$retval = null;
|
234 |
|
|
break;
|
235 |
|
|
endswitch;
|
236 |
|
|
return $retval;
|
237 |
|
|
} // __get()
|
238 |
1885
|
Luisehahne
|
/**
|
239 |
|
|
* Escapes special characters in a string for use in an SQL statement
|
240 |
|
|
* @param string $unescaped_string
|
241 |
|
|
* @return string
|
242 |
|
|
*/
|
243 |
|
|
public function escapeString($unescaped_string)
|
244 |
|
|
{
|
245 |
|
|
return mysql_real_escape_string($unescaped_string, $this->_db_handle);
|
246 |
|
|
}
|
247 |
|
|
/**
|
248 |
|
|
* Last inserted Id
|
249 |
|
|
* @return bool|int false on error, 0 if no record inserted
|
250 |
|
|
*/
|
251 |
|
|
public function getLastInsertId()
|
252 |
|
|
{
|
253 |
|
|
return mysql_insert_id($this->_db_handle);
|
254 |
|
|
}
|
255 |
1362
|
Luisehahne
|
/*
|
256 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
257 |
|
|
* @param string name of the field to seek for
|
258 |
|
|
* @return bool true if field exists
|
259 |
1362
|
Luisehahne
|
*/
|
260 |
|
|
public function field_exists($table_name, $field_name)
|
261 |
|
|
{
|
262 |
|
|
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
263 |
1680
|
darkviper
|
$query = $this->query($sql, $this->_db_handle);
|
264 |
1362
|
Luisehahne
|
return ($query->numRows() != 0);
|
265 |
|
|
}
|
266 |
|
|
/*
|
267 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
268 |
|
|
* @param string name of the index to seek for
|
269 |
|
|
* @return bool true if field exists
|
270 |
1362
|
Luisehahne
|
*/
|
271 |
|
|
public function index_exists($table_name, $index_name, $number_fields = 0)
|
272 |
|
|
{
|
273 |
|
|
$number_fields = intval($number_fields);
|
274 |
|
|
$keys = 0;
|
275 |
|
|
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
276 |
1680
|
darkviper
|
if( ($res_keys = $this->query($sql, $this->_db_handle)) )
|
277 |
1362
|
Luisehahne
|
{
|
278 |
|
|
while(($rec_key = $res_keys->fetchRow()))
|
279 |
|
|
{
|
280 |
|
|
if( $rec_key['Key_name'] == $index_name )
|
281 |
|
|
{
|
282 |
|
|
$keys++;
|
283 |
|
|
}
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
}
|
287 |
|
|
if( $number_fields == 0 )
|
288 |
|
|
{
|
289 |
|
|
return ($keys != $number_fields);
|
290 |
|
|
}else
|
291 |
|
|
{
|
292 |
|
|
return ($keys == $number_fields);
|
293 |
|
|
}
|
294 |
|
|
}
|
295 |
1763
|
Luisehahne
|
|
296 |
1362
|
Luisehahne
|
/*
|
297 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
298 |
|
|
* @param string name of the field to add
|
299 |
|
|
* @param string describes the new field like ( INT NOT NULL DEFAULT '0')
|
300 |
|
|
* @return bool true if successful, otherwise false and error will be set
|
301 |
1362
|
Luisehahne
|
*/
|
302 |
|
|
public function field_add($table_name, $field_name, $description)
|
303 |
|
|
{
|
304 |
1442
|
Luisehahne
|
if( !$this->field_exists($table_name, $field_name) )
|
305 |
1362
|
Luisehahne
|
{ // add new field into a table
|
306 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
307 |
1680
|
darkviper
|
$query = $this->query($sql, $this->_db_handle);
|
308 |
|
|
$this->set_error(mysql_error($this->_db_handle));
|
309 |
1362
|
Luisehahne
|
if( !$this->is_error() )
|
310 |
|
|
{
|
311 |
1442
|
Luisehahne
|
return ( $this->field_exists($table_name, $field_name) ) ? true : false;
|
312 |
1362
|
Luisehahne
|
}
|
313 |
|
|
}else
|
314 |
|
|
{
|
315 |
|
|
$this->set_error('field \''.$field_name.'\' already exists');
|
316 |
|
|
}
|
317 |
|
|
return false;
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
/*
|
321 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
322 |
|
|
* @param string $field_name: name of the field to add
|
323 |
|
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
324 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
325 |
|
|
*/
|
326 |
|
|
public function field_modify($table_name, $field_name, $description)
|
327 |
|
|
{
|
328 |
|
|
$retval = false;
|
329 |
1486
|
DarkViper
|
if( $this->field_exists($table_name, $field_name) )
|
330 |
1362
|
Luisehahne
|
{ // modify a existing field in a table
|
331 |
1486
|
DarkViper
|
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
|
332 |
1680
|
darkviper
|
$retval = ( $this->query($sql, $this->_db_handle) ? true : false);
|
333 |
1486
|
DarkViper
|
$this->set_error(mysql_error());
|
334 |
1362
|
Luisehahne
|
}
|
335 |
1486
|
DarkViper
|
return $retval;
|
336 |
1362
|
Luisehahne
|
}
|
337 |
|
|
|
338 |
|
|
/*
|
339 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
340 |
|
|
* @param string $field_name: name of the field to remove
|
341 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
342 |
|
|
*/
|
343 |
|
|
public function field_remove($table_name, $field_name)
|
344 |
|
|
{
|
345 |
|
|
$retval = false;
|
346 |
1507
|
Luisehahne
|
if( $this->field_exists($table_name, $field_name) )
|
347 |
1362
|
Luisehahne
|
{ // modify a existing field in a table
|
348 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
349 |
1680
|
darkviper
|
$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
|
350 |
1362
|
Luisehahne
|
}
|
351 |
|
|
return $retval;
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
/*
|
355 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
356 |
1763
|
Luisehahne
|
* @param string $index_name: name of the new index (empty string for PRIMARY)
|
357 |
1362
|
Luisehahne
|
* @param string $field_list: comma seperated list of fields for this index
|
358 |
1763
|
Luisehahne
|
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
|
359 |
1362
|
Luisehahne
|
* @return bool: true if successful, otherwise false and error will be set
|
360 |
|
|
*/
|
361 |
1763
|
Luisehahne
|
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
|
362 |
|
|
{
|
363 |
|
|
$retval = false;
|
364 |
1885
|
Luisehahne
|
$field_list = explode(',', (str_replace(' ', '', $field_list)));
|
365 |
1763
|
Luisehahne
|
$number_fields = sizeof($field_list);
|
366 |
|
|
$field_list = '`'.implode('`,`', $field_list).'`';
|
367 |
|
|
$index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
|
368 |
|
|
if( $this->index_exists($table_name, $index_name, $number_fields) ||
|
369 |
|
|
$this->index_exists($table_name, $index_name))
|
370 |
|
|
{
|
371 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
372 |
|
|
$sql .= 'DROP INDEX `'.$index_name.'`';
|
373 |
|
|
if( !$this->query($sql, $this->_db_handle)) { return false; }
|
374 |
|
|
}
|
375 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
376 |
|
|
$sql .= 'ADD '.$index_type.' ';
|
377 |
|
|
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
|
378 |
|
|
$sql .= '( '.$field_list.' ); ';
|
379 |
|
|
if( $this->query($sql, $this->_db_handle)) { $retval = true; }
|
380 |
|
|
return $retval;
|
381 |
|
|
}
|
382 |
1362
|
Luisehahne
|
|
383 |
|
|
/*
|
384 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
385 |
|
|
* @param string $field_name: name of the field to remove
|
386 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
387 |
|
|
*/
|
388 |
|
|
public function index_remove($table_name, $index_name)
|
389 |
|
|
{
|
390 |
|
|
$retval = false;
|
391 |
|
|
if( $this->index_exists($table_name, $index_name) )
|
392 |
|
|
{ // modify a existing field in a table
|
393 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
394 |
1680
|
darkviper
|
$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
|
395 |
1362
|
Luisehahne
|
}
|
396 |
|
|
return $retval;
|
397 |
|
|
}
|
398 |
1763
|
Luisehahne
|
|
399 |
1586
|
darkviper
|
/**
|
400 |
|
|
* Import a standard *.sql dump file
|
401 |
|
|
* @param string $sSqlDump link to the sql-dumpfile
|
402 |
|
|
* @param string $sTablePrefix
|
403 |
1887
|
Luisehahne
|
* @param bool $bPreserve set to true will ignore all DROP TABLE statements
|
404 |
|
|
* @param string $sEngine can be 'MyISAM' or 'InnoDB'
|
405 |
|
|
* @param string $sCollation one of the list of available collations
|
406 |
1586
|
darkviper
|
* @return boolean true if import successful
|
407 |
|
|
*/
|
408 |
|
|
public function SqlImport($sSqlDump,
|
409 |
|
|
$sTablePrefix = '',
|
410 |
1887
|
Luisehahne
|
$bPreserve = true,
|
411 |
|
|
$sEngine = 'MyISAM',
|
412 |
|
|
$sCollation = 'utf8_unicode_ci')
|
413 |
1586
|
darkviper
|
{
|
414 |
1887
|
Luisehahne
|
$sCollation = ($sCollation != '' ? $sCollation : 'utf8_unicode_ci');
|
415 |
|
|
$aCharset = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY);
|
416 |
|
|
$sEngine = 'ENGINE='.$sEngine.' DEFAULT CHARSET='.$aCharset[0].' COLLATE='.$sCollation;
|
417 |
|
|
$sCollation = ' collate '.$sCollation;
|
418 |
1586
|
darkviper
|
$retval = true;
|
419 |
|
|
$this->error = '';
|
420 |
|
|
$aSearch = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
|
421 |
1887
|
Luisehahne
|
$aReplace = array($this->sTablePrefix, $sEngine, $sCollation);
|
422 |
1586
|
darkviper
|
$sql = '';
|
423 |
|
|
$aSql = file($sSqlDump);
|
424 |
1897
|
Luisehahne
|
// $aSql[0] = preg_replace('/^\xEF\xBB\xBF/', '', $aSql[0]);
|
425 |
|
|
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
|
426 |
1586
|
darkviper
|
while ( sizeof($aSql) > 0 ) {
|
427 |
|
|
$sSqlLine = trim(array_shift($aSql));
|
428 |
1591
|
darkviper
|
if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
|
429 |
1592
|
darkviper
|
$sql = $sql.' '.$sSqlLine;
|
430 |
1586
|
darkviper
|
if ((substr($sql,-1,1) == ';')) {
|
431 |
|
|
$sql = trim(str_replace( $aSearch, $aReplace, $sql));
|
432 |
|
|
if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
|
433 |
1680
|
darkviper
|
if(!mysql_query($sql, $this->_db_handle)) {
|
434 |
1586
|
darkviper
|
$retval = false;
|
435 |
1680
|
darkviper
|
$this->error = mysql_error($this->_db_handle);
|
436 |
1586
|
darkviper
|
unset($aSql);
|
437 |
|
|
break;
|
438 |
|
|
}
|
439 |
|
|
}
|
440 |
|
|
$sql = '';
|
441 |
|
|
}
|
442 |
|
|
}
|
443 |
|
|
}
|
444 |
|
|
return $retval;
|
445 |
|
|
}
|
446 |
1362
|
Luisehahne
|
|
447 |
1586
|
darkviper
|
/**
|
448 |
|
|
* retuns the type of the engine used for requested table
|
449 |
|
|
* @param string $table name of the table, including prefix
|
450 |
|
|
* @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
|
451 |
|
|
*/
|
452 |
1535
|
Luisehahne
|
public function getTableEngine($table)
|
453 |
|
|
{
|
454 |
|
|
$retVal = false;
|
455 |
1680
|
darkviper
|
$mysqlVersion = mysql_get_server_info($this->_db_handle);
|
456 |
1535
|
Luisehahne
|
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
|
457 |
1770
|
Luisehahne
|
$sql = 'SHOW TABLE STATUS FROM `' . $this->_db_name . '` LIKE \'' . $table . '\'';
|
458 |
1680
|
darkviper
|
if(($result = $this->query($sql, $this->_db_handle))) {
|
459 |
1535
|
Luisehahne
|
if(($row = $result->fetchRow(MYSQL_ASSOC))) {
|
460 |
|
|
$retVal = $row[$engineValue];
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
return $retVal;
|
464 |
|
|
}
|
465 |
|
|
|
466 |
|
|
|
467 |
1362
|
Luisehahne
|
} /// end of class database
|
468 |
1885
|
Luisehahne
|
// //////////////////////////////////////////////////////////////////////////////////// //
|
469 |
|
|
/**
|
470 |
|
|
* WbDatabaseException
|
471 |
|
|
*
|
472 |
|
|
* @category Core
|
473 |
|
|
* @package Core_database
|
474 |
|
|
* @author Werner v.d.Decken <wkl@isteam.de>
|
475 |
|
|
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
476 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
477 |
|
|
* @version 2.9.0
|
478 |
|
|
* @revision $Revision$
|
479 |
|
|
* @lastmodified $Date$
|
480 |
|
|
* @description Exceptionhandler for the WbDatabase and depending classes
|
481 |
|
|
*/
|
482 |
|
|
class WbDatabaseException extends AppException {}
|
483 |
1362
|
Luisehahne
|
|
484 |
1549
|
Luisehahne
|
define('MYSQL_SEEK_FIRST', 0);
|
485 |
|
|
define('MYSQL_SEEK_LAST', -1);
|
486 |
|
|
|
487 |
4
|
ryan
|
class mysql {
|
488 |
|
|
|
489 |
1680
|
darkviper
|
private $result = null;
|
490 |
|
|
private $_db_handle = null;
|
491 |
4
|
ryan
|
// Run a query
|
492 |
1680
|
darkviper
|
function query($statement, $dbHandle) {
|
493 |
|
|
$this->_db_handle = $dbHandle;
|
494 |
1889
|
Luisehahne
|
$this->result = @mysql_query($statement, $this->_db_handle);
|
495 |
|
|
if($this->result === false) {
|
496 |
|
|
if(DEBUG) {
|
497 |
|
|
throw new WbDatabaseException(mysql_error($this->_db_handle));
|
498 |
|
|
}else{
|
499 |
|
|
throw new WbDatabaseException('Error in SQL-Statement');
|
500 |
|
|
}
|
501 |
|
|
}
|
502 |
1680
|
darkviper
|
$this->error = mysql_error($this->_db_handle);
|
503 |
4
|
ryan
|
return $this->result;
|
504 |
|
|
}
|
505 |
1763
|
Luisehahne
|
|
506 |
4
|
ryan
|
// Fetch num rows
|
507 |
|
|
function numRows() {
|
508 |
|
|
return mysql_num_rows($this->result);
|
509 |
|
|
}
|
510 |
1011
|
Ruebenwurz
|
|
511 |
|
|
// Fetch row $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
|
512 |
|
|
function fetchRow($typ = MYSQL_BOTH) {
|
513 |
|
|
return mysql_fetch_array($this->result, $typ);
|
514 |
4
|
ryan
|
}
|
515 |
1011
|
Ruebenwurz
|
|
516 |
1549
|
Luisehahne
|
function rewind()
|
517 |
|
|
{
|
518 |
|
|
return $this->seekRow();
|
519 |
|
|
}
|
520 |
|
|
|
521 |
|
|
function seekRow( $position = MYSQL_SEEK_FIRST )
|
522 |
|
|
{
|
523 |
|
|
$pmax = $this->numRows() - 1;
|
524 |
|
|
$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
|
525 |
|
|
return mysql_data_seek($this->result, $p);
|
526 |
|
|
}
|
527 |
|
|
|
528 |
4
|
ryan
|
// Get error
|
529 |
|
|
function error() {
|
530 |
|
|
if(isset($this->error)) {
|
531 |
|
|
return $this->error;
|
532 |
|
|
} else {
|
533 |
|
|
return null;
|
534 |
|
|
}
|
535 |
|
|
}
|
536 |
|
|
|
537 |
|
|
}
|
538 |
1885
|
Luisehahne
|
// //////////////////////////////////////////////////////////////////////////////////// //
|
539 |
1364
|
Luisehahne
|
/* this function is placed inside this file temporarely until a better place is found */
|
540 |
|
|
/* function to update a var/value-pair(s) in table ****************************
|
541 |
|
|
* nonexisting keys are inserted
|
542 |
|
|
* @param string $table: name of table to use (without prefix)
|
543 |
|
|
* @param mixed $key: a array of key->value pairs to update
|
544 |
|
|
* or a string with name of the key to update
|
545 |
|
|
* @param string $value: a sting with needed value, if $key is a string too
|
546 |
|
|
* @return bool: true if any keys are updated, otherwise false
|
547 |
|
|
*/
|
548 |
|
|
function db_update_key_value($table, $key, $value = '')
|
549 |
|
|
{
|
550 |
|
|
global $database;
|
551 |
|
|
if( !is_array($key))
|
552 |
|
|
{
|
553 |
|
|
if( trim($key) != '' )
|
554 |
|
|
{
|
555 |
|
|
$key = array( trim($key) => trim($value) );
|
556 |
|
|
} else {
|
557 |
|
|
$key = array();
|
558 |
|
|
}
|
559 |
|
|
}
|
560 |
|
|
$retval = true;
|
561 |
|
|
foreach( $key as $index=>$val)
|
562 |
|
|
{
|
563 |
|
|
$index = strtolower($index);
|
564 |
1680
|
darkviper
|
$sql = 'SELECT COUNT(`setting_id`) '
|
565 |
|
|
. 'FROM `'.TABLE_PREFIX.$table.'` '
|
566 |
|
|
. 'WHERE `name` = \''.$index.'\' ';
|
567 |
1364
|
Luisehahne
|
if($database->get_one($sql))
|
568 |
|
|
{
|
569 |
|
|
$sql = 'UPDATE ';
|
570 |
|
|
$sql_where = 'WHERE `name` = \''.$index.'\'';
|
571 |
|
|
}else {
|
572 |
|
|
$sql = 'INSERT INTO ';
|
573 |
|
|
$sql_where = '';
|
574 |
|
|
}
|
575 |
|
|
$sql .= '`'.TABLE_PREFIX.$table.'` ';
|
576 |
|
|
$sql .= 'SET `name` = \''.$index.'\', ';
|
577 |
|
|
$sql .= '`value` = \''.$val.'\' '.$sql_where;
|
578 |
|
|
if( !$database->query($sql) )
|
579 |
|
|
{
|
580 |
|
|
$retval = false;
|
581 |
|
|
}
|
582 |
|
|
}
|
583 |
|
|
return $retval;
|
584 |
|
|
}
|