
<?php interface Db_Driver_Interface { public function __construct(); public function __destruct(); public function connect(); public function disconnect(); public function query($aQuery); public function fetchRow($aQueryResult = ''); public function getResult(); public function numRows($aQueryResult = ''); public function affectedRows(); public function insertId(); public function freeResult($aQueryResult = ''); public function getTotalQueriesCount(); public function getTotalExecutionTime(); } ?>
<?php abstract class Db_Driver_Abstract { protected $dbHost; protected $dbUser; protected $dbPassword; protected $dbName; protected $totalQueries; protected $totalTime; protected $log; public function __construct(Main_Settings $aSettings, Main_Log $aLog) { $this->dbHost = $aSettings->dbHost; $this->dbUser = $aSettings->dbUser; $this->dbName = $aSettings->dbName; $this->dbPassword = $aSettings->dbPassword; $this->totalQueries = 0; $this->totalTime = 0; $this->log = $aLog; } public function __destruct(){} public function numRows($aQueryResult = ''){} public function affectedRows(){} public function freeResult($aQueryResult = ''){} public function getTotalQueriesCount() { return $this->totalQueries; } public function getTotalExecutionTime() { return $total; } public function logStatistics() { $this->log->addEntry(_SYSTEM_LOG_TOTAL_QUERIES_COUNT_, $this->getTotalQueriesCount(), NOTE); $this->log->addEntry(_SYSTEM_LOG_TOTAL_EXECUTION_TIME_, $this->getTotalExecutionTime() . 's', NOTE); } } ?>
<?php class Db_Driver_Mysql extends Db_Driver_Abstract implements Db_Driver_Interface { private $connectId; private $queryResult; public function connect() { if ($this->connectId) { $this->log->addEntry(_SYSTEM_LOG_DATABASE_HOST_CONNECTED_SUCCESSFULLY_, $this->dbHost, NOTE); if (!$dbSelect) { throw new Exception(_ENGINE_CANT_SELECT_DATABASE_); $this->log->addEntry(_SYSTEM_LOG_CAMT_SELECT_DATABASE_, $this->dbName, ERROR); return false; } else { $this->log->addEntry(_SYSTEM_LOG_DATABASE_SELECTED_SUCCESSFULLY_, $this->dbName, NOTE); return true; } } else { throw new Exception(_ENGINE_CANT_CONNECT_TO_DATABASE_); return false; } } public function disconnect() { if (!$dbClose) { throw new Exception(_ENGINE_CANT_CLOSE_DATABASE_); return false; } else { return true; } } public function query($aQuery) { $start = get_micro_time(); $this->queryResult = ''; // $this->queryResult = @mysql_db_query($this->dbName, $aQuery, $this->connectId); // method below is mutch faster then above... if(!$this->queryResult){ return false; } else { $this->log->addEntry(_SYSTEM_LOG_QUERY_COMPLITED_SUCCESSFULLY_, $aQuery, NOTE); $this->totalQueries++; $this->totalTime += get_micro_time() - $start; return true; } } public function fetchRow($aQueryResult='') { if(!$array){ return false; }else{ return $array; } } function insertId() { } public function numRows($aQueryResult = '') { if(!$numrows) { return false; }else{ return $numrows; } } public function getResult() { return $this->queryResult; } ?>
Logka teg jest taka, że mam interface i abstrakcje klasy po której dziedzicza inne sterowniki. Przykład sterownka dla mysql. Zastanawiam się czy jeśli napisze później sterownik na inną bazę to czy wszystkie zapytania zostaną wykonane ?
Jeszcze jedno pytanie dotyczące funkcji mysql_db_query. Dlaczego jest ona w moim przykładzie o wiele wolniejsza niż mysql_query? (komentarz w klasie).
No i najważniejsze czy waszym zdaniem wszystko jest zrobione tak jak należy ?