Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Abstrakcyjna klasa obslugi bazy danych
Beynar
post
Post #1





Grupa: Zarejestrowani
Postów: 60
Pomógł: 1
Dołączył: 6.12.2007

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


Napisalem abstrakycjna klase do obslugi bazy mysql
Co o niej sadzicie?
Chce na niej bazowac w duzym projekcie i nie chcialbym wtopic
Jak robic cachowanie? Nie mam pomyslu...
  1. <?php
  2.  
  3.  class db{
  4.  
  5.  public $_link; // connection id
  6.  public $_database; // selected db
  7.  public $_query; // current query
  8.  public $_resource; // data resource
  9.  public $_data; // data
  10.  public $_rows; // number of processed rows
  11.  public $_queries=0; // number of queris
  12.  public $_lastId; // last primary key
  13.  
  14.  
  15.  public function connect($host, $login, $pass, $_database){
  16.  if($_link=mysql_connect($host, $login, $pass))
  17.  $this -> _link = $_link;
  18.  else{
  19.  $this -> error();
  20.  return false;
  21.  }
  22.  if($_database=mysql_select_db($_database))
  23.  $this -> _database = $_database;
  24.  else{
  25.  $this -> error();
  26.  return false;
  27.  }
  28.  return true;
  29.  }
  30.  
  31.  
  32.  public function query($_query){
  33.  if(!$this -> _link || !$this -> _database){
  34.  $this -> error("can not do _query - no _database selected");
  35.  return false;
  36.  }
  37.  $this -> _query = $_query;
  38.  if($this -> _resource)
  39.  @mysql_free_result($this -> _resource);
  40.  if($_resource=mysql_query($_query, $this -> _link))
  41.  $this -> _resource = $_resource;
  42.  else{
  43.  $this -> error();
  44.  return false;
  45.  }
  46.  $this -> _queries++;
  47.  $this -> _lastId = mysql_insert_id($this -> _link);
  48.  return $this -> _resource;
  49.  }
  50.  
  51.  public function getData(){
  52.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  53.  $this -> error("can not get data from resource - no database selected or there is no data recosurce");
  54.  return false;
  55.  }
  56.  while($_data=mysql_fetch_array($this -> _resource, MYSQL_ASSOC))
  57.  $this -> _data[]=$_data;
  58.  if(!is_array($_data))
  59.  return $this -> _data;
  60.  else
  61.  $this -> error();
  62.  }
  63.  
  64.  public function getOnce(){
  65.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  66.  $this -> error("can not get data from resource - no database selected or there is no data recosurce");
  67.  return false;
  68.  }
  69.  if($_data=mysql_fetch_array($this -> _resource, MYSQL_ASSOC)){
  70.  $this -> _data[0]=$_data;
  71.  return true;
  72.  }
  73.  else
  74.  return false;
  75.  }
  76.  
  77.  public function numRows(){
  78.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  79.  $this -> error("can not get data from _resource because no _database selected or there is n
    o data recosurce"
    );
  80.  return false;
  81.  }
  82.  $_rows=mysql_num_rows($this -> _resource);
  83.  $this -> _rows = $_rows;
  84.  return $_rows;
  85.  }
  86.  
  87.  
  88.  public function flush(){
  89.  if($this -> _resource)
  90.  @mysql_free_result($this -> _resource);
  91.  unset($this -> _query);
  92.  unset($this -> _resource);
  93.  unset($this -> _data);
  94.  unset($this -> _rows);
  95.  $this -> _queries=0;
  96.  unset($this -> _lastId);
  97.  return true;
  98.  }
  99.  
  100.  private function error($error=false){
  101.  if($error)
  102.  echo "<p style=\"font-weight:bold;color:red;\">".$error."</p>";
  103.  echo "<p style=\"font-weight:bold;color:red\">".mysql_errno()." = ".mysql_error()."</p>";
  104.  if($this -> _query!="")
  105.  echo "<p style=\"font-weight:bold;color:red\">".$this -> _query."</p>";
  106.  }
  107.  
  108.  public function __destruct(){
  109.  $this -> flush();
  110.  if($this -> _link && $this -> _db){
  111.  if(!mysql_close($this -> _link)){
  112.  $this -> _error();
  113.  return false;
  114.  }
  115.  else
  116.  return true;
  117.  }
  118.  else 
  119.  return true;
  120.  }
  121.  }
  122.  ?>


Ten post edytował Beynar 11.12.2007, 22:59:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
ayeo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


Właściwości powinny być private, używaj metod dostępowych gdzie trzeba. Łączenie z baza to zadanie dla konstruktora (zamykanie polaczenia dla destruktora). Metoda numRows jest zupelnie niepotrzebna, można użyć count() bezpośrednio w kodzie. Sama klasa powinna być Singletonem (albo częścią rejestru). Żadna metoda klasy nie powinna (mam tu na myśli error()) wysyłać nic bezpośrednio na wyjście (echo). Funkcje bazy takie jak INSERT czy UPDATE powinny być zaimplementowane osobno, a nie razem z SELECT bo nie zwracają przecież tablicy wyników! Sama warstwa abstrakcji powinna korzystać ze sterownika bazy danych (osobna klasa) i na podstawie parametrów wybierać odpowiedni (np MYSQL) - wzorzec STRATEGY, FACTORY...
Nazywanie publicznych właściwości od _ jest mylące, ale o w sumie Twoja sprawa. Poza tym i tak dalej w kodzie używasz czystego SQLa. Odczyteywanie _last_id z właściwości klasy jest błędem bo jeśli inny User doda coś do bazy na innej sesji to otrzymujesz błędny wynik! Nie do zmiany bazy danych musisz tworzyć nową instancje! Brak jakichkolwiek zabezpieczeń...
Już mi się nie chce dalej, a troche jeszcze tego jest (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)

Co do cachowania to dość rozległy temat.

PS to chyba nie jest abstrakcyjna klasa (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

Ten post edytował ayeo 12.12.2007, 00:38:13
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: 27.12.2025 - 09:40