<?php

/*
Konvertierung des klassischen DB-Modells zum generischen DB-Modell
*/


include_once ("../lib/mc_lib.inc.php");


// ******************
// **** Class DB ****
// ******************

class DB extends mysqli {

  private $dbCreds = array();

  public function __construct($dbCreds) {
      mysqli_report(MYSQLI_REPORT_OFF);      
      parent::__construct($dbCreds["dbHost"], $dbCreds["dbUser"], $dbCreds["dbPassword"], $dbCreds["dbName"], $dbCreds["dbPort"]);
      $this->dbCreds = $dbCreds;
  }

  public function getDbCreds () {return $this->dbCreds;}
  
  public function dbConnectErrNo () {return $this->connect_errno;}

  public function dbConnectErrMsg () {return $this->connect_error;}
  
  public function dbError () {return $this->db->error;}

  public function dbHostInfo () {return $this->db->host_info;}
  
  public function dbSetReportMode ($dbReportMode) {mysqli_report($dbReportMode);}

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

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

  public function dbTransactionCommit () {return $this->commit();}
  
  public function dbTransactionRollback () {return $this->rollback();}

  public function dbQuery ($dbQuery) {return $this->query($dbQuery);}

  public function dbNumRows (&$dbResult) {return $dbResult->num_rows;}

  public function dbFetchRow (&$dbResult) {return $dbResult->fetch_row();}
  
  public function dbFetchAssoc (&$dbResult) {return $dbResult->fetch_assoc();}

  public function dbFreeResult (&$dbResult) {return $dbResult->free();}

  public function dbClose () {
    $this->db->close()    ;
    $this->db = null;
    $this->dbCreds = array();
  }
  
  public function dbQ ($dbQuery) {
    if ($dbResult = $this->query($dbQuery)) :
       return $dbResult;
    else :
       die($dbQuery . "<br>" . $this->dbError());
    endif;
  }

  public function getOne ($dbQuery) {
    $dbResult = $this->query($dbQuery);
    if ($row = $dbResult->fetch_row()) :
       return $row[0];
    endif;
    return "";
  }  

  
  // * Wrapper functions for compatibility *
  
  public static function isError(&$dbResult) {
    if ($dbResult === false) {
       return true;   // Error occurred
    } else {
       return false;  // No error
    }
  }
}



// Returns the tablenames of a specified database
function dbGetTableNames($db) {
  global $PHP_SELF;
  $tableNames = array();
  // $db->dbSetReportMode(MYSQLI_REPORT_ERROR);
  $dbSqlQuery = "SHOW TABLES";
  // $dbResult = $db->dbQuery($dbSqlQuery);
  $dbResult = $db->dbQ($dbSqlQuery);
  // if ($dbResult) : 
     while ($row = $db->dbFetchRow($dbResult)) {
         $tableNames[] = $row[0];
     }
     $db->dbFreeResult($dbResult);
  // else :
  //    echo("Abfrage fehlgeschlagen: " . $db->error);
  // endif;
  $db->dbClose($db);
  return $tableNames;
}

// Returns the fieldnames of a specified table
function dbGetTableFieldNames($db, $dbTable) {
  global $PHP_SELF;
  $fieldNames = array();
  if ($dbTable != "") :
     $dbSqlQuery = "SHOW COLUMNS FROM " . $dbTable;
     $dbResult = $db->dbQuery($dbSqlQuery);
     if ($dbResult) :
     	 $count = 0;
         while ($row = $db->dbFetchRow($dbResult)) {
           $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++;
         }
         $db->dbFreeResult($dbResult);
     endif;
  endif;
  return $fieldNames;
}


?>
