Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sterownik Bazy danych
Black-Berry
post
Post #1





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

Ostrzeżenie: (0%)
-----


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:

  1. <?php
  2. interface Db_Driver_Interface
  3. {
  4. public function __construct();
  5. public function __destruct();
  6. public function connect();
  7. public function disconnect();
  8. public function query($aQuery);
  9. public function fetchRow($aQueryResult = '');
  10. public function getResult();
  11. public function numRows($aQueryResult = '');
  12. public function affectedRows();
  13. public function insertId();
  14. public function freeResult($aQueryResult = '');
  15. public function getTotalQueriesCount();
  16. public function getTotalExecutionTime();
  17. }
  18. ?>


  1. <?php
  2. abstract class Db_Driver_Abstract
  3. {
  4. protected $dbHost;
  5. protected $dbUser;
  6. protected $dbPassword;
  7. protected $dbName;
  8. protected $totalQueries;
  9. protected $totalTime;
  10. protected $log;
  11.  
  12. public function __construct(Main_Settings $aSettings, Main_Log $aLog)
  13. {
  14. $this->dbHost = $aSettings->dbHost;
  15. $this->dbUser = $aSettings->dbUser;
  16. $this->dbName = $aSettings->dbName;
  17. $this->dbPassword = $aSettings->dbPassword;
  18. $this->totalQueries = 0;
  19. $this->totalTime = 0;
  20. $this->log = $aLog;
  21. }
  22. public function __destruct(){}
  23. public function numRows($aQueryResult = ''){}
  24. public function affectedRows(){}
  25. public function freeResult($aQueryResult = ''){}
  26. public function getTotalQueriesCount()
  27. {
  28. return $this->totalQueries;
  29. }
  30.  
  31. public function getTotalExecutionTime()
  32. {
  33. $total = round($this->totalTime, 5);
  34. return $total;
  35. }
  36.  
  37. public function logStatistics()
  38. {
  39. $this->log->addEntry(_SYSTEM_LOG_TOTAL_QUERIES_COUNT_, $this->getTotalQueriesCount(), NOTE);
  40. $this->log->addEntry(_SYSTEM_LOG_TOTAL_EXECUTION_TIME_, $this->getTotalExecutionTime() . 's', NOTE);
  41. }
  42. }
  43. ?>


  1. <?php
  2. class Db_Driver_Mysql extends Db_Driver_Abstract implements Db_Driver_Interface
  3. {
  4. private $connectId;
  5. private $queryResult;
  6.  
  7.  
  8. public function connect()
  9. {
  10. $this->connectId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword);
  11. if ($this->connectId) {
  12. $this->log->addEntry(_SYSTEM_LOG_DATABASE_HOST_CONNECTED_SUCCESSFULLY_, $this->dbHost, NOTE);
  13. $dbSelect = @mysql_select_db($this->dbName, $this->connectId);
  14. if (!$dbSelect) {
  15. throw new Exception(_ENGINE_CANT_SELECT_DATABASE_);
  16. mysql_close($this->connectId); 
  17. $this->log->addEntry(_SYSTEM_LOG_CAMT_SELECT_DATABASE_, $this->dbName, ERROR);
  18. return false;
  19. } else {
  20. $this->log->addEntry(_SYSTEM_LOG_DATABASE_SELECTED_SUCCESSFULLY_, $this->dbName, NOTE);
  21. return true;
  22. }
  23. } else {
  24. throw new Exception(_ENGINE_CANT_CONNECT_TO_DATABASE_);
  25. return false;
  26. }
  27. }
  28.  
  29. public function disconnect()
  30. {
  31. $dbClose = @mysql_close($this->connectId);
  32. if (!$dbClose) {
  33. throw new Exception(_ENGINE_CANT_CLOSE_DATABASE_);
  34. return false;
  35. } else {
  36. return true;
  37. }
  38. }
  39.  
  40. public function query($aQuery)
  41. {
  42. $start = get_micro_time();
  43. $this->queryResult = '';
  44. // $this->queryResult = @mysql_db_query($this->dbName, $aQuery, $this->connectId);
  45. // method below is mutch faster then above...
  46. $this->queryResult = @mysql_query( $aQuery);
  47. if(!$this->queryResult){
  48. $this->log->addEntry(_SYSTEM_LOG_CANT_EXECUTE_QUERY_, $aQuery .'('.mysql_error().')' , WARNING);
  49. return false;
  50. } else {
  51. $this->log->addEntry(_SYSTEM_LOG_QUERY_COMPLITED_SUCCESSFULLY_, $aQuery, NOTE);
  52. $this->totalQueries++;
  53. $this->totalTime += get_micro_time() - $start;
  54. return true;
  55. }
  56. }
  57.  
  58. public function fetchRow($aQueryResult='')
  59. {
  60. $queryResult = (empty($aQueryResult)) ? $this->queryResult : $aQueryResult;
  61. $array = mysql_fetch_array($queryResult, MYSQL_ASSOC);
  62. if(!$array){
  63. return false;
  64. }else{
  65. return $array;
  66. }
  67. }
  68.  
  69. function insertId()
  70. {
  71. return mysql_insert_id($this ->connectId);
  72. }
  73.  
  74. public function numRows($aQueryResult = '')
  75. {
  76. $queryResult = (empty($aQueryResult)) ? $this->queryResult : $aQueryResult;
  77. $numrows = mysql_num_rows($queryResult);
  78. if(!$numrows)
  79. {
  80. return false;
  81. }else{
  82. return $numrows;
  83. }
  84. }
  85.  
  86. public function getResult()
  87. {
  88. return $this->queryResult;
  89. }
  90. ?>


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 ?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 07:52