Klasa DB Manager

Witam.
Mam zaszczyt zaprezentować klasę własnej produkcji. Jest to klasa do komunikacji z bazą danych oparta na PDO. Klasa jest już w wersji 0.2 gdyż na forum.php.pl poddano mi kilka pomysłów, jak i wytknięto kilka wad.

W porównaniu do wersji 0.1 wprowadziłem następujące zmiany:
- przerobiona metoda queryandfetch (można wybrać co ma zostać zwrócone)
- nowa metoda numRows
- nowa zmienna i metoda numExecutes zwracająca ilość zapytań wysłanych do bazy

ToDo:
- napisać obsługę Exceptions
- cache zapytań

Zapraszam do przeczytania dalszej części.

Do użycia skryptu potrzebny jest plik configuracyjny. Oto jego budowa:

PHP:
  1. <?php
  2. define('DB_HOST','localhost');
  3. define('DB_USER','arj');
  4. define('DB_PASS','');
  5. define('DB_NAME','test');
  6. define('DB_KIND','mysql');
  7. define('DB_CHARSET','utf8');
  8. define('DB_COLCON','utf8_general_ci');
  9. ?>

Oto sama klasa dbmanager.class.php

PHP:
  1. <?php
  2. /**
  3. *  Database Manager
  4. *
  5. *  @package Darion
  6. *  @author Arkadiusz 'ARJ' Jasak
  7. *  @copyright Copyright (c) 2006 Arkadiusz 'ARJ' Jasak (ajasak[at]gmail.com)
  8. *  @version 0.2
  9. */
  10. class DBmanager{
  11. /**
  12. * Object instance
  13. *
  14. * @var object
  15. * @access private
  16. */
  17. static private $thisInstance = null;
  18.  
  19. /**
  20. * Query results
  21. *
  22. * @var array
  23. * @access private
  24. */
  25. private $result = array();
  26.  
  27. /**
  28. * PDO instance
  29. *
  30. * @var object
  31. * @access private
  32. */
  33. private $dbh;
  34.  
  35. /**
  36. * Number of executes
  37. *
  38. * @access private
  39. * @var int
  40. */
  41. private $numExecutes = 0;
  42.  
  43. /**
  44. * Constructor
  45. * Connect to database and set character encoding
  46. * @access public
  47. */
  48. public function __construct(){
  49. try{
  50. $this->dbh = new PDO(DB_KIND.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
  51. $this->dbh->exec('SET CHARACTER SET '.DB_CHARSET);
  52. $this->dbh->exec('SET collation_connection = '.DB_COLCON.';');
  53. }catch (PDOException $e){
  54. echo 'Error!: ' . $e->getMessage() . '
  55. ';
  56. }
  57. }
  58.  
  59. /**
  60. * Query and fetch
  61. *
  62. * @param string $_query
  63. * @param string $_mode
  64. * @param int $_param
  65. * @access public
  66. * @return array
  67. */
  68. public function queryandfetch($_query, $_mode, $_param = NULL){
  69. $this->numExecutes++;
  70. $this->result = null;
  71. $this->result = $this->dbh->query($_query);
  72.  
  73. switch ($_mode){
  74. case 'assoc':
  75. return $this->result->fetchAll(PDO::FETCH_ASSOC);
  76. break;
  77. case 'both':
  78. return $this->result->fetchAll(PDO::FETCH_BOTH);
  79. break;
  80. case 'column':
  81. return $this->result->fetchAll(PDO::FETCH_COLUMN, $_param);
  82. break;
  83. default:
  84. return $this->result->fetchAll(PDO::FETCH_ASSOC);
  85. }
  86. }
  87.  
  88. /**
  89. * Exec: insert, update, delete
  90. *
  91. * @param string $_exec
  92. * @access public
  93. * @return int or false
  94. */
  95. public function exec($_exec){
  96. $this->numExecutes++;
  97. $this->result = $this->dbh->exec($_exec);
  98. if($this->result == false){
  99. return false;
  100. }else{
  101. return $this->result;
  102. }
  103. }
  104.  
  105. /**
  106. * Count rows number
  107. *
  108. * @param string $_table
  109. * @param string $_where
  110. * @return int
  111. */
  112. public function numRows($_table, $_where){
  113. $this->numExecutes++;
  114. $this->result = $this->dbh->query("SELECT COUNT(*) FROM ".$_table." WHERE ".$_where);
  115. return $this->result->fetchColumn();
  116. }
  117.  
  118. /**
  119. * Return number of executes
  120. *
  121. * @access public
  122. * @return int
  123. */
  124. public function numExecutes(){
  125. return $this->numExecutes;
  126. }
  127.  
  128. /**
  129. * Singleton
  130. *
  131. * @access public
  132. * @return object
  133. */
  134. static public function getInstance() {
  135. if(self::$thisInstance == null)
  136. {
  137. self::$thisInstance = new DBmanager();
  138. }
  139. return self::$thisInstance;
  140. }
  141.  
  142. /**
  143. * Destructor
  144. * Close connection with database
  145. * @access public
  146. */
  147. public function __destruct(){
  148. $this->dbh = null;
  149. }
  150. }
  151. ?>

Jeżeli będzie trzeba, zaprezentuję przykłady użycia.
Klasę można ściągnąć tutaj.

Dodaj komentarz

XHTML: Tagi które możesz użyć: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>