Kilka tygodni temu pisałem o obiektowości i moim rozumieniu singletonów. Moje rozumowanie zostało naprostowane za co teraz dziękuję (IMG:
http://forum.php.pl/style_emoticons/default/smile.gif) Prosze o ocenę tego co pisze teraz:
<?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()
{
$total = round($this->totalTime, 5
); 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()
{
$this->connectId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword); 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){
$this->log->addEntry(_SYSTEM_LOG_CANT_EXECUTE_QUERY_
, $aQuery .'('.mysql_error().')' , WARNING
); 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='')
{
$queryResult = (empty($aQueryResult)) ?
$this->queryResult : $aQueryResult; if(!$array){
return false;
}else{
return $array;
}
}
function insertId()
{
}
public function numRows($aQueryResult = '')
{
$queryResult = (empty($aQueryResult)) ?
$this->queryResult : $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 ?