From 488cf4007b721f016719ca00593721dfe4025fb8 Mon Sep 17 00:00:00 2001 From: Joe Bordes Date: Tue, 30 Jan 2018 18:53:11 +0100 Subject: [PATCH] chore(PearDatabase) cbSR and fix #233 --- include/database/PearDatabase.php | 693 +++++++++++++++++------------- 1 file changed, 398 insertions(+), 295 deletions(-) diff --git a/include/database/PearDatabase.php b/include/database/PearDatabase.php index 77b2438d48..7b71d2738e 100644 --- a/include/database/PearDatabase.php +++ b/include/database/PearDatabase.php @@ -13,9 +13,9 @@ * permissions and limitations under the License. You may obtain a copy of the License * at *************************************************************************************************/ -require_once('include/logging.php'); -include('include/adodb/adodb.inc.php'); -require_once("include/adodb/adodb-xmlschema.inc.php"); +require_once 'include/logging.php'; +include 'include/adodb/adodb.inc.php'; +require_once 'include/adodb/adodb-xmlschema.inc.php'; $log = LoggerManager::getLogger('VT'); $logsqltm = LoggerManager::getLogger('SQLTIME'); @@ -24,20 +24,21 @@ // See function convertPS2Sql in PearDatabase below class PreparedQMark2SqlValue { // Constructor - function __construct($vals){ + public function __construct($vals) { $this->ctr = 0; $this->vals = $vals; } - function call($matches){ + + public function call($matches) { /** * If ? is found as expected in regex used in function convert2sql * /('[^']*')|(\"[^\"]*\")|([?])/ * */ - if($matches[3]=='?'){ + if ($matches[3]=='?') { $this->ctr++; return $this->vals[$this->ctr-1]; - }else{ + } else { return $matches[0]; } } @@ -51,15 +52,15 @@ class PerformancePrefs { /** * Get performance parameter configured value or default one */ - static function get($key, $defvalue=false) { + public static function get($key, $defvalue = false) { return $defvalue; } /** Get boolean value */ - static function getBoolean($key, $defvalue=false) { + public static function getBoolean($key, $defvalue = false) { return self::get($key, $defvalue); } /** Get Integer value */ - static function getInteger($key, $defvalue=false) { + public static function getInteger($key, $defvalue = false) { return (int)self::get($key, $defvalue); } } @@ -68,16 +69,16 @@ static function getInteger($key, $defvalue=false) { * Cache Class for PearDatabase */ class PearDatabaseCache { - var $_queryResultCache = Array(); - var $_parent; + public $_queryResultCache = array(); + public $_parent; // Cache the result if rows is less than this - var $_CACHE_RESULT_ROW_LIMIT; + public $_CACHE_RESULT_ROW_LIMIT; /** * Constructor */ - function __construct($parent) { + public function __construct($parent) { $this->_parent = $parent; $this->_CACHE_RESULT_ROW_LIMIT = PerformancePrefs::getInteger('CACHE_RESULT_ROW_LIMIT', 100); } @@ -85,44 +86,49 @@ function __construct($parent) { /** * Reset the cache contents */ - function resetCache() { + public function resetCache() { unset($this->_queryResultCache); - $this->_queryResultCache = Array(); + $this->_queryResultCache = array(); } /** * Cache SQL Query Result (perferably only SELECT SQL) */ - function cacheResult($result, $sql, $params=false) { + public function cacheResult($result, $sql, $params = false) { // We don't want to cache NON-SELECT query results now - if(stripos(trim($sql), 'SELECT ') !== 0) { + if (stripos(trim($sql), 'SELECT ') !== 0) { return; } // If the result is too big, don't cache it - if($this->_parent->num_rows($result) > $this->_CACHE_RESULT_ROW_LIMIT) { + if ($this->_parent->num_rows($result) > $this->_CACHE_RESULT_ROW_LIMIT) { global $log; $log->fatal("[" . get_class($this) . "] Cannot cache result! $sql [Exceeds limit ". $this->_CACHE_RESULT_ROW_LIMIT . ", Total Rows " . $this->_parent->num_rows($result) . "]"); return false; } $usekey = $sql; - if(!empty($params)) $usekey = $this->_parent->convert2Sql($sql, $this->_parent->flatten_array($params)); + if (!empty($params)) { + $usekey = $this->_parent->convert2Sql($sql, $this->_parent->flatten_array($params)); + } $this->_queryResultCache[$usekey] = $result; } /** * Get the cached result for re-use */ - function getCacheResult($sql, $params=false) { + public function getCacheResult($sql, $params = false) { $result = false; $usekey = $sql; - if(!empty($params)) $usekey = $this->_parent->convert2Sql($sql, $this->_parent->flatten_array($params)); + if (!empty($params)) { + $usekey = $this->_parent->convert2Sql($sql, $this->_parent->flatten_array($params)); + } $result = $this->_queryResultCache[$usekey]; // Rewind the result for re-use - if($result) { + if ($result) { // If result not in use rewind it - if($result->EOF) $result->MoveFirst(); - else if($result->CurrentRow() != 0) { + if ($result->EOF) { + $result->MoveFirst(); + } elseif ($result->CurrentRow() != 0) { global $log; $log->fatal("[" . get_class($this) . "] Cannot reuse result! $usekey [Rows Total " . $this->_parent->num_rows($result) . ", Currently At: " . $result->CurrentRow() . "]"); @@ -134,49 +140,51 @@ function getCacheResult($sql, $params=false) { } } -class PearDatabase{ - var $database = null; - var $dieOnError = false; - var $dbType = null; - var $dbHostName = null; - var $dbName = null; - var $userName=null; - var $userPassword=null; - var $query_time = 0; - var $log = null; - var $lastmysqlrow = -1; - var $enableSQLlog = false; - var $continueInstallOnError = true; +class PearDatabase { + public $database = null; + public $dieOnError = false; + public $dbType = null; + public $dbHostName = null; + public $dbName = null; + public $userName=null; + public $userPassword=null; + public $query_time = 0; + public $log = null; + public $lastmysqlrow = -1; + public $enableSQLlog = false; + public $continueInstallOnError = true; // If you want to avoid executing PreparedStatement, set this to true // PreparedStatement will be converted to normal SQL statement for execution - var $avoidPreparedSql = false; + public $avoidPreparedSql = false; /** * Performance tunning parameters * See the constructor for initialization */ - var $isdb_default_utf8_charset = true; - var $enableCache = false; + public $isdb_default_utf8_charset = true; + public $enableCache = false; - var $_cacheinstance = false; // Will be auto-matically initialized if $enableCache is true + public $_cacheinstance = false; // Will be auto-matically initialized if $enableCache is true /** * API's to control cache behavior */ - function __setCacheInstance($cacheInstance) { + public function __setCacheInstance($cacheInstance) { $this->_cacheinstance = $cacheInstance; } /** Return the cache instance reference (using &) */ - function &getCacheInstance() { + public function &getCacheInstance() { return $this->_cacheinstance; } - function isCacheEnabled() { + public function isCacheEnabled() { return ($this->enableCache && ($this->getCacheInstance() != false)); } - function clearCache() { - if($this->isCacheEnabled()) $this->getCacheInstance()->resetCache(); + public function clearCache() { + if ($this->isCacheEnabled()) { + $this->getCacheInstance()->resetCache(); + } } - function toggleCache($newstatus) { + public function toggleCache($newstatus) { $oldstatus = $this->enableCache; $this->enableCache = $newstatus; return $oldstatus; @@ -185,10 +193,9 @@ function toggleCache($newstatus) { /** * Manage instance usage of this class */ - static function &getInstance() { + public static function &getInstance() { global $adb, $log; - - if(!isset($adb)) { + if (!isset($adb)) { $adb = new self(); } return $adb; @@ -197,65 +204,93 @@ static function &getInstance() { /* * Reset query result for resuing if cache is enabled. */ - function resetQueryResultToEOF(&$result) { - if($result) { - if($result->MoveLast()) { + public function resetQueryResultToEOF(&$result) { + if ($result) { + if ($result->MoveLast()) { $result->MoveNext(); } } } - function isMySQL() { return (stripos($this->dbType ,'mysql') === 0);} - function isOracle() { return $this->dbType=='oci8'; } - function isPostgres() { return $this->dbType=='pgsql'; } + public function isMySQL() { + return (stripos($this->dbType, 'mysql') === 0); + } + public function isOracle() { + return $this->dbType=='oci8'; + } + public function isPostgres() { + return $this->dbType=='pgsql'; + } - function println($msg) { - require_once('include/logging.php'); + public function println($msg) { + require_once 'include/logging.php'; $log1 = LoggerManager::getLogger('VT'); - if(is_array($msg)) { - $log1->info("PearDatabse ->".print_r($msg,true)); + if (is_array($msg)) { + $log1->info('PearDatabase ->'.print_r($msg, true)); } else { - $log1->info("PearDatabase ->".$msg); + $log1->info('PearDatabase ->'.$msg); } return $msg; } - function setDieOnError($value){ $this->dieOnError = $value; } - function setDatabaseType($type){ $this->dbType = $type; } - function setUserName($name){ $this->userName = $name; } + public function setDieOnError($value) { + $this->dieOnError = $value; + } + public function setDatabaseType($type) { + $this->dbType = $type; + } + public function setUserName($name) { + $this->userName = $name; + } - function setOption($name, $value){ - if(isset($this->database)) $this->database->setOption($name, $value); + public function setOption($name, $value) { + if (isset($this->database)) { + $this->database->setOption($name, $value); + } } - function setUserPassword($pass){ $this->userPassword = $pass; } - function setDatabaseName($db){ $this->dbName = $db; } - function setDatabaseHost($host){ $this->dbHostName = $host; } + public function setUserPassword($pass) { + $this->userPassword = $pass; + } + public function setDatabaseName($db) { + $this->dbName = $db; + } + public function setDatabaseHost($host) { + $this->dbHostName = $host; + } - function getDataSourceName(){ + public function getDataSourceName() { return $this->dbType. "://".$this->userName.":".$this->userPassword."@". $this->dbHostName . "/". $this->dbName; } - function startTransaction() { - if($this->isPostgres()) return; + public function startTransaction() { + if ($this->isPostgres()) { + return; + } $this->checkConnection(); $this->println("TRANS Started"); $this->database->StartTrans(); } - function completeTransaction() { - if($this->isPostgres()) return; - if($this->database->HasFailedTrans()) $this->println("TRANS Rolled Back"); - else $this->println("TRANS Commited"); - + public function completeTransaction() { + if ($this->isPostgres()) { + return; + } + if ($this->database->HasFailedTrans()) { + $this->println('TRANS Rolled Back'); + } else { + $this->println('TRANS Commited'); + } $this->database->CompleteTrans(); - $this->println("TRANS Completed"); + $this->println('TRANS Completed'); } - function hasFailedTransaction(){ return $this->database->HasFailedTrans(); } + public function hasFailedTransaction() { + return $this->database->HasFailedTrans(); + } - function checkError($msg='', $dieOnError=false) { - if($this->dieOnError || $dieOnError) { + public function checkError($msg = '', $dieOnError = false) { + if ($this->dieOnError || $dieOnError) { $bt = debug_backtrace(); $ut = array(); foreach ($bt as $t) { @@ -269,15 +304,15 @@ function checkError($msg='', $dieOnError=false) { return false; } - function change_key_case($arr) { - return is_array($arr)?array_change_key_case($arr):$arr; + public function change_key_case($arr) { + return is_array($arr) ? array_change_key_case($arr) : $arr; } - var $req_flist; - function checkConnection(){ + public $req_flist; + public function checkConnection() { global $log; - if(!isset($this->database)) { - $this->println("TRANS creating new connection"); + if (!isset($this->database)) { + $this->println('TRANS creating new connection'); $this->connect(false); } else { //$this->println("checkconnect using old connection"); @@ -287,31 +322,36 @@ function checkConnection(){ /** * Put out the SQL timing information */ - function logSqlTiming($startat, $endat, $sql, $params=false) { + public function logSqlTiming($startat, $endat, $sql, $params = false) { global $logsqltm, $SQL_LOG_INCLUDE_CALLER; // Specifically for timing the SQL execution, you need to enable DEBUG in log4php.properties - if($logsqltm->isDebugEnabled()){ + if ($logsqltm->isDebugEnabled()) { if (!empty($SQL_LOG_INCLUDE_CALLER)) { $callers = debug_backtrace(); $callerscount = count($callers); $callerfunc = ''; - for($calleridx = 0; $calleridx < $callerscount; ++$calleridx) { - if($calleridx == 0) { + for ($calleridx = 0; $calleridx < $callerscount; ++$calleridx) { + if ($calleridx == 0) { // Ignore the first caller information, it will be generally from this file! continue; } // Caller function will be in next information block - if($calleridx < $callerscount) { + if ($calleridx < $callerscount) { $callerfunc = $callers[$calleridx+1]['function']; - if(!empty($callerfunc)) $callerfunc = " ($callerfunc) "; + if (!empty($callerfunc)) { + $callerfunc = " ($callerfunc) "; + } } $logsqltm->debug( "CALLER: (" . $callers[$calleridx]['line'] . ') ' . - $callers[$calleridx]['file'] . $callerfunc); + $callers[$calleridx]['file'] . $callerfunc + ); } } $logsqltm->debug("SQL: " . $sql); - if($params != null && count($params) > 0) $logsqltm->debug("PARAMS: [" . implode(",", $params) . "]"); + if ($params != null && count($params) > 0) { + $logsqltm->debug("PARAMS: [" . implode(",", $params) . "]"); + } $logsqltm->debug("EXEC: " . ($endat - $startat) ." micros [START=$startat, END=$endat]"); $logsqltm->debug(""); } @@ -320,14 +360,13 @@ function logSqlTiming($startat, $endat, $sql, $params=false) { /** * Execute SET NAMES UTF-8 on the connection based on configuration. */ - function executeSetNamesUTF8SQL($force = false) { + public function executeSetNamesUTF8SQL($force = false) { global $default_charset; // Performance Tuning: If database default charset is UTF-8, we don't need this - if($default_charset == 'UTF-8' && ($force || !$this->isdb_default_utf8_charset)) { - + if ($default_charset == 'UTF-8' && ($force || !$this->isdb_default_utf8_charset)) { $sql_start_time = microtime(true); - $setnameSql = "SET NAMES utf8"; + $setnameSql = 'SET NAMES utf8'; $this->database->Execute($setnameSql); $this->logSqlTiming($sql_start_time, microtime(true), $setnameSql); } @@ -342,27 +381,29 @@ function executeSetNamesUTF8SQL($force = false) { * * like: INSERT INTO TABLE1 VALUES (a,b), (c,d) */ - function query_batch($prefixsql, $valuearray) { - if(PerformancePrefs::getBoolean('ALLOW_SQL_QUERY_BATCH')) { + public function query_batch($prefixsql, $valuearray) { + if (PerformancePrefs::getBoolean('ALLOW_SQL_QUERY_BATCH')) { $sql = $prefixsql; $suffixsql = $valuearray; - if(!is_array($valuearray)) $suffixsql = implode(',', $valuearray); + if (!is_array($valuearray)) { + $suffixsql = implode(',', $valuearray); + } $this->query($prefixsql . $suffixsql); } else { - if(is_array($valuearray) && !empty($valuearray)) { - foreach($valuearray as $suffixsql) { + if (is_array($valuearray) && !empty($valuearray)) { + foreach ($valuearray as $suffixsql) { $this->query($prefixsql . $suffixsql); } } } } - function query($sql, $dieOnError=false, $msg='') { + public function query($sql, $dieOnError = false, $msg = '') { global $log, $default_charset; // Performance Tuning: Have we cached the result earlier? - if($this->isCacheEnabled()) { + if ($this->isCacheEnabled()) { $fromcache = $this->getCacheInstance()->getCacheResult($sql); - if($fromcache) { + if ($fromcache) { $log->debug("Using query result from cache: $sql"); return $fromcache; } @@ -377,10 +418,12 @@ function query($sql, $dieOnError=false, $msg='') { $this->logSqlTiming($sql_start_time, microtime(true), $sql); $this->lastmysqlrow = -1; - if(!$result)$this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError); + if (!$result) { + $this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError); + } // Performance Tuning: Cache the query result - if($this->isCacheEnabled()) { + if ($this->isCacheEnabled()) { $this->getCacheInstance()->cacheResult($result, $sql); } return $result; @@ -389,24 +432,25 @@ function query($sql, $dieOnError=false, $msg='') { /** * Convert PreparedStatement to SQL statement */ - function convert2Sql($ps, $vals) { - if(empty($vals)) { return $ps; } + public function convert2Sql($ps, $vals) { + if (empty($vals)) { + return $ps; + } // TODO: Checks need to be added array out of bounds situations - for($index = 0; $index < count($vals); $index++) { + for ($index = 0; $index < count($vals); $index++) { // Package import pushes data after XML parsing, so type-cast it - if(is_a($vals[$index], 'SimpleXMLElement')) { + if (is_a($vals[$index], 'SimpleXMLElement')) { $vals[$index] = (string) $vals[$index]; } - if(is_string($vals[$index])) { - if($vals[$index] == '') { + if (is_string($vals[$index])) { + if ($vals[$index] == '') { $vals[$index] = $this->database->Quote($vals[$index]); - } - else { + } else { $vals[$index] = "'".$this->sql_escape_string($vals[$index]). "'"; } } - if($vals[$index] === null) { - $vals[$index] = "NULL"; + if ($vals[$index] === null) { + $vals[$index] = 'NULL'; } } $sql = preg_replace_callback("/('[^']*')|(\"[^\"]*\")|([?])/", array(new PreparedQMark2SqlValue($vals),"call"), $ps); @@ -419,13 +463,15 @@ function convert2Sql($ps, $vals) { * @param $dieOnError -- Set to true, when query execution fails * @param $msg -- Error message on query execution failure */ - function pquery($sql, $params, $dieOnError=false, $msg='') { + public function pquery($sql, $params, $dieOnError = false, $msg = '') { global $log, $default_charset; - if (!isset($params)) $params = array(); + if (!isset($params)) { + $params = array(); + } // Performance Tuning: Have we cached the result earlier? - if($this->isCacheEnabled()) { + if ($this->isCacheEnabled()) { $fromcache = $this->getCacheInstance()->getCacheResult($sql, $params); - if($fromcache) { + if ($fromcache) { $log->debug("Using query result from cache: $sql"); return $fromcache; } @@ -442,7 +488,7 @@ function pquery($sql, $params, $dieOnError=false, $msg='') { $log->debug('Prepared sql query parameters : [' . implode(",", $params) . ']'); } - if($this->avoidPreparedSql || empty($params)) { + if ($this->avoidPreparedSql || empty($params)) { $sql = $this->convert2Sql($sql, $params); $result = $this->database->Execute($sql); } else { @@ -452,10 +498,12 @@ function pquery($sql, $params, $dieOnError=false, $msg='') { $this->logSqlTiming($sql_start_time, $sql_end_time, $sql, $params); $this->lastmysqlrow = -1; - if(!$result)$this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError); + if (!$result) { + $this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError); + } // Performance Tuning: Cache the query result - if($this->isCacheEnabled()) { + if ($this->isCacheEnabled()) { $this->getCacheInstance()->cacheResult($result, $sql, $params); } return $result; @@ -467,11 +515,15 @@ function pquery($sql, $params, $dieOnError=false, $msg='') { * $input = array(10, 20, array(30, 40), array('key1' => '50', 'key2'=>array(60), 70)); * returns array(10, 20, 30, 40, 50, 60, 70); */ - function flatten_array($input, $output=null) { - if($input == null) return null; - if($output == null) $output = array(); - foreach($input as $value) { - if(is_array($value)) { + public function flatten_array($input, $output = null) { + if ($input == null) { + return null; + } + if ($output == null) { + $output = array(); + } + foreach ($input as $value) { + if (is_array($value)) { $output = $this->flatten_array($value, $output); } else { $output[] = $value; @@ -480,12 +532,14 @@ function flatten_array($input, $output=null) { return $output; } - function getEmptyBlob($is_string=true) { - if ($is_string) return 'null'; + public function getEmptyBlob($is_string = true) { + if ($is_string) { + return 'null'; + } return null; } - function updateBlob($tablename, $colname, $id, $data) { + public function updateBlob($tablename, $colname, $id, $data) { $this->println("updateBlob t=".$tablename." c=".$colname." id=".$id); $this->checkConnection(); $this->executeSetNamesUTF8SQL(); @@ -496,9 +550,9 @@ function updateBlob($tablename, $colname, $id, $data) { $this->println("updateBlob t=".$tablename." c=".$colname." id=".$id." status=".$result); return $result; - } + } - function updateBlobFile($tablename, $colname, $id, $filename) { + public function updateBlobFile($tablename, $colname, $id, $filename) { $this->println("updateBlobFile t=".$tablename." c=".$colname." id=".$id." f=".$filename); $this->checkConnection(); $this->executeSetNamesUTF8SQL(); @@ -511,7 +565,7 @@ function updateBlobFile($tablename, $colname, $id, $filename) { return $result; } - function limitQuery($sql,$start,$count, $dieOnError=false, $msg='') { + public function limitQuery($sql, $start, $count, $dieOnError = false, $msg = '') { global $log; $log->debug(' limitQuery sql = '.$sql .' st = '.$start .' co = '.$count); $this->checkConnection(); @@ -519,28 +573,32 @@ function limitQuery($sql,$start,$count, $dieOnError=false, $msg='') { $this->executeSetNamesUTF8SQL(); $sql_start_time = microtime(true); - $result = $this->database->SelectLimit($sql,$count,$start); + $result = $this->database->SelectLimit($sql, $count, $start); $this->logSqlTiming($sql_start_time, microtime(true), "$sql LIMIT $count, $start"); - if(!$result) $this->checkError($msg.' Limit Query Failed:' . $sql . '::', $dieOnError); + if (!$result) { + $this->checkError($msg.' Limit Query Failed:' . $sql . '::', $dieOnError); + } return $result; } - function getOne($sql, $dieOnError=false, $msg='') { + public function getOne($sql, $dieOnError = false, $msg = '') { $this->println("ADODB getOne sql=".$sql); $this->checkConnection(); $this->executeSetNamesUTF8SQL(); $sql_start_time = microtime(true); $result = $this->database->GetOne($sql); $this->logSqlTiming($sql_start_time, microtime(true), "$sql GetONE"); - if(!$result) $this->checkError($msg.' Get one Query Failed:' . $sql . '::', $dieOnError); + if (!$result) { + $this->checkError($msg.' Get one Query Failed:' . $sql . '::', $dieOnError); + } return $result; } - function getFieldsDefinition(&$result) { + public function getFieldsDefinition(&$result) { //$this->println("ADODB getFieldsArray"); $field_array = array(); - if(!isset($result) || empty($result)) { + if (!isset($result) || empty($result)) { return 0; } @@ -559,10 +617,10 @@ function getFieldsDefinition(&$result) { return $field_array; } - function getFieldsArray(&$result) { + public function getFieldsArray(&$result) { //$this->println("ADODB getFieldsArray"); $field_array = array(); - if(! isset($result) || empty($result)) { + if (! isset($result) || empty($result)) { return 0; } @@ -581,7 +639,7 @@ function getFieldsArray(&$result) { return $field_array; } - function getRowCount(&$result){ + public function getRowCount(&$result) { global $log; if (isset($result) && !empty($result)) { $rows = $result->RecordCount(); @@ -592,152 +650,170 @@ function getRowCount(&$result){ } /* ADODB newly added. replacement for mysql_num_rows */ - function num_rows(&$result) { + public function num_rows(&$result) { return $this->getRowCount($result); } /* ADODB newly added. replacement form mysql_num_fields */ - function num_fields(&$result) { + public function num_fields(&$result) { return $result->FieldCount(); } /* ADODB newly added. replacement for mysql_fetch_array() */ - function fetch_array(&$result) { - if($result->EOF) { + public function fetch_array(&$result) { + if ($result->EOF) { //$this->println("ADODB fetch_array return null"); - return NULL; + return null; } $arr = $result->FetchRow(); - if(is_array($arr)) + if (is_array($arr)) { $arr = array_map('to_html', $arr); + } return $this->change_key_case($arr); } // adds new functions to the PearDatabase class to come around the whole broken query_result() idea // Code-Contribution given by weigelt@metux.de - Starts - function run_query_record_html($query) { - if (!is_array($rec = $this->run_query_record($query))) + public function run_query_record_html($query) { + if (!is_array($rec = $this->run_query_record($query))) { return $rec; - foreach ($rec as $walk => $cur) + } + foreach ($rec as $walk => $cur) { $r[$walk] = to_html($cur); + } return $r; } - function sql_quote($data) { + public function sql_quote($data) { if (is_array($data)) { - switch($data{'type'}) { - case 'text': - case 'numeric': - case 'integer': - case 'oid': - return $this->quote($data{'value'}); + switch ($data{'type'}) { + case 'text': + case 'numeric': + case 'integer': + case 'oid': + return $this->quote($data{'value'}); break; - case 'timestamp': - return $this->formatDate($data{'value'}); + case 'timestamp': + return $this->formatDate($data{'value'}); break; - default: - throw new Exception("unhandled type: ".serialize($cur)); + default: + throw new Exception("unhandled type: ".serialize($cur)); } - } else + } else { return $this->quote($data); + } } - function sql_insert_data($table, $data) { - if (!$table) - throw new Exception("missing table name"); - if (!is_array($data)) - throw new Exception("data must be an array"); - if (!count($table)) - throw new Exception("no data given"); - + public function sql_insert_data($table, $data) { + if (!$table) { + throw new Exception('missing table name'); + } + if (!is_array($data)) { + throw new Exception('data must be an array'); + } + if (!count($table)) { + throw new Exception('no data given'); + } $sql_fields = ''; $sql_data = ''; - foreach($data as $walk => $cur) { + foreach ($data as $walk => $cur) { $sql_fields .= ($sql_fields?',':'').$walk; $sql_data .= ($sql_data?',':'').$this->sql_quote($cur); } return 'INSERT INTO '.$table.' ('.$sql_fields.') VALUES ('.$sql_data.')'; } - function run_insert_data($table,$data) { - $query = $this->sql_insert_data($table,$data); + public function run_insert_data($table, $data) { + $query = $this->sql_insert_data($table, $data); $res = $this->query($query); $this->query("commit;"); } - function run_query_record($query) { + public function run_query_record($query) { $result = $this->query($query); - if (!$result) + if (!$result) { return; - if (!is_object($result)) + } + if (!is_object($result)) { throw new Exception("query \"$query\" failed: ".serialize($result)); + } $res = $result->FetchRow(); return $this->change_key_case($res); } - function run_query_allrecords($query) { + public function run_query_allrecords($query) { $result = $this->query($query); $records = array(); $sz = $this->num_rows($result); - for ($i=0; $i<$sz; $i++) + for ($i=0; $i<$sz; $i++) { $records[$i] = $this->change_key_case($result->FetchRow()); + } return $records; } - function run_query_field($query,$field='') { + public function run_query_field($query, $field = '') { $rowdata = $this->run_query_record($query); - if(isset($field) && $field != '') + if (isset($field) && $field != '') { return $rowdata{$field}; - else + } else { return array_shift($rowdata); + } } - function run_query_list($query,$field){ + public function run_query_list($query, $field) { $records = $this->run_query_allrecords($query); - foreach($records as $walk => $cur) + foreach ($records as $walk => $cur) { $list[] = $cur{$field}; + } } - function run_query_field_html($query,$field){ - return to_html($this->run_query_field($query,$field)); + public function run_query_field_html($query, $field) { + return to_html($this->run_query_field($query, $field)); } - function result_get_next_record($result){ + public function result_get_next_record($result) { return $this->change_key_case($result->FetchRow()); } // create an IN expression from an array/list - function sql_expr_datalist($a) { - if (!is_array($a)) + public function sql_expr_datalist($a) { + if (!is_array($a)) { throw new Exception("not an array"); - if (!count($a)) + } + if (!count($a)) { throw new Exception("empty arrays not allowed"); + } $l = ''; - foreach($a as $walk => $cur) + foreach ($a as $walk => $cur) { $l .= ($l?',':'').$this->quote($cur); + } return ' ( '.$l.' ) '; } // create an IN expression from an record list, take $field within each record - function sql_expr_datalist_from_records($a,$field) { - if (!is_array($a)) + public function sql_expr_datalist_from_records($a, $field) { + if (!is_array($a)) { throw new Exception("not an array"); - if (!$field) + } + if (!$field) { throw new Exception("missing field"); - if (!count($a)) + } + if (!count($a)) { throw new Exception("empty arrays not allowed"); - foreach($a as $walk => $cur) + } + foreach ($a as $walk => $cur) { $l .= ($l?',':'').$this->quote($cur{$field}); + } return ' ( '.$l.' ) '; } - function sql_concat($list) { + public function sql_concat($list) { switch ($this->dbType) { case 'mysql': case 'mysqli': - return 'concat('.implode(',',$list).')'; + return 'concat('.implode(',', $list).')'; case 'pgsql': - return '('.implode('||',$list).')'; + return '('.implode('||', $list).')'; default: throw new Exception("unsupported dbtype \"".$this->dbType."\""); } @@ -745,30 +821,34 @@ function sql_concat($list) { // Code-Contribution given by weigelt@metux.de - Ends /* ADODB newly added. replacement for mysql_result() */ - function query_result(&$result, $row, $col=0) { - if (!is_object($result)) + public function query_result(&$result, $row, $col = 0) { + if (!is_object($result)) { throw new Exception("result is not an object"); + } $result->Move($row); $rowdata = $this->change_key_case($result->FetchRow()); //$this->println($rowdata); //Commented strip_selected_tags and added to_html function for HTML tags vulnerability - if ($col == 'fieldlabel') + if ($col == 'fieldlabel') { $coldata = $rowdata[$col]; - else + } else { $coldata = (isset($rowdata[$col]) ? to_html($rowdata[$col]) : ''); + } return $coldata; } // Function to get particular row from the query result - function query_result_rowdata(&$result, $row=0) { - if (!is_object($result)) + public function query_result_rowdata(&$result, $row = 0) { + if (!is_object($result)) { throw new Exception("result is not an object"); + } $result->Move($row); $rowdata = $this->change_key_case($result->FetchRow()); - foreach($rowdata as $col => $coldata) { - if($col != 'fieldlabel') + foreach ($rowdata as $col => $coldata) { + if ($col != 'fieldlabel') { $rowdata[$col] = to_html($coldata); + } } return $rowdata; } @@ -785,14 +865,15 @@ function query_result_rowdata(&$result, $row=0) { * @param $row The row number to fetch. It's default value is 0 * */ - function raw_query_result_rowdata(&$result, $row=0) { - if (!is_object($result)) + public function raw_query_result_rowdata(&$result, $row = 0) { + if (!is_object($result)) { throw new Exception("result is not an object"); + } $result->Move($row); return $this->change_key_case($result->FetchRow()); } - function getAffectedRowCount(&$result){ + public function getAffectedRowCount(&$result) { global $log; $log->debug('getAffectedRowCount'); $rows =$this->database->Affected_Rows(); @@ -800,39 +881,42 @@ function getAffectedRowCount(&$result){ return $rows; } - function requireSingleResult($sql, $dieOnError=false,$msg='', $encode=true) { + public function requireSingleResult($sql, $dieOnError = false, $msg = '', $encode = true) { $result = $this->query($sql, $dieOnError, $msg); - if($this->getRowCount($result ) == 1) + if ($this->getRowCount($result) == 1) { return $result; + } $this->log->error('Rows Returned:'. $this->getRowCount($result) .' More than 1 row returned for '. $sql); return ''; } /* function which extends requireSingleResult api to execute prepared statment */ - function requirePsSingleResult($sql, $params, $dieOnError=false,$msg='', $encode=true) { + public function requirePsSingleResult($sql, $params, $dieOnError = false, $msg = '', $encode = true) { $result = $this->pquery($sql, $params, $dieOnError, $msg); - if($this->getRowCount($result ) == 1) + if ($this->getRowCount($result) == 1) { return $result; + } $this->log->error('Rows Returned:'. $this->getRowCount($result) .' More than 1 row returned for '. $sql); return ''; } - function fetchByAssoc(&$result, $rowNum = -1, $encode=true) { - if($result->EOF) { + public function fetchByAssoc(&$result, $rowNum = -1, $encode = true) { + if ($result->EOF) { $this->println("ADODB fetchByAssoc return null"); - return NULL; + return null; } - if(isset($result) && $rowNum < 0) { + if (isset($result) && $rowNum < 0) { $row = $this->change_key_case($result->GetRowAssoc(false)); $result->MoveNext(); - if($encode && is_array($row)) + if ($encode && is_array($row)) { return array_map('to_html', $row); + } return $row; } - if($this->getRowCount($result) > $rowNum) { + if ($this->getRowCount($result) > $rowNum) { $result->Move($rowNum); } $this->lastmysqlrow = $rowNum; @@ -840,44 +924,46 @@ function fetchByAssoc(&$result, $rowNum = -1, $encode=true) { $result->MoveNext(); $this->println($row); - if($encode && is_array($row)) + if ($encode && is_array($row)) { return array_map('to_html', $row); + } return $row; } - function getNextRow(&$result, $encode=true){ + public function getNextRow(&$result, $encode = true) { global $log; $log->info('getNextRow'); - if(isset($result)){ + if (isset($result)) { $row = $this->change_key_case($result->FetchRow()); - if($row && $encode && is_array($row)) + if ($row && $encode && is_array($row)) { return array_map('to_html', $row); + } return $row; } return null; } - function fetch_row(&$result, $encode=true) { + public function fetch_row(&$result, $encode = true) { return $this->getNextRow($result); } - function field_name(&$result, $col) { + public function field_name(&$result, $col) { return $result->FetchField($col); } - function getQueryTime(){ + public function getQueryTime() { return $this->query_time; } - function connect($dieOnError = false) { + public function connect($dieOnError = false) { global $dbconfig; - if(!isset($this->dbType)) { + if (!isset($this->dbType)) { $this->println("ADODB Connect : DBType not specified"); return; } $this->database = ADONewConnection($this->dbType); - if (isset($dbconfig['persistent']) and $dbconfig['persistent']) { + if (isset($dbconfig['persistent']) && $dbconfig['persistent']) { $this->database->PConnect($this->dbHostName, $this->userName, $this->userPassword, $this->dbName); } else { $this->database->Connect($this->dbHostName, $this->userName, $this->userPassword, $this->dbName); @@ -887,7 +973,7 @@ function connect($dieOnError = false) { // 'SET NAMES UTF8' needs to be executed even if database has default CHARSET UTF8 // as mysql server might be running with different charset! // We will notice problem reading UTF8 characters otherwise. - if($this->isdb_default_utf8_charset) { + if ($this->isdb_default_utf8_charset) { $this->executeSetNamesUTF8SQL(true); } } @@ -895,56 +981,57 @@ function connect($dieOnError = false) { /** * Constructor */ - function __construct($dbtype='',$host='',$dbname='',$username='',$passwd='') { + public function __construct($dbtype = '', $host = '', $dbname = '', $username = '', $passwd = '') { global $currentModule; $this->log = LoggerManager::getLogger('PearDatabase_'. $currentModule); - $this->resetSettings($dbtype,$host,$dbname,$username,$passwd); + $this->resetSettings($dbtype, $host, $dbname, $username, $passwd); // Initialize performance parameters $this->enableCache = PerformancePrefs::getBoolean('CACHE_QUERY_RESULT', false); - if(!isset($this->dbType)) { + if (!isset($this->dbType)) { $this->println("ADODB Connect : DBType not specified"); return; } // Initialize the cache object to use. - if(isset($this->enableCache) && $this->enableCache) { + if (isset($this->enableCache) && $this->enableCache) { $this->__setCacheInstance(new PearDatabaseCache($this)); } } - function resetSettings($dbtype,$host,$dbname,$username,$passwd){ + public function resetSettings($dbtype, $host, $dbname, $username, $passwd) { global $dbconfig; - if($host == '') { + if ($host == '') { $this->disconnect(); $this->setDatabaseType($dbconfig['db_type']); $this->setUserName($dbconfig['db_username']); $this->setUserPassword($dbconfig['db_password']); - $this->setDatabaseHost( $dbconfig['db_hostname']); + $this->setDatabaseHost($dbconfig['db_hostname']); $this->setDatabaseName($dbconfig['db_name']); - if($dbconfig['log_sql']) + if ($dbconfig['log_sql']) { $this->enableSQLlog = ($dbconfig['log_sql'] == true); + } } else { $this->disconnect(); $this->setDatabaseType($dbtype); $this->setDatabaseName($dbname); $this->setUserName($username); $this->setUserPassword($passwd); - $this->setDatabaseHost( $host); + $this->setDatabaseHost($host); } } - function quote($string){ + public function quote($string) { return $this->database->qstr($string); } - function disconnect() { + public function disconnect() { $this->println("ADODB disconnect"); - if(isset($this->database)){ - if($this->dbType == "mysql"){ + if (isset($this->database)) { + if ($this->dbType == "mysql") { mysql_close($this->database); - } elseif($this->dbType == 'mysqli'){ + } elseif ($this->dbType == 'mysqli') { mysqli_close($this->database); } else { $this->database->disconnect(); @@ -953,37 +1040,49 @@ function disconnect() { } } - function setDebug($value) { + public function setDebug($value) { $this->database->debug = $value; } // ADODB newly added methods - function createTables($schemaFile, $dbHostName=false, $userName=false, $userPassword=false, $dbName=false, $dbType=false) { + public function createTables($schemaFile, $dbHostName = false, $userName = false, $userPassword = false, $dbName = false, $dbType = false) { $this->println("ADODB createTables ".$schemaFile); - if($dbHostName!=false) $this->dbHostName=$dbHostName; - if($userName!=false) $this->userName=$userPassword; - if($userPassword!=false) $this->userPassword=$userPassword; - if($dbName!=false) $this->dbName=$dbName; - if($dbType!=false) $this->dbType=$dbType; + if ($dbHostName!=false) { + $this->dbHostName=$dbHostName; + } + if ($userName!=false) { + $this->userName=$userPassword; + } + if ($userPassword!=false) { + $this->userPassword=$userPassword; + } + if ($dbName!=false) { + $this->dbName=$dbName; + } + if ($dbType!=false) { + $this->dbType=$dbType; + } $this->checkConnection(); $db = $this->database; - $schema = new adoSchema( $db ); + $schema = new adoSchema($db); //Debug Adodb XML Schema - $schema->XMLS_DEBUG = TRUE; + $schema->XMLS_DEBUG = true; //Debug Adodb $schema->debug = true; - $sql = $schema->ParseSchema( $schemaFile ); + $sql = $schema->ParseSchema($schemaFile); $this->println("--------------Starting the table creation------------------"); - $result = $schema->ExecuteSchema( $sql, $this->continueInstallOnError ); - if($result) print $db->errorMsg(); + $result = $schema->ExecuteSchema($sql, $this->continueInstallOnError); + if ($result) { + print $db->errorMsg(); + } // needs to return in a decent way $this->println("ADODB createTables ".$schemaFile." status=".$result); return $result; } - function createTable($tablename, $flds) { + public function createTable($tablename, $flds) { $this->println("ADODB createTable table=".$tablename." flds=".$flds); $this->checkConnection(); $dict = NewDataDictionary($this->database); @@ -993,14 +1092,14 @@ function createTable($tablename, $flds) { return $result; } - function alterTable($tablename, $flds, $oper) { + public function alterTable($tablename, $flds, $oper) { $this->println("ADODB alterTableTable table=".$tablename." flds=".$flds." oper=".$oper); $this->checkConnection(); $dict = NewDataDictionary($this->database); - if($oper == 'Add_Column') { + if ($oper == 'Add_Column') { $sqlarray = $dict->AddColumnSQL($tablename, $flds); - } else if($oper == 'Delete_Column') { + } elseif ($oper == 'Delete_Column') { $sqlarray = $dict->DropColumnSQL($tablename, $flds); } $this->println("sqlarray"); @@ -1012,29 +1111,32 @@ function alterTable($tablename, $flds, $oper) { return $result; } - function getColumnNames($tablename) { + public function getColumnNames($tablename) { $this->println("ADODB getColumnNames table=".$tablename); $this->checkConnection(); $adoflds = $this->database->MetaColumns($tablename); $i=0; $colNames = array(); - if (is_array($adoflds)) - foreach($adoflds as $fld) { - $colNames[$i] = $fld->name; - $i++; + if (is_array($adoflds)) { + foreach ($adoflds as $fld) { + $colNames[$i] = $fld->name; + $i++; + } } return $colNames; } - function formatString($tablename,$fldname, $str) { + public function formatString($tablename, $fldname, $str) { $this->checkConnection(); $adoflds = $this->database->MetaColumns($tablename); - foreach ( $adoflds as $fld ) { - if(strcasecmp($fld->name,$fldname)==0) { + foreach ($adoflds as $fld) { + if (strcasecmp($fld->name, $fldname)==0) { $fldtype =strtoupper($fld->type); - if(strcmp($fldtype,'CHAR')==0 || strcmp($fldtype,'VARCHAR') == 0 || strcmp($fldtype,'VARCHAR2') == 0 || strcmp($fldtype,'LONGTEXT')==0 || strcmp($fldtype,'TEXT')==0) { + if (strcmp($fldtype, 'CHAR')==0 || strcmp($fldtype, 'VARCHAR') == 0 || strcmp($fldtype, 'VARCHAR2') == 0 || + strcmp($fldtype, 'LONGTEXT')==0 || strcmp($fldtype, 'TEXT')==0 + ) { return $this->database->Quote($str); - } else if(strcmp($fldtype,'DATE') ==0 || strcmp($fldtype,'TIMESTAMP')==0) { + } elseif (strcmp($fldtype, 'DATE') ==0 || strcmp($fldtype, 'TIMESTAMP')==0) { return $this->formatDate($str); } else { return $str; @@ -1045,29 +1147,29 @@ function formatString($tablename,$fldname, $str) { return $str; } - function formatDate($datetime, $strip_quotes=false) { + public function formatDate($datetime, $strip_quotes = false) { $this->checkConnection(); $db = $this->database; $date = $db->DBTimeStamp($datetime); /* Asha: Stripping single quotes to use the date as parameter for Prepared statement */ - if($strip_quotes == true) { + if ($strip_quotes == true) { return trim($date, "'"); } return $date; } - function getDBDateString($datecolname) { + public function getDBDateString($datecolname) { $this->checkConnection(); $db = $this->database; - return $db->SQLDate('Y-m-d, H:i:s' ,$datecolname); + return $db->SQLDate('Y-m-d, H:i:s', $datecolname); } - function getUniqueID($seqname) { + public function getUniqueID($seqname) { $this->checkConnection(); - return $this->database->GenID($seqname."_seq",1); + return $this->database->GenID($seqname."_seq", 1); } - function get_tables() { + public function get_tables() { $this->checkConnection(); $result = $this->database->MetaTables('TABLES'); $this->println($result); @@ -1075,20 +1177,20 @@ function get_tables() { } //To get a function name with respect to the database type which escapes strings in given text - function sql_escape_string($str) - { - if (is_null($str)) return 'NULL'; + public function sql_escape_string($str) { + if (is_null($str)) { + return 'NULL'; + } $result_data = $this->database->qstr($str); $result_data = substr($result_data, 1, -1); return $result_data; } // Function to get the last insert id based on the type of database - function getLastInsertID($seqname = '') { - if($this->isPostgres()) { + public function getLastInsertID($seqname = '') { + if ($this->isPostgres()) { $result = pg_query("SELECT currval('".$seqname."_seq')"); - if($result) - { + if ($result) { $row = pg_fetch_row($result); $last_insert_id = $row[0]; } @@ -1099,19 +1201,20 @@ function getLastInsertID($seqname = '') { } // Function to escape the special characters in database name based on database type. - function escapeDbName($dbName='') { - if ($dbName == '') $dbName = $this->dbName; - if($this->isMySql()) { + public function escapeDbName($dbName = '') { + if ($dbName == '') { + $dbName = $this->dbName; + } + if ($this->isMySql()) { $dbName = "`{$dbName}`"; } return $dbName; } } /* End of class */ -if(empty($adb)) { +if (empty($adb)) { $adb = new PearDatabase(); $adb->connect(); } //$adb->database->setFetchMode(ADODB_FETCH_NUM); - ?> \ No newline at end of file