Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Moja klasa do obsługi baz danych, To moje w 100% własne podejście
sweter
post
Post #1





Grupa: Zarejestrowani
Postów: 623
Pomógł: 11
Dołączył: 1.01.2009
Skąd: Wrocław

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


Witam,
wykonałem taką klasę do obsługi DB:
  1. class db{
  2.  
  3. protected $query;
  4. protected $no = 4;
  5. protected $limit;
  6. protected $table;
  7. protected $offset;
  8. protected $columns;
  9. protected $group;
  10. protected $where = "";
  11. protected $order;
  12. protected $columns_query;
  13. private $connection;
  14.  
  15.  
  16. public function __construct(){
  17.  
  18. $this->connection = mysql_connect('127.0.0.1','root','p');
  19. mysql_select_db('test');
  20.  
  21.  
  22. $this->columns_query = array();
  23. $this->query = "SELECT * FROM ".$this->table." ";
  24. foreach($this->columns AS $col){
  25. $this->columns_query[] = $col[0];
  26. }
  27.  
  28.  
  29. }
  30.  
  31. public function __destruct(){
  32. mysql_close($this->connection);
  33. }
  34.  
  35.  
  36.  
  37. public function getColumns($value){
  38. $query = "SELECT ";
  39. $order = "";
  40. $this->columns_query = array();
  41. foreach($value AS $col){
  42. $query .= $col.", ";
  43. $this->columns_query[] = $col;
  44. }
  45. $query = substr($query, 0, -2);
  46. $query .= " FROM ".$this->table." ";
  47. $this->query = $query;
  48. return $this;
  49. }
  50.  
  51. private function columnExist($start, $name){
  52. $find = false;
  53. for($q=0; $q<$this->no; $q++){
  54. if($this->columns[$q][0]==strtolower(substr($name, $start, strlen($this->columns[$q][0])))){
  55. $find = true;
  56. return strtolower(substr($name, $start, strlen($this->columns[$q][0])));
  57. }
  58. }
  59. return $find;
  60. }
  61.  
  62. public function __call($name, $value){
  63. if(substr($name, 0, 8)=="filterBy"){
  64. $col = $this->columnExist(8, $name);
  65. if($col!=1){
  66. $this->where .= $col."='".$value[0]."' AND ";
  67. }else{
  68. echo "<pre>I can't find column named '".strtolower(substr($name, 8, strlen($name)-8))."'</pre>";
  69. }
  70. }else if(substr($name, 0, 7)=="orderBy"){
  71. $col = $this->columnExist(7, $name);
  72. if($col!=1){
  73. $this->order = "ORDER BY ".$col." ".strtoupper($value[0]);
  74. }else{
  75. echo "<pre>I can't find column named '".strtolower(substr($name, 8, strlen($name)-8))."'</pre>";
  76. }
  77. }else if($name=="setLimit"){
  78. $this->limit = $value[0];
  79. }else if($name=="setOffset"){
  80. $this->offset = $value[0];
  81. }else if(substr($name, 0, 7)=="groupBy"){
  82. $col = $this->columnExist(7, $name);
  83. if($col!=1){
  84. $this->group = $col;
  85. }
  86. }
  87. return $this;
  88. }
  89.  
  90. public function where($where){
  91. $this->query .= "(".$where.") AND ";
  92. return $this;
  93. }
  94.  
  95. public function find(){
  96.  
  97.  
  98. if(strlen($this->where)>0){
  99. $this->where = substr($this->where, 0, -5);
  100. $this->query .= "WHERE ".$this->where." ";
  101. }
  102.  
  103.  
  104. if($this->group!=NULL){
  105. $this->query .= "GROUP BY ".$this->group." ";
  106. }
  107.  
  108. $this->query .= $this->order." ";
  109.  
  110. if($this->offset!=NULL || $this->limit!=NULL){
  111. $this->query .= "LIMIT ";
  112. if($this->offset!=NULL){
  113. $this->query .= $this->offset.",";
  114. }else{
  115. $this->query .= "0,";
  116. }
  117. if($this->limit!=NULL){
  118. $this->query .= $this->limit;
  119. }else{
  120. $this->query .= "0";
  121. }
  122. }
  123.  
  124. echo "Wykonuję zapytanie o treści:<br />".$this->query."<br />";
  125.  
  126. $query = mysql_query($this->query);
  127.  
  128. $return = array();
  129. $q = 0;
  130. while($row = mysql_fetch_array($query)){
  131. foreach($this->columns_query AS $col){
  132. $return[$q][$col] = $row[$col];
  133. }
  134. $q++;
  135. }
  136. return $return;
  137.  
  138. }
  139.  
  140.  
  141. }

Każda tabela z bazy danych musi być reprezentowana, przez jedną klasę, dziedziczącą po powyższej klasie. Przykładowo:
  1. class db_uzytkownicy extends db{
  2. protected $no = 4; // ilość kolumn w tabeli
  3. protected $table = "uzytkownicy"; // nazwa tabeli w bazie danych
  4.  
  5. // spis kolumn w tabeli
  6. protected $columns = array(
  7. array('id', 'INT', 10), // nazwa, typ, długość
  8. array('imie', 'VARCHAR', 255),
  9. array('email', 'VARCHAR', 255),
  10. array('nazwisko', 'VARCHAR', 255),
  11. );
  12.  
  13. }


Jak oceniacie powyższy kod? Proszę o surowe i konstruktywne opinie: co poprawić, co polepszyć, co usunąć?
Pozdrawiam (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
IceManSpy
post
Post #2





Grupa: Zarejestrowani
Postów: 1 006
Pomógł: 111
Dołączył: 23.07.2010
Skąd: Kraków

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


Nie powinno być echo w metodach klasy. Bo jeśli bym chciał to wykorzystać to po co ktoś ma patrzyć jakie zapytanie zostało wykonane?
Co się stanie jak ilość kolumn będzie inna niż rzeczywista ilość?
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: 22.10.2025 - 17:12