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