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:
Oto sama klasa dbmanager.class.php
-
<?php
-
/**
-
* Database Manager
-
*
-
* @package Darion
-
* @author Arkadiusz 'ARJ' Jasak
-
* @copyright Copyright (c) 2006 Arkadiusz 'ARJ' Jasak (ajasak[at]gmail.com)
-
* @version 0.2
-
*/
-
class DBmanager{
-
/**
-
* Object instance
-
*
-
* @var object
-
* @access private
-
*/
-
-
/**
-
* Query results
-
*
-
* @var array
-
* @access private
-
*/
-
-
/**
-
* PDO instance
-
*
-
* @var object
-
* @access private
-
*/
-
private $dbh;
-
-
/**
-
* Number of executes
-
*
-
* @access private
-
* @var int
-
*/
-
private $numExecutes = 0;
-
-
/**
-
* Constructor
-
* Connect to database and set character encoding
-
* @access public
-
*/
-
public function __construct(){
-
try{
-
$this->dbh = new PDO(DB_KIND.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
-
$this->dbh->exec('SET CHARACTER SET '.DB_CHARSET);
-
$this->dbh->exec('SET collation_connection = '.DB_COLCON.';');
-
}catch (PDOException $e){
-
';
-
}
-
}
-
-
/**
-
* Query and fetch
-
*
-
* @param string $_query
-
* @param string $_mode
-
* @param int $_param
-
* @access public
-
* @return array
-
*/
-
public function queryandfetch($_query, $_mode, $_param = NULL){
-
$this->numExecutes++;
-
$this->result = null;
-
$this->result = $this->dbh->query($_query);
-
-
switch ($_mode){
-
case 'assoc':
-
return $this->result->fetchAll(PDO::FETCH_ASSOC);
-
break;
-
case 'both':
-
return $this->result->fetchAll(PDO::FETCH_BOTH);
-
break;
-
case 'column':
-
return $this->result->fetchAll(PDO::FETCH_COLUMN, $_param);
-
break;
-
default:
-
return $this->result->fetchAll(PDO::FETCH_ASSOC);
-
}
-
}
-
-
/**
-
* Exec: insert, update, delete
-
*
-
* @param string $_exec
-
* @access public
-
* @return int or false
-
*/
-
$this->numExecutes++;
-
$this->result = $this->dbh->exec($_exec);
-
if($this->result == false){
-
return false;
-
}else{
-
return $this->result;
-
}
-
}
-
-
/**
-
* Count rows number
-
*
-
* @param string $_table
-
* @param string $_where
-
* @return int
-
*/
-
public function numRows($_table, $_where){
-
$this->numExecutes++;
-
$this->result = $this->dbh->query("SELECT COUNT(*) FROM ".$_table." WHERE ".$_where);
-
return $this->result->fetchColumn();
-
}
-
-
/**
-
* Return number of executes
-
*
-
* @access public
-
* @return int
-
*/
-
public function numExecutes(){
-
return $this->numExecutes;
-
}
-
-
/**
-
* Singleton
-
*
-
* @access public
-
* @return object
-
*/
-
if(self::$thisInstance == null)
-
{
-
self::$thisInstance = new DBmanager();
-
}
-
return self::$thisInstance;
-
}
-
-
/**
-
* Destructor
-
* Close connection with database
-
* @access public
-
*/
-
public function __destruct(){
-
$this->dbh = null;
-
}
-
}
-
?>
Jeżeli będzie trzeba, zaprezentuję przykłady użycia.
Klasę można ściągnąć tutaj.