Project

General

Profile

« Previous | Next » 

Revision 2120

Added by darkviper over 9 years ago

  1. framework/WbAdaptor some litte fixes
    + framework/WbDatabaseHelper contains now all maintenance methods from WbDatabase
    ! framework/WbDatabase all maintenance methods has been moved to framework/WbDatabaseHelper

View differences:

WbDatabase.php
47 47
		define('MYSQL_CLIENT_SSL',         2048);
48 48
	}
49 49

  
50
class WbDatabase {
50
class WbDatabase extends WbDatabaseHelper {
51 51

  
52 52
	private static $_oInstances = array();
53 53

  
......
113 113
		if ($url != '') {
114 114
		// parse URL and extract connection data
115 115
			$aIni = parse_url($url);
116
			$scheme   = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql';
116
			$scheme   = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysqli';
117 117
			$hostname = isset($aIni['host']) ? $aIni['host'] : '';
118 118
			$username = isset($aIni['user']) ? $aIni['user'] : '';
119 119
			$password = isset($aIni['pass']) ? $aIni['pass'] : '';
......
144 144
			throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.':'.$hostport.'\'');
145 145
		} else {
146 146
            if ($this->sCharset) {
147
                @mysqli_query($this->oDbHandle, 'SET NAMES \''.$this->sCharset.'\'');
147
                @mysqli_query($this->oDbHandle, 'SET NAMES '.$this->sCharset);
148
                mysqli_set_charset($this->oDbHandle, $this->sCharset);
148 149
            }
149 150
            $this->connected = true;
150 151
		}
......
328 329
	{
329 330
		return mysqli_insert_id($this->oDbHandle);
330 331
	}
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 332

  
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 333
} /// end of class database
553 334
// //////////////////////////////////////////////////////////////////////////////////// //
554 335
/**
......
600 381
 * @return object
601 382
 * @throws WbDatabaseException
602 383
 */
603
	function query($sStatement)
384
	public function query($sStatement)
604 385
	{
605 386
		$this->result = @mysqli_query($this->oDbHandle, $sStatement);
606 387
		if ($this->result === false) {
......
618 399
 * @return integer
619 400
 * @description number of returned records
620 401
 */
621
	function numRows()
402
	public function numRows()
622 403
	{
623 404
		return mysqli_num_rows($this->result);
624 405
	}
......
628 409
 * @return array
629 410
 * @description get current record and increment pointer
630 411
 */
631
	function fetchRow($typ = MYSQL_BOTH)
412
	public function fetchRow($typ = MYSQLI_BOTH)
632 413
	{
633 414
		return mysqli_fetch_array($this->result, $typ);
634 415
	}
......
639 420
 * @return object
640 421
 * @description get current record as an object and increment pointer
641 422
 */
642
	function fetchObject($sClassName = null, array $aParams = null)
423
	public function fetchObject($sClassName = null, array $aParams = null)
643 424
	{
644 425
		if ($sClassName === null || class_exists($sClassName)) {
645 426
			return mysqli_fetch_object($this->result, $sClassName, $aParams);
......
648 429
		}
649 430
	}
650 431
/**
432
 * fetchArray
433
 * @param  int $iType MYSQL_ASSOC(default) | MYSQL_BOTH | MYSQL_NUM
434
 * @return array of current record
435
 * @description get current record and increment pointer
436
 */
437
	public function fetchArray($iType = MYSQLI_ASSOC)
438
	{
439
        if ($iType < MYSQLI_ASSOC || $iType > MYSQLI_BOTH) {
440
            $iType = MYSQLI_ASSOC;
441
        }
442
		return mysqli_fetch_array($this->result, $iType);
443
	}
444
/**
445
 * fetchAll
446
 * @param  int $iType MYSQL_ASSOC(default) | MYSQL_NUM
447
 * @return array of rows
448
 * @description get all records of the result set
449
 */
450
    public function fetchAll($iType = MYSQL_ASSOC)
451
    {
452
        $iType = $iType != MYSQL_NUM ? MYSQL_ASSOC : MYSQL_NUM;
453
        return mysqli_fetch_all($this->result, $iType);
454
    }
455
/**
651 456
 * rewind
652 457
 * @return bool
653 458
 * @description set the recordpointer to the first record || false on error
654 459
 */
655
	function rewind()
460
	public function rewind()
656 461
	{
657 462
		return $this->seekRow(MYSQL_SEEK_FIRST);
658 463
	}
......
662 467
 * @return bool
663 468
 * @description set the pointer to the given record || false on error
664 469
 */
665
	function seekRow( $position = MYSQL_SEEK_FIRST )
470
	public function seekRow( $position = MYSQL_SEEK_FIRST )
666 471
	{
667 472
		$pmax = $this->numRows() - 1;
668 473
		$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
......
673 478
 * @return bool
674 479
 * @description remove retult object from memeory
675 480
 */
676
	function freeResult()
481
	public function freeResult()
677 482
	{
678 483
		return mysqli_free_result($this->result);
679 484
	}
......
681 486
 * Get error
682 487
 * @return string || null if no error
683 488
 */
684
	function error()
489
	public function error()
685 490
	{
686 491
		if (isset($this->error)) {
687 492
			return $this->error;
......
704 509
	function db_update_key_value($table, $key, $value = '')
705 510
	{
706 511
		$oDb = WbDatabase::getInstance();
512
        $table = preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $table);
707 513
		if (!is_array($key)) {
708 514
			if (trim($key) != '') {
709 515
				$key = array( trim($key) => trim($value) );

Also available in: Unified diff