|
|
|
|
|
|
| |
This package allows you to switch easily from one db server to another one, from a db connection to another one, keeping the minimum objects necessary, with its factory. It allows a strong errors management thanks to several exception types. Its methods let you, in addition to usual db classes functionnalities, manage transactions, queries preparation, queries results sets limitation...
|
<?php
/**
* class aDBException extends Exception
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBException extends Exception {
/**
* Class in which Exception was caught
*
* @var string
*/
private $sCallerClass = null;
/**
* Method in which Exception was caught
*
* @var string
*/
private $sCallerFunc = null;
/**
* public function __construct
* Constructor.
*
* @param string $sMessage : Exception message
* @param string $sClass : Class in which Exception was caught
* @param string $sFunction : Method in which Exception was caught
*/
public function __construct ($sMessage, $iCode, $sClass = 'unknown', $sFunction = 'unknown') {
$this -> sCallerClass = $sClass;
$this -> sCallerFunc = $sFunction;
parent::__construct ($sMessage, $iCode);
}
/**
* public function __toString
* Display Exception message
*
* @return string
*/
public function __toString() {
return $this -> sCallerClass.' :: '.$this -> sCallerFunc.'() : ['.$this -> code.'] '.$this->message;
}
}
/**
* class aDBExceptionIllegalClass extends aDBException
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBExceptionIllegalClass extends aDBException {
/**
* Class constants : Exception messages and codes
*
*/
const ILLEGAL_CLASS_NAME = '{CLASS} is not implemented';
const UNEXPECTED_INSTANCE_ERROR = 'Unexpected instance error';
const CODE_ILLEGAL_CLASS_NAME = 0;
const CODE_UNEXPECTED_INSTANCE_ERROR = 1;
}
/**
* class aDBExceptionTypesError extends aDBException
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBExceptionTypesError extends aDBException {
/**
* Class constants : Exception messages and codes
*
*/
const MUST_BE_AN_ARRAY = '{PARAM} must be an array';
const MUST_BE_AN_INT = '{PARAM} must be an integer';
const CODE_MUST_BE_AN_ARRAY = 0;
const CODE_MUST_BE_AN_INT = 1;
}
/**
* class aDBExceptionInvalidClassCalls extends aDBException
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBExceptionInvalidClassCalls extends aDBException {
/**
* Class constants : Exception messages and codes
*
*/
const NO_QUERY_TO_PREPARE = 'No query has been prepared';
const NEEDLE_NOT_FOUND = '{NEEDLE} was not found in prepared query {QUERY}';
const PARAM_TYPE_NOT_FOUND = 'Parameter type asked for query preparation does not exist';
const PROP_NOT_GETABLE = '{PROP} is not a getable property';
const INVALID_OPTION = '{OPTION} is not a valid option';
const CODE_NO_QUERY_TO_PREPARE = 0;
const CODE_NEEDLE_NOT_FOUND = 1;
const CODE_PARAM_TYPE_NOT_FOUND = 2;
const CODE_PROP_NOT_GETABLE = 3;
const CODE_INVALID_OPTION = 4;
}
/**
* class aDBExceptionDbConnectorErrors extends aDBException
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBExceptionDbConnectorErrors extends aDBException {
/**
* Class constants : Exception messages and codes
*
*/
const CONNEXION_FAILED = 'Connexion failed with message [{MSG}]';
const QUERY_FAILED = 'Query [{QRY}] failed with message [{MSG}]';
const FETCH_FAILED = 'Fetch failed with message [{MSG}]';
const INVALID_SEEK_POSITION = 'Invalid seek position ({OFFSET})';
const INVALID_QRY_RESOURCE = 'Invalid query resource';
const REQUEST_ERROR = 'Undefined error in the request';
const CONNECTION_LINK_MISSING = 'No database connection found';
const COULD_NOT_FREE_RESULT = 'Free result failed with message [{MSG}]';
const CODE_CONNEXION_FAILED = 0;
const CODE_QUERY_FAILED = 1;
const CODE_FETCH_FAILED = 2;
const CODE_INVALID_SEEK_POSITION = 3;
const CODE_INVALID_QRY_RESOURCE = 4;
const CODE_REQUEST_ERROR = 5;
const CODE_CONNECTION_LINK_MISSING = 6;
const CODE_COULD_NOT_FREE_RESULT = 7;
}
/**
* class aDBFactory
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class aDBFactory {
/**
* static property : array of aDB instances
*
* @var array of aDB object
*/
private static $_instance;
/**
* public static getInstance
* Factory.
* Checks that the requested DB type is implemented.
* Then, checks if there is an existing instance of it. If not, creates it with configuration and options if any.
* If yes, gets it! Checks if there is a new configuration to apply and apply it if so. Same story for the options.
* Returns the instance...
*
* @param string $saDBType : name of the requested aDB class
* @param array $aConConf : array of aDB connection configuration
* @param unknown_type $aOptions : array of aDB options
* @return aDB object : the requested aDB instance
*/
public static function getInstance ($saDBType, $aConConf = null, $aOptions = null) {
if (!class_exists($saDBType)) {
throw new aDBExceptionIllegalClass (str_replace ('{CLASS}', $saDBType, aDBExceptionIllegalClass::ILLEGAL_CLASS_NAME), aDBExceptionIllegalClass::CODE_ILLEGAL_CLASS_NAME, get_class($this), __FUNCTION__);
}
if (isset (self::$_instance[$saDBType])) {
if (!self::$_instance[$saDBType] instanceof $saDBType) {
throw new aDBExceptionIllegalClass (aDBExceptionIllegalClass::UNEXPECTED_INSTANCE_ERROR, aDBExceptionIllegalClass::CODE_UNEXPECTED_INSTANCE_ERROR, get_class($this), __FUNCTION__);
} else {
if (!is_null ($aConConf)) {
self::$_instance[$saDBType] -> connect ($aConConf);
}
if (!is_null ($aOptions)) {
foreach ($aOptions as $sOption => $mValue) {
self::$_instance[$saDBType] -> setOption ($sOption, $mValue);
}
}
return self::$_instance[$saDBType];
}
} else {
self::$_instance[$saDBType] = new $saDBType ($aConConf, $aOptions);
return self::$_instance[$saDBType];
}
}
}
/**
* class sqlIterator implements Iterator, SeekableIterator, Countable
* @author johan <johan.barbier@gmail.com>
* @version 20070524
*/
class sqlIterator implements Iterator, SeekableIterator, Countable {
/**
* Number of items to retrieve
*
* @var integer
*/
private $iCount;
/**
* Starting offset
*
* @var integer
*/
private $iOffset = 0;
/**
* Total number of items for the request
*
* @var integer
*/
private $iMax;
/**
* Current internal position
* @var integer
*/
private $iPos = -1;
/**
* aDB object
*
* @var aDB
*/
private $aDB;
/**
* Query resource
*
* @var resource
*/
private $rQry;
/**
* fetch mode
*
* @var aDB class constant
*/
private $aDB_MODE;
private $keptaDB_MODE;
/**
* Output
*
* @var mixed
*/
private $mOutput = false;
/**
* public function __construct
* Constructor
* set some parameters
*
* @param aDB $aDB : aDB object
* @param resource $rQry : query resource
* @param aDB class constant $aDB_MODE : fetch mode
* @param integer $iOffset : starting offset
* @param integer $iCount : fetch length
* @param boolean $bIsSeekable : resource must be rewinded or not
*/
public function __construct (aDB $aDB, $rQry, $aDB_MODE = aDB::BOTH, $iOffset = 0, $iCount = null, $bIsSeekable = true) {
if (!is_int($iOffset)) {
$this -> aDB -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '4d parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
$iOffset = 0;
}
if (!is_int($iCount) && !is_null ($iCount)) {
$this -> aDB -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '5d parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
$iCount = null;
}
$this -> keptaDB_MODE = $aDB_MODE;
$this -> aDB_MODE = (($iMode = aDB::FETCH_GROUP^$aDB_MODE) !== 0 && $iMode !== $aDB_MODE && $iMode < $aDB_MODE)?$iMode:$aDB_MODE;
$this -> rQry = $rQry;
$this -> aDB = $aDB;
$this -> iOffset = $iOffset;
$this -> iCount = $iCount;
$this -> iMax = $this -> count ();
if (true === $bIsSeekable && $this -> aDB_MODE !== aDB::FETCH_EXTRACT) {
$this -> rewind ();
}
}
/**
* publid function current
* returns current result set
*
* @return array
*/
public function current () {
if ($this -> aDB_MODE === aDB::FETCH_EXTRACT) {
if (is_array ($this -> mOutput)) {
foreach ($this -> mOutput as $sK => $sV) {
global $$sK;
$$sK = $sV;
}
}
return true;
} else {
switch ($this -> keptaDB_MODE) {
case aDB::FETCH_GROUP:
case aDB::FETCH_GROUP|aDB::BOTH:
$mFirst = array_shift ($this -> mOutput);
array_shift ($this -> mOutput);
return array ($mFirst => $this -> mOutput);
break;
case aDB::FETCH_GROUP|aDB::FETCH_ASSOC:
case aDB::FETCH_GROUP|aDB::FETCH_NUM:
$mFirst = array_shift ($this -> mOutput);
return array ($mFirst => $this -> mOutput);
break;
default:
return $this -> mOutput;
break;
}
}
}
/**
* public function next
* goes to the next result set
*
*/
public function next () {
$this -> iPos ++;
if ($this -> aDB_MODE === aDB::FETCH_EXTRACT) {
$this -> mOutput = $this -> aDB -> __fetch ($this -> rQry, aDB::FETCH_ASSOC);
} else {
$this -> mOutput = $this -> aDB -> __fetch ($this -> rQry, $this -> aDB_MODE);
}
}
/**
* public function valid
* Checks the validity of the current position
*
* @return boolean
*/
public function valid () {
if (($this -> iOffset + $this -> iPos) >= $this -> iMax) {
return false;
}
if (!is_null ($this -> iCount) && $this -> iCount > 0 && $this -> iPos >= $this -> iCount) {
return false;
}
if (false === $this -> mOutput) {
$this -> next ();
}
return true;
}
/**
* public function rewind
* moves the offset to the first position
*
*/
public function rewind () {
$this -> seek ($this -> iOffset);
}
/**
* public function seek
* seeks a given offset
*
* @param integer $iOffset
*/
public function seek ($iOffset) {
if (false === $this -> aDB -> __seek ($iOffset)) {
$this -> aDB -> interceptException ('aDBExceptionDbConnectorErrors', str_replace ('{OFFSET}', $iOffset, aDBExceptionDbConnectorErrors::INVALID_SEEK_POSITION), aDBExceptionDbConnectorErrors::CODE_INVALID_SEEK_POSITION, get_class($this), __FUNCTION__);
return false;
}
$this -> iOffset = $iOffset;
$this -> iPos = -1;
return true;
}
/**
* public function key
* Returns the current internal position
*
* @return integer
*/
public function key () {
return $this -> iPos;
}
/**
* public function count
* count the total number of result sets
*
* @return integer
*/
public function count () {
return $this -> aDB -> count ($this -> rQry);
}
/**
* public function getOffset
* Returns the current request position
*
* @return integer
*/
public function getOffset () {
return $this -> iPos + $this -> iOffset;
}
}
/**
* abstract class aDB
* @author johan <johan.barbier@gmail.com>
* @version 20070521
*/
abstract class aDB {
/**
* fetch modes
*
*/
const BOTH = 0;
const FETCH_ASSOC = 1;
const FETCH_NUM = 2;
const FETCH_GROUP = 4;
const FETCH_EXTRACT = 8;
/**
* Allowed parameter bindings
*
*/
const PARAM_STR = 1000;
const PARAM_INT = 1001;
/**
* DB Configuration
*
* @var array
*/
protected $aConConf = array (
'HOST' => '', 'LOGIN' => '', 'PWD' => '', 'DB' => '');
/**
* aDB options
*
* @var array
*/
protected $aOptions = array (
'AUTOCONNECT' => true,
'EXCEPTION_ON_ERROR' => true
);
/**
* Fetch length
*
* @var integer
*/
protected $iFetchLength;
/**
* Starting offset
*
* @var integer
*/
protected $iOffset = 0;
/**
* DB connection link resource
*
* @var resource
*/
protected $rLink;
/**
* Query
*
* @var string
*/
protected $sQuery;
/**
* Errors log when no exception
*
* @var array
*/
protected static $aErrorLog;
/**
* public function __construct
* Constructor.
* Automatic connection if AUTOCONNECT option is true
*
* @param array $aConConf
* @param array $aOptions
*/
public function __construct ($aConConf = null, $aOptions = null) {
if (is_array ($aOptions)) {
foreach ($aOptions as $sK => $sV) {
if (isset ($this -> aOptions[$sK])) {
$this -> aOptions[$sK] = $sV;
}
}
}
if (true === $this -> aOptions['AUTOCONNECT'] && !is_null ($aConConf)) {
$this -> connect ($aConConf);
}
}
/**
* public function set_limit
* Sets a limit to the query : starting offset and fetch length
* Is this method is called without any parameter, it will cancel previous set_limit call (useful if you used a limitation, and then do not want any more limitation)
*
* @param integer $iOffset
* @param integer $iCount
*/
public function set_limit ($iOffset = 0, $iCount = null) {
if (!is_int($iOffset)) {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '1st parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
return false;
}
if (!is_int($iCount) && !is_null ($iCount)) {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '2d parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
return false;
}
$this -> iFetchLength = $iCount;
$this -> iOffset = $iOffset;
return true;
}
/**
* public function next_limit
* Update the limit : aDB::iFetchLength remains the same, but aDB::iOffset is incremennted with aDB::iFetchLength.
* So, the offset is positionned on the very next result set.
*/
public function next_limit () {
if (!is_int($this -> iOffset)) {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '1st parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
return false;
}
if (!is_int($this -> iFetchLength) && !is_null ($this -> iFetchLength)) {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '2d parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
return false;
}
$this -> iOffset += $this -> iFetchLength;
return true;
}
/**
* public function connect
* Connects to the DB server
* If DB NAME has been set in the configuration, automatically selects it.
*
*/
public function connect ($aConConf) {
if (is_array ($aConConf)) {
foreach ($aConConf as $sK => $sV) {
if (isset ($this -> aConConf[$sK])) {
$this -> aConConf[$sK] = $sV;
}
}
} else {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '1st parameter', aDBExceptionTypesError::MUST_BE_AN_ARRAY), aDBExceptionTypesError::CODE_MUST_BE_AN_ARRAY, get_class($this), __FUNCTION__);
return false;
}
if (false === ($this -> rLink = $this -> _connect ())) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', str_replace ('{MSG}', $this -> _errorMsg (), aDBExceptionDbConnectorErrors::CONNEXION_FAILED), aDBExceptionDbConnectorErrors::CODE_CONNEXION_FAILED, get_class($this), __FUNCTION__);
return false;
}
if (!empty ($this -> aConConf['DB'])) {
$this -> select_db ($this -> aConConf['DB']);
}
return true;
}
/**
* public function select_db
* Selects a db
*
* @param string $sDbName
*/
public function select_db ($sDbName = null) {
if (!isset ($this -> rLink)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::CONNECTION_LINK_MISSING, aDBExceptionDbConnectorErrors::CODE_CONNECTION_LINK_MISSING, get_class($this), __FUNCTION__);
return false;
}
if (!is_null ($sDbName)) {
$this -> aConConf['DB'] = $sDbName;
}
if (false === $this -> _select_db ($this -> aConConf['DB'])) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', str_replace ('{MSG}', $this -> _errorMsg (), aDBExceptionDbConnectorErrors::CONNEXION_FAILED), aDBExceptionDbConnectorErrors::CODE_CONNEXION_FAILED, get_class($this), __FUNCTION__);
return false;
}
return true;
}
/**
* public function query
* Queries the DB server
*
* @param string $sQuery : query string
* @param boolean $bOverWriteQry : is sets to true, overwrites aDB::sQuery property; if not, does nothing
* @return resource : query resource
*/
public function query ($sQuery, $bOverWriteQry = true) {
if (!isset ($this -> rLink)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::CONNECTION_LINK_MISSING, aDBExceptionDbConnectorErrors::CODE_CONNECTION_LINK_MISSING, get_class($this), __FUNCTION__);
return false;
}
if (false === ($rRes = @$this -> _query ($sQuery))) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', str_replace (array ('{QRY}', '{MSG}'), array ($sQuery, $this -> _errorMsg ()), aDBExceptionDbConnectorErrors::QUERY_FAILED), aDBExceptionDbConnectorErrors::CODE_QUERY_FAILED, get_class($this), __FUNCTION__);
return false;
}
if (true === $bOverWriteQry) {
$this -> rQry = $rRes;
}
return $rRes;
}
/**
* public function fetch
* using current query resource or given query resource, instanciates an iterator to move through result sets
*
* @param aDB Class Constant $aDB_MODE : fetch mode
* @param resource $rQry : query resource
* @param boolean $bIsSeekable : if sets to true, query is seekable
* @return sqlIterator : the Iterator
*/
public function fetch ($aDB_MODE = aDB::BOTH, $rQry = null, $bIsSeekable = true) {
if (is_null ($rQry) && is_null ($this -> rQry)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::INVALID_QRY_RESOURCE, aDBExceptionDbConnectorErrors::CODE_INVALID_QRY_RESOURCE, get_class($this), __FUNCTION__);
return false;
}
if (!is_null ($rQry)) {
return new sqlIterator ($this, $rQry, $aDB_MODE, $this -> iOffset, $this -> iFetchLength, $bIsSeekable);
} else {
return new sqlIterator ($this, $this -> rQry, $aDB_MODE, $this -> iOffset, $this -> iFetchLength, $bIsSeekable);
}
}
/**
* public function fetchColumn
* using current query resource or given query resource, returns the value of a given column
*
* @param integer $iColumn : column number
* @param resource $rQry : query resource
* @param boolean $bIsSeekable : if sets to true, query is seekable
* @return mixed : column value
*/
public function fetchColumn ( $iColumn = 0, $rQry = null, $bIsSeekable = true) {
if (!is_int($iColumn)) {
$this -> interceptException ('aDBExceptionTypesError', str_replace ('{PARAM}', '2d parameter', aDBExceptionTypesError::MUST_BE_AN_INT), aDBExceptionTypesError::CODE_MUST_BE_AN_INT, get_class($this), __FUNCTION__);
return false;
}
if (is_null ($rQry) && is_null ($this -> rQry)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::INVALID_QRY_RESOURCE, aDBExceptionDbConnectorErrors::CODE_INVALID_QRY_RESOURCE, get_class($this), __FUNCTION__);
return false;
}
if (!is_null ($rQry)) {
$it = new sqlIterator ($this, $rQry, aDB::FETCH_NUM, $this -> iOffset, null, $bIsSeekable);
} else {
$it = new sqlIterator ($this, $this -> rQry, aDB::FETCH_NUM, $this -> iOffset, null, $bIsSeekable);
}
$it -> next ();
$aRes = $it -> current ();
if (empty ($aRes) || !isset ($aRes[$iColumn])) {
return false;
}
return $aRes[$iColumn];
}
/**
* public function fetchAll
* using current query resource or given query resource, returns an array with all the result sets.
* The array's structure depends on the fetch mode.
*
* @param aDB Class Constant $aDB_MODE
* @param resource $rQry
* @param boolean $bIsSeekable : if sets to true, query is seekable
* @return array
*/
public function fetchAll ($aDB_MODE = aDB::BOTH, $rQry = null, $bIsSeekable = true) {
if (is_null ($rQry) && is_null ($this -> rQry)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::INVALID_QRY_RESOURCE, aDBExceptionDbConnectorErrors::CODE_INVALID_QRY_RESOURCE, get_class($this), __FUNCTION__);
return false;
}
$aRes = array ();
if (!is_null ($rQry)) {
$it = new sqlIterator ($this, $rQry, $aDB_MODE, $this -> iOffset, $this -> iFetchLength, $bIsSeekable);
} else {
$it = new sqlIterator ($this, $this -> rQry, $aDB_MODE, $this -> iOffset, $this -> iFetchLength, $bIsSeekable);
}
$it -> next ();
while ($it -> valid ()) {
$aRes[] = $it -> current ();
$it -> next ();
}
return $aRes;
}
/**
* public function count
* count the total number of result sets of the given query
*
* @param resource $rQry
* @return integer
*/
public function count ($rQry = null) {
if (is_null ($rQry) && is_null ($this -> rQry)) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::INVALID_QRY_RESOURCE, aDBExceptionDbConnectorErrors::CODE_INVALID_QRY_RESOURCE, get_class($this), __FUNCTION__);
return false;
}
if (!is_null ($rQry)) {
return @$this -> _count ($rQry);
} else {
return @$this -> _count ($this -> rQry);
}
}
/**
* public function prepare
* Prepare the given query
*
* @param string $sQuery
*/
public function prepare ($sQuery) {
$this -> sQuery = $this -> _escape ($sQuery);
return true;
}
/**
* public function bindValue
* Binds a value and a type to a needle
*
* @param string $sNeedle : string to be replaced
* @param mixed $mValue : value
* @param aDB Class Constant $cType : defines the type of the value
*/
public function bindValue ($sNeedle, $mValue, $cType = null) {
if (is_null ($this -> sQuery)) {
$this -> interceptException ('aDBExceptionInvalidClassCalls', aDBExceptionInvalidClassCalls::NO_QUERY_TO_PREPARE, aDBExceptionInvalidClassCalls::CODE_NO_QUERY_TO_PREPARE, get_class($this), __FUNCTION__);
return false;
}
if (false === strpos($this -> sQuery, $sNeedle)) {
$this -> interceptException ('aDBExceptionInvalidClassCalls', str_replace (array ('{NEEDLE}', '{QUERY}'), array ($sNeedle, $this -> sQuery), aDBExceptionInvalidClassCalls::NEEDLE_NOT_FOUND), aDBExceptionInvalidClassCalls::CODE_NEEDLE_NOT_FOUND, get_class($this), __FUNCTION__);
return false;
}
if (is_null ($cType)) {
$sType = gettype ($mValue);
switch ($sType) {
case 'integer':
case 'double':
case 'boolean':
$cType = aDB::PARAM_INT;
break;
case 'string':
$cType = aDB::PARAM_STR;
break;
default:
$this -> interceptException ('aDBExceptionInvalidClassCalls', aDBExceptionInvalidClassCalls::PARAM_TYPE_NOT_FOUND, aDBExceptionInvalidClassCalls::CODE_PARAM_TYPE_NOT_FOUND, get_class($this), __FUNCTION__);
return false;
break;
}
}
switch ($cType) {
case aDB::PARAM_STR:
$mValue = $this -> _escape ($mValue);
$mValue = "'".$mValue."'";
$this -> sQuery = str_replace ($sNeedle, $mValue, $this -> sQuery);
break;
case aDB::PARAM_INT:
$this -> sQuery = str_replace ($sNeedle, $mValue, $this -> sQuery);
break;
default:
$this -> interceptException ('aDBExceptionInvalidClassCalls', aDBExceptionInvalidClassCalls::PARAM_TYPE_NOT_FOUND, aDBExceptionInvalidClassCalls::CODE_PARAM_TYPE_NOT_FOUND, get_class($this), __FUNCTION__);
return false;
break;
}
return true;
}
/**
* public function execute
* Execute a prepared query
*
*/
public function execute () {
if (is_null ($this -> sQuery)) {
$this -> interceptException ('aDBExceptionInvalidClassCalls', aDBExceptionInvalidClassCalls::NO_QUERY_TO_PREPARE, aDBExceptionInvalidClassCalls::CODE_NO_QUERY_TO_PREPARE, get_class($this), __FUNCTION__);
return false;
}
$this -> query ($this -> sQuery);
return true;
}
/**
* public function escape
* Escapes a string
*
* @param string $sString
*/
public function escape ($sString) {
return $this -> _escape ($sString);
}
/**
* public function startTransaction
* Starts a transaction
*
*/
public function startTransaction () {
$this -> _startTransaction ();
return true;
}
/**
* public function commitTransaction
* Commits a transaction
*
*/
public function commitTransaction () {
$this -> _commitTransaction ();
return true;
}
/**
* public function rollbackTransaction
* Rolls a transaction back
*
*/
public function rollbackTransaction () {
$this -> _rollbackTransaction ();
return true;
}
/**
* public function savePoint
* Creates a savepoint
*
*/
public function savePoint ($sSavePointName) {
$this -> _savePoint ($sSavePointName);
return true;
}
/**
* public function rollbackToSavePoint
* Rollbacks to a savepoint
*
*/
public function rollbackToSavePoint ($sSavePointName) {
$this -> _rollbackToSavePoint ($sSavePointName);
return true;
}
/**
* public function releaseSavePoint
* Releases a savepoint
*
*/
public function releaseSavePoint ($sSavePointName) {
$this -> _releaseSavePoint ($sSavePointName);
return true;
}
/**
* public function lastInsertId
* Returns the last inserted ID
*
* @return integer
*/
public function lastInsertId () {
return @$this -> _lastInsertId ();
}
/**
* public function freeResult
* Free requested query result resource
*
* @return boolean
*/
public function freeResult ($rQry = null) {
if (is_null ($rQry)) {
$rQry = $this -> rQry;
}
if (false === ($bRes = @$this -> _freeResult ($rQry))) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', str_replace ('{MSG}', $this -> _errorMsg (), aDBExceptionDbConnectorErrors::COULD_NOT_FREE_RESULT), aDBExceptionDbConnectorErrors::CODE_COULD_NOT_FREE_RESULT, get_class($this), __FUNCTION__);
}
return $bRes;
}
/**
* public function __fetch
* public fetch method used by the Iterator
*
* @param resurce $rQry
* @param aDB Class Constant $aDB_MODE
* @return array
*/
public function __fetch ($rQry, $aDB_MODE) {
return @$this -> _fetch ($rQry, $aDB_MODE);
}
/**
* public function __seek
* Seek method used by the Iterator
*
* @param integer $iOffset
* @return resource
*/
public function __seek ($iOffset) {
return @$this -> _seek ($iOffset);
}
/**
* public function errorMsg
* Returns db api error message
*
* @return string
*/
public function errorMsg () {
return $this -> _errorMsg ();
}
/**
* public function interceptException
* If EXCEPTION_ON_ERROR mode is active, throw an aDBException.
* If not, logs any error in the aDB::aErrorLogs array
*
* @param string $sErrorMsg
* @param string $sClass
* @param string $sFunction
*/
public function interceptException ($sExceptionClass, $sErrorMsg, $iCode, $sClass = 'unknown', $sFunction = 'unknown') {
if (true === $this -> aOptions['EXCEPTION_ON_ERROR']) {
throw new $sExceptionClass ($sErrorMsg, $iCode, $sClass, $sFunction);
} else {
aDB::$aErrorLog[] = $sClass.'::'.$sFunction.'() : '.$sErrorMsg.' ['.$iCode.']';
}
}
/**
* public function server_info
* Gets server info if any
*
* @param resource $rQry
* @return mixed
*/
public function server_info ($rLink = null) {
if (!is_null ($rLink)) {
if (false === ($mInfos = @$this -> _server_info ($rLink))) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::REQUEST_ERROR, aDBExceptionDbConnectorErrors::CODE_REQUEST_ERROR, get_class($this), __FUNCTION__);
return false;
}
return $mInfos;
} else {
if (false === ($mInfos = @$this -> _server_info ($this ->rLink))) {
$this -> interceptException ('aDBExceptionDbConnectorErrors', aDBExceptionDbConnectorErrors::REQUEST_ERROR, aDBExceptionDbConnectorErrors::CODE_REQUEST_ERROR, get_class($this), __FUNCTION__);
return false;
}
return $mInfos;
}
}
/**
* public function setOption
* Sets an existing option
*
* @param string $sOption
* @param mixed $mValue
*/
public function setOption ($sOption, $mValue) {
if (false === array_key_exists ($sOption, $this -> aOptions)) {
$this -> interceptException ('aDBExceptionInvalidClassCalls', str_replace ('{OPTION}', $sOption, aDBExceptionInvalidClassCalls::INVALID_OPTION), aDBExceptionInvalidClassCalls::CODE_INVALID_OPTION, get_class($this), __FUNCTION__);
return false;
}
$this -> aOptions[$sOption] = $mValue;
return true;
}
/**
* public function __get
* returns an authorized property value
*
* @param string $sProp
* @return mixed
*/
public function __get ($sProp) {
switch ($sProp) {
case 'QUERY':
return $this -> sQuery;
break;
case 'ERRORS':
return $this -> aErrorLog;
break;
case 'LAST_ERROR':
if (is_array ($this -> aOptions['EXCEPTION_ON_ERROR'])) {
return end ($this -> aErrorLog);
}
return false;
break;
default:
$this -> interceptException ('aDBExceptionInvalidClassCalls', str_replace ('{PROP}', $sProp, aDBExceptionInvalidClassCalls::PROP_NOT_GETABLE), aDBExceptionInvalidClassCalls::CODE_PROP_NOT_GETABLE, get_class($this), __FUNCTION__);
return false;
break;
}
}
abstract protected function _connect ();
abstract protected function _errorMsg ();
abstract protected function _select_db ($sDbName);
abstract protected function _seek ($iOffset);
abstract protected function _query ($sQuery);
abstract protected function _fetch ($rQry, $aDB_MODE);
abstract protected function _count ($rQry);
abstract protected function _lastInsertId ();
abstract protected function _startTransaction ();
abstract protected function _commitTransaction ();
abstract protected function _rollbackTransaction ();
abstract protected function _escape ($sString);
abstract protected function _server_info ($rLink);
abstract protected function _freeResult ($rQry);
abstract protected function _savePoint ($sSavePointName);
abstract protected function _rollbackToSavePoint ($sSavePointName);
abstract protected function _releaseSavePoint ($sSavePointName);
}
class mssql extends aDB {
protected function _connect () {
return @mssql_connect ($this -> aConConf['HOST'], $this -> aConConf['LOGIN'], $this -> aConConf['PWD']);
}
protected function _errorMsg () {
return @mssql_get_last_message();
}
protected function _select_db ($sDbName) {
return @mssql_select_db ($sDbName, $this -> rLink);
}
protected function _seek ($iOffset) {
return @mssql_data_seek ($this -> rQry, $iOffset);
}
protected function _query ($sQuery) {
return @mssql_query ($sQuery, $this -> rLink);
}
protected function _fetch ($rQry, $aDB_MODE) {
switch ($aDB_MODE) {
case aDB::FETCH_ASSOC:
$sMode = 'MSSQL_ASSOC';
break;
case aDB::FETCH_NUM:
$sMode = 'MSSQL_NUM';
break;
default:
$sMode = 'MSSQL_BOTH';
break;
}
return @mssql_fetch_array ($rQry, constant ($sMode));
}
protected function _count ($rQry) {
return @mssql_num_rows ($rQry);
}
protected function _lastInsertId () {
$sQuery = 'SELECT @@IDENTITY';
$rQry = $this -> query ($sQuery, false);
return $this -> fetchColumn (0, $rQry, false);
}
protected function _startTransaction () {
$sQuery = 'BEGIN TRANSACTION';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _commitTransaction () {
$sQuery = 'COMMIT TRANSACTION';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _rollbackTransaction () {
$sQuery = 'ROLLBACK TRANSACTION';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _escape ($sString) {
return @str_replace ("'", "''", $sString);
}
protected function _server_info ($rLink) {
return false;
}
protected function _freeResult ($rQry) {
return @mssql_free_result ($rQry);
}
protected function _savePoint ($sSavePointName) {
$sQuery = 'SAVE TRANSACTION '.$sSavePointName;
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _rollbackToSavePoint ($sSavePointName) {
$sQuery = 'ROLLBACK TRANSACTION '.$sSavePointName;
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _releaseSavePoint ($sSavePointName) {
}
}
class mysql extends aDB {
protected function _connect () {
return @mysql_connect ($this -> aConConf['HOST'], $this -> aConConf['LOGIN'], $this -> aConConf['PWD']);
}
protected function _errorMsg () {
return @mysql_error();
}
protected function _select_db ($sDbName) {
return @mysql_select_db ($sDbName, $this -> rLink);
}
protected function _seek ($iOffset) {
return @mysql_data_seek ($this -> rQry, $iOffset);
}
protected function _query ($sQuery) {
return @mysql_query ($sQuery, $this -> rLink);
}
protected function _fetch ($rQry, $aDB_MODE) {
switch ($aDB_MODE) {
case aDB::FETCH_ASSOC:
$sMode = 'MYSQL_ASSOC';
break;
case aDB::FETCH_NUM:
$sMode = 'MYSQL_NUM';
break;
default:
$sMode = 'MYSQL_BOTH';
break;
}
return @mysql_fetch_array ($rQry, constant ($sMode));
}
protected function _count ($rQry) {
return @mysql_num_rows ($rQry);
}
protected function _lastInsertId () {
return @mysql_insert_id ($this -> rQry);
}
protected function _startTransaction () {
$sQuery = 'START TRANSACTION';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _commitTransaction () {
$sQuery = 'COMMIT';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _rollbackTransaction () {
$sQuery = 'ROLLBACK';
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _escape ($sString) {
return @mysql_real_escape_string ($sString, $this -> rLink);
}
protected function _server_info ($rLink) {
return @mysql_get_server_info ($rLink);
}
protected function _freeResult ($rQry) {
return @mysql_free_result ($rQry);
}
protected function _savePoint ($sSavePointName) {
$sQuery = 'SAVEPOINT '.$sSavePointName;
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _rollbackToSavePoint ($sSavePointName) {
$sQuery = 'ROLLBACK TO SAVEPOINT '.$sSavePointName;
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
protected function _releaseSavePoint ($sSavePointName) {
$sQuery = 'RELEASE SAVEPOINT '.$sSavePointName;
$rQry = $this -> query ($sQuery, false);
return $rQry;
}
}
?> | | |
|
| Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server | | | MySQL database class Categories : PHP, MySQL, Databases, PHP Classes | | | Ajax PHP Tree (Left and Right) with MySQL Categories : PHP, Databases, MySQL, AJAX, PHP Classes | | | YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | TAB_STRUCT Class: Is supporting Class for the DBXML Class Categories : PHP, PHP Classes, MySQL, XML, Databases | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | Mssql database Manager Categories : PHP, Databases, MS SQL Server, Classes and Objects, PHP Classes | | | PHP Transfer data from text file to Mysql Table Categories : PHP, PHP Classes, Filesystem, Databases, MySQL | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Recordset Class for MSSQL database Categories : PHP Classes, Databases, PHP, MS SQL Server | |
|
|