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 |
1998
|
darkviper
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
24 |
1866
|
Luisehahne
|
* @author Dietmar W. <dietmar.woellbrink@websitebaker.org>
|
25 |
1998
|
darkviper
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
26 |
1866
|
Luisehahne
|
* @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 |
1998
|
darkviper
|
protected $oDbHandle = null; // readonly from outside
|
42 |
|
|
protected $sDbName = '';
|
43 |
|
|
protected $sInstanceIdentifier = '';
|
44 |
1885
|
Luisehahne
|
protected $sTablePrefix = '';
|
45 |
1889
|
Luisehahne
|
protected $sCharset = '';
|
46 |
1885
|
Luisehahne
|
protected $connected = false;
|
47 |
|
|
protected $error = '';
|
48 |
|
|
protected $error_type = '';
|
49 |
|
|
protected $iQueryCount = 0;
|
50 |
1362
|
Luisehahne
|
|
51 |
1998
|
darkviper
|
/**
|
52 |
|
|
* __constructor
|
53 |
|
|
* prevent from public instancing
|
54 |
|
|
*/
|
55 |
1686
|
darkviper
|
protected function __construct() {}
|
56 |
1998
|
darkviper
|
/**
|
57 |
|
|
* prevent from cloning
|
58 |
|
|
*/
|
59 |
1683
|
darkviper
|
private function __clone() {}
|
60 |
|
|
/**
|
61 |
1686
|
darkviper
|
* get a valid instance of this class
|
62 |
|
|
* @param string $sIdentifier selector for several different instances
|
63 |
1998
|
darkviper
|
* @return WbDatabase object
|
64 |
1686
|
darkviper
|
*/
|
65 |
1998
|
darkviper
|
public static function getInstance($sIdentifier = 'core')
|
66 |
|
|
{
|
67 |
1686
|
darkviper
|
if( !isset(self::$_oInstances[$sIdentifier])) {
|
68 |
|
|
$c = __CLASS__;
|
69 |
1974
|
darkviper
|
$oInstance = new $c;
|
70 |
1998
|
darkviper
|
$oInstance->sInstanceIdentifier = $sIdentifier;
|
71 |
1974
|
darkviper
|
self::$_oInstances[$sIdentifier] = $oInstance;
|
72 |
1686
|
darkviper
|
}
|
73 |
|
|
return self::$_oInstances[$sIdentifier];
|
74 |
|
|
}
|
75 |
|
|
/**
|
76 |
|
|
* disconnect and kills an existing instance
|
77 |
1998
|
darkviper
|
* @param string $sIdentifier selector for instance to kill
|
78 |
1686
|
darkviper
|
*/
|
79 |
1998
|
darkviper
|
public static function killInstance($sIdentifier)
|
80 |
|
|
{
|
81 |
1686
|
darkviper
|
if($sIdentifier != 'core') {
|
82 |
|
|
if( isset(self::$_oInstances[$sIdentifier])) {
|
83 |
|
|
self::$_oInstances[$sIdentifier]->disconnect();
|
84 |
|
|
unset(self::$_oInstances[$sIdentifier]);
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
/**
|
89 |
1998
|
darkviper
|
* Establish connection
|
90 |
1683
|
darkviper
|
* @param string $url
|
91 |
|
|
* @return bool
|
92 |
1998
|
darkviper
|
* @throws WbDatabaseException
|
93 |
|
|
* @description opens a connection using connect URL<br />
|
94 |
|
|
* Example for SQL-Url: 'mysql://user:password@example.com[:3306]/database?charset=utf8&tableprefix=xx_'
|
95 |
1683
|
darkviper
|
*/
|
96 |
1998
|
darkviper
|
public function doConnect($url = '')
|
97 |
|
|
{
|
98 |
|
|
if ($this->connected) { return $this->connected; } // prevent from reconnecting
|
99 |
1885
|
Luisehahne
|
$this->connected = false;
|
100 |
1998
|
darkviper
|
if ($url != '') {
|
101 |
|
|
// parse URL and extract connection data
|
102 |
1680
|
darkviper
|
$aIni = parse_url($url);
|
103 |
1866
|
Luisehahne
|
$scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql';
|
104 |
|
|
$hostname = isset($aIni['host']) ? $aIni['host'] : '';
|
105 |
|
|
$username = isset($aIni['user']) ? $aIni['user'] : '';
|
106 |
|
|
$password = isset($aIni['pass']) ? $aIni['pass'] : '';
|
107 |
|
|
$hostport = isset($aIni['port']) ? $aIni['port'] : '3306';
|
108 |
|
|
$hostport = $hostport == '3306' ? '' : ':'.$hostport;
|
109 |
|
|
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\');
|
110 |
1885
|
Luisehahne
|
$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
|
111 |
|
|
$aQuery = explode('&', $sTmp);
|
112 |
1998
|
darkviper
|
foreach ($aQuery as $sArgument) {
|
113 |
1885
|
Luisehahne
|
$aArg = explode('=', $sArgument);
|
114 |
1998
|
darkviper
|
switch (strtolower($aArg[0])) {
|
115 |
1885
|
Luisehahne
|
case 'charset':
|
116 |
|
|
$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
|
117 |
|
|
break;
|
118 |
|
|
case 'tableprefix':
|
119 |
|
|
$this->sTablePrefix = $aArg[1];
|
120 |
|
|
break;
|
121 |
|
|
default:
|
122 |
|
|
break;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
1998
|
darkviper
|
$this->sDbName = $db_name;
|
126 |
|
|
} else {
|
127 |
1885
|
Luisehahne
|
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
128 |
1680
|
darkviper
|
}
|
129 |
1998
|
darkviper
|
$this->oDbHandle = @mysql_connect($hostname.$hostport, $username, $password, true);
|
130 |
|
|
if (!$this->oDbHandle) {
|
131 |
|
|
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.$hostport.'\'');
|
132 |
4
|
ryan
|
} else {
|
133 |
1998
|
darkviper
|
if (!@mysql_select_db($db_name, $this->oDbHandle)) {
|
134 |
1885
|
Luisehahne
|
throw new WbDatabaseException('unable to select database \''.$db_name.
|
135 |
1998
|
darkviper
|
'\' on \''.$scheme.'://'.
|
136 |
|
|
$hostname.$hostport.'\''
|
137 |
|
|
);
|
138 |
4
|
ryan
|
} else {
|
139 |
1998
|
darkviper
|
if ($this->sCharset) {
|
140 |
|
|
@mysql_query('SET NAMES \''.$this->sCharset.'\'', $this->oDbHandle);
|
141 |
1885
|
Luisehahne
|
}
|
142 |
4
|
ryan
|
$this->connected = true;
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
return $this->connected;
|
146 |
|
|
}
|
147 |
1998
|
darkviper
|
/**
|
148 |
|
|
* disconnect database
|
149 |
|
|
* @return bool
|
150 |
|
|
* @description Disconnect current object from the database<br />
|
151 |
|
|
* the 'core' connection can NOT be disconnected!
|
152 |
|
|
*/
|
153 |
|
|
public function disconnect()
|
154 |
|
|
{
|
155 |
|
|
if ($this->connected == true && $oInstance->sInstanceIdentifier != 'core') {
|
156 |
|
|
mysql_close($this->oDbHandle);
|
157 |
1974
|
darkviper
|
$this->connected = false;
|
158 |
4
|
ryan
|
return true;
|
159 |
|
|
}
|
160 |
1974
|
darkviper
|
return false;
|
161 |
4
|
ryan
|
}
|
162 |
1998
|
darkviper
|
/**
|
163 |
|
|
* Alias for doQuery()
|
164 |
|
|
*/
|
165 |
|
|
public function query($statement)
|
166 |
|
|
{
|
167 |
|
|
return $this->doQuery($statement);
|
168 |
|
|
}
|
169 |
|
|
/**
|
170 |
|
|
* execute query
|
171 |
|
|
* @param string $statement the SQL-statement to execute
|
172 |
|
|
* @return null|\mysql
|
173 |
|
|
*/
|
174 |
|
|
public function doQuery($statement) {
|
175 |
1662
|
darkviper
|
$this->iQueryCount++;
|
176 |
4
|
ryan
|
$mysql = new mysql();
|
177 |
1998
|
darkviper
|
$mysql->query($statement, $this->oDbHandle);
|
178 |
|
|
$this->set_error($mysql->error($this->oDbHandle));
|
179 |
|
|
if ($mysql->error($this->oDbHandle)) {
|
180 |
4
|
ryan
|
return null;
|
181 |
|
|
} else {
|
182 |
|
|
return $mysql;
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
1998
|
darkviper
|
/**
|
186 |
|
|
* Alias for getOne()
|
187 |
|
|
*/
|
188 |
1686
|
darkviper
|
public function get_one( $statement )
|
189 |
1362
|
Luisehahne
|
{
|
190 |
1998
|
darkviper
|
return $this->getOne($statement);
|
191 |
|
|
}
|
192 |
|
|
// Gets the first column of the first row
|
193 |
|
|
/**
|
194 |
|
|
* Gets the first column of the first row
|
195 |
|
|
* @param string $statement SQL-statement
|
196 |
|
|
* @return null|mixed
|
197 |
|
|
*/
|
198 |
|
|
public function getOne( $statement )
|
199 |
|
|
{
|
200 |
1662
|
darkviper
|
$this->iQueryCount++;
|
201 |
1998
|
darkviper
|
$fetch_row = mysql_fetch_array(mysql_query($statement, $this->oDbHandle));
|
202 |
4
|
ryan
|
$result = $fetch_row[0];
|
203 |
1998
|
darkviper
|
$this->set_error(mysql_error($this->oDbHandle));
|
204 |
|
|
if (mysql_error($this->oDbHandle)) {
|
205 |
4
|
ryan
|
return null;
|
206 |
|
|
} else {
|
207 |
|
|
return $result;
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
1998
|
darkviper
|
/**
|
211 |
|
|
* Alias for setError()
|
212 |
|
|
*/
|
213 |
|
|
public function set_error($message = null)
|
214 |
|
|
{
|
215 |
|
|
$this->setError($message = null);
|
216 |
|
|
}
|
217 |
4
|
ryan
|
// Set the DB error
|
218 |
1998
|
darkviper
|
/**
|
219 |
|
|
* setError
|
220 |
|
|
* @param string $message
|
221 |
|
|
*/
|
222 |
|
|
public function setError($message = null)
|
223 |
|
|
{
|
224 |
4
|
ryan
|
$this->error = $message;
|
225 |
|
|
}
|
226 |
1998
|
darkviper
|
/**
|
227 |
|
|
* Alias for isError
|
228 |
|
|
*/
|
229 |
|
|
public function is_error()
|
230 |
|
|
{
|
231 |
|
|
return $this->isError();
|
232 |
|
|
}
|
233 |
|
|
/**
|
234 |
|
|
* isError
|
235 |
|
|
* @return bool
|
236 |
|
|
*/
|
237 |
|
|
public function isError()
|
238 |
|
|
{
|
239 |
4
|
ryan
|
return (!empty($this->error)) ? true : false;
|
240 |
|
|
}
|
241 |
1998
|
darkviper
|
/**
|
242 |
|
|
* Alias for getError
|
243 |
|
|
*/
|
244 |
|
|
public function get_error()
|
245 |
|
|
{
|
246 |
|
|
return $this->getError();
|
247 |
|
|
}
|
248 |
|
|
/**
|
249 |
|
|
* get last Error
|
250 |
|
|
* @return string
|
251 |
|
|
*/
|
252 |
|
|
public function getError()
|
253 |
|
|
{
|
254 |
4
|
ryan
|
return $this->error;
|
255 |
|
|
}
|
256 |
1613
|
darkviper
|
/**
|
257 |
1885
|
Luisehahne
|
* Protect class from property injections
|
258 |
|
|
* @param string name of property
|
259 |
|
|
* @param mixed value
|
260 |
|
|
* @throws WbDatabaseException
|
261 |
1866
|
Luisehahne
|
*/
|
262 |
1998
|
darkviper
|
public function __set($name, $value)
|
263 |
|
|
{
|
264 |
1885
|
Luisehahne
|
throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
|
265 |
1866
|
Luisehahne
|
}
|
266 |
|
|
/**
|
267 |
1613
|
darkviper
|
* default Getter for some properties
|
268 |
1866
|
Luisehahne
|
* @param string name of the Property
|
269 |
1998
|
darkviper
|
* @return NULL on error | valid property
|
270 |
1362
|
Luisehahne
|
*/
|
271 |
1613
|
darkviper
|
public function __get($sPropertyName)
|
272 |
1362
|
Luisehahne
|
{
|
273 |
1998
|
darkviper
|
switch ($sPropertyName) {
|
274 |
1613
|
darkviper
|
case 'DbHandle':
|
275 |
1998
|
darkviper
|
case 'getDbHandle': // << set deprecated
|
276 |
|
|
case 'db_handle': // << set deprecated
|
277 |
|
|
$retval = $this->oDbHandle;
|
278 |
1613
|
darkviper
|
break;
|
279 |
1866
|
Luisehahne
|
case 'LastInsertId':
|
280 |
1998
|
darkviper
|
case 'getLastInsertId': // << set deprecated
|
281 |
|
|
$retval = mysql_insert_id($this->oDbHandle);
|
282 |
1866
|
Luisehahne
|
break;
|
283 |
1613
|
darkviper
|
case 'DbName':
|
284 |
1998
|
darkviper
|
case 'getDbName': // << set deprecated
|
285 |
|
|
case 'db_name': // << set deprecated
|
286 |
|
|
$retval = $this->sDbName;
|
287 |
1613
|
darkviper
|
break;
|
288 |
1885
|
Luisehahne
|
case 'TablePrefix':
|
289 |
1998
|
darkviper
|
case 'getTablePrefix': // << set deprecated
|
290 |
1885
|
Luisehahne
|
$retval = $this->sTablePrefix;
|
291 |
|
|
break;
|
292 |
1974
|
darkviper
|
case 'QueryCount':
|
293 |
1998
|
darkviper
|
case 'getQueryCount': // << set deprecated
|
294 |
1662
|
darkviper
|
$retval = $this->iQueryCount;
|
295 |
|
|
break;
|
296 |
1613
|
darkviper
|
default:
|
297 |
|
|
$retval = null;
|
298 |
|
|
break;
|
299 |
1998
|
darkviper
|
}
|
300 |
1613
|
darkviper
|
return $retval;
|
301 |
|
|
} // __get()
|
302 |
1885
|
Luisehahne
|
/**
|
303 |
|
|
* Escapes special characters in a string for use in an SQL statement
|
304 |
|
|
* @param string $unescaped_string
|
305 |
|
|
* @return string
|
306 |
|
|
*/
|
307 |
|
|
public function escapeString($unescaped_string)
|
308 |
|
|
{
|
309 |
1998
|
darkviper
|
return mysql_real_escape_string($unescaped_string, $this->oDbHandle);
|
310 |
1885
|
Luisehahne
|
}
|
311 |
|
|
/**
|
312 |
|
|
* Last inserted Id
|
313 |
|
|
* @return bool|int false on error, 0 if no record inserted
|
314 |
|
|
*/
|
315 |
|
|
public function getLastInsertId()
|
316 |
|
|
{
|
317 |
1998
|
darkviper
|
return mysql_insert_id($this->oDbHandle);
|
318 |
1885
|
Luisehahne
|
}
|
319 |
1998
|
darkviper
|
/**
|
320 |
|
|
* Alias for isField()
|
321 |
|
|
*/
|
322 |
|
|
public function field_exists($table_name, $field_name)
|
323 |
|
|
{
|
324 |
|
|
return $this->isField($table_name, $field_name);
|
325 |
|
|
}
|
326 |
1362
|
Luisehahne
|
/*
|
327 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
328 |
|
|
* @param string name of the field to seek for
|
329 |
|
|
* @return bool true if field exists
|
330 |
1362
|
Luisehahne
|
*/
|
331 |
1998
|
darkviper
|
public function isField($table_name, $field_name)
|
332 |
1362
|
Luisehahne
|
{
|
333 |
|
|
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
334 |
1998
|
darkviper
|
$query = $this->query($sql, $this->oDbHandle);
|
335 |
1362
|
Luisehahne
|
return ($query->numRows() != 0);
|
336 |
|
|
}
|
337 |
1998
|
darkviper
|
/**
|
338 |
|
|
* Alias for isIndex()
|
339 |
|
|
*/
|
340 |
|
|
public function index_exists($table_name, $index_name, $number_fields = 0)
|
341 |
|
|
{
|
342 |
|
|
return $this->isIndex($table_name, $index_name, $number_fields = 0);
|
343 |
|
|
}
|
344 |
1362
|
Luisehahne
|
/*
|
345 |
1998
|
darkviper
|
* isIndex
|
346 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
347 |
|
|
* @param string name of the index to seek for
|
348 |
|
|
* @return bool true if field exists
|
349 |
1362
|
Luisehahne
|
*/
|
350 |
1998
|
darkviper
|
public function isIndex($table_name, $index_name, $number_fields = 0)
|
351 |
1362
|
Luisehahne
|
{
|
352 |
|
|
$number_fields = intval($number_fields);
|
353 |
|
|
$keys = 0;
|
354 |
|
|
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
355 |
1998
|
darkviper
|
if (($res_keys = $this->doQuery($sql, $this->oDbHandle))) {
|
356 |
|
|
while (($rec_key = $res_keys->fetchRow(MYSQL_ASSOC))) {
|
357 |
|
|
if ( $rec_key['Key_name'] == $index_name ) {
|
358 |
1362
|
Luisehahne
|
$keys++;
|
359 |
|
|
}
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
}
|
363 |
1998
|
darkviper
|
if ( $number_fields == 0 ) {
|
364 |
1362
|
Luisehahne
|
return ($keys != $number_fields);
|
365 |
1998
|
darkviper
|
} else {
|
366 |
1362
|
Luisehahne
|
return ($keys == $number_fields);
|
367 |
|
|
}
|
368 |
|
|
}
|
369 |
1998
|
darkviper
|
/**
|
370 |
|
|
* Alias for addField()
|
371 |
|
|
*/
|
372 |
|
|
public function field_add($table_name, $field_name, $description)
|
373 |
|
|
{
|
374 |
|
|
return $this->addField($table_name, $field_name, $description);
|
375 |
|
|
}
|
376 |
1362
|
Luisehahne
|
/*
|
377 |
1866
|
Luisehahne
|
* @param string full name of the table (incl. TABLE_PREFIX)
|
378 |
|
|
* @param string name of the field to add
|
379 |
|
|
* @param string describes the new field like ( INT NOT NULL DEFAULT '0')
|
380 |
|
|
* @return bool true if successful, otherwise false and error will be set
|
381 |
1362
|
Luisehahne
|
*/
|
382 |
1998
|
darkviper
|
public function addField($table_name, $field_name, $description)
|
383 |
1362
|
Luisehahne
|
{
|
384 |
1998
|
darkviper
|
if (!$this->isField($table_name, $field_name)) {
|
385 |
|
|
// add new field into a table
|
386 |
1362
|
Luisehahne
|
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
387 |
1998
|
darkviper
|
$query = $this->doQuery($sql, $this->oDbHandle);
|
388 |
|
|
$this->set_error(mysql_error($this->oDbHandle));
|
389 |
|
|
if (!$this->isError()) {
|
390 |
|
|
return ( $this->isField($table_name, $field_name) ) ? true : false;
|
391 |
1362
|
Luisehahne
|
}
|
392 |
1998
|
darkviper
|
} else {
|
393 |
1362
|
Luisehahne
|
$this->set_error('field \''.$field_name.'\' already exists');
|
394 |
|
|
}
|
395 |
|
|
return false;
|
396 |
|
|
}
|
397 |
1998
|
darkviper
|
/**
|
398 |
|
|
* Alias for modifyField()
|
399 |
|
|
*/
|
400 |
|
|
public function field_modify($table_name, $field_name, $description)
|
401 |
|
|
{
|
402 |
|
|
return $this->modifyField($table_name, $field_name, $description);
|
403 |
|
|
}
|
404 |
1362
|
Luisehahne
|
/*
|
405 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
406 |
|
|
* @param string $field_name: name of the field to add
|
407 |
|
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
408 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
409 |
|
|
*/
|
410 |
1998
|
darkviper
|
public function modifyField($table_name, $field_name, $description)
|
411 |
1362
|
Luisehahne
|
{
|
412 |
|
|
$retval = false;
|
413 |
1998
|
darkviper
|
if ($this->isField($table_name, $field_name)) {
|
414 |
|
|
// modify a existing field in a table
|
415 |
1486
|
DarkViper
|
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
|
416 |
1998
|
darkviper
|
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false);
|
417 |
|
|
$this->setError(mysql_error());
|
418 |
1362
|
Luisehahne
|
}
|
419 |
1486
|
DarkViper
|
return $retval;
|
420 |
1362
|
Luisehahne
|
}
|
421 |
1998
|
darkviper
|
/**
|
422 |
|
|
* Alias for removeField()
|
423 |
|
|
*/
|
424 |
|
|
public function field_remove($table_name, $field_name)
|
425 |
|
|
{
|
426 |
|
|
return $this->removeField($table_name, $field_name);
|
427 |
|
|
}
|
428 |
1362
|
Luisehahne
|
/*
|
429 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
430 |
|
|
* @param string $field_name: name of the field to remove
|
431 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
432 |
|
|
*/
|
433 |
1998
|
darkviper
|
public function removeField($table_name, $field_name)
|
434 |
1362
|
Luisehahne
|
{
|
435 |
|
|
$retval = false;
|
436 |
1998
|
darkviper
|
if ($this->isField($table_name, $field_name)) {
|
437 |
|
|
// modify a existing field in a table
|
438 |
1362
|
Luisehahne
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
439 |
1998
|
darkviper
|
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
440 |
1362
|
Luisehahne
|
}
|
441 |
|
|
return $retval;
|
442 |
|
|
}
|
443 |
1998
|
darkviper
|
/**
|
444 |
|
|
* Alias for addIndex()
|
445 |
|
|
*/
|
446 |
|
|
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
|
447 |
|
|
{
|
448 |
|
|
return $this->addIndex($table_name, $index_name, $field_list, $index_type);
|
449 |
|
|
}
|
450 |
1362
|
Luisehahne
|
/*
|
451 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
452 |
1763
|
Luisehahne
|
* @param string $index_name: name of the new index (empty string for PRIMARY)
|
453 |
1362
|
Luisehahne
|
* @param string $field_list: comma seperated list of fields for this index
|
454 |
1763
|
Luisehahne
|
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
|
455 |
1362
|
Luisehahne
|
* @return bool: true if successful, otherwise false and error will be set
|
456 |
|
|
*/
|
457 |
1998
|
darkviper
|
public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY')
|
458 |
1763
|
Luisehahne
|
{
|
459 |
|
|
$retval = false;
|
460 |
1885
|
Luisehahne
|
$field_list = explode(',', (str_replace(' ', '', $field_list)));
|
461 |
1763
|
Luisehahne
|
$number_fields = sizeof($field_list);
|
462 |
|
|
$field_list = '`'.implode('`,`', $field_list).'`';
|
463 |
|
|
$index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
|
464 |
1998
|
darkviper
|
if ( $this->isIndex($table_name, $index_name, $number_fields) ||
|
465 |
|
|
$this->isIndex($table_name, $index_name))
|
466 |
1763
|
Luisehahne
|
{
|
467 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
468 |
|
|
$sql .= 'DROP INDEX `'.$index_name.'`';
|
469 |
1998
|
darkviper
|
if (!$this->doQuery($sql, $this->oDbHandle)) { return false; }
|
470 |
1763
|
Luisehahne
|
}
|
471 |
|
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
472 |
|
|
$sql .= 'ADD '.$index_type.' ';
|
473 |
|
|
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
|
474 |
|
|
$sql .= '( '.$field_list.' ); ';
|
475 |
1998
|
darkviper
|
if ($this->doQuery($sql, $this->oDbHandle)) { $retval = true; }
|
476 |
1763
|
Luisehahne
|
return $retval;
|
477 |
|
|
}
|
478 |
1998
|
darkviper
|
/**
|
479 |
|
|
* Alias for removeIndex()
|
480 |
|
|
*/
|
481 |
|
|
public function index_remove($table_name, $index_name)
|
482 |
|
|
{
|
483 |
|
|
return $this->removeIndex($table_name, $index_name);
|
484 |
|
|
}
|
485 |
1362
|
Luisehahne
|
/*
|
486 |
|
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
487 |
|
|
* @param string $field_name: name of the field to remove
|
488 |
|
|
* @return bool: true if successful, otherwise false and error will be set
|
489 |
|
|
*/
|
490 |
1998
|
darkviper
|
public function removeIndex($table_name, $index_name)
|
491 |
1362
|
Luisehahne
|
{
|
492 |
|
|
$retval = false;
|
493 |
1998
|
darkviper
|
if ($this->isIndex($table_name, $index_name)) {
|
494 |
|
|
// modify a existing field in a table
|
495 |
1362
|
Luisehahne
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
496 |
1998
|
darkviper
|
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
497 |
1362
|
Luisehahne
|
}
|
498 |
|
|
return $retval;
|
499 |
|
|
}
|
500 |
1586
|
darkviper
|
/**
|
501 |
1998
|
darkviper
|
* Alias for importSql()
|
502 |
|
|
*/
|
503 |
|
|
public function SqlImport($sSqlDump,
|
504 |
|
|
$sTablePrefix = '',
|
505 |
|
|
$bPreserve = true,
|
506 |
|
|
$sEngine = 'MyISAM',
|
507 |
|
|
$sCollation = 'utf8_unicode_ci')
|
508 |
|
|
{
|
509 |
|
|
return $this->importSql($sSqlDump, $sTablePrefix, $bPreserve, $sEngine, $sCollation);
|
510 |
|
|
}
|
511 |
|
|
/**
|
512 |
1586
|
darkviper
|
* Import a standard *.sql dump file
|
513 |
|
|
* @param string $sSqlDump link to the sql-dumpfile
|
514 |
|
|
* @param string $sTablePrefix
|
515 |
1887
|
Luisehahne
|
* @param bool $bPreserve set to true will ignore all DROP TABLE statements
|
516 |
|
|
* @param string $sEngine can be 'MyISAM' or 'InnoDB'
|
517 |
|
|
* @param string $sCollation one of the list of available collations
|
518 |
1586
|
darkviper
|
* @return boolean true if import successful
|
519 |
1998
|
darkviper
|
* @description Import a standard *.sql dump file<br />
|
520 |
|
|
* The file can include placeholders TABLE_PREFIX, TABLE_COLLATION and TABLE_ENGINE
|
521 |
1586
|
darkviper
|
*/
|
522 |
1998
|
darkviper
|
public function importSql($sSqlDump,
|
523 |
|
|
$sTablePrefix = '', /* unused argument, for backward compatibility only! */
|
524 |
1887
|
Luisehahne
|
$bPreserve = true,
|
525 |
|
|
$sEngine = 'MyISAM',
|
526 |
|
|
$sCollation = 'utf8_unicode_ci')
|
527 |
1586
|
darkviper
|
{
|
528 |
1887
|
Luisehahne
|
$sCollation = ($sCollation != '' ? $sCollation : 'utf8_unicode_ci');
|
529 |
|
|
$aCharset = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY);
|
530 |
|
|
$sEngine = 'ENGINE='.$sEngine.' DEFAULT CHARSET='.$aCharset[0].' COLLATE='.$sCollation;
|
531 |
|
|
$sCollation = ' collate '.$sCollation;
|
532 |
1586
|
darkviper
|
$retval = true;
|
533 |
|
|
$this->error = '';
|
534 |
|
|
$aSearch = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
|
535 |
1887
|
Luisehahne
|
$aReplace = array($this->sTablePrefix, $sEngine, $sCollation);
|
536 |
1586
|
darkviper
|
$sql = '';
|
537 |
|
|
$aSql = file($sSqlDump);
|
538 |
1897
|
Luisehahne
|
// $aSql[0] = preg_replace('/^\xEF\xBB\xBF/', '', $aSql[0]);
|
539 |
|
|
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
|
540 |
1998
|
darkviper
|
while (sizeof($aSql) > 0) {
|
541 |
1586
|
darkviper
|
$sSqlLine = trim(array_shift($aSql));
|
542 |
1591
|
darkviper
|
if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
|
543 |
1592
|
darkviper
|
$sql = $sql.' '.$sSqlLine;
|
544 |
1586
|
darkviper
|
if ((substr($sql,-1,1) == ';')) {
|
545 |
|
|
$sql = trim(str_replace( $aSearch, $aReplace, $sql));
|
546 |
|
|
if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
|
547 |
1998
|
darkviper
|
if (!mysql_query($sql, $this->oDbHandle)) {
|
548 |
1586
|
darkviper
|
$retval = false;
|
549 |
1998
|
darkviper
|
$this->error = mysql_error($this->oDbHandle);
|
550 |
1586
|
darkviper
|
unset($aSql);
|
551 |
|
|
break;
|
552 |
|
|
}
|
553 |
|
|
}
|
554 |
|
|
$sql = '';
|
555 |
|
|
}
|
556 |
|
|
}
|
557 |
|
|
}
|
558 |
|
|
return $retval;
|
559 |
|
|
}
|
560 |
|
|
/**
|
561 |
|
|
* retuns the type of the engine used for requested table
|
562 |
|
|
* @param string $table name of the table, including prefix
|
563 |
|
|
* @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
|
564 |
|
|
*/
|
565 |
1535
|
Luisehahne
|
public function getTableEngine($table)
|
566 |
|
|
{
|
567 |
|
|
$retVal = false;
|
568 |
1998
|
darkviper
|
$mysqlVersion = mysql_get_server_info($this->oDbHandle);
|
569 |
1535
|
Luisehahne
|
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
|
570 |
1998
|
darkviper
|
$sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\'';
|
571 |
|
|
if (($result = $this->doQuery($sql, $this->oDbHandle))) {
|
572 |
|
|
if (($row = $result->fetchRow(MYSQL_ASSOC))) {
|
573 |
1535
|
Luisehahne
|
$retVal = $row[$engineValue];
|
574 |
|
|
}
|
575 |
|
|
}
|
576 |
|
|
return $retVal;
|
577 |
|
|
}
|
578 |
|
|
|
579 |
|
|
|
580 |
1362
|
Luisehahne
|
} /// end of class database
|
581 |
1885
|
Luisehahne
|
// //////////////////////////////////////////////////////////////////////////////////// //
|
582 |
|
|
/**
|
583 |
|
|
* WbDatabaseException
|
584 |
|
|
*
|
585 |
|
|
* @category Core
|
586 |
|
|
* @package Core_database
|
587 |
1998
|
darkviper
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
588 |
|
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
589 |
1885
|
Luisehahne
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
590 |
|
|
* @version 2.9.0
|
591 |
|
|
* @revision $Revision$
|
592 |
|
|
* @lastmodified $Date$
|
593 |
|
|
* @description Exceptionhandler for the WbDatabase and depending classes
|
594 |
|
|
*/
|
595 |
|
|
class WbDatabaseException extends AppException {}
|
596 |
1362
|
Luisehahne
|
|
597 |
1998
|
darkviper
|
/* extend global constants of mysql */
|
598 |
|
|
if(!defined('MYSQL_SEEK_FIRST')) { define('MYSQL_SEEK_FIRST', 0); }
|
599 |
|
|
if(!defined('MYSQL_SEEK_LAST')) { define('MYSQL_SEEK_LAST', -1); }
|
600 |
1549
|
Luisehahne
|
|
601 |
1998
|
darkviper
|
/**
|
602 |
|
|
* mysql
|
603 |
|
|
*
|
604 |
|
|
* @category Core
|
605 |
|
|
* @package Core_database
|
606 |
|
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
607 |
|
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
608 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
609 |
|
|
* @version 2.9.0
|
610 |
|
|
* @revision $Revision$
|
611 |
|
|
* @lastmodified $Date$
|
612 |
|
|
* @description MYSQL result object for requests
|
613 |
|
|
*
|
614 |
|
|
*/
|
615 |
4
|
ryan
|
class mysql {
|
616 |
|
|
|
617 |
1680
|
darkviper
|
private $result = null;
|
618 |
|
|
private $_db_handle = null;
|
619 |
1998
|
darkviper
|
|
620 |
|
|
/**
|
621 |
|
|
* query sql statement
|
622 |
|
|
* @param string $statement
|
623 |
|
|
* @param object $dbHandle
|
624 |
|
|
* @return object
|
625 |
|
|
* @throws WbDatabaseException
|
626 |
|
|
*/
|
627 |
|
|
function query($statement, $dbHandle)
|
628 |
|
|
{
|
629 |
|
|
$this->oDbHandle = $dbHandle;
|
630 |
|
|
$this->result = @mysql_query($statement, $this->oDbHandle);
|
631 |
|
|
if ($this->result === false) {
|
632 |
|
|
if (DEBUG) {
|
633 |
|
|
throw new WbDatabaseException(mysql_error($this->oDbHandle));
|
634 |
|
|
} else {
|
635 |
1889
|
Luisehahne
|
throw new WbDatabaseException('Error in SQL-Statement');
|
636 |
|
|
}
|
637 |
|
|
}
|
638 |
1998
|
darkviper
|
$this->error = mysql_error($this->oDbHandle);
|
639 |
4
|
ryan
|
return $this->result;
|
640 |
|
|
}
|
641 |
1998
|
darkviper
|
/**
|
642 |
|
|
* numRows
|
643 |
|
|
* @return integer
|
644 |
|
|
* @description number of returned records
|
645 |
|
|
*/
|
646 |
|
|
function numRows()
|
647 |
|
|
{
|
648 |
4
|
ryan
|
return mysql_num_rows($this->result);
|
649 |
|
|
}
|
650 |
1998
|
darkviper
|
/**
|
651 |
|
|
* fetchRow
|
652 |
|
|
* @param int $typ MYSQL_BOTH(default) | MYSQL_ASSOC | MYSQL_NUM
|
653 |
|
|
* @return array
|
654 |
|
|
* @description get current record and increment pointer
|
655 |
|
|
*/
|
656 |
|
|
function fetchRow($typ = MYSQL_BOTH)
|
657 |
|
|
{
|
658 |
1011
|
Ruebenwurz
|
return mysql_fetch_array($this->result, $typ);
|
659 |
4
|
ryan
|
}
|
660 |
1998
|
darkviper
|
/**
|
661 |
|
|
* fetchObject
|
662 |
|
|
* @param string $sClassname Name of the class to use. Is no given use stdClass
|
663 |
|
|
* @param string $aParams optional array of arguments for the constructor
|
664 |
|
|
* @return object
|
665 |
|
|
* @description get current record as an object and increment pointer
|
666 |
|
|
*/
|
667 |
|
|
function fetchObject($sClassName = null, array $aParams = null)
|
668 |
|
|
{
|
669 |
|
|
return mysql_fetch_object($this->result, $sClassName, $aParams);
|
670 |
|
|
}
|
671 |
|
|
/**
|
672 |
|
|
* rewind
|
673 |
|
|
* @return bool
|
674 |
|
|
* @description set the recordpointer to the first record || false on error
|
675 |
|
|
*/
|
676 |
1549
|
Luisehahne
|
function rewind()
|
677 |
|
|
{
|
678 |
1998
|
darkviper
|
return $this->seekRow(MYSQL_SEEK_FIRST);
|
679 |
1549
|
Luisehahne
|
}
|
680 |
1998
|
darkviper
|
/**
|
681 |
|
|
* seekRow
|
682 |
|
|
* @param int $position
|
683 |
|
|
* @return bool
|
684 |
|
|
* @description set the pointer to the given record || false on error
|
685 |
|
|
*/
|
686 |
1549
|
Luisehahne
|
function seekRow( $position = MYSQL_SEEK_FIRST )
|
687 |
|
|
{
|
688 |
|
|
$pmax = $this->numRows() - 1;
|
689 |
|
|
$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
|
690 |
|
|
return mysql_data_seek($this->result, $p);
|
691 |
|
|
}
|
692 |
1998
|
darkviper
|
/**
|
693 |
|
|
* freeResult
|
694 |
|
|
* @return bool
|
695 |
|
|
* @description remove retult object from memeory
|
696 |
|
|
*/
|
697 |
|
|
function freeResult()
|
698 |
|
|
{
|
699 |
|
|
return mysql_free_result($this->result);
|
700 |
|
|
}
|
701 |
|
|
/**
|
702 |
|
|
* Get error
|
703 |
|
|
* @return string || null if no error
|
704 |
|
|
*/
|
705 |
|
|
function error()
|
706 |
|
|
{
|
707 |
|
|
if (isset($this->error)) {
|
708 |
4
|
ryan
|
return $this->error;
|
709 |
|
|
} else {
|
710 |
|
|
return null;
|
711 |
|
|
}
|
712 |
|
|
}
|
713 |
|
|
|
714 |
|
|
}
|
715 |
1885
|
Luisehahne
|
// //////////////////////////////////////////////////////////////////////////////////// //
|
716 |
1364
|
Luisehahne
|
/* this function is placed inside this file temporarely until a better place is found */
|
717 |
|
|
/* function to update a var/value-pair(s) in table ****************************
|
718 |
|
|
* nonexisting keys are inserted
|
719 |
|
|
* @param string $table: name of table to use (without prefix)
|
720 |
|
|
* @param mixed $key: a array of key->value pairs to update
|
721 |
|
|
* or a string with name of the key to update
|
722 |
|
|
* @param string $value: a sting with needed value, if $key is a string too
|
723 |
|
|
* @return bool: true if any keys are updated, otherwise false
|
724 |
|
|
*/
|
725 |
|
|
function db_update_key_value($table, $key, $value = '')
|
726 |
|
|
{
|
727 |
1998
|
darkviper
|
$oDb = WbDatabase::getInstance();
|
728 |
|
|
if (!is_array($key)) {
|
729 |
|
|
if (trim($key) != '') {
|
730 |
1364
|
Luisehahne
|
$key = array( trim($key) => trim($value) );
|
731 |
|
|
} else {
|
732 |
|
|
$key = array();
|
733 |
|
|
}
|
734 |
|
|
}
|
735 |
|
|
$retval = true;
|
736 |
|
|
foreach( $key as $index=>$val)
|
737 |
|
|
{
|
738 |
|
|
$index = strtolower($index);
|
739 |
1680
|
darkviper
|
$sql = 'SELECT COUNT(`setting_id`) '
|
740 |
1998
|
darkviper
|
. 'FROM `'.$oDb->TablePrefix.$table.'` '
|
741 |
1680
|
darkviper
|
. 'WHERE `name` = \''.$index.'\' ';
|
742 |
1998
|
darkviper
|
if ($oDb->getOne($sql)) {
|
743 |
1364
|
Luisehahne
|
$sql = 'UPDATE ';
|
744 |
|
|
$sql_where = 'WHERE `name` = \''.$index.'\'';
|
745 |
1998
|
darkviper
|
} else {
|
746 |
1364
|
Luisehahne
|
$sql = 'INSERT INTO ';
|
747 |
|
|
$sql_where = '';
|
748 |
|
|
}
|
749 |
1998
|
darkviper
|
$sql .= '`'.$oDb->TablePrefix.$table.'` ';
|
750 |
1364
|
Luisehahne
|
$sql .= 'SET `name` = \''.$index.'\', ';
|
751 |
|
|
$sql .= '`value` = \''.$val.'\' '.$sql_where;
|
752 |
1998
|
darkviper
|
if (!$oDb->doQuery($sql)) {
|
753 |
1364
|
Luisehahne
|
$retval = false;
|
754 |
|
|
}
|
755 |
|
|
}
|
756 |
|
|
return $retval;
|
757 |
|
|
}
|