1
0
Fork 0

New database system that uses PDO if present/Test script for doing database upgrades.

This commit is contained in:
Michael Vogel 2014-04-28 23:55:47 +02:00
parent 2846dfeb05
commit 405dd794fe
13 changed files with 3256 additions and 0 deletions

340
include/dba_pdo.php Normal file
View file

@ -0,0 +1,340 @@
<?php
require_once('include/datetime.php');
$objDDDBLResultHandler = new \DDDBL\DataObjectPool('Result-Handler');
/**
* create handler, which returns just the PDOStatement object
* this allows usage of the cursor to scroll through
* big result-sets
*
**/
$cloPDOStatementResultHandler = function(\DDDBL\Queue $objQueue) {
$objPDO = $objQueue->getState()->get('PDOStatement');
$objQueue->getState()->update(array('result' => $objPDO));
# delete handler which closes the PDOStatement-cursor
# this will be done manual if using this handler
$objQueue->deleteHandler(QUEUE_CLOSE_CURSOR_POSITION);
};
$objDDDBLResultHandler->add('PDOStatement', array('HANDLER' => $cloPDOStatementResultHandler));
/**
*
* MySQL database class
*
* For debugging, insert 'dbg(1);' anywhere in the program flow.
* dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
* When logging, all binary info is converted to text and html entities are escaped so that
* the debugging stream is safe to view within both terminals and web pages.
*
*/
if(! class_exists('dba')) {
class dba {
private $debug = 0;
private $db;
private $result;
public $connected = false;
public $error = false;
function __construct($server,$user,$pass,$db,$install = false) {
global $a;
# work around, to store the database - configuration in DDDBL
$objDataObjectPool = new \DDDBL\DataObjectPool('Database-Definition');
$objDataObjectPool->add('DEFAULT', array('CONNECTION' => "mysql:host=$server;dbname=$db",
'USER' => $user,
'PASS' => $pass,
'DEFAULT' => true));
$stamp1 = microtime(true);
$server = trim($server);
$user = trim($user);
$pass = trim($pass);
$db = trim($db);
if (!(strlen($server) && strlen($user))){
$this->connected = false;
$this->db = null;
return;
}
if($install) {
if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
$this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
$this->connected = false;
$this->db = null;
return;
}
}
}
# etablish connection to database and store PDO object
\DDDBL\connect();
$this->db = \DDDBL\getDB();
if(\DDDBL\isConnected()) {
$this->connected = true;
}
if(! $this->connected) {
$this->db = null;
if(! $install)
system_unavailable();
}
$a->save_timestamp($stamp1, "network");
}
public function getdb() {
return $this->db;
}
public function q($sql, $onlyquery = false) {
global $a;
$strHandler = (true === $onlyquery) ? 'PDOStatement' : 'MULTI';
$strQueryAlias = md5($sql);
$strSQLType = strtoupper(strstr($sql, ' ', true));
$objPreparedQueryPool = new \DDDBL\DataObjectPool('Query-Definition');
# check if query do not exists till now, if so create its definition
if(!$objPreparedQueryPool->exists($strQueryAlias))
$objPreparedQueryPool->add($strQueryAlias, array('QUERY' => $sql,
'HANDLER' => $strHandler));
if((! $this->db) || (! $this->connected))
return false;
$this->error = '';
$stamp1 = microtime(true);
try {
$r = \DDDBL\get($strQueryAlias);
# bad workaround to emulate the bizzare behavior of mysql_query
if(in_array($strSQLType, array('INSERT', 'UPDATE', 'DELETE', 'CREATE', 'DROP', 'SET')))
$result = true;
} catch (\Exception $objException) {
$result = false;
$intErrorCode = $objPreparedQueryPool->get($strQueryAlias)->get('PDOStatement')->errorCode();
}
$stamp2 = microtime(true);
$duration = (float)($stamp2-$stamp1);
$a->save_timestamp($stamp1, "database");
if(x($a->config,'system') && x($a->config['system'],'db_log')) {
if (($duration > $a->config["system"]["db_loglimit"])) {
$duration = round($duration, 3);
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
basename($backtrace[1]["file"])."\t".
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
substr($sql, 0, 2000)."\n", FILE_APPEND);
}
}
if($intErrorCode)
$this->error = $intErrorCode;
if(strlen($this->error)) {
logger('dba: ' . $this->error);
}
if($this->debug) {
$mesg = '';
if($result === false)
$mesg = 'false';
elseif($result === true)
$mesg = 'true';
else {
# this needs fixing, but is a bug itself
#$mesg = mysql_num_rows($result) . ' results' . EOL;
}
$str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
. (($this->error) ? ' error: ' . $this->error : '')
. EOL;
logger('dba: ' . $str );
}
/**
* If dbfail.out exists, we will write any failed calls directly to it,
* regardless of any logging that may or may nor be in effect.
* These usually indicate SQL syntax errors that need to be resolved.
*/
if($result === false) {
logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
if(file_exists('dbfail.out'))
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
}
if(($result === true) || ($result === false))
return $result;
if ($onlyquery) {
$this->result = $result;
return true;
}
//$a->save_timestamp($stamp1, "database");
if($this->debug)
logger('dba: ' . printable(print_r($r, true)));
return($r);
}
public function qfetch() {
if (!$this->result)
return false;
return $this->result->fetch();
}
public function qclose() {
if ($this->result)
$this->result->closeCursor();
}
public function dbg($dbg) {
$this->debug = $dbg;
}
public function escape($str) {
if($this->db && $this->connected) {
$strQuoted = $this->db->quote($str);
# this workaround is needed, because quote creates "'" and the beginning and the end
# of the string, which is correct. but until now the queries set this delimiter manually,
# so we must remove them from here and wait until everything uses prepared statements
return mb_substr($strQuoted, 1, mb_strlen($strQuoted) - 2);
}
}
function __destruct() {
if ($this->db)
\DDDBL\disconnect();
}
}}
if(! function_exists('printable')) {
function printable($s) {
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
$s = str_replace("\x00",'.',$s);
if(x($_SERVER,'SERVER_NAME'))
$s = escape_tags($s);
return $s;
}}
// Procedural functions
if(! function_exists('dbg')) {
function dbg($state) {
global $db;
if($db)
$db->dbg($state);
}}
if(! function_exists('dbesc')) {
function dbesc($str) {
global $db;
if($db && $db->connected)
return($db->escape($str));
else
return(str_replace("'","\\'",$str));
}}
// Function: q($sql,$args);
// Description: execute SQL query with printf style args.
// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
// 'user', 1);
if(! function_exists('q')) {
function q($sql) {
global $db;
$args = func_get_args();
unset($args[0]);
if($db && $db->connected) {
$stmt = @vsprintf($sql,$args); // Disabled warnings
//logger("dba: q: $stmt", LOGGER_ALL);
if($stmt === false)
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
return $db->q($stmt);
}
/**
*
* This will happen occasionally trying to store the
* session data after abnormal program termination
*
*/
logger('dba: no database: ' . print_r($args,true));
return false;
}}
/**
*
* Raw db query, no arguments
*
*/
if(! function_exists('dbq')) {
function dbq($sql) {
global $db;
if($db && $db->connected)
$ret = $db->q($sql);
else
$ret = false;
return $ret;
}}
// Caller is responsible for ensuring that any integer arguments to
// dbesc_array are actually integers and not malformed strings containing
// SQL injection vectors. All integer array elements should be specifically
// cast to int to avoid trouble.
if(! function_exists('dbesc_array_cb')) {
function dbesc_array_cb(&$item, $key) {
if(is_string($item))
$item = dbesc($item);
}}
if(! function_exists('dbesc_array')) {
function dbesc_array(&$arr) {
if(is_array($arr) && count($arr)) {
array_walk($arr,'dbesc_array_cb');
}
}}
if(! function_exists('dba_timer')) {
function dba_timer() {
return microtime(true);
}}

1429
include/dbstructure.php Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,83 @@
<?php
namespace DDDBL;
require_once __DIR__ . '/inc/DataObjectPool.class.php';
require_once __DIR__ . '/inc/DataObject.class.php';
require_once __DIR__ . '/inc/Singleton.class.php';
require_once __DIR__ . '/inc/Queue.class.php';
require_once __DIR__ . '/inc/exceptions/UnexpectedParameterTypeException.class.php';
require_once __DIR__ . '/inc/exceptions/QueryException.class.php';
require_once __DIR__ . '/inc/database.func.php';
# position of handler, which gets the active database-connection into the queue
define('QUEUE_GET_DB_CONNECTION_POSITION', 10);
define('QUEUE_GET_QUERY_POSITION', 20);
define('QUEUE_BIND_DATA_TYPE_POSITION', 30);
define('QUEUE_PREPARE_QUERY_POSITION', 40);
define('QUEUE_EXECUTE_QUERY_POSITION', 50);
define('QUEUE_FORMAT_RESULT_POSITION', 60);
define('QUEUE_CLOSE_CURSOR_POSITION', 70);
###############################################
### set validator for "Database-Definition" ###
###############################################
$objDBDefinitionValidator = function ($arrValues) {
foreach(array('CONNECTION', 'USER', 'PASS') AS $strDefinitionField)
if(!isset($arrValues[$strDefinitionField]) || !is_string($arrValues[$strDefinitionField]))
return false;
if(isset($arrValues['PDO']) && !is_a($arrValues['PDO'], '\PDO'))
return false;
return true;
};
$objDataObjectPool = new DataObjectPool('Database-Definition');
$objDataObjectPool->setValidator($objDBDefinitionValidator);
############################################
### set validator for "Query-Definition" ###
############################################
$objQueryDefinitionValidator = function ($arrValues) {
if(!isset($arrValues['QUERY']) || !is_string($arrValues['QUERY']))
return false;
if(isset($arrValues['HANDLER']) && !is_string($arrValues['HANDLER']))
return false;
return true;
};
$objDataObjectPool = new DataObjectPool('Query-Definition');
$objDataObjectPool->setValidator($objQueryDefinitionValidator);
##########################################
### set validator for "Result-Handler" ###
##########################################
$objResultHandlerValidator = function ($arrValues) {
if(!isset($arrValues['HANDLER']) || !is_callable($arrValues['HANDLER']))
return false;
return true;
};
$objDataObjectPool = new DataObjectPool('Result-Handler');
$objDataObjectPool->setValidator($objResultHandlerValidator);
#########################################
### register queue and result handler ###
#########################################
require_once __DIR__ . '/handler/register_queue_handler.inc.php';
require_once __DIR__ . '/handler/register_result_handler.inc.php';

184
library/dddbl2/dddbl.php Normal file
View file

@ -0,0 +1,184 @@
<?php
namespace DDDBL;
require_once __DIR__ . '/config.inc.php';
/**
* @throws \Exception - if no parameter are given
* @throws UnexpectedParameterTypeException - if first parameter is not a string
*
* @returns (mixed) - the result of the query-definition execution
*
* expect a list of parameter with at least one value. the
* list is handled over to the queue, which will executed
* with them
*
* in the end a result of the execution of the query-definition
* through the stored handler is returned
*
**/
function get() {
$arrParameter = func_get_args();
if(empty($arrParameter))
throw new \Exception ("no parameter given for execution");
if(!is_string($arrParameter[0]))
throw new UnexpectedParameterTypeException('string', $arrParameter[0]);
# get instance of queue and work with a copy of it
$objQueue = Singleton::getInstance('\DDDBL\Queue');
$objQueue = $objQueue->getClone();
return $objQueue->execute($arrParameter);
}
/**
* @param $strFile - the file with the query definitions to store
*
* store all query-definitions from the given file
*
**/
function storeQueryFileContent($strFile) {
storeDefinitionsFromFileInGroup($strFile, 'Query-Definition');
}
/**
* @param $strDir - the dir with query-definitions files
* @param $strMatch - a rule files in the dir have to match
*
* iterate through all files in the given dir. if a file matches
* the rule in $strMatch, the definitions in the file will be stored
* as query-definitions. all files are match in default.
*
**/
function loadQueryDefinitionsInDir($strDir, $strMatch = '*') {
walkDirForCallback($strDir, '\DDDBL\storeQueryFileContent', $strMatch);
}
/**
* @param $strFile - the file with the database definitions to store
*
* store all database definition from the given file
*
**/
function storeDBFileContent($strFile) {
$cloAdditionalHandler = function ($objDataObjectPool, $arrDefinition) {
if(!empty($arrDefinition['DEFAULT']) && true == (boolean) $arrDefinition['DEFAULT'])
$objDataObjectPool->add('DEFAULT', $arrDefinition);
};
storeDefinitionsFromFileInGroup($strFile, 'Database-Definition', $cloAdditionalHandler);
}
/**
* @param $strDir - the dir with query-definitions files
* @param $strMatch - a rule files in the dir have to match
*
* iterate through all files in the given dir. if a file matches
* the rule in $strMatch, the definitions in the file will be stored
* as database-definitions. all files are matched in default.
*
**/
function loadDBDefinitionsInDir($strDir, $strMatch = '*') {
walkDirForCallback($strDir, '\DDDBL\loadDBDefinitionsInDir', $strMatch);
}
/**
* @param $strPath - the path to the dir to handle
* @param $strCallback - the callback to call when a matching file is found
* @param $strFilenameMatch - the rule to filename has to match
*
* @throws UnexpectedParameterTypeException - if the given path or filematch-rule is not a string
* @throws UnexpectedParameterTypeException - if the given callback is not a callable
* @throws \Exception - if given path is not a directory
* @throws \Exception - if the directory is not readable
*
* reads all files of the given directory (just directory, not recursive)
* and checks, if the filename matches against the given rule. if
* a match is found the given callback is called with the full
* path to the file
*
**/
function walkDirForCallback($strPath, $strCallback, $strFilenameMatch) {
if(!is_string($strPath))
throw new UnexpectedParameterTypeException('string', $strPath);
if(!is_callable($strCallback))
throw new UnexpectedParameterTypeException('callable', $strCallback);
if(!is_string($strFilenameMatch))
throw new UnexpectedParameterTypeException('string', $strFilenameMatch);
if(!is_dir($strPath))
throw new \Exception ('given path is not an directory: ' . $strPath);
$resDirHandle = opendir($strPath);
if(!is_resource($resDirHandle))
throw new \Exception ('could not read directory: ' . $strPath);
while($strFile = readdir($resDirHandle))
if(is_file($strPath.$strFile) && fnmatch($strFilenameMatch, $strFile))
call_user_func_array($strCallback, array($strPath.$strFile));
closedir($resDirHandle);
}
/**
* @param $strFile - the file with definitions
* @param $strGroup - the group the definitions should be stored in
*
* @throws UnexpectedParameterTypeException - if the given file is not a string
* @throws UnexpectedParameterTypeException - if the given optional handler is not a callable
* @throws \Exception - if the given file is not a file or do not exists
* @throws \Exception - if the given file is not readable
*
* generic function to store all definitions in a given file
* in the specified group.
*
* if an additional handler is given, it is called AFTER the storage of the
* definition. when called it will get the reference to the DataObjectPool and the
* found definition as parameter.
*
**/
function storeDefinitionsFromFileInGroup($strFile, $strGroup, $cloAdditionalHandler = null) {
if(!is_string($strGroup))
throw new UnexpectedParameterTypeException('string', $strGroup);
if(!is_null($cloAdditionalHandler) && !is_callable($cloAdditionalHandler))
throw new UnexpectedParameterTypeException('callable', $cloAdditionalHandler);
if(!is_file($strFile) || !file_exists($strFile))
throw new \Exception ("given file is not a file or doesn't exists: $strFile");
if(!is_readable($strFile))
throw new \Exception ("given file is not readable: $strFile");
$arrDefinitions = parse_ini_file($strFile, true);
$objDataObjectPool = new DataObjectPool($strGroup);
foreach($arrDefinitions AS $strDefinitionAlias => $arrDefinition) {
$objDataObjectPool->add($strDefinitionAlias, $arrDefinition);
if(!is_null($cloAdditionalHandler))
$cloAdditionalHandler($objDataObjectPool, $arrDefinition);
}
}

View file

@ -0,0 +1,208 @@
<?php
namespace DDDBL;
$objQueue = Singleton::getInstance('\DDDBL\Queue');
#############################
### db-connection handler ###
#############################
# get (or first establish) connection to database
# and store the DataObject of the connection in the Queue-State
$cloStoreDBConnection = function(\DDDBL\Queue $objQueue, array $arrParameter) {
if(!isConnected())
connect();
$objQueue->getState()->update(array('DB' => getDBDataObject()));
};
$objQueue->addHandler(QUEUE_GET_DB_CONNECTION_POSITION, $cloStoreDBConnection);
###############################
### query-definition-loader ###
###############################
# get the DataObject of the query and store it in the queue
$cloGetQuery = function(\DDDBL\Queue $objQueue, array $arrParameter) {
$objDataObjectPool = new DataObjectPool('Query-Definition');
# get the first entry of the parameter-list; this is the query-alias
$strAlias = array_shift($arrParameter);
if(empty($strAlias) || !is_string($strAlias))
throw new \Exception('no query-alias defined!');
if(!$objDataObjectPool->exists($strAlias))
throw new \Exception("given query alias is unknown: $strAlias");
$objQueue->getState()->update(array('QUERY' => $objDataObjectPool->get($strAlias)));
};
$objQueue->addHandler(QUEUE_GET_QUERY_POSITION, $cloGetQuery);
#################################
### set BIND-DATA-TYPE option ###
#################################
# check if the query has a BIND-DATA-TYPE config.
# if not check if there is one given for the database-connection.
# if yes, store it as setting for the query, otherwise
# set false for this option
$cloSetBindDataTypeConfig = function(\DDDBL\Queue $objQueue, array $arrParameter) {
$objDB = $objQueue->getState()->get('DB');
$objQuery = $objQueue->getState()->get('QUERY');
# skip this step, if the query itselfs has its own
if($objQuery->exists('BIND-DATA-TYPE')) {
$objQuery->update(array('BIND-DATA-TYPE' => (bool) $objQuery->get('BIND-DATA-TYPE'))); #bugfix for php-bug #38409
return;
}
# set type to false, if no config is available, otherwise use the given config
if(!$objDB->exists('BIND-DATA-TYPE'))
$objQuery->update(array('BIND-DATA-TYPE' => false));
else
$objQuery->update(array('BIND-DATA-TYPE' => (bool) $objDB->get('BIND-DATA-TYPE')));
};
$objQueue->addHandler(QUEUE_BIND_DATA_TYPE_POSITION, $cloSetBindDataTypeConfig);
#####################
### prepare query ###
#####################
# get the stored query and prepare() it for the given database-connection
# store the resulting PDOStatement
$cloPrepareQuery = function(\DDDBL\Queue $objQueue, array $arrParameter) {
# if query is not prepared yet, do this now
if(!$objQueue->getState()->get('QUERY')->exists('PDOStatement')) {
$objPDO = $objQueue->getState()->get('DB')->get('PDO');
$objPDO = $objPDO->prepare($objQueue->getState()->get('QUERY')->get('QUERY'));
$objQueue->getState()->get('QUERY')->update(array('PDOStatement' => $objPDO));
}
# copy reference of prepared statement into queue for execution
$objQueue->getState()->update(array('PDOStatement' => $objQueue->getState()->get('QUERY')->get('PDOStatement')));
};
$objQueue->addHandler(QUEUE_PREPARE_QUERY_POSITION, $cloPrepareQuery);
#########################
### execute the query ###
#########################
# handler, which maps the data-type of a variable to the PDO-constants
$cloMapDataType = function($mixedParameter) {
$arrDataTypeMap = array('NULL' => \PDO::PARAM_NULL,
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR);
$strDataType = gettype($mixedParameter);
if(!isset($arrDataTypeMap[$strDataType]))
throw new \Exception ("could not bind parameters data type - type is not supported by PDO: $strDataType");
return $arrDataTypeMap[$strDataType];
};
# bind the given parameter to the prepared statement,
# then set the fetch mode and execute the query
$cloQueryExcecute = function(\DDDBL\Queue $objQueue, array $arrParameter) use ($cloMapDataType) {
$objPDO = $objQueue->getState()->get('PDOStatement');
# remove the alias from the parameter list
array_shift($arrParameter);
$objQuery = $objQueue->getState()->get('QUERY');
if(true === $objQuery->get('BIND-DATA-TYPE')) {
foreach($arrParameter AS $intIndex => $mixedParameter)
$objPDO->bindValue($intIndex + 1, $mixedParameter, $cloMapDataType($mixedParameter));
} else {
foreach($arrParameter AS $intIndex => $mixedParameter)
$objPDO->bindValue($intIndex + 1, $mixedParameter);
}
$objPDO->setFetchMode(\PDO::FETCH_ASSOC);
# execute the query. if execution fails, throw an exception
if(!$objPDO->execute())
throw new QueryException($objPDO, $objQuery->getAll());
};
$objQueue->addHandler(QUEUE_EXECUTE_QUERY_POSITION, $cloQueryExcecute);
###############################
### format the query result ###
###############################
# if a result-handler for the query is configured, call it
$cloFormatQueryResult = function(\DDDBL\Queue $objQueue, array $arrParameter) {
$objQuery = $objQueue->getState()->get('QUERY');
if(!$objQuery->exists('HANDLER'))
return ;
# get the handler and its config
$strHandlerConfig = $objQuery->get('HANDLER');
$arrHandlerConfig = preg_split('/\s+/', $strHandlerConfig);
$strHandler = array_shift($arrHandlerConfig);
# remove handler-name from config
$strHandlerConfig = trim(str_replace($strHandler, '', $strHandlerConfig));
$objDataObjectPool = new DataObjectPool('Result-Handler');
if(!$objDataObjectPool->exists($strHandler))
throw new \Exception ("unknown result-handler: $strHandler");
$objHandler = $objDataObjectPool->get($strHandler);
$cloHandler = $objHandler->get('HANDLER');
$cloHandler($objQueue, $strHandlerConfig);
};
$objQueue->addHandler(QUEUE_FORMAT_RESULT_POSITION, $cloFormatQueryResult);
####################
### close cursor ###
####################
# closing the cursor of the PDOStatement. this will free
# the result and enable the connection to execute the next query
$cloCloseCursor = function(\DDDBL\Queue $objQueue, array $arrParameter) {
$objQueryResult = $objQueue->getState()->get('PDOStatement');
$objQueryResult->closeCursor();
};
$objQueue->addHandler(QUEUE_CLOSE_CURSOR_POSITION, $cloCloseCursor);

View file

@ -0,0 +1,102 @@
<?php
namespace DDDBL;
$objDataObjectPool = new DataObjectPool('Result-Handler');
#################################
### handler for: SINGLE_VALUE ###
#################################
$cloSingleValueHandler = function(\DDDBL\Queue $objQueue) {
$arrResult = $objQueue->getState()->get('PDOStatement')->fetch();
$objQueue->getState()->update(array('result' => (empty($arrResult)) ? null : reset($arrResult)));
};
$objDataObjectPool->add('SINGLE_VALUE', array('HANDLER' => $cloSingleValueHandler));
###########################
### handler for: SINGLE ###
###########################
$cloSingleHandler = function(\DDDBL\Queue $objQueue) {
$arrResult = $objQueue->getState()->get('PDOStatement')->fetch();
$objQueue->getState()->update(array('result' => (empty($arrResult)) ? null : $arrResult));
};
$objDataObjectPool->add('SINGLE', array('HANDLER' => $cloSingleHandler));
##########################
### handler for: MULTI ###
##########################
$cloMultiHandler = function(\DDDBL\Queue $objQueue) {
$arrResult = $objQueue->getState()->get('PDOStatement')->fetchAll();
$objQueue->getState()->update(array('result' => (empty($arrResult)) ? array() : $arrResult));
};
$objDataObjectPool->add('MULTI', array('HANDLER' => $cloMultiHandler));
#########################
### handler for: LIST ###
#########################
$cloListHandler = function(\DDDBL\Queue $objQueue) {
$objResultCursor = $objQueue->getState()->get('PDOStatement');
$arrResult = array();
while($arrRow = $objResultCursor->fetch())
array_push($arrResult, current($arrRow));
$objQueue->getState()->update(array('result' => $arrResult));
};
$objDataObjectPool->add('LIST', array('HANDLER' => $cloListHandler));
#############################
### handler for: GROUP_BY ###
#############################
$cloGroupedByHandler = function(\DDDBL\Queue $objQueue, $strGroupColumn) {
$objResultCursor = $objQueue->getState()->get('PDOStatement');
$arrResult = array();
while($arrRow = $objResultCursor->fetch()) {
if(!isset($arrRow[$strGroupColumn]))
throw new \Exception ("could not group result by non-existing column: $strGroupColumn");
$arrResult[$arrRow[$strGroupColumn]][] = $arrRow;
}
$objQueue->getState()->update(array('result' => $arrResult));
};
$objDataObjectPool->add('GROUP_BY', array('HANDLER' => $cloGroupedByHandler));
#############################
### handler for: NOT_NULL ###
#############################
$cloNotNullHandler = function(\DDDBL\Queue $objQueue) {
$arrResult = $objQueue->getState()->get('PDOStatement')->fetch();
$objQueue->getState()->update(array('result' => (empty($arrResult)) ? false : true));
};
$objDataObjectPool->add('NOT_NULL', array('HANDLER' => $cloNotNullHandler));

View file

@ -0,0 +1,195 @@
<?php
namespace DDDBL;
/**
* a DataObject is a generic object
* to store data under given keys.
*
* it allows getting, adding, updating and deleting
* data.
*
* a validation callback can be provided
* to ensure, that the stored data
* validate correctly.
*
**/
class DataObject {
/**
* list of stored data
**/
private $arrData = array();
/**
* callback to validate all stored data
**/
private $cloValidator = null;
/**
* @param $cloValidator - optional validator callback to validate stored data
* @param $arrData - optional list of data to store in object
*
* @throws UnexpectedParameterTypeException - if validator callback is not a callable
*
* initiates the data-object and stores the validator callback. if no
* callback is given, a default callback is stored, which validates against
* everything.
*
* if optional data are given, they are passed to DataObject::add(), to be stored
* immediatley
*
**/
public function __construct($cloValidator = null, array $arrData = array()) {
if(!is_null($cloValidator) && !is_callable($cloValidator))
throw new UnexpectedParameterTypeException('callable', $cloValidator);
$this->cloValidator = (!is_null($cloValidator)) ? $cloValidator : function() {return true; };
if(!empty($arrData))
$this->add($arrData);
}
/**
* @param $arrData - list of data to store in object
*
* @throws \Exception - if a key is already in use
* @throws \Exception - if the final data-set do not validate
*
* add the list of data to the existing ones. the given data
* must have the following format:
* array([key] => data
* [key] => data, [..])
*
* if a key in the given data is already used in stored
* data the addition is aborted and an exception is
* thrown.
*
* the stored data are only modified on success
*
**/
public function add(array $arrData) {
$arrMergedData = array_merge($this->arrData, $arrData);
foreach($arrData AS $strKey => $mixData)
if(array_key_exists($strKey, $this->arrData))
throw new \Exception("could not store data, key is already in use: $strKey");
$cloValidator = $this->cloValidator;
if(!$cloValidator($arrMergedData))
throw new \Exception("given data do not validate");
$this->arrData = $arrMergedData;
}
/**
* @param $arrData - list of data to update
*
* @throws \Exception - if the final data-set do not validate
*
* update the stored data with the given data-set. for
* the structure of $arrData have a look at DataObject:add()
*
* existing keys are overwritten with new values. new
* keys are added to the data-set.
*
* if validation of final set fails, an exception is
* thrown. no data are modified on failure.
*
**/
public function update(array $arrData) {
$arrMergedData = array_merge($this->arrData, $arrData);
$cloValidator = $this->cloValidator;
if(!$cloValidator($arrMergedData))
throw new \Exception("given data do not validate");
$this->arrData = $arrMergedData;
}
/**
* @param $strKey - the key of the value to delete
*
* @throws UnexpectedParameterTypeException - if given key is not a string
*
* delete the value stored under the given key.
* if given key do not exists, nothing is done!
*
**/
public function delete($strKey) {
if(!is_string($strKey))
throw new UnexpectedParameterTypeException('string', $strKey);
if($this->exists($strKey))
unset($this->arrData[$strKey]);
}
/**
* @param $strKey - the key to check
*
* @throws UnexpectedParameterTypeException - if given key is not a string
*
* @return (boolean) true, if key exists
* @return (boolean) false, if key do not exists
*
* check if the given key exists
*
**/
public function exists($strKey) {
if(!is_string($strKey))
throw new UnexpectedParameterTypeException('string', $strKey);
if(!array_key_exists($strKey, $this->arrData))
return false;
return true;
}
/**
* @param $strKey - the key to get the value from
*
* @throws UnexpectedParameterTypeException - if given key is not a string
* @throws \Exception - if given key is unknown
*
* @return (mixed) the value stored under the given key
*
* return the value stored under the given key
*
**/
public function get($strKey) {
if(!is_string($strKey))
throw new UnexpectedParameterTypeException('string', $strKey);
if(!$this->exists($strKey))
throw new \Exception("unknown key: $strKey");
return $this->arrData[$strKey];
}
/**
* return all stored data in the structure of:
* array([key] => data
* [key] => data, [..])
*
**/
public function getAll() {
return $this->arrData;
}
}

View file

@ -0,0 +1,207 @@
<?php
namespace DDDBL;
/**
* The DataObjectPool is a class to manage
* the DataObjects for different types.
*
* DataObjects are stored within groups. Every group
* has a validatator, with is applyed to
* every DataObject stored in the group.
* If no validatator is set, no validation will
* be done.
*
* A DataObject is referenced by an identifier,
* which is uniqiue within a group.
*
* when creating a DataObjectPool instance,
* the wanted group is set. All following
* operations are done at this group.
*
**/
class DataObjectPool {
/**
* the actual group to operate on
*
**/
private $strGroup = null;
/**
* list of validators for each group. structure:
* array([group] => validator-callback,
* [group-n] => validator-callback-n, [..])
*
**/
static private $arrValidatorList = array();
/**
* list of DataObjects. stored in the following structure:
* array([group][uniqueue-identifier] => DataObject-reference,
* [group-n][uniqueue-identifier-n] => DataObject-reference-n, [..])
*
**/
static private $arrDataObjects = array();
/**
* @param $strGroup - the group of DataObjects to operate on
*
* @throws UnexpectedParameterTypeException - if given group is not a string
*
* create an instance of DataObjectPool and store the group
* to operate on
*
**/
public function __construct($strGroup) {
if(!is_string($strGroup))
throw new UnexpectedParameterTypeException('string', $strGroup);
$this->strGroup = $strGroup;
if(!array_key_exists($this->strGroup, self::$arrValidatorList))
self::$arrValidatorList[$this->strGroup] = null;
if(!array_key_exists($this->strGroup, self::$arrDataObjects))
self::$arrDataObjects[$this->strGroup] = array();
}
/**
* @param $cloValidator - the validator to set for the group
*
* @throws UnexpectedParameterTypeException - if given validator is not a callable
*
* set the validator for the active group. this validator
* is given to each newly created DataObject.
* if it is changed, the existing DataObjects are
* *NOT* revalidated.
*
**/
public function setValidator($cloValidator) {
if(!is_callable($cloValidator))
throw new UnexpectedParameterTypeException('string', $cloValidator);
self::$arrValidatorList[$this->strGroup] = $cloValidator;
}
/**
* @param $strIdentifier - the unique identifier of the DataObject
* @param $arrData - the data to store in the DataObject
*
* @see DataObject:add()
*
* @throws UnexpectedParameterTypeException - if identifier is not a string
* @throws \Exception - if given identifier is not unique
*
* @returns (DataObject) - reference to the created DataObject-instance
*
* create a new DataObject and store it in the pool. The given
* identifier is the key to retrieve the DataObject from the pool.
* The given data are stored within the DataObject.
*
* After creation and storage of the DataObject, a reference
* to the object is returned
*
**/
public function add($strIdentifier, array $arrData) {
if(!is_string($strIdentifier))
throw new UnexpectedParameterTypeException('string', $strIdentifier);
if($this->exists($strIdentifier))
throw new \Exception ("identifier already in use: $strIdentifier");
$objDataObject = new DataObject(self::$arrValidatorList[$this->strGroup], $arrData);
self::$arrDataObjects[$this->strGroup][$strIdentifier] = $objDataObject;
return $objDataObject;
}
/**
* @param $strIdentifier - the identifier of the object to delete
*
* @throws UnexpectedParameterTypeException - if given identifier is not a string
* @throws \Exception - if the given identifier is not known
*
* delete the stored DataObject from the DataObjectPool
*
**/
public function delete($strIdentifier) {
if(!is_string($strIdentifier))
throw new UnexpectedParameterTypeException('string', $strIdentifier);
if(!$this->exists($strIdentifier))
throw new \Exception ("DataObject not found, identifier unknown: $strIdentifier");
unset(self::$arrDataObjects[$this->strGroup][$strIdentifier]);
}
/**
* @param $strIdentifier - the identifier to check
*
* @throws UnexpectedParameterTypeException - if given identifier is not a string
*
* @returns (boolean) true, if the identifier exists
* @returns (boolean) false, if the identifier do not exists
*
**/
public function exists($strIdentifier) {
if(!is_string($strIdentifier))
throw new UnexpectedParameterTypeException('string', $strIdentifier);
if(!isset(self::$arrDataObjects[$this->strGroup][$strIdentifier]))
return false;
return true;
}
/**
* @param $strIdentifier - the identifier of the DataObject to retrieve
*
* @throws UnexpectedParameterTypeException - if given identifier is not a string
* @throws \Exception - if given identifier is unknown
*
* @returns (DataObject) - reference to the DataObject
*
* returns a reference to the DataObject stored under the identifer
*
**/
public function get($strIdentifier) {
if(!is_string($strIdentifier))
throw new UnexpectedParameterTypeException('string', $strIdentifier);
if(!$this->exists($strIdentifier))
throw new \Exception ("DataObject not found, identifier unknown: $strIdentifier");
return self::$arrDataObjects[$this->strGroup][$strIdentifier];
}
/**
* @returns (array) - list of all DataObjects of the active group
*
* returns an array of all stored DataObjects of the active group
* with the following structure:
*
* array([identifier] => DataObject-reference,
* [identifier-n] => DataObject-reference-n, [..])
*
**/
public function getAll() {
return self::$arrDataObjects[$this->strGroup];
}
}

View file

@ -0,0 +1,138 @@
<?php
namespace DDDBL;
/**
* this class implements a queue of handler, which
* are called in a specified order.
*
* this allows the combiniation of different steps,
* like database-connection management, query execution
* and result parsing in a simple list of actions.
*
* Queue::getClone() returns a clone of the queue,
* which allows modifications of the queue by
* the executed handler.
* in this way different problems, like substituions,
* test-cases, statistics and much more can be solved,
* without destroying the configured order for other queries.
*
**/
class Queue {
/**
* the sorted (!) queue of handler to execute
*
**/
private $arrHandlerQueue = array();
/**
* @see \DDDBL\DataObject
*
* an DataObject, which is used to store the states of the queue
*
**/
private $objState = null;
/**
* @param $intPosition - the position to store the handler at
* @param $cloHandler - the handler to store in the queue
*
* @throws UnexpectedParameterTypeException - if the first parameter is not an integer
* @throws UnexpectedParameterTypeException - if the second parameter is not a callable
* @throws \Exception - if there is already a handler stored under the given position
*
* store the given handler under the given position in the queue.
* if the position is already in use an expection is thrown.
*
**/
public function addHandler($intPosition, $cloHandler) {
if(!is_int($intPosition))
throw new UnexpectedParameterTypeException('integer', $intPosition);
if(!is_callable($cloHandler))
throw new UnexpectedParameterTypeException('callable', $cloHandler);
if(!empty($this->arrHandlerQueue[$intPosition]))
throw new \Exception("there is already a handler stored for position: $intPosition");
$this->arrHandlerQueue[$intPosition] = $cloHandler;
ksort($this->arrHandlerQueue);
}
/**
* @param $intPosition - the position the handler for deletion is stored under
*
* @throws UnexpectedParameterTypeException - if the parameter is not an integer
*
* delete the handler stored under the given position
*
**/
public function deleteHandler($intPosition) {
if(!is_int($intPosition))
throw new UnexpectedParameterTypeException('integer', $intPosition);
if(array_key_exists($intPosition, $this->arrHandlerQueue))
unset($this->arrHandlerQueue[$intPosition]);
}
/**
* @returns (\DDDBL\Queue) - a clone of the queue-instance
*
* return a clone of the acutal queue
*
**/
public function getClone() {
return clone $this;
}
/**
* @param $arrParameter - the parameter to use when executing the queue-handler
*
* @returns (mixed) the state of "result"
*
* execute all handler in the queue, in the given
* order from low to high. after execution return the
* state "result".
*
* handler which generates an output
* are expected to store the result in this state
*
**/
public function execute(array $arrParameter) {
$this->getState()->add(array('result' => null));
foreach($this->arrHandlerQueue AS $cloHandler)
$cloHandler($this, $arrParameter);
return $this->getState()->get('result');
}
/**
* @returns (DataObject) - the DataObject which handles the states of the queue
*
* returns a reference to the DataObject, which
* stores all states of the queue.
*
* if no object exists till now, a new one is created
*
**/
public function getState() {
if(!is_object($this->objState))
$this->objState = new DataObject();
return $this->objState;
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace DDDBL;
/**
* simple implementation of generic singleton
* for all classes, which allows additional instances
* if needed
*
**/
class Singleton {
/**
* @param $strClass - the class we want an instance from
*
* @throws UnexpectedParameterTypeException - if given parameter is not a string
* @throws \Exception - if given class do not exists
*
* @return (object) - an instance of the given classname
*
* get a reference to the instance of the given class.
* if instance do not exists, create one. after creation
* always return reference to this reference
*
**/
static function getInstance($strClass) {
if(!is_string($strClass))
throw new UnexpectedParameterTypeException('string', $strClass);
if(!class_exists($strClass))
throw new \Exception ("class do not exists: $strClass");
static $arrObjectList = array();
if(!isset($arrObjectList[$strClass]))
$arrObjectList[$strClass] = new $strClass();
return $arrObjectList[$strClass];
}
}

View file

@ -0,0 +1,206 @@
<?php
namespace DDDBL;
/**
* @returns (PDO) - reference to PDO object
* @returns (boolean) false, if there is no connection to the database
*
* if there is a connection to the database,
* the PDO object is returned otherwise false
*
**/
function getDB() {
if(!isConnected())
return false;
$objDB = getDBDataObject();
return $objDB->get('PDO');
}
/**
* @returns (boolean) true, if connection exists or is established
* @returns (boolean) false, if connection could not be established
*
* if no connection to the database exists, establishe one.
*
**/
function connect() {
if(isConnected())
return true;
$objDB = getDBDataObject();
try {
$objPDO = new \PDO($objDB->get('CONNECTION'),
$objDB->get('USER'),
$objDB->get('PASS'));
} catch (\Exception $objException) {
return false;
}
$objDB->update(array('PDO' => $objPDO));
return true;
}
/**
* disconnect from the database
*
**/
function disconnect() {
$objDB = getDBDataObject();
$objPDO = $objDB->get('PDO');
$objPDO = null;
$objDB->delete('PDO');
}
/**
* check if a connection to the database is established
*
**/
function isConnected() {
$objDB = getDBDataObject();
if(!$objDB->exists('PDO'))
return false;
return true;
}
/**
* @returns (boolean) true, if transaction started
* @returns (boolean) false, if transaction could not be started
* @returns (boolean) false, if no connection to database exists
*
* start a transaction
*
**/
function startTransaction() {
return mapMethod('beginTransaction');
}
/**
* @returns (boolean) true, if there is an active transaction
* @returns (boolean) false, if there is no active transaction
* @returns (boolean) false, if no connection to database exists
*
* check if there is an active transaction
*
**/
function inTransaction() {
return mapMethod('inTransaction');
}
/**
* @returns (boolean) true, if rollback was successfull
* @returns (boolean) false, if rollback was not successfull
* @returns (boolean) false, if no connection to database exists
*
* perform a rollback of the active transaction
*
**/
function rollback() {
return mapMethod('rollback');
}
/**
* @returns (boolean) true, if commit was successfull
* @returns (boolean) false, if commit was not successfull
* @returns (boolean) false, if no connection to database exists
*
* commit the active transaction
*
**/
function commit() {
return mapMethod('commit');
}
/**
* @returns (array) - list of error-information
*
* get information about an error
*
**/
function getErrorInfo() {
return mapMethod('errorInfo');
}
/**
* change the active database-connection. all db-functions
* are performed at the new connection.
*
* ATTENTION: the old connection is *not* closed!
*
**/
function changeDB($strIdentifier) {
$objDataObjectPool = new DataObjectPool('Database-Definition');
$objNewDB = $objDataObjectPool->get($strIdentifier);
$objDataObjectPool->delete('DEFAULT');
$objDataObjectPool->add('DEFAULT', $objNewDB->getAll());
}
/**
* @returns (DataObject) - reference to the DataObject of default connection
*
* returns the DataObject of the default connection.
*
**/
function getDBDataObject() {
$objDataObjectPool = new DataObjectPool('Database-Definition');
return $objDataObjectPool->get('DEFAULT');
}
/**
* @throws UnexpectedParameterTypeException - if the given parameter is not a string
*
* @returns (boolean) false, if no connection is established
*
* check if a connection to the database is established. if so,
* the given parameter is used, as method to call at the
* PDO object. the result of the call is returned
*
**/
function mapMethod($strMethod) {
if(!is_string($strMethod))
throw new UnexpectedParameterTypeException('string', $strMethod);
if(!isConnected())
return false;
$objDB = getDBDataObject();
$objPDO = $objDB->get('PDO');
return $objPDO->$strMethod();
}

View file

@ -0,0 +1,94 @@
<?php
namespace DDDBL;
/**
*
* create an exception with relevant information, if a query fails
*
**/
class QueryException extends \Exception {
/**
*
* @param $objPDO - the PDO object which caused the error when executed
* @param $arrQueryDefinition - the complete query definition
*
* create an error message which contains all relevant informations
* and print them as exception
*
**/
public function __construct(\PDOStatement $objPDO, $arrQueryDefinition) {
$strMessage = self::createErrorMessage($objPDO, $arrQueryDefinition);
parent::__construct($strMessage);
}
/**
*
* @param $objPDO - the PDO object related with the error
* @param $arrQueryDefinition - the complete query definition
*
* @return (string) the complete exception message
*
* build and return the exception message out of the given error info
* and query definition
*
**/
private function createErrorMessage($objPDO, $arrQueryDefinition) {
$strMessage = self::flattenQueryErrorInfo($objPDO);
$strMessage .= self::flattenQueryDefiniton($arrQueryDefinition);
return $strMessage;
}
/**
*
* @param $objPDO - PDO object to get error information from
*
* @return (string) a flatten error info from the query object
*
* build and return a flatten error-info
* from the driver specific error message
*
**/
private function flattenQueryErrorInfo($objPDO) {
$arrErrorInfo = $objPDO->errorInfo();
$strMessage = '';
if(!empty($arrErrorInfo) && !empty($arrErrorInfo[0]) && '00000' !== $arrErrorInfo[0])
$strMessage = "\nError-Code: {$arrErrorInfo[0]}\nError-Message: {$arrErrorInfo[2]}\n";
return $strMessage;
}
/**
*
* @param $arrQueryDefinition - the complete query definition
*
* @return (string) a text version of the query definition
*
* create an text, which contains all information
* of the query definition
*
**/
private function flattenQueryDefiniton($arrQueryDefinition) {
$strMessage = "\nQuery-Definiton:\n";
foreach($arrQueryDefinition AS $strKeyword => $strContent)
$strMessage .= "$strKeyword: $strContent\n";
return $strMessage . "\n";
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace DDDBL;
/**
* exception if the given parameter
* has an unexpected data-type
*
**/
class UnexpectedParameterTypeException extends \Exception {
/**
* @param $strExpected - the expected datatype
* @param $mixedValue - the given parameter
*
* determines the datatype of the given parameter and
* creates and stores the exception message
*
**/
public function __construct($strExpected, $mixedValue) {
parent::__construct("value of type $strExpected expected, but got: " . gettype($mixedValue));
}
}