I'm working to an application and i decided to create a 3-layer arhitecture.
In data layer i will create an abstract class for provider and each provider will have its class with the specific implementation derived from this one. My question is: what methods should have the abstract class? I'm not talking about the specific ones like : connect, query etc i'm trying to find out if you implement some additional methods that helped you at a later time.
Data layer
Moderator: General Moderators
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
This is my last database class, based on it i'm creating the new ones:
As you may notice there aren't methods for SP's and transactions
Code: Select all
class Database {
//variable used to build singleton pattern
static private $instance = null;
private $strDbServer;
private $strDbUser;
private $strDbPassword;
private $strDbDatabase;
//connection id
public $objConnection;
//the recorde set returned by a query
public $arrResultSet;
//new record id generated by an insert query
public $intNewId;
//rows number returned by a query
public $intRowsNumber;
//rows affected by a query
public $intRowsAffected ;
//fields of a record set
public $intFieldsNumber;
//records are loaded as associative arrays
public $arrRowsData;
//a record is loaded like an object, fields are properties
public $objRowsObject ;
#---------------------------------------------------------------
# Add Date: Wed Jul 20 17:30:40 EEST 2005
#
# Description: class constructor
#---------------------------------------------------------------
private function __construct(){
$this->strDbServer = CONF::APP_MYSQL_SERVER;
$this->strDbDatabase = CONF::APP_MYSQL_DATABASE;
$this->strDbUser = CONF::APP_MYSQL_USER;
$this->strDbPassword = CONF::APP_MYSQL_PASSWORD;
}
function __destruct() {
if(!empty($this->objConnection)) {
mysql_close($this->objConnection);
}
}
//i will use this function to create a singleton pattern
static function instance() {
if(is_null(Database::$instance)){
Database::$instance = new Database();
}
return Database::$instance;
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 17:30:53 EEST 2005
#
# Description: change database server
#---------------------------------------------------------------
public function setDatabaseServer($_strDbServer,$_strDbUser,$_strDbPassword,$_strDbDatabase){
$this->strDbServer = $_strDbServer;
$this->strDbUser = $_strDbUser;
$this->strDbPassword = $_strDbPassword;
$this->strDbDatabase = $_strDbDatabase;
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:00:15 EEST 2005
#
# Description: send quesry to database server
#---------------------------------------------------------------
public function setQuery($_strSql) {
//connect to mysql server
$this->getConnection();
//send query to database server
$this->arrResultSet = mysql_query($_strSql,$this->objConnection);
if(!$this->arrResultSet) {
//Query has problems, email to admin
trigger_error(mysql_error(),E_USER_ERROR);
}
else {
//load the number of affected rows
$this->intRowsAffected = $this->getRowsAffected();
//i will pre-set some variables
if(eregi("^SELECT", $_strSql)) {
//if i have an select type query
$this->intRowsNumber = $this->getRowsNumber();
$this->intFieldsNumber = $this->getFieldsNumber();
}
else {
$this->intRowsNumber = 0;
$this->intFieldsNumber = 0;
}
}
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:25:35 EEST 2005
#
# Description: returns an object with properties that correspond to the fetched row
#---------------------------------------------------------------
public function getRowsObject() {
if($this->intRowsAffected != 0) {
$this->objRowsObject = mysql_fetch_object($this->arrResultSet);
}
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:29:52 EEST 2005
#
# Description: will fetch a result set that has multiple records
#---------------------------------------------------------------
public function getFetchedArray() {
$arrData = array();
if($this->intRowsAffected != 0) {
if($this->intRowsAffected > 1) {
while($row = mysql_fetch_array($this->arrResultSet,MYSQL_ASSOC)){
$arrData[] = $row;
}
}
else {
$arrData = mysql_fetch_array($this->arrResultSet,MYSQL_ASSOC);
}
}
return $arrData;
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:30:22 EEST 2005
#
# Description: will fetch a result set with just one row
#---------------------------------------------------------------
public function getFetchedRow(){
if($this->intRowsAffected != 0) {
return mysql_fetch_row($this->arrResultSet);
}
else {
return null;
}
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 17:49:29 EEST 2005
#
# Description: connect to a mysql server and select database
#---------------------------------------------------------------
private function getConnection(){
$this->objConnection = mysql_connect($this->strDbServer,$this->strDbUser,$this->strDbPassword);
if(!$this->objConnection) {
//Could not connect to database server
trigger_error(mysql_error(),E_USER_ERROR);
}
else {
if(!mysql_select_db($this->strDbDatabase,$this->objConnection)) {
//Could not find database on server
trigger_error(mysql_error(),E_USER_ERROR);
}
}
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:00:26 EEST 2005
#
# Description: if was an insert operation get last insert id
#---------------------------------------------------------------
private function getNewId(){
return mysql_insert_id($this->objConnection);
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:19:02 EEST 2005
#
# Description: get the number of rows affected by a query
#---------------------------------------------------------------
private function getRowsAffected() {
return mysql_affected_rows($this->objConnection);
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:19:20 EEST 2005
#
# Description: get the number of records in a result set
#---------------------------------------------------------------
private function getRowsNumber() {
return mysql_num_rows($this->arrResultSet);
}
#---------------------------------------------------------------
# Add Date: Wed Jul 20 18:20:08 EEST 2005
#
# Description: get the field number in a result set
#---------------------------------------------------------------
private function getFieldsNumber() {
return mysql_num_fields($this->arrResultSet);
}
}