<?php
/*=======================================================================
 *
 * lib_db.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


// namespace MC {

class MC_DB
{
  public $dbHost;
  public $dbUser;
  public $dbPassword;
  public $dbName;
  public $dbPort;
  public $dbConnection;
  public $dbQueryStmt;
  public $dbResult;

  public function __construct ($parDB) 
  {
  	$this->dbSetEnvironment($parDB);
  	$this->dbConnection($parDB);
  }

  protected function dbSetEnvironment ($parDB) 
  {
  	$this->dbHost =     $parDB["dbHost"];
  	$this->dbUser =     $parDB["dbUser"];
  	$this->dbPassword = $parDB["dbPassword"];
  	$this->dbName =     $parDB["dbName"];
  	$this->dbPort =     $parDB["dbPort"];
  }

  protected function dbConnectionNew (&$parDB) {
  	if ($parDB["dbHost"] != '' && $parDB["dbUser"] != '' && $parDB["dbPassword"] != '' && $parDB["dbName"] != '' && $parDB["dbPort"] != '') :
  	   $this->dbSetEnvironment($parDB);
  	endif;
    $this->dbConnection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName, $this->dbPort);
  }

  function dbConnectErrNo () {return $this->dbConnection->connect_errno;}

  function dbConnectErrMsg () {return $this->dbConnection->connect_error;}

  function dbTransactionBegin () {return $this->dbConnection->begin_transaction();}

  function dbTransactionCommit () {return $this->dbConnection->commit();}

  function dbHostInfo () {return $this->dbConnection->host_info;}

  function dbQuery ($dbQueryStmt) {
  	$this->dbQueryStmt = $dbQueryStmt;
    $this->dbResult = $this->dbConnection->query($dbQueryStmt);
    return $this->dbResult;
  }

  function dbAffectedRows () {return $this->dbConnection->affected_rows;}

  function dbFetchRow () {return mysqli_fetch_row($this->dbResult);}

  function dbFreeResult () {
    mysqli_free_result($this->dbResult);
    $this->dbResult = "";
    $this->dbQueryStmt = "";
  }

  function dbClose () {
  	if (is_object($this->dbConnection)) :
  	   mysqli_close($this->dbConnection);
  	endif;
  	$this->dbConnection = "";
  }

  // Checks for a special database connection (object).
  // If it does not exist then try to get a new connection by given database credentials.
  // If this does not succeed then get the global defined connection (object)
  function dbConnection (&$parDB) {
    global $db, $PHP_SELF;
    if (!is_object($this->dbConnection) || $this->dbConnection == "") :
       $this->dbConnectionNew($parDB);
       if ($this->dbConnectErrNo()) :
          $this->dbConnection = "";
       endif;
    endif;
    if (!is_object($this->dbConnection) || $this->dbConnection == "") :
       if (is_object($db)) :
        $this->dbConnection = $db;
       endif;
    endif;
    if ($this->dbConnectErrNo()) :    
    	die("$PHP_SELF: " .  $this->dbConnectErrMsg());
    endif;
  }

  // Returns last insert ID
  function dbGetLastInsertID () {
    return $this->dbConnection->insert_id;
  }

  // Returns the tablenames of a specified database
  function dbGetTableNames() {
    $tableNames = array();
    $this->dbQueryStmt = "SHOW TABLES";
    if ($this->dbQuery($this->dbQueryStmt)) :
        while ($row = $this->dbFetchRow()) {
          $tableNames[] = $row[0];
        }
        $this->dbFreeResult();
    endif;
    return $tableNames;
  }

  // Returns the fieldnames of a specified table
  function dbGetTableFieldNames($dbTable) {
  	global $PHP_SELF;
    $fieldNames = array();
    if ($dbTable != "") :
       $this->dbQueryStmt = "SHOW COLUMNS FROM " . $dbTable;
       if ($this->dbQuery($this->dbQueryStmt)) :
       	   $count = 0;
           while ($row = $this->dbFetchRow()) {
             $fieldNames[$count]["name"]    = $row[0];
             $fieldNames[$count]["type"]    = $row[1];
             $fieldNames[$count]["null"]    = $row[2];
             $fieldNames[$count]["key"]     = $row[3];
             $fieldNames[$count]["default"] = $row[4];
             $fieldNames[$count]["extra"]   = $row[5];
             $count++;
           }
           $this->dbFreeResult();
       endif;
    endif;
    return $fieldNames;
  }
}  // class DB

//} // namespace
?>