Project

General

Profile

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: 1998 $
29
 * @lastmodified $Date: 2013-11-13 03:02:17 +0100 (Wed, 13 Nov 2013) $
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

    
37
class WbDatabase {
38

    
39
	private static $_oInstances = array();
40

    
41
	protected $oDbHandle    = null; // readonly from outside
42
	protected $sDbName      = '';
43
	protected $sInstanceIdentifier = '';
44
	protected $sTablePrefix = '';
45
	protected $sCharset     = '';
46
	protected $connected    = false;
47
	protected $error        = '';
48
	protected $error_type   = '';
49
	protected $iQueryCount  = 0;
50

    
51
/**
52
 * __constructor
53
 *  prevent from public instancing
54
 */
55
	protected function  __construct() {}
56
/**
57
 * prevent from cloning
58
 */
59
	private function __clone() {}
60
/**
61
 * get a valid instance of this class
62
 * @param string $sIdentifier selector for several different instances
63
 * @return WbDatabase object
64
 */
65
	public static function getInstance($sIdentifier = 'core')
66
	{
67
		if( !isset(self::$_oInstances[$sIdentifier])) {
68
            $c = __CLASS__;
69
			$oInstance = new $c;
70
			$oInstance->sInstanceIdentifier = $sIdentifier;
71
            self::$_oInstances[$sIdentifier] = $oInstance;
72
		}
73
		return self::$_oInstances[$sIdentifier];
74
	}
75
/**
76
 * disconnect and kills an existing instance
77
 * @param string $sIdentifier selector for instance to kill
78
 */
79
	public static function killInstance($sIdentifier)
80
	{
81
		if($sIdentifier != 'core') {
82
			if( isset(self::$_oInstances[$sIdentifier])) {
83
				self::$_oInstances[$sIdentifier]->disconnect();
84
				unset(self::$_oInstances[$sIdentifier]);
85
			}
86
		}
87
	}
88
/**
89
 * Establish connection
90
 * @param string $url
91
 * @return bool
92
 * @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
 */
96
	public function doConnect($url = '')
97
	{
98
		if ($this->connected) { return $this->connected; } // prevent from reconnecting
99
		$this->connected = false;
100
		if ($url != '') {
101
		// parse URL and extract connection data
102
			$aIni = parse_url($url);
103
			$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
			$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
111
			$aQuery = explode('&', $sTmp);
112
			foreach ($aQuery as $sArgument) {
113
				$aArg = explode('=', $sArgument);
114
				switch (strtolower($aArg[0])) {
115
					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
			$this->sDbName = $db_name;
126
		} else {
127
			throw new WbDatabaseException('Missing parameter: unable to connect database');
128
		}
129
		$this->oDbHandle = @mysql_connect($hostname.$hostport, $username, $password, true);
130
		if (!$this->oDbHandle) {
131
			throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.$hostport.'\'');
132
		} else {
133
			if (!@mysql_select_db($db_name, $this->oDbHandle)) {
134
				throw new WbDatabaseException('unable to select database \''.$db_name.
135
				                              '\' on \''.$scheme.'://'.
136
				                              $hostname.$hostport.'\''
137
				                             );
138
			} else {
139
				if ($this->sCharset) {
140
					@mysql_query('SET NAMES \''.$this->sCharset.'\'', $this->oDbHandle);
141
				}
142
				$this->connected = true;
143
			}
144
		}
145
		return $this->connected;
146
	}
147
/**
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
			$this->connected = false;
158
			return true;
159
		}
160
		return false;
161
	}
162
/**
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
		$this->iQueryCount++;
176
		$mysql = new mysql();
177
		$mysql->query($statement, $this->oDbHandle);
178
		$this->set_error($mysql->error($this->oDbHandle));
179
		if ($mysql->error($this->oDbHandle)) {
180
			return null;
181
		} else {
182
			return $mysql;
183
		}
184
	}
185
/**
186
 * Alias for getOne()
187
 */
188
	public function get_one( $statement )
189
	{
190
		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
		$this->iQueryCount++;
201
		$fetch_row = mysql_fetch_array(mysql_query($statement, $this->oDbHandle));
202
		$result = $fetch_row[0];
203
		$this->set_error(mysql_error($this->oDbHandle));
204
		if (mysql_error($this->oDbHandle)) {
205
			return null;
206
		} else {
207
			return $result;
208
		}
209
	}
210
/**
211
 * Alias for setError()
212
 */
213
	public function set_error($message = null)
214
	{
215
		$this->setError($message = null);
216
	}
217
	// Set the DB error
218
/**
219
 * setError
220
 * @param string $message
221
 */
222
	public function setError($message = null)
223
	{
224
		$this->error = $message;
225
	}
226
/**
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
		return (!empty($this->error)) ? true : false;
240
	}
241
/**
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
		return $this->error;
255
	}
256
/**
257
 * Protect class from property injections
258
 * @param string name of property
259
 * @param mixed value
260
 * @throws WbDatabaseException
261
 */	
262
	public function __set($name, $value)
263
	{
264
		throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
265
	}
266
/**
267
 * default Getter for some properties
268
 * @param string name of the Property
269
 * @return NULL on error | valid property
270
 */
271
	public function __get($sPropertyName)
272
	{
273
		switch ($sPropertyName) {
274
			case 'DbHandle':
275
			case 'getDbHandle': // << set deprecated
276
			case 'db_handle': // << set deprecated
277
				$retval = $this->oDbHandle;
278
				break;
279
			case 'LastInsertId':
280
			case 'getLastInsertId': // << set deprecated
281
				$retval = mysql_insert_id($this->oDbHandle);
282
				break;
283
			case 'DbName':
284
			case 'getDbName': // << set deprecated
285
			case 'db_name': // << set deprecated
286
				$retval = $this->sDbName;
287
				break;
288
			case 'TablePrefix':
289
			case 'getTablePrefix': // << set deprecated
290
				$retval = $this->sTablePrefix;			
291
				break;
292
			case 'QueryCount':
293
			case 'getQueryCount': // << set deprecated
294
				$retval = $this->iQueryCount;
295
				break;
296
			default:
297
				$retval = null;
298
				break;
299
		}
300
		return $retval;
301
	} // __get()
302
/**
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
		return mysql_real_escape_string($unescaped_string, $this->oDbHandle);
310
	}
311
/**
312
 * Last inserted Id
313
 * @return bool|int false on error, 0 if no record inserted
314
 */	
315
	public function getLastInsertId()
316
	{
317
		return mysql_insert_id($this->oDbHandle);
318
	}
319
/**
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
/*
327
 * @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
 */
331
	public function isField($table_name, $field_name)
332
	{
333
		$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
334
		$query = $this->query($sql, $this->oDbHandle);
335
		return ($query->numRows() != 0);
336
	}
337
/**
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
/*
345
 * isIndex
346
 * @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
 */
350
	public function isIndex($table_name, $index_name, $number_fields = 0)
351
	{
352
		$number_fields = intval($number_fields);
353
		$keys = 0;
354
		$sql = 'SHOW INDEX FROM `'.$table_name.'`';
355
		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
					$keys++;
359
				}
360
			}
361

    
362
		}
363
		if ( $number_fields == 0 ) {
364
			return ($keys != $number_fields);
365
		} else {
366
			return ($keys == $number_fields);
367
		}
368
	}
369
/**
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
/*
377
 * @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
 */
382
	public function addField($table_name, $field_name, $description)
383
	{
384
		if (!$this->isField($table_name, $field_name)) {
385
		// add new field into a table
386
			$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
387
			$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
			}
392
		} else {
393
			$this->set_error('field \''.$field_name.'\' already exists');
394
		}
395
		return false;
396
	}
397
/**
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
/*
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
	public function modifyField($table_name, $field_name, $description)
411
	{
412
		$retval = false;
413
		if ($this->isField($table_name, $field_name)) {
414
		// modify a existing field in a table
415
			$sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
416
			$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false);
417
			$this->setError(mysql_error());
418
		}
419
		return $retval;
420
	}
421
/**
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
/*
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
	public function removeField($table_name, $field_name)
434
	{
435
		$retval = false;
436
		if ($this->isField($table_name, $field_name)) {
437
		// modify a existing field in a table
438
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
439
			$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
440
		}
441
		return $retval;
442
	}
443
/**
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
/*
451
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
452
 * @param string $index_name: name of the new index (empty string for PRIMARY)
453
 * @param string $field_list: comma seperated list of fields for this index
454
 * @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
455
 * @return bool: true if successful, otherwise false and error will be set
456
 */
457
     public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY')
458
     {
459
        $retval = false;
460
        $field_list = explode(',', (str_replace(' ', '', $field_list)));
461
        $number_fields = sizeof($field_list);
462
        $field_list = '`'.implode('`,`', $field_list).'`';
463
        $index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
464
        if ( $this->isIndex($table_name, $index_name, $number_fields) ||
465
             $this->isIndex($table_name, $index_name))
466
        {
467
            $sql  = 'ALTER TABLE `'.$table_name.'` ';
468
            $sql .= 'DROP INDEX `'.$index_name.'`';
469
            if (!$this->doQuery($sql, $this->oDbHandle)) { return false; }
470
        }
471
        $sql  = 'ALTER TABLE `'.$table_name.'` ';
472
        $sql .= 'ADD '.$index_type.' ';
473
        $sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
474
        $sql .= '( '.$field_list.' ); ';
475
        if ($this->doQuery($sql, $this->oDbHandle)) { $retval = true; }
476
        return $retval;
477
    }
478
/**
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
/*
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
	public function removeIndex($table_name, $index_name)
491
	{
492
		$retval = false;
493
		if ($this->isIndex($table_name, $index_name)) {
494
		// modify a existing field in a table
495
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
496
			$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
497
		}
498
		return $retval;
499
	}
500
/**
501
 * 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
 * Import a standard *.sql dump file
513
 * @param string $sSqlDump link to the sql-dumpfile
514
 * @param string $sTablePrefix
515
 * @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
 * @return boolean true if import successful
519
 * @description Import a standard *.sql dump file<br />
520
 *              The file can include placeholders TABLE_PREFIX, TABLE_COLLATION and TABLE_ENGINE
521
 */
522
	public function importSql($sSqlDump,
523
	                          $sTablePrefix = '', /* unused argument, for backward compatibility only! */
524
	                          $bPreserve    = true,
525
	                          $sEngine      = 'MyISAM',
526
	                          $sCollation   = 'utf8_unicode_ci')
527
	{
528
		$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
		$retval = true;
533
		$this->error = '';
534
		$aSearch  = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
535
		$aReplace = array($this->sTablePrefix, $sEngine, $sCollation);
536
		$sql = '';
537
		$aSql = file($sSqlDump);
538
//		$aSql[0] = preg_replace('/^\xEF\xBB\xBF/', '', $aSql[0]);
539
		$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
540
		while (sizeof($aSql) > 0) {
541
			$sSqlLine = trim(array_shift($aSql));
542
			if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
543
				$sql = $sql.' '.$sSqlLine;
544
				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
						if (!mysql_query($sql, $this->oDbHandle)) {
548
							$retval = false;
549
							$this->error = mysql_error($this->oDbHandle);
550
							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
	public function getTableEngine($table)
566
	{
567
		$retVal = false;
568
		$mysqlVersion = mysql_get_server_info($this->oDbHandle);
569
		$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
570
		$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
				$retVal = $row[$engineValue];
574
			}
575
		}
576
		return $retVal;
577
	}
578

    
579

    
580
} /// end of class database
581
// //////////////////////////////////////////////////////////////////////////////////// //
582
/**
583
 * WbDatabaseException
584
 *
585
 * @category     Core
586
 * @package      Core_database
587
 * @author       Manuela v.d.Decken <manuela@isteam.de>
588
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
589
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
590
 * @version      2.9.0
591
 * @revision     $Revision: 1998 $
592
 * @lastmodified $Date: 2013-11-13 03:02:17 +0100 (Wed, 13 Nov 2013) $
593
 * @description  Exceptionhandler for the WbDatabase and depending classes
594
 */
595
class WbDatabaseException extends AppException {}
596

    
597
/* 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

    
601
/**
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: 1998 $
611
 * @lastmodified $Date: 2013-11-13 03:02:17 +0100 (Wed, 13 Nov 2013) $
612
 * @description  MYSQL result object for requests
613
 *
614
 */
615
class mysql {
616

    
617
	private $result = null;
618
	private $_db_handle = null;
619

    
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
				throw new WbDatabaseException('Error in SQL-Statement');
636
			}
637
		}
638
		$this->error = mysql_error($this->oDbHandle);
639
		return $this->result;
640
	}
641
/**
642
 * numRows
643
 * @return integer
644
 * @description number of returned records
645
 */
646
	function numRows()
647
	{
648
		return mysql_num_rows($this->result);
649
	}
650
/**
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
		return mysql_fetch_array($this->result, $typ);
659
	}
660
/**
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
	function rewind()
677
	{
678
		return $this->seekRow(MYSQL_SEEK_FIRST);
679
	}
680
/**
681
 * seekRow
682
 * @param int $position
683
 * @return bool
684
 * @description set the pointer to the given record || false on error
685
 */
686
	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
/**
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
			return $this->error;
709
		} else {
710
			return null;
711
		}
712
	}
713

    
714
}
715
// //////////////////////////////////////////////////////////////////////////////////// //
716
/* 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
		$oDb = WbDatabase::getInstance();
728
		if (!is_array($key)) {
729
			if (trim($key) != '') {
730
				$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
			$sql = 'SELECT COUNT(`setting_id`) '
740
			     . 'FROM `'.$oDb->TablePrefix.$table.'` '
741
			     . 'WHERE `name` = \''.$index.'\' ';
742
			if ($oDb->getOne($sql)) {
743
				$sql = 'UPDATE ';
744
				$sql_where = 'WHERE `name` = \''.$index.'\'';
745
			} else {
746
				$sql = 'INSERT INTO ';
747
				$sql_where = '';
748
			}
749
			$sql .= '`'.$oDb->TablePrefix.$table.'` ';
750
			$sql .= 'SET `name` = \''.$index.'\', ';
751
			$sql .= '`value` = \''.$val.'\' '.$sql_where;
752
			if (!$oDb->doQuery($sql)) {
753
				$retval = false;
754
			}
755
		}
756
		return $retval;
757
	}
(18-18/36)