Database abstraction class definition
This class is simply a front for MySQL. It is published here in support of AAWS class definition.
Code
<?php
if ( !defined( 'MYSTORE' ) ) {
die( 'This file is not a valid entry point' );
}
class DBClass {
/* credentials to connect to MySQL server */
const dbHost = 'localhost';
const dbLogin = 'mystore_login';
const dbPass = 'mystore_password';
/* a default db to connect */
var $dbName ='mystore_database';
/* one link to connect them all */
private static $mysql_link = false;
/* the DBClass object created by DBClass::get_object() */
private static $theObj = false;
var $link;
var $errMsg;
var $wait;
var $ready;
var $result;
/*
class function returns an instance of a DBClass
$db_obj = DBClass::get_object();
*/
public static function get_object() {
if(self::$theObj) return self::$theObj;
self::$theObj = new self();
return self::$theObj;
}
function __construct() {
$this->errMsg = '';
$this->wait = 200000; // 0.2 seconds
$this->ready = False;
if( ! self::$mysql_link ) {
$this->connect();
}
if( self::$mysql_link )
$this->selectDB();
} //end function queryClass
function connect() {
//simply connects to the host with login and password.
if (! ( self::dbHost
&& self::dbLogin
&& self::dbPass) ) {
$this->errMsg = 'Connection parameters not defined.';
return False;
}
$wait = $this->wait;
for($y=0 ; $y < 5; $y++) {
self::$mysql_link = mysql_connect(self::dbHost, self::dbLogin, self::dbPass) ;
if (self::$mysql_link) break;
usleep($wait); $wait += $wait ; //increase wait each failure
}
if (! self::$mysql_link) {
$this->errMsg = 'Cannot establish link to DB host: '.self::dbHost;
return False;
}
return True;
} //end function linkToDB
function selectDB($dbName = '') {
//selects the database. Most applications will have all tables
//in a single database, but sometimes there are multiple.
if($dbName) $this->dbName = $dbName;
if (! $this->dbName) {
$this->errMsg = 'DB Name not defined.';
return False;
}
$wait = $this->wait;
$this->errMsg = '';
for($y=0 ; $y < 5; $y++) {
$db = mysql_select_db($this->dbName, self::$mysql_link);
if ( $db ) break;
usleep($wait); $wait += $wait ; //increase wait each failure
}
if (! $db ) {
$this->errMsg = "Cannot select DB `{$this->dbName}`.";
return False;
}
$this->ready = True;
return True;
} //end function linkToDB
function close_link() {
mysql_free_result(self::$mysql_link);
mysql_close(self::$mysql_link);
$this->ready = False;
}
//this is the main funnel for all queries
function &query($query) {
//Returns result, and also caches it in $this->result.
if (! $this->ready ) return false;
unset($this->result);
$this->errMsg = '';
//PHP reuses connections, which means you can never know which
//database this script is currently connected to.
//For that reason, this function always calls selectDB() first
if (! $this->selectDB()) return false;
// ---Performing SQL query
$this->result = mysql_query($query, self::$mysql_link) ;
//---calling code responsible for freeing result
return $this->result;
}
//These simply return info about last query
function error_number() {
return mysql_errno( self::$mysql_link );
}
function error_message() {
return mysql_error( self::$mysql_link );
}
function result_rows($result = false) {
if($result) {
if(is_resource($result))
return mysql_num_rows($result );
else return 0;
}
else {
if(is_resource ($this->result))
return mysql_num_rows( $this->result );
else return 0;
}
}
function affectedOnLastQuery() {
if (! $this->ready ) return false;
$affected = mysql_affected_rows(self::$mysql_link) ;
return $affected;
}
function insertIdOnLastQuery() {
if (! $this->ready ) return false;
$id = mysql_insert_id(self::$mysql_link) ;
return $id;
}
function get_row($result=false) {
if($result) return mysql_fetch_object($result);
if($this->result) return mysql_fetch_object($this->result);
return false;
}
function free_result($result = false) {
if($result) {
if(is_resource($result))
mysql_free_result($result);
}
else {
if(is_resource ($this->result))
mysql_free_result($this->result);
}
}
function escape_string($string='') {
return mysql_real_escape_string($string, self::$mysql_link);
}
/* return string for use in a SET `fld`="value" */
function escape_set($fld, $str) {
return '`'.$fld.'`="' . $this->escape_string($str) . '"';
}
function escape_null_set($fld, $str) {
if($str===null)
return '`'.$fld.'`='.'NULL';
return '`'.$fld.'`="'.$this->escape_string($str) . '"';
}
/* convert a row array to a series of SET parameters */
function get_set($array) {
$setarr = array();
foreach($array as $fld=>$val) {
$setarr[] = $this->escape_set(fld, $val);
}
return implode(',', $setarr);
}
}
