issue 2119. supporting php 5.4. db classes.

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12686 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2013-02-04 09:36:46 +00:00
parent 7fe03148f0
commit 41fdaf00c3
29 changed files with 1846 additions and 798 deletions

View file

@ -1,4 +1,5 @@
<?php <?php
if(!defined('__XE_LOADED_DB_CLASS__')) if(!defined('__XE_LOADED_DB_CLASS__'))
{ {
define('__XE_LOADED_DB_CLASS__', 1); define('__XE_LOADED_DB_CLASS__', 1);
@ -47,6 +48,7 @@ if(!defined('__XE_LOADED_DB_CLASS__'))
*/ */
class DB class DB
{ {
/** /**
* priority of DBMS * priority of DBMS
* @var array * @var array
@ -85,12 +87,12 @@ class DB
* @var array * @var array
*/ */
var $master_db = NULL; var $master_db = NULL;
/** /**
* array of slave databases connection strings * array of slave databases connection strings
* @var array * @var array
*/ */
var $slave_db = NULL; var $slave_db = NULL;
var $result = NULL; var $result = NULL;
/** /**
@ -98,22 +100,26 @@ class DB
* @var int * @var int
*/ */
var $errno = 0; var $errno = 0;
/** /**
* error message * error message
* @var string * @var string
*/ */
var $errstr = ''; var $errstr = '';
/** /**
* query string of latest executed query * query string of latest executed query
* @var string * @var string
*/ */
var $query = ''; var $query = '';
var $connection = ''; var $connection = '';
/** /**
* elapsed time of latest executed query * elapsed time of latest executed query
* @var int * @var int
*/ */
var $elapsed_time = 0; var $elapsed_time = 0;
/** /**
* elapsed time of latest executed DB class * elapsed time of latest executed DB class
* @var int * @var int
@ -125,7 +131,6 @@ class DB
* @var boolean * @var boolean
*/ */
var $transaction_started = FALSE; var $transaction_started = FALSE;
var $is_connected = FALSE; var $is_connected = FALSE;
/** /**
@ -160,15 +165,27 @@ class DB
*/ */
function &getInstance($db_type = NULL) function &getInstance($db_type = NULL)
{ {
if(!$db_type) $db_type = Context::getDBType(); if(!$db_type)
if(!$db_type && Context::isInstalled()) return new Object(-1, 'msg_db_not_setted'); {
$db_type = Context::getDBType();
}
if(!$db_type && Context::isInstalled())
{
return new Object(-1, 'msg_db_not_setted');
}
if(!isset($GLOBALS['__DB__'])) $GLOBALS['__DB__'] = array(); if(!isset($GLOBALS['__DB__']))
{
$GLOBALS['__DB__'] = array();
}
if(!isset($GLOBALS['__DB__'][$db_type])) if(!isset($GLOBALS['__DB__'][$db_type]))
{ {
$class_name = 'DB' . ucfirst($db_type); $class_name = 'DB' . ucfirst($db_type);
$class_file = _XE_PATH_ . "classes/db/$class_name.class.php"; $class_file = _XE_PATH_ . "classes/db/$class_name.class.php";
if(!file_exists($class_file)) return new Object(-1, 'msg_db_not_setted'); if(!file_exists($class_file))
{
return new Object(-1, 'msg_db_not_setted');
}
// get a singletone instance of the database driver class // get a singletone instance of the database driver class
require_once($class_file); require_once($class_file);
@ -227,7 +244,12 @@ class DB
if(is_array($this->supported_list)) if(is_array($this->supported_list))
{ {
foreach($this->supported_list AS $key => $value) foreach($this->supported_list AS $key => $value)
if($value->enable) array_push($enableList, $value); {
if($value->enable)
{
array_push($enableList, $value);
}
}
} }
return $enableList; return $enableList;
} }
@ -249,7 +271,12 @@ class DB
if(is_array($this->supported_list)) if(is_array($this->supported_list))
{ {
foreach($this->supported_list AS $key => $value) foreach($this->supported_list AS $key => $value)
if(!$value->enable) array_push($disableList, $value); {
if(!$value->enable)
{
array_push($disableList, $value);
}
}
} }
return $disableList; return $disableList;
} }
@ -277,18 +304,27 @@ class DB
{ {
$db_type = $supported_list[$i]; $db_type = $supported_list[$i];
if(version_compare(phpversion(), '5.0') < 0 && preg_match('/pdo/i',$db_type)) continue; if(version_compare(phpversion(), '5.0') < 0 && preg_match('/pdo/i', $db_type))
{
continue;
}
$class_name = sprintf("DB%s%s", strtoupper(substr($db_type, 0, 1)), strtolower(substr($db_type, 1))); $class_name = sprintf("DB%s%s", strtoupper(substr($db_type, 0, 1)), strtolower(substr($db_type, 1)));
$class_file = sprintf(_XE_PATH_ . "classes/db/%s.class.php", $class_name); $class_file = sprintf(_XE_PATH_ . "classes/db/%s.class.php", $class_name);
if(!file_exists($class_file)) continue; if(!file_exists($class_file))
{
continue;
}
unset($oDB); unset($oDB);
require_once($class_file); require_once($class_file);
$tmp_fn = create_function('', "return new {$class_name}();"); $tmp_fn = create_function('', "return new {$class_name}();");
$oDB = $tmp_fn(); $oDB = $tmp_fn();
if(!$oDB) continue; if(!$oDB)
{
continue;
}
$obj = NULL; $obj = NULL;
$obj->db_type = $db_type; $obj->db_type = $db_type;
@ -353,8 +389,14 @@ class DB
*/ */
function isConnected($type = 'master', $indx = 0) function isConnected($type = 'master', $indx = 0)
{ {
if($type == 'master') return $this->master_db["is_connected"] ? TRUE : FALSE; if($type == 'master')
else return $this->slave_db[$indx]["is_connected"] ? TRUE : FALSE; {
return $this->master_db["is_connected"] ? TRUE : FALSE;
}
else
{
return $this->slave_db[$indx]["is_connected"] ? TRUE : FALSE;
}
} }
/** /**
@ -376,7 +418,10 @@ class DB
*/ */
function actFinish() function actFinish()
{ {
if(!$this->query) return; if(!$this->query)
{
return;
}
$this->act_finish = getMicroTime(); $this->act_finish = getMicroTime();
$elapsed_time = $this->act_finish - $this->act_start; $elapsed_time = $this->act_finish - $this->act_start;
$this->elapsed_time = $elapsed_time; $this->elapsed_time = $elapsed_time;
@ -402,10 +447,16 @@ class DB
{ {
$debug_file = _XE_PATH_ . "files/_debug_db_query.php"; $debug_file = _XE_PATH_ . "files/_debug_db_query.php";
$buff = array(); $buff = array();
if(!file_exists($debug_file)) $buff[] = '<?php exit(); ?>'; if(!file_exists($debug_file))
{
$buff[] = '<?php exit(); ?>';
}
$buff[] = print_r($log, TRUE); $buff[] = print_r($log, TRUE);
if(@!$fp = fopen($debug_file, "a")) return; if(@!$fp = fopen($debug_file, "a"))
{
return;
}
fwrite($fp, implode("\n", $buff) . "\n\n"); fwrite($fp, implode("\n", $buff) . "\n\n");
fclose($fp); fclose($fp);
} }
@ -479,8 +530,14 @@ class DB
{ {
static $cache_file = array(); static $cache_file = array();
if(!$query_id) return new Object(-1, 'msg_invalid_queryid'); if(!$query_id)
if(!$this->db_type) return; {
return new Object(-1, 'msg_invalid_queryid');
}
if(!$this->db_type)
{
return;
}
$this->actDBClassStart(); $this->actDBClassStart();
@ -530,7 +587,6 @@ class DB
return $result; return $result;
} }
/** /**
* Look for query cache file * Look for query cache file
* @param string $query_id query id for finding * @param string $query_id query id for finding
@ -542,8 +598,14 @@ class DB
// first try finding cache file // first try finding cache file
$cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type); $cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type);
if(file_exists($cache_file)) $cache_time = filemtime($cache_file); if(file_exists($cache_file))
else $cache_time = -1; {
$cache_time = filemtime($cache_file);
}
else
{
$cache_time = -1;
}
// if there is no cache file or is not new, find original xml query file and parse it // if there is no cache file or is not new, find original xml query file and parse it
if($cache_time < filemtime($xml_file) || $cache_time < filemtime(_XE_PATH_ . 'classes/db/DB.class.php') || $cache_time < filemtime(_XE_PATH_ . 'classes/xml/XmlQueryParser.150.class.php')) if($cache_time < filemtime($xml_file) || $cache_time < filemtime(_XE_PATH_ . 'classes/db/DB.class.php') || $cache_time < filemtime(_XE_PATH_ . 'classes/xml/XmlQueryParser.150.class.php'))
@ -556,7 +618,6 @@ class DB
return $cache_file; return $cache_file;
} }
/** /**
* Execute query and return the result * Execute query and return the result
* @param string $cache_file cache file of query * @param string $cache_file cache file of query
@ -569,13 +630,22 @@ class DB
{ {
global $lang; global $lang;
if(!file_exists($cache_file)) return new Object(-1, 'msg_invalid_queryid'); if(!file_exists($cache_file))
{
return new Object(-1, 'msg_invalid_queryid');
}
if($source_args) $args = @clone($source_args); if($source_args)
{
$args = clone $source_args;
}
$output = include($cache_file); $output = include($cache_file);
if( (is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool()) return $output; if((is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool())
{
return $output;
}
// execute appropriate query // execute appropriate query
switch($output->getAction()) switch($output->getAction())
@ -601,15 +671,20 @@ class DB
break; break;
} }
if($this->isError()) $output = $this->getError(); if($this->isError())
else if(!is_a($output, 'Object') && !is_subclass_of($output, 'Object')) $output = new Object(); {
$output = $this->getError();
}
else if(!is_a($output, 'Object') && !is_subclass_of($output, 'Object'))
{
$output = new Object();
}
$output->add('_query', $this->query); $output->add('_query', $this->query);
$output->add('_elapsed_time', sprintf("%0.5f", $this->elapsed_time)); $output->add('_elapsed_time', sprintf("%0.5f", $this->elapsed_time));
return $output; return $output;
} }
/** /**
* Returns counter cache data * Returns counter cache data
* @param array|string $tables tables to get data * @param array|string $tables tables to get data
@ -619,27 +694,51 @@ class DB
function getCountCache($tables, $condition) function getCountCache($tables, $condition)
{ {
return FALSE; return FALSE;
if(!$tables) return FALSE; if(!$tables)
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path); {
return FALSE;
}
if(!is_dir($this->count_cache_path))
{
return FileHandler::makeDir($this->count_cache_path);
}
$condition = md5($condition); $condition = md5($condition);
if(!is_array($tables)) $tables_str = $tables; if(!is_array($tables))
else $tables_str = implode('.',$tables); {
$tables_str = $tables;
}
else
{
$tables_str = implode('.', $tables);
}
$cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str); $cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
if(!is_dir($cache_path)) FileHandler::makeDir($cache_path); if(!is_dir($cache_path))
{
FileHandler::makeDir($cache_path);
}
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition); $cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
if(!file_exists($cache_filename)) return FALSE; if(!file_exists($cache_filename))
{
return FALSE;
}
$cache_mtime = filemtime($cache_filename); $cache_mtime = filemtime($cache_filename);
if(!is_array($tables)) $tables = array($tables); if(!is_array($tables))
{
$tables = array($tables);
}
foreach($tables as $alias => $table) foreach($tables as $alias => $table)
{ {
$table_filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table); $table_filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
if(!file_exists($table_filename) || filemtime($table_filename) > $cache_mtime) return FALSE; if(!file_exists($table_filename) || filemtime($table_filename) > $cache_mtime)
{
return FALSE;
}
} }
$count = (int) FileHandler::readFile($cache_filename); $count = (int) FileHandler::readFile($cache_filename);
@ -656,16 +755,31 @@ class DB
function putCountCache($tables, $condition, $count = 0) function putCountCache($tables, $condition, $count = 0)
{ {
return FALSE; return FALSE;
if(!$tables) return FALSE; if(!$tables)
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path); {
return FALSE;
}
if(!is_dir($this->count_cache_path))
{
return FileHandler::makeDir($this->count_cache_path);
}
$condition = md5($condition); $condition = md5($condition);
if(!is_array($tables)) $tables_str = $tables; if(!is_array($tables))
else $tables_str = implode('.',$tables); {
$tables_str = $tables;
}
else
{
$tables_str = implode('.', $tables);
}
$cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str); $cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
if(!is_dir($cache_path)) FileHandler::makeDir($cache_path); if(!is_dir($cache_path))
{
FileHandler::makeDir($cache_path);
}
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition); $cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
@ -680,10 +794,19 @@ class DB
function resetCountCache($tables) function resetCountCache($tables)
{ {
return FALSE; return FALSE;
if(!$tables) return FALSE; if(!$tables)
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path); {
return FALSE;
}
if(!is_dir($this->count_cache_path))
{
return FileHandler::makeDir($this->count_cache_path);
}
if(!is_array($tables)) $tables = array($tables); if(!is_array($tables))
{
$tables = array($tables);
}
foreach($tables as $alias => $table) foreach($tables as $alias => $table)
{ {
$filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table); $filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
@ -702,13 +825,34 @@ class DB
{ {
$result = array(); $result = array();
if(function_exists('mysql_connect')) $result[] = 'MySQL'; if(function_exists('mysql_connect'))
if(function_exists('cubrid_connect')) $result[] = 'Cubrid'; {
if(function_exists('ibase_connect')) $result[] = 'FireBird'; $result[] = 'MySQL';
if(function_exists('pg_connect')) $result[] = 'Postgre'; }
if(function_exists('sqlite_open')) $result[] = 'sqlite2'; if(function_exists('cubrid_connect'))
if(function_exists('mssql_connect')) $result[] = 'MSSQL'; {
if(function_exists('PDO')) $result[] = 'sqlite3(PDO)'; $result[] = 'Cubrid';
}
if(function_exists('ibase_connect'))
{
$result[] = 'FireBird';
}
if(function_exists('pg_connect'))
{
$result[] = 'Postgre';
}
if(function_exists('sqlite_open'))
{
$result[] = 'sqlite2';
}
if(function_exists('mssql_connect'))
{
$result[] = 'MSSQL';
}
if(function_exists('PDO'))
{
$result[] = 'sqlite3(PDO)';
}
return $result; return $result;
} }
@ -720,7 +864,10 @@ class DB
*/ */
function dropTable($table_name) function dropTable($table_name)
{ {
if(!$table_name) return; if(!$table_name)
{
return;
}
$query = sprintf("drop table %s%s", $this->prefix, $table_name); $query = sprintf("drop table %s%s", $this->prefix, $table_name);
$this->_query($query); $this->_query($query);
} }
@ -734,35 +881,57 @@ class DB
function getSelectSql($query, $with_values = TRUE) function getSelectSql($query, $with_values = TRUE)
{ {
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '')
{
return new Object(-1, "Invalid query");
}
$select = 'SELECT ' . $select; $select = 'SELECT ' . $select;
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$from = ' FROM ' . $from; $from = ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '')
{
$where = ' WHERE ' . $where;
}
$tableObjects = $query->getTables(); $tableObjects = $query->getTables();
$index_hint_list = ''; $index_hint_list = '';
foreach($tableObjects as $tableObject) foreach($tableObjects as $tableObject)
{ {
if(is_a($tableObject, 'CubridTableWithHint')) if(is_a($tableObject, 'CubridTableWithHint'))
{
$index_hint_list .= $tableObject->getIndexHintString() . ', '; $index_hint_list .= $tableObject->getIndexHintString() . ', ';
} }
}
$index_hint_list = substr($index_hint_list, 0, -2); $index_hint_list = substr($index_hint_list, 0, -2);
if($index_hint_list != '') if($index_hint_list != '')
{
$index_hint_list = 'USING INDEX ' . $index_hint_list; $index_hint_list = 'USING INDEX ' . $index_hint_list;
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '')
{
$groupBy = ' GROUP BY ' . $groupBy;
}
$orderBy = $query->getOrderByString(); $orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '')
{
$orderBy = ' ORDER BY ' . $orderBy;
}
$limit = $query->getLimitString(); $limit = $query->getLimitString();
if($limit != '') $limit = ' LIMIT ' . $limit; if($limit != '')
{
$limit = ' LIMIT ' . $limit;
}
return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
@ -814,11 +983,17 @@ class DB
$sql .= $tables[0]->getAlias(); $sql .= $tables[0]->getAlias();
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$sql .= ' FROM ' . $from; $sql .= ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $sql .= ' WHERE ' . $where; if($where != '')
{
$sql .= ' WHERE ' . $where;
}
return $sql; return $sql;
} }
@ -833,13 +1008,22 @@ class DB
function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE) function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE)
{ {
$columnsList = $query->getUpdateString($with_values); $columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query"); if($columnsList == '')
{
return new Object(-1, "Invalid query");
}
$tables = $query->getFromString($with_values); $tables = $query->getFromString($with_values);
if($tables == '') return new Object(-1, "Invalid query"); if($tables == '')
{
return new Object(-1, "Invalid query");
}
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '')
{
$where = ' WHERE ' . $where;
}
$priority = $with_priority ? $query->getPriority() : ''; $priority = $with_priority ? $query->getPriority() : '';
@ -884,16 +1068,22 @@ class DB
if($type == 'master') if($type == 'master')
{ {
if(!$this->master_db['is_connected']) if(!$this->master_db['is_connected'])
{
$this->_connect($type); $this->_connect($type);
}
$this->connection = 'Master ' . $this->master_db['db_hostname']; $this->connection = 'Master ' . $this->master_db['db_hostname'];
return $this->master_db["resource"]; return $this->master_db["resource"];
} }
if($indx === NULL) if($indx === NULL)
{
$indx = $this->_getSlaveConnectionStringIndex($type); $indx = $this->_getSlaveConnectionStringIndex($type);
}
if(!$this->slave_db[$indx]['is_connected']) if(!$this->slave_db[$indx]['is_connected'])
{
$this->_connect($type, $indx); $this->_connect($type, $indx);
}
$this->connection = 'Slave ' . $this->slave_db[$indx]['db_hostname']; $this->connection = 'Slave ' . $this->slave_db[$indx]['db_hostname'];
return $this->slave_db[$indx]["resource"]; return $this->slave_db[$indx]["resource"];
@ -906,9 +1096,13 @@ class DB
function _dbInfoExists() function _dbInfoExists()
{ {
if(!$this->master_db) if(!$this->master_db)
{
return FALSE; return FALSE;
}
if(count($this->slave_db) === 0) if(count($this->slave_db) === 0)
{
return FALSE; return FALSE;
}
return TRUE; return TRUE;
} }
@ -932,12 +1126,18 @@ class DB
function close($type = 'master', $indx = 0) function close($type = 'master', $indx = 0)
{ {
if(!$this->isConnected($type, $indx)) if(!$this->isConnected($type, $indx))
{
return; return;
}
if($type == 'master') if($type == 'master')
{
$connection = &$this->master_db; $connection = &$this->master_db;
}
else else
{
$connection = &$this->slave_db[$indx]; $connection = &$this->slave_db[$indx];
}
$this->_close($connection["resource"]); $this->_close($connection["resource"]);
@ -961,11 +1161,15 @@ class DB
function begin() function begin()
{ {
if(!$this->isConnected() || $this->transaction_started) if(!$this->isConnected() || $this->transaction_started)
{
return; return;
}
if($this->_begin()) if($this->_begin())
{
$this->transaction_started = TRUE; $this->transaction_started = TRUE;
} }
}
/** /**
* DB transaction rollback * DB transaction rollback
@ -984,10 +1188,14 @@ class DB
function rollback() function rollback()
{ {
if(!$this->isConnected() || !$this->transaction_started) if(!$this->isConnected() || !$this->transaction_started)
{
return; return;
}
if($this->_rollback()) if($this->_rollback())
{
$this->transaction_started = FALSE; $this->transaction_started = FALSE;
} }
}
/** /**
* DB transaction commit * DB transaction commit
@ -1007,10 +1215,14 @@ class DB
function commit($force = FALSE) function commit($force = FALSE)
{ {
if(!$force && (!$this->isConnected() || !$this->transaction_started)) if(!$force && (!$this->isConnected() || !$this->transaction_started))
{
return; return;
}
if($this->_commit()) if($this->_commit())
{
$this->transaction_started = FALSE; $this->transaction_started = FALSE;
} }
}
/** /**
* Execute the query * Execute the query
@ -1034,7 +1246,9 @@ class DB
function _query($query, $connection = NULL) function _query($query, $connection = NULL)
{ {
if($connection == NULL) if($connection == NULL)
{
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
}
// Notify to start a query execution // Notify to start a query execution
$this->actStart($query); $this->actStart($query);
@ -1062,9 +1276,13 @@ class DB
&& $db_info->master_db["db_password"] == $db_info->slave_db[0]["db_password"] && $db_info->master_db["db_password"] == $db_info->slave_db[0]["db_password"]
&& $db_info->master_db["db_database"] == $db_info->slave_db[0]["db_database"] && $db_info->master_db["db_database"] == $db_info->slave_db[0]["db_database"]
) )
{
$this->slave_db[0] = &$this->master_db; $this->slave_db[0] = &$this->master_db;
}
else else
{
$this->slave_db = $db_info->slave_db; $this->slave_db = $db_info->slave_db;
}
$this->prefix = $db_info->master_db["db_table_prefix"]; $this->prefix = $db_info->master_db["db_table_prefix"];
$this->use_prepared_statements = $db_info->use_prepared_statements; $this->use_prepared_statements = $db_info->use_prepared_statements;
} }
@ -1101,16 +1319,24 @@ class DB
function _connect($type = 'master', $indx = 0) function _connect($type = 'master', $indx = 0)
{ {
if($this->isConnected($type, $indx)) if($this->isConnected($type, $indx))
{
return; return;
}
// Ignore if no DB information exists // Ignore if no DB information exists
if(!$this->_dbInfoExists()) if(!$this->_dbInfoExists())
{
return; return;
}
if($type == 'master') if($type == 'master')
{
$connection = &$this->master_db; $connection = &$this->master_db;
}
else else
{
$connection = &$this->slave_db[$indx]; $connection = &$this->slave_db[$indx];
}
$result = $this->__connect($connection); $result = $this->__connect($connection);
if($result === NULL || $result === FALSE) if($result === NULL || $result === FALSE)
@ -1146,7 +1372,10 @@ class DB
*/ */
function actDBClassFinish() function actDBClassFinish()
{ {
if(!$this->query) return; if(!$this->query)
{
return;
}
$this->act_dbclass_finish = getMicroTime(); $this->act_dbclass_finish = getMicroTime();
$elapsed_dbclass_time = $this->act_dbclass_finish - $this->act_dbclass_start; $elapsed_dbclass_time = $this->act_dbclass_finish - $this->act_dbclass_start;
$this->elapsed_dbclass_time = $elapsed_dbclass_time; $this->elapsed_dbclass_time = $elapsed_dbclass_time;
@ -1176,6 +1405,5 @@ class DB
} }
} }
/* End of file DB.class.php */ /* End of file DB.class.php */
/* Location: ./classes/db/DB.class.php */ /* Location: ./classes/db/DB.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* - DB child class * - DB child class
* - Cubrid DBMS to use the class * - Cubrid DBMS to use the class
@ -10,11 +11,13 @@
*/ */
class DBCubrid extends DB class DBCubrid extends DB
{ {
/** /**
* prefix of XE tables(One more XE can be installed on a single DB) * prefix of XE tables(One more XE can be installed on a single DB)
* @var string * @var string
*/ */
var $prefix = 'xe_'; var $prefix = 'xe_';
/** /**
* max size of constant in CUBRID(if string is larger than this, '...'+'...' should be used) * max size of constant in CUBRID(if string is larger than this, '...'+'...' should be used)
* @var int * @var int
@ -67,7 +70,10 @@ class DBCubrid extends DB
*/ */
function isSupported() function isSupported()
{ {
if (!function_exists('cubrid_connect')) return FALSE; if(!function_exists('cubrid_connect'))
{
return FALSE;
}
return TRUE; return TRUE;
} }
@ -211,7 +217,10 @@ class DBCubrid extends DB
$value = $param->getUnescapedValue(); $value = $param->getUnescapedValue();
$type = $param->getType(); $type = $param->getType();
if($param->isColumnName()) continue; if($param->isColumnName())
{
continue;
}
switch($type) switch($type)
{ {
@ -225,7 +234,8 @@ class DBCubrid extends DB
$bind_type = 'string'; $bind_type = 'string';
} }
if(is_array($value)){ if(is_array($value))
{
foreach($value as $v) foreach($value as $v)
{ {
$bound = @cubrid_bind($req, ++$position, $v, $bind_type); $bound = @cubrid_bind($req, ++$position, $v, $bind_type);
@ -255,12 +265,12 @@ class DBCubrid extends DB
return false; return false;
} }
return $req; return $req;
} }
// Execute the query // Execute the query
$result = @cubrid_execute($connection, $query); $result = @cubrid_execute($connection, $query);
// error check // error check
if (!$result) { if(!$result)
{
$this->_setError(); $this->_setError();
return false; return false;
} }
@ -290,7 +300,10 @@ class DBCubrid extends DB
function _fetch($result, $arrayIndexEndValue = NULL) function _fetch($result, $arrayIndexEndValue = NULL)
{ {
$output = array(); $output = array();
if (!$this->isConnected() || $this->isError() || !$result) return array(); if(!$this->isConnected() || $this->isError() || !$result)
{
return array();
}
if($this->use_prepared_statements == 'Y') if($this->use_prepared_statements == 'Y')
{ {
@ -303,32 +316,53 @@ class DBCubrid extends DB
$col_names = cubrid_column_names($result); $col_names = cubrid_column_names($result);
$max = count($col_types); $max = count($col_types);
for ($count = 0; $count < $max; $count++) { for($count = 0; $count < $max; $count++)
if (preg_match ("/^char/", $col_types[$count]) > 0) { {
if(preg_match("/^char/", $col_types[$count]) > 0)
{
$char_type_fields[] = $col_names[$count]; $char_type_fields[] = $col_names[$count];
} }
} }
while ($tmp = cubrid_fetch ($result, CUBRID_OBJECT)) { while($tmp = cubrid_fetch($result, CUBRID_OBJECT))
if (is_array ($char_type_fields)) { {
foreach ($char_type_fields as $val) { if(is_array($char_type_fields))
{
foreach($char_type_fields as $val)
{
$tmp->{$val} = rtrim($tmp->{$val}); $tmp->{$val} = rtrim($tmp->{$val});
} }
} }
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp; if($arrayIndexEndValue)
else $output[] = $tmp; {
$output[$arrayIndexEndValue--] = $tmp;
}
else
{
$output[] = $tmp;
}
} }
unset($char_type_fields); unset($char_type_fields);
if ($result) cubrid_close_request($result); if($result)
{
cubrid_close_request($result);
}
if(count($output)==1){ if(count($output) == 1)
{
// If call is made for pagination, always return array // If call is made for pagination, always return array
if(isset($arrayIndexEndValue)) return $output; if(isset($arrayIndexEndValue))
{
return $output;
}
// Else return object instead of array // Else return object instead of array
else return $output[0]; else
{
return $output[0];
}
} }
return $output; return $output;
} }
@ -355,7 +389,8 @@ class DBCubrid extends DB
*/ */
function _makeSequence() function _makeSequence()
{ {
if($_GLOBALS['XE_EXISTS_SEQUENCE']) return; if($_GLOBALS['XE_EXISTS_SEQUENCE'])
return;
// check cubrid serial // check cubrid serial
$query = sprintf('select count(*) as "count" from "db_serial" where name=\'%ssequence\'', $this->prefix); $query = sprintf('select count(*) as "count" from "db_serial" where name=\'%ssequence\'', $this->prefix);
@ -363,7 +398,8 @@ class DBCubrid extends DB
$output = $this->_fetch($result); $output = $this->_fetch($result);
// if do not create serial // if do not create serial
if ($output->count == 0) { if($output->count == 0)
{
$query = sprintf('select max("a"."srl") as "srl" from ' . $query = sprintf('select max("a"."srl") as "srl" from ' .
'( select max("document_srl") as "srl" from ' . '( select max("document_srl") as "srl" from ' .
'"%sdocuments" UNION ' . '"%sdocuments" UNION ' .
@ -376,10 +412,12 @@ class DBCubrid extends DB
$result = $this->_query($query); $result = $this->_query($query);
$output = $this->_fetch($result); $output = $this->_fetch($result);
$srl = $output->srl; $srl = $output->srl;
if ($srl < 1) { if($srl < 1)
{
$start = 1; $start = 1;
} }
else { else
{
$start = $srl + 1000000; $start = $srl + 1000000;
} }
@ -391,7 +429,6 @@ class DBCubrid extends DB
$_GLOBALS['XE_EXISTS_SEQUENCE'] = TRUE; $_GLOBALS['XE_EXISTS_SEQUENCE'] = TRUE;
} }
/** /**
* Check a table exists status * Check a table exists status
* @param string $target_name * @param string $target_name
@ -399,22 +436,29 @@ class DBCubrid extends DB
*/ */
function isTableExists($target_name) function isTableExists($target_name)
{ {
if($target_name == 'sequence') { if($target_name == 'sequence')
{
$query = sprintf("select \"name\" from \"db_serial\" where \"name\" = '%s%s'", $this->prefix, $target_name); $query = sprintf("select \"name\" from \"db_serial\" where \"name\" = '%s%s'", $this->prefix, $target_name);
} }
else { else
{
$query = sprintf("select \"class_name\" from \"db_class\" where \"class_name\" = '%s%s'", $this->prefix, $target_name); $query = sprintf("select \"class_name\" from \"db_class\" where \"class_name\" = '%s%s'", $this->prefix, $target_name);
} }
$result = $this->_query($query); $result = $this->_query($query);
if (cubrid_num_rows($result) > 0) { if(cubrid_num_rows($result) > 0)
{
$output = TRUE; $output = TRUE;
} }
else { else
{
$output = FALSE; $output = FALSE;
} }
if ($result) cubrid_close_request ($result); if($result)
{
cubrid_close_request($result);
}
return $output; return $output;
} }
@ -432,31 +476,46 @@ class DBCubrid extends DB
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = FALSE) function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = FALSE)
{ {
$type = strtoupper($this->column_type[$type]); $type = strtoupper($this->column_type[$type]);
if ($type == 'INTEGER') $size = ''; if($type == 'INTEGER')
{
$size = '';
}
$query = sprintf("alter class \"%s%s\" add \"%s\" ", $this->prefix, $table_name, $column_name); $query = sprintf("alter class \"%s%s\" add \"%s\" ", $this->prefix, $table_name, $column_name);
if ($type == 'char' || $type == 'varchar') { if($type == 'char' || $type == 'varchar')
if ($size) $size = $size * 3; {
if($size)
{
$size = $size * 3;
}
} }
if ($size) { if($size)
{
$query .= sprintf("%s(%s) ", $type, $size); $query .= sprintf("%s(%s) ", $type, $size);
} }
else { else
{
$query .= sprintf("%s ", $type); $query .= sprintf("%s ", $type);
} }
if ($default) { if($default)
if ($type == 'INTEGER' || $type == 'BIGINT' || $type=='INT') { {
if($type == 'INTEGER' || $type == 'BIGINT' || $type == 'INT')
{
$query .= sprintf("default %d ", $default); $query .= sprintf("default %d ", $default);
} }
else { else
{
$query .= sprintf("default '%s' ", $default); $query .= sprintf("default '%s' ", $default);
} }
} }
if ($notnull) $query .= "not null "; if($notnull)
{
$query .= "not null ";
}
return $this->_query($query); return $this->_query($query);
} }
@ -485,10 +544,19 @@ class DBCubrid extends DB
$query = sprintf("select \"attr_name\" from \"db_attribute\" where " . "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name); $query = sprintf("select \"attr_name\" from \"db_attribute\" where " . "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name);
$result = $this->_query($query); $result = $this->_query($query);
if (cubrid_num_rows ($result) > 0) $output = TRUE; if(cubrid_num_rows($result) > 0)
else $output = FALSE; {
$output = TRUE;
}
else
{
$output = FALSE;
}
if ($result) cubrid_close_request ($result); if($result)
{
cubrid_close_request($result);
}
return $output; return $output;
} }
@ -505,7 +573,8 @@ class DBCubrid extends DB
*/ */
function addIndex($table_name, $index_name, $target_columns, $is_unique = FALSE) function addIndex($table_name, $index_name, $target_columns, $is_unique = FALSE)
{ {
if (!is_array ($target_columns)) { if(!is_array($target_columns))
{
$target_columns = array($target_columns); $target_columns = array($target_columns);
} }
@ -539,11 +608,17 @@ class DBCubrid extends DB
$query = sprintf("select \"index_name\" from \"db_index\" where " . "\"class_name\" = '%s%s' and (\"index_name\" = '%s' or \"index_name\" = '%s') ", $this->prefix, $table_name, $this->prefix . $index_name, $index_name); $query = sprintf("select \"index_name\" from \"db_index\" where " . "\"class_name\" = '%s%s' and (\"index_name\" = '%s' or \"index_name\" = '%s') ", $this->prefix, $table_name, $this->prefix . $index_name, $index_name);
$result = $this->_query($query); $result = $this->_query($query);
if ($this->isError ()) return FALSE; if($this->isError())
{
return FALSE;
}
$output = $this->_fetch($result); $output = $this->_fetch($result);
if (!$output) return FALSE; if(!$output)
{
return FALSE;
}
return TRUE; return TRUE;
} }
@ -579,15 +654,23 @@ class DBCubrid extends DB
); );
$result = $this->_query($query); $result = $this->_query($query);
if ($this->isError ()) return FALSE; if($this->isError())
{
return FALSE;
}
$output = $this->_fetch($result); $output = $this->_fetch($result);
if (!$output) return FALSE; if(!$output)
{
return FALSE;
}
if(!is_array($output)) { if(!is_array($output))
{
$indexes_to_be_deleted = array($output); $indexes_to_be_deleted = array($output);
} }
else { else
{
$indexes_to_be_deleted = $output; $indexes_to_be_deleted = $output;
} }
@ -618,7 +701,10 @@ class DBCubrid extends DB
*/ */
function createTableByXmlFile($file_name) function createTableByXmlFile($file_name)
{ {
if (!file_exists ($file_name)) return; if(!file_exists($file_name))
{
return;
}
// read xml file // read xml file
$buff = FileHandler::readFile($file_name); $buff = FileHandler::readFile($file_name);
return $this->_createTable($buff); return $this->_createTable($buff);
@ -642,10 +728,14 @@ class DBCubrid extends DB
$table_name = $xml_obj->table->attrs->name; $table_name = $xml_obj->table->attrs->name;
// if the table already exists exit function // if the table already exists exit function
if ($this->isTableExists($table_name)) return; if($this->isTableExists($table_name))
{
return;
}
// If the table name is sequence, it creates a serial // If the table name is sequence, it creates a serial
if ($table_name == 'sequence') { if($table_name == 'sequence')
{
$query = sprintf('create serial "%s" start with 1 increment by 1' . $query = sprintf('create serial "%s" start with 1 increment by 1' .
' minvalue 1 ' . ' minvalue 1 ' .
'maxvalue 10000000000000000000000000000000000000' . ' nocycle;', $this->prefix . $table_name); 'maxvalue 10000000000000000000000000000000000000' . ' nocycle;', $this->prefix . $table_name);
@ -659,10 +749,12 @@ class DBCubrid extends DB
$query = sprintf('create class "%s";', $table_name); $query = sprintf('create class "%s";', $table_name);
$this->_query($query); $this->_query($query);
if (!is_array ($xml_obj->table->column)) { if(!is_array($xml_obj->table->column))
{
$columns[] = $xml_obj->table->column; $columns[] = $xml_obj->table->column;
} }
else { else
{
$columns = $xml_obj->table->column; $columns = $xml_obj->table->column;
} }
@ -672,7 +764,8 @@ class DBCubrid extends DB
$unique_list = array(); $unique_list = array();
$index_list = array(); $index_list = array();
foreach ($columns as $column) { foreach($columns as $column)
{
$name = $column->attrs->name; $name = $column->attrs->name;
$type = $column->attrs->type; $type = $column->attrs->type;
$size = $column->attrs->size; $size = $column->attrs->size;
@ -682,7 +775,8 @@ class DBCubrid extends DB
$unique = $column->attrs->unique; $unique = $column->attrs->unique;
$default = $column->attrs->default; $default = $column->attrs->default;
switch ($this->column_type[$type]) { switch($this->column_type[$type])
{
case 'integer' : case 'integer' :
$size = NULL; $size = NULL;
break; break;
@ -692,29 +786,30 @@ class DBCubrid extends DB
} }
if(isset($default) && ($type == 'varchar' || $type == 'char' || if(isset($default) && ($type == 'varchar' || $type == 'char' ||
$type == 'text' || $type == 'tinytext' || $type == 'bigtext')) { $type == 'text' || $type == 'tinytext' || $type == 'bigtext'))
{
$default = sprintf("'%s'", $default); $default = sprintf("'%s'", $default);
} }
if ($type == 'varchar' || $type == 'char') { if($type == 'varchar' || $type == 'char')
if($size) $size = $size * 3; {
if($size)
$size = $size * 3;
} }
$column_schema[] = sprintf ('"%s" %s%s %s %s', $column_schema[] = sprintf('"%s" %s%s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default " . $default : '', $notnull ? 'not null' : '');
$name,
$this->column_type[$type],
$size?'('.$size.')':'',
isset($default)?"default ".$default:'',
$notnull?'not null':'');
if ($primary_key) { if($primary_key)
{
$primary_list[] = $name; $primary_list[] = $name;
} }
else if ($unique) { else if($unique)
{
$unique_list[$unique][] = $name; $unique_list[$unique][] = $name;
} }
else if ($index) { else if($index)
{
$index_list[$index][] = $name; $index_list[$index][] = $name;
} }
} }
@ -722,29 +817,31 @@ class DBCubrid extends DB
$query .= implode(',', $column_schema) . ';'; $query .= implode(',', $column_schema) . ';';
$this->_query($query); $this->_query($query);
if (count ($primary_list)) { if(count($primary_list))
{
$query = sprintf("alter class \"%s\" add attribute constraint " . "\"pkey_%s\" PRIMARY KEY(%s);", $table_name, $table_name, '"' . implode('","', $primary_list) . '"'); $query = sprintf("alter class \"%s\" add attribute constraint " . "\"pkey_%s\" PRIMARY KEY(%s);", $table_name, $table_name, '"' . implode('","', $primary_list) . '"');
$this->_query($query); $this->_query($query);
} }
if (count ($unique_list)) { if(count($unique_list))
foreach ($unique_list as $key => $val) { {
foreach($unique_list as $key => $val)
{
$query = sprintf("create unique index \"%s\" on \"%s\" " . "(%s);", $key, $table_name, '"' . implode('","', $val) . '"'); $query = sprintf("create unique index \"%s\" on \"%s\" " . "(%s);", $key, $table_name, '"' . implode('","', $val) . '"');
$this->_query($query); $this->_query($query);
} }
} }
if (count ($index_list)) { if(count($index_list))
foreach ($index_list as $key => $val) { {
foreach($index_list as $key => $val)
{
$query = sprintf("create index \"%s\" on \"%s\" (%s);", $key, $table_name, '"' . implode('","', $val) . '"'); $query = sprintf("create index \"%s\" on \"%s\" (%s);", $key, $table_name, '"' . implode('","', $val) . '"');
$this->_query($query); $this->_query($query);
} }
} }
} }
/** /**
* Handles insertAct * Handles insertAct
* @param Object $queryObject * @param Object $queryObject
@ -759,12 +856,16 @@ class DBCubrid extends DB
$with_values = FALSE; $with_values = FALSE;
} }
$query = $this->getInsertSql($queryObject, $with_values); $query = $this->getInsertSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query); $result = $this->_query($query);
if ($result && !$this->transaction_started) { if($result && !$this->transaction_started)
{
$this->_commit(); $this->_commit();
} }
unset($this->param); unset($this->param);
@ -785,18 +886,23 @@ class DBCubrid extends DB
$with_values = FALSE; $with_values = FALSE;
} }
$query = $this->getUpdateSql($queryObject, $with_values); $query = $this->getUpdateSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query); $result = $this->_query($query);
if ($result && !$this->transaction_started) $this->_commit(); if($result && !$this->transaction_started)
{
$this->_commit();
}
unset($this->param); unset($this->param);
return $result; return $result;
} }
/** /**
* Handles deleteAct * Handles deleteAct
* @param Object $queryObject * @param Object $queryObject
@ -811,13 +917,19 @@ class DBCubrid extends DB
$with_values = FALSE; $with_values = FALSE;
} }
$query = $this->getDeleteSql($queryObject, $with_values); $query = $this->getDeleteSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query); $result = $this->_query($query);
if ($result && !$this->transaction_started) $this->_commit(); if($result && !$this->transaction_started)
{
$this->_commit();
}
unset($this->param); unset($this->param);
return $result; return $result;
@ -832,8 +944,10 @@ class DBCubrid extends DB
* @param boolean $with_values * @param boolean $with_values
* @return Object * @return Object
*/ */
function _executeSelectAct($queryObject, $connection = NULL, $with_values = TRUE) { function _executeSelectAct($queryObject, $connection = NULL, $with_values = TRUE)
if ($this->use_prepared_statements == 'Y') { {
if($this->use_prepared_statements == 'Y')
{
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
$with_values = FALSE; $with_values = FALSE;
} }
@ -842,16 +956,21 @@ class DBCubrid extends DB
{ {
return $this->queryPageLimit($queryObject, $connection, $with_values); return $this->queryPageLimit($queryObject, $connection, $with_values);
} }
else { else
{
$query = $this->getSelectSql($queryObject, $with_values); $query = $this->getSelectSql($queryObject, $with_values);
if(is_a($query, 'Object')) if(is_a($query, 'Object'))
{
return; return;
}
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if($this->isError()) if($this->isError())
{
return $this->queryError($queryObject); return $this->queryError($queryObject);
}
$data = $this->_fetch($result); $data = $this->_fetch($result);
$buff = new Object (); $buff = new Object ();
@ -867,9 +986,11 @@ class DBCubrid extends DB
* @param Object $queryObject * @param Object $queryObject
* @return Object * @return Object
*/ */
function queryError($queryObject){ function queryError($queryObject)
{
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){ if($limit && $limit->isPageHandler())
{
$buff = new Object (); $buff = new Object ();
$buff->total_count = 0; $buff->total_count = 0;
$buff->total_page = 0; $buff->total_page = 0;
@ -888,7 +1009,8 @@ class DBCubrid extends DB
* @param boolean $with_values * @param boolean $with_values
* @return Object Object with page info containing * @return Object Object with page info containing
*/ */
function queryPageLimit($queryObject, $connection, $with_values){ function queryPageLimit($queryObject, $connection, $with_values)
{
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
// Total count // Total count
$temp_where = $queryObject->getWhereString($with_values, FALSE); $temp_where = $queryObject->getWhereString($with_values, FALSE);
@ -898,7 +1020,8 @@ class DBCubrid extends DB
$temp_select = $queryObject->getSelectString($with_values); $temp_select = $queryObject->getSelectString($with_values);
$uses_distinct = strpos(strtolower($temp_select), "distinct") !== FALSE; $uses_distinct = strpos(strtolower($temp_select), "distinct") !== FALSE;
$uses_groupby = $queryObject->getGroupByString() != ''; $uses_groupby = $queryObject->getGroupByString() != '';
if($uses_distinct || $uses_groupby) { if($uses_distinct || $uses_groupby)
{
$count_query = sprintf('select %s %s %s %s' $count_query = sprintf('select %s %s %s %s'
, $temp_select , $temp_select
, 'FROM ' . $queryObject->getFromString($with_values) , 'FROM ' . $queryObject->getFromString($with_values)
@ -916,22 +1039,34 @@ class DBCubrid extends DB
$total_count = (int) (isset($count_output->count) ? $count_output->count : NULL); $total_count = (int) (isset($count_output->count) ? $count_output->count : NULL);
$list_count = $limit->list_count->getValue(); $list_count = $limit->list_count->getValue();
if (!$list_count) $list_count = 20; if(!$list_count)
{
$list_count = 20;
}
$page_count = $limit->page_count->getValue(); $page_count = $limit->page_count->getValue();
if (!$page_count) $page_count = 10; if(!$page_count)
{
$page_count = 10;
}
$page = $limit->page->getValue(); $page = $limit->page->getValue();
if (!$page) $page = 1; if(!$page)
{
$page = 1;
}
// total pages // total pages
if ($total_count) { if($total_count)
{
$total_page = (int) (($total_count - 1) / $list_count) + 1; $total_page = (int) (($total_count - 1) / $list_count) + 1;
} }
else { else
{
$total_page = 1; $total_page = 1;
} }
// check the page variables // check the page variables
if ($page > $total_page) { if($page > $total_page)
{
// If requested page is bigger than total number of pages, return empty list // If requested page is bigger than total number of pages, return empty list
$buff = new Object (); $buff = new Object ();
@ -948,7 +1083,9 @@ class DBCubrid extends DB
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if($this->isError()) if($this->isError())
{
return $this->queryError($queryObject); return $this->queryError($queryObject);
}
$virtual_no = $total_count - ($page - 1) * $list_count; $virtual_no = $total_count - ($page - 1) * $list_count;
$data = $this->_fetch($result, $virtual_no); $data = $this->_fetch($result, $virtual_no);
@ -968,7 +1105,8 @@ class DBCubrid extends DB
* @param boolean $force * @param boolean $force
* @return DBParser * @return DBParser
*/ */
function getParser($force = FALSE){ function getParser($force = FALSE)
{
return new DBParser('"', '"', $this->prefix); return new DBParser('"', '"', $this->prefix);
} }
@ -980,32 +1118,50 @@ class DBCubrid extends DB
* @param int $list_count * @param int $list_count
* @return string select paging sql * @return string select paging sql
*/ */
function getSelectPageSql($query, $with_values = TRUE, $start_count = 0, $list_count = 0) { function getSelectPageSql($query, $with_values = TRUE, $start_count = 0, $list_count = 0)
{
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '')
{
return new Object(-1, "Invalid query");
}
$select = 'SELECT ' . $select; $select = 'SELECT ' . $select;
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$from = ' FROM ' . $from; $from = ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') if($where != '')
{
$where = ' WHERE ' . $where; $where = ' WHERE ' . $where;
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '')
{
$groupBy = ' GROUP BY ' . $groupBy;
}
$orderBy = $query->getOrderByString(); $orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '')
{
$orderBy = ' ORDER BY ' . $orderBy;
}
$limit = $query->getLimitString(); $limit = $query->getLimitString();
if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count); if($limit != '')
{
$limit = sprintf(' LIMIT %d, %d', $start_count, $list_count);
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
}
}
/* End of file DBCubrid.class.php */ /* End of file DBCubrid.class.php */
/* Location: ./classes/db/DBCubrid.class.php */ /* Location: ./classes/db/DBCubrid.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* - DBMSSQL * - DBMSSQL
* - Modified to use MSSQL driver by sol (sol@ngleader.com) * - Modified to use MSSQL driver by sol (sol@ngleader.com)
@ -9,6 +10,7 @@
*/ */
class DBMssql extends DB class DBMssql extends DB
{ {
/** /**
* prefix of XE tables(One more XE can be installed on a single DB) * prefix of XE tables(One more XE can be installed on a single DB)
* @var string * @var string
@ -61,7 +63,10 @@ class DBMssql extends DB
*/ */
function isSupported() function isSupported()
{ {
if (!extension_loaded("sqlsrv")) return false; if(!extension_loaded("sqlsrv"))
{
return false;
}
return true; return true;
} }
@ -107,7 +112,10 @@ class DBMssql extends DB
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
//if(!is_numeric($string)) $string = str_replace("'","''",$string); //if(!is_numeric($string)) $string = str_replace("'","''",$string);
return $string; return $string;
@ -121,7 +129,10 @@ class DBMssql extends DB
function _begin() function _begin()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
if(sqlsrv_begin_transaction($connection) === false) return; if(sqlsrv_begin_transaction($connection) === false)
{
return;
}
return true; return true;
} }
@ -164,12 +175,21 @@ class DBMssql extends DB
{ {
foreach($this->param as $k => $o) foreach($this->param as $k => $o)
{ {
if($o->isColumnName()) continue; if($o->isColumnName())
{
continue;
}
if($o->getType() == 'number') if($o->getType() == 'number')
{ {
$value = $o->getUnescapedValue(); $value = $o->getUnescapedValue();
if(is_array($value)) $_param = array_merge($_param, $value); if(is_array($value))
else $_param[] = $o->getUnescapedValue(); {
$_param = array_merge($_param, $value);
}
else
{
$_param[] = $o->getUnescapedValue();
}
} }
else else
{ {
@ -177,9 +197,14 @@ class DBMssql extends DB
if(is_array($value)) if(is_array($value))
{ {
foreach($value as $v) foreach($value as $v)
{
$_param[] = array($v, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8')); $_param[] = array($v, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
} }
else $_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8')); }
else
{
$_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
}
} }
} }
} }
@ -207,7 +232,9 @@ class DBMssql extends DB
// Error Check // Error Check
if(!$result) if(!$result)
{
$this->setError(print_r(sqlsrv_errors(), true)); $this->setError(print_r(sqlsrv_errors(), true));
}
$this->param = array(); $this->param = array();
@ -224,7 +251,9 @@ class DBMssql extends DB
*/ */
function _getParametersByReference($_param) function _getParametersByReference($_param)
{ {
$copy = array(); $args = array(); $i = 0; $copy = array();
$args = array();
$i = 0;
foreach($_param as $key => $value) foreach($_param as $key => $value)
{ {
if(is_array($value)) if(is_array($value))
@ -254,30 +283,47 @@ class DBMssql extends DB
function _fetch($result, $arrayIndexEndValue = NULL) function _fetch($result, $arrayIndexEndValue = NULL)
{ {
$output = array(); $output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output; if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
$c = sqlsrv_num_fields($result); $c = sqlsrv_num_fields($result);
$m = null; $m = null;
while(sqlsrv_fetch($result)) while(sqlsrv_fetch($result))
{ {
if(!$m) $m = sqlsrv_field_metadata($result); if(!$m)
{
$m = sqlsrv_field_metadata($result);
}
unset($row); unset($row);
for($i = 0; $i < $c; $i++) for($i = 0; $i < $c; $i++)
{ {
$row->{$m[$i]['Name']} = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING('utf-8')); $row->{$m[$i]['Name']} = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING('utf-8'));
} }
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $row; if($arrayIndexEndValue)
else $output[] = $row; {
$output[$arrayIndexEndValue--] = $row;
}
else
{
$output[] = $row;
}
} }
if(count($output) == 1) if(count($output) == 1)
{ {
if(isset($arrayIndexEndValue)) return $output; if(isset($arrayIndexEndValue))
else return $output[0]; {
return $output;
}
else
{
return $output[0];
}
} }
return $output; return $output;
} }
/** /**
@ -309,7 +355,10 @@ class DBMssql extends DB
$result = $this->_query($query); $result = $this->_query($query);
$tmp = $this->_fetch($result); $tmp = $this->_fetch($result);
if(!$tmp) return false; if(!$tmp)
{
return false;
}
return true; return true;
} }
@ -325,15 +374,34 @@ class DBMssql extends DB
*/ */
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false) function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{ {
if($this->isColumnExists($table_name, $column_name)) return; if($this->isColumnExists($table_name, $column_name))
{
return;
}
$type = $this->column_type[$type]; $type = $this->column_type[$type];
if(strtoupper($type)=='INTEGER') $size = ''; if(strtoupper($type) == 'INTEGER')
{
$size = '';
}
$query = sprintf("alter table %s%s add %s ", $this->prefix, $table_name, $column_name); $query = sprintf("alter table %s%s add %s ", $this->prefix, $table_name, $column_name);
if($size) $query .= sprintf(" %s(%s) ", $type, $size); if($size)
else $query .= sprintf(" %s ", $type); {
if($default) $query .= sprintf(" default '%s' ", $default); $query .= sprintf(" %s(%s) ", $type, $size);
if($notnull) $query .= " not null "; }
else
{
$query .= sprintf(" %s ", $type);
}
if($default)
{
$query .= sprintf(" default '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
}
return $this->_query($query); return $this->_query($query);
} }
@ -346,7 +414,10 @@ class DBMssql extends DB
*/ */
function dropColumn($table_name, $column_name) function dropColumn($table_name, $column_name)
{ {
if(!$this->isColumnExists($table_name, $column_name)) return; if(!$this->isColumnExists($table_name, $column_name))
{
return;
}
$query = sprintf("alter table %s%s drop %s ", $this->prefix, $table_name, $column_name); $query = sprintf("alter table %s%s drop %s ", $this->prefix, $table_name, $column_name);
$this->_query($query); $this->_query($query);
} }
@ -361,9 +432,15 @@ class DBMssql extends DB
{ {
$query = sprintf("select syscolumns.name as name from syscolumns, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = syscolumns.id and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name); $query = sprintf("select syscolumns.name as name from syscolumns, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = syscolumns.id and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name);
$result = $this->_query($query); $result = $this->_query($query);
if($this->isError()) return; if($this->isError())
{
return;
}
$tmp = $this->_fetch($result); $tmp = $this->_fetch($result);
if(!$tmp->name) return false; if(!$tmp->name)
{
return false;
}
return true; return true;
} }
@ -379,8 +456,14 @@ class DBMssql extends DB
*/ */
function addIndex($table_name, $index_name, $target_columns, $is_unique = false) function addIndex($table_name, $index_name, $target_columns, $is_unique = false)
{ {
if($this->isIndexExists($table_name, $index_name)) return; if($this->isIndexExists($table_name, $index_name))
if(!is_array($target_columns)) $target_columns = array($target_columns); {
return;
}
if(!is_array($target_columns))
{
$target_columns = array($target_columns);
}
$query = sprintf("create %s index %s on %s%s (%s)", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name, implode(',', $target_columns)); $query = sprintf("create %s index %s on %s%s (%s)", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name, implode(',', $target_columns));
$this->_query($query); $this->_query($query);
@ -395,7 +478,10 @@ class DBMssql extends DB
*/ */
function dropIndex($table_name, $index_name, $is_unique = false) function dropIndex($table_name, $index_name, $is_unique = false)
{ {
if(!$this->isIndexExists($table_name, $index_name)) return; if(!$this->isIndexExists($table_name, $index_name))
{
return;
}
$query = sprintf("drop index %s%s.%s", $this->prefix, $table_name, $index_name); $query = sprintf("drop index %s%s.%s", $this->prefix, $table_name, $index_name);
$this->_query($query); $this->_query($query);
} }
@ -411,10 +497,16 @@ class DBMssql extends DB
$query = sprintf("select sysindexes.name as name from sysindexes, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = sysindexes.id and sysindexes.name = '%s'", $this->prefix, $table_name, $index_name); $query = sprintf("select sysindexes.name as name from sysindexes, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = sysindexes.id and sysindexes.name = '%s'", $this->prefix, $table_name, $index_name);
$result = $this->_query($query); $result = $this->_query($query);
if($this->isError()) return; if($this->isError())
{
return;
}
$tmp = $this->_fetch($result); $tmp = $this->_fetch($result);
if(!$tmp->name) return false; if(!$tmp->name)
{
return false;
}
return true; return true;
} }
@ -435,7 +527,10 @@ class DBMssql extends DB
*/ */
function createTableByXmlFile($file_name) function createTableByXmlFile($file_name)
{ {
if(!file_exists($file_name)) return; if(!file_exists($file_name))
{
return;
}
// read xml file // read xml file
$buff = FileHandler::readFile($file_name); $buff = FileHandler::readFile($file_name);
return $this->_createTable($buff); return $this->_createTable($buff);
@ -457,7 +552,10 @@ class DBMssql extends DB
$xml_obj = $oXml->parse($xml_doc); $xml_obj = $oXml->parse($xml_doc);
// Create a table schema // Create a table schema
$table_name = $xml_obj->table->attrs->name; $table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return; if($this->isTableExists($table_name))
{
return;
}
if($table_name == 'sequence') if($table_name == 'sequence')
{ {
@ -469,8 +567,14 @@ class DBMssql extends DB
{ {
$table_name = $this->prefix . $table_name; $table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column; if(!is_array($xml_obj->table->column))
else $columns = $xml_obj->table->column; {
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
$primary_list = array(); $primary_list = array();
$unique_list = array(); $unique_list = array();
@ -489,18 +593,20 @@ class DBMssql extends DB
$default = $column->attrs->default; $default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment; $auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('[%s] %s%s %s %s %s', $column_schema[] = sprintf('[%s] %s%s %s %s %s', $name, $this->column_type[$type], !isset($typeList[$type]) && $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : 'null', $auto_increment ? 'identity(1,1)' : '');
$name,
$this->column_type[$type],
!isset($typeList[$type])&&$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'null',
$auto_increment?'identity(1,1)':''
);
if($primary_key) $primary_list[] = $name; if($primary_key)
else if($unique) $unique_list[$unique][] = $name; {
else if($index) $index_list[$index][] = $name; $primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
} }
if(count($primary_list)) if(count($primary_list))
@ -510,7 +616,10 @@ class DBMssql extends DB
$schema = sprintf('create table [%s] (%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n")); $schema = sprintf('create table [%s] (%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"));
$output = $this->_query($schema); $output = $this->_query($schema);
if(!$output) return false; if(!$output)
{
return false;
}
if(count($unique_list)) if(count($unique_list))
{ {
@ -533,8 +642,6 @@ class DBMssql extends DB
} }
} }
/** /**
* Handles insertAct * Handles insertAct
* @todo Lookup _filterNumber against sql injection - see if it is still needed and how to integrate * @todo Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
@ -570,19 +677,30 @@ class DBMssql extends DB
function getUpdateSql($query, $with_values = true, $with_priority = false) function getUpdateSql($query, $with_values = true, $with_priority = false)
{ {
$columnsList = $query->getUpdateString($with_values); $columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query"); if($columnsList == '')
{
return new Object(-1, "Invalid query");
}
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$tables = $query->getTables(); $tables = $query->getTables();
$alias_list = ''; $alias_list = '';
foreach($tables as $table) foreach($tables as $table)
{
$alias_list .= $table->getAlias(); $alias_list .= $table->getAlias();
}
implode(',', explode(' ', $alias_list)); implode(',', explode(' ', $alias_list));
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '')
{
$where = ' WHERE ' . $where;
}
$priority = $with_priority ? $query->getPriority() : ''; $priority = $with_priority ? $query->getPriority() : '';
@ -614,31 +732,56 @@ class DBMssql extends DB
//$limitOffset = $query->getLimit()->getOffset(); //$limitOffset = $query->getLimit()->getOffset();
//if($limitOffset) //if($limitOffset)
// TODO Implement Limit with offset with subquery // TODO Implement Limit with offset with subquery
$limit = '';$limitCount = ''; $limit = '';
$limitCount = '';
$limitQueryPart = $query->getLimit(); $limitQueryPart = $query->getLimit();
if($limitQueryPart) if($limitQueryPart)
{
$limitCount = $limitQueryPart->getLimit(); $limitCount = $limitQueryPart->getLimit();
if($limitCount != '') $limit = 'SELECT TOP ' . $limitCount; }
if($limitCount != '')
{
$limit = 'SELECT TOP ' . $limitCount;
}
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '')
{
return new Object(-1, "Invalid query");
}
if($limit != '') if($limit != '')
{
$select = $limit . ' ' . $select; $select = $limit . ' ' . $select;
}
else else
{
$select = 'SELECT ' . $select; $select = 'SELECT ' . $select;
}
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$from = ' FROM ' . $from; $from = ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '')
{
$where = ' WHERE ' . $where;
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '')
{
$groupBy = ' GROUP BY ' . $groupBy;
}
$orderBy = $query->getOrderByString(); $orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '')
{
$orderBy = ' ORDER BY ' . $orderBy;
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
} }
@ -655,7 +798,10 @@ class DBMssql extends DB
{ {
$query = $this->getSelectSql($queryObject); $query = $this->getSelectSql($queryObject);
if(strpos($query, "substr")) $query = str_replace ("substr", "substring", $query); if(strpos($query, "substr"))
{
$query = str_replace("substr", "substring", $query);
}
// TODO Decide if we continue to pass parameters like this // TODO Decide if we continue to pass parameters like this
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
@ -663,8 +809,14 @@ class DBMssql extends DB
$query .= (__DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if ($this->isError ()) return $this->queryError($queryObject); if($this->isError())
else return $this->queryPageLimit($queryObject, $result, $connection); {
return $this->queryError($queryObject);
}
else
{
return $this->queryPageLimit($queryObject, $result, $connection);
}
} }
/** /**
@ -696,8 +848,10 @@ class DBMssql extends DB
return $buff; return $buff;
} }
else else
{
return; return;
} }
}
/** /**
* If select query execute, return page info * If select query execute, return page info
@ -740,20 +894,28 @@ class DBMssql extends DB
$list_count = $limit->list_count->getValue(); $list_count = $limit->list_count->getValue();
if(!$list_count) if(!$list_count)
{
$list_count = 20; $list_count = 20;
}
$page_count = $limit->page_count->getValue(); $page_count = $limit->page_count->getValue();
if(!$page_count) if(!$page_count)
{
$page_count = 10; $page_count = 10;
}
$page = $limit->page->getValue(); $page = $limit->page->getValue();
if(!$page) if(!$page)
{
$page = 1; $page = 1;
}
// Total pages // Total pages
if($total_count) if($total_count)
{ {
$total_page = (int) (($total_count - 1) / $list_count) + 1; $total_page = (int) (($total_count - 1) / $list_count) + 1;
} }
else else
{
$total_page = 1; $total_page = 1;
}
// check the page variables // check the page variables
if($page > $total_page) if($page > $total_page)

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Class to use MySQL DBMS * Class to use MySQL DBMS
* mysql handling class * mysql handling class
@ -11,6 +12,7 @@
*/ */
class DBMysql extends DB class DBMysql extends DB
{ {
/** /**
* prefix of a tablename (One or more XEs can be installed in a single DB) * prefix of a tablename (One or more XEs can be installed in a single DB)
* @var string * @var string
@ -62,7 +64,10 @@ class DBMysql extends DB
*/ */
function isSupported() function isSupported()
{ {
if(!function_exists('mysql_connect')) return false; if(!function_exists('mysql_connect'))
{
return false;
}
return true; return true;
} }
@ -76,7 +81,9 @@ class DBMysql extends DB
{ {
// Ignore if no DB information exists // Ignore if no DB information exists
if(strpos($connection["db_hostname"], ':') === false && $connection["db_port"]) if(strpos($connection["db_hostname"], ':') === false && $connection["db_port"])
{
$connection["db_hostname"] .= ':' . $connection["db_port"]; $connection["db_hostname"] .= ':' . $connection["db_port"];
}
// Attempt to connect // Attempt to connect
$result = @mysql_connect($connection["db_hostname"], $connection["db_userid"], $connection["db_password"]); $result = @mysql_connect($connection["db_hostname"], $connection["db_userid"], $connection["db_password"]);
@ -133,8 +140,14 @@ class DBMysql extends DB
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
if(!is_numeric($string)) $string = @mysql_real_escape_string($string); {
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string))
{
$string = @mysql_real_escape_string($string);
}
return $string; return $string;
} }
@ -180,7 +193,10 @@ class DBMysql extends DB
// Run the query statement // Run the query statement
$result = mysql_query($query, $connection); $result = mysql_query($query, $connection);
// Error Check // Error Check
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection)); if(mysql_error($connection))
{
$this->setError(mysql_errno($connection), mysql_error($connection));
}
// Return result // Return result
return $result; return $result;
} }
@ -194,16 +210,31 @@ class DBMysql extends DB
function _fetch($result, $arrayIndexEndValue = NULL) function _fetch($result, $arrayIndexEndValue = NULL)
{ {
$output = array(); $output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output; if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
while($tmp = $this->db_fetch_object($result)) while($tmp = $this->db_fetch_object($result))
{ {
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp; if($arrayIndexEndValue)
else $output[] = $tmp; {
$output[$arrayIndexEndValue--] = $tmp;
}
else
{
$output[] = $tmp;
}
} }
if(count($output) == 1) if(count($output) == 1)
{ {
if(isset($arrayIndexEndValue)) return $output; if(isset($arrayIndexEndValue))
else return $output[0]; {
return $output;
}
else
{
return $output[0];
}
} }
$this->db_free_result($result); $this->db_free_result($result);
return $output; return $output;
@ -239,7 +270,10 @@ class DBMysql extends DB
$query = sprintf("select password('%s') as password, old_password('%s') as old_password", $this->addQuotes($password), $this->addQuotes($password)); $query = sprintf("select password('%s') as password, old_password('%s') as old_password", $this->addQuotes($password), $this->addQuotes($password));
$result = $this->_query($query); $result = $this->_query($query);
$tmp = $this->_fetch($result); $tmp = $this->_fetch($result);
if($tmp->password == $saved_password || $tmp->old_password == $saved_password) return true; if($tmp->password == $saved_password || $tmp->old_password == $saved_password)
{
return true;
}
return false; return false;
} }
@ -253,7 +287,10 @@ class DBMysql extends DB
$query = sprintf("show tables like '%s%s'", $this->prefix, $this->addQuotes($target_name)); $query = sprintf("show tables like '%s%s'", $this->prefix, $this->addQuotes($target_name));
$result = $this->_query($query); $result = $this->_query($query);
$tmp = $this->_fetch($result); $tmp = $this->_fetch($result);
if(!$tmp) return false; if(!$tmp)
{
return false;
}
return true; return true;
} }
@ -270,13 +307,28 @@ class DBMysql extends DB
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false) function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{ {
$type = $this->column_type[$type]; $type = $this->column_type[$type];
if(strtoupper($type)=='INTEGER') $size = ''; if(strtoupper($type) == 'INTEGER')
{
$size = '';
}
$query = sprintf("alter table `%s%s` add `%s` ", $this->prefix, $table_name, $column_name); $query = sprintf("alter table `%s%s` add `%s` ", $this->prefix, $table_name, $column_name);
if($size) $query .= sprintf(" %s(%s) ", $type, $size); if($size)
else $query .= sprintf(" %s ", $type); {
if($default) $query .= sprintf(" default '%s' ", $default); $query .= sprintf(" %s(%s) ", $type, $size);
if($notnull) $query .= " not null "; }
else
{
$query .= sprintf(" %s ", $type);
}
if($default)
{
$query .= sprintf(" default '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
}
return $this->_query($query); return $this->_query($query);
} }
@ -303,7 +355,10 @@ class DBMysql extends DB
{ {
$query = sprintf("show fields from `%s%s`", $this->prefix, $table_name); $query = sprintf("show fields from `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query); $result = $this->_query($query);
if($this->isError()) return; if($this->isError())
{
return;
}
$output = $this->_fetch($result); $output = $this->_fetch($result);
if($output) if($output)
{ {
@ -311,7 +366,10 @@ class DBMysql extends DB
foreach($output as $key => $val) foreach($output as $key => $val)
{ {
$name = strtolower($val->Field); $name = strtolower($val->Field);
if($column_name == $name) return true; if($column_name == $name)
{
return true;
}
} }
} }
return false; return false;
@ -329,7 +387,10 @@ class DBMysql extends DB
*/ */
function addIndex($table_name, $index_name, $target_columns, $is_unique = false) function addIndex($table_name, $index_name, $target_columns, $is_unique = false)
{ {
if(!is_array($target_columns)) $target_columns = array($target_columns); if(!is_array($target_columns))
{
$target_columns = array($target_columns);
}
$query = sprintf("alter table `%s%s` add %s index `%s` (%s);", $this->prefix, $table_name, $is_unique ? 'unique' : '', $index_name, implode(',', $target_columns)); $query = sprintf("alter table `%s%s` add %s index `%s` (%s);", $this->prefix, $table_name, $is_unique ? 'unique' : '', $index_name, implode(',', $target_columns));
$this->_query($query); $this->_query($query);
@ -348,7 +409,6 @@ class DBMysql extends DB
$this->_query($query); $this->_query($query);
} }
/** /**
* Check index status of the table * Check index status of the table
* @param string $table_name table name * @param string $table_name table name
@ -360,14 +420,26 @@ class DBMysql extends DB
//$query = sprintf("show indexes from %s%s where key_name = '%s' ", $this->prefix, $table_name, $index_name); //$query = sprintf("show indexes from %s%s where key_name = '%s' ", $this->prefix, $table_name, $index_name);
$query = sprintf("show indexes from `%s%s`", $this->prefix, $table_name); $query = sprintf("show indexes from `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query); $result = $this->_query($query);
if($this->isError()) return; if($this->isError())
{
return;
}
$output = $this->_fetch($result); $output = $this->_fetch($result);
if(!$output) return; if(!$output)
if(!is_array($output)) $output = array($output); {
return;
}
if(!is_array($output))
{
$output = array($output);
}
for($i = 0; $i < count($output); $i++) for($i = 0; $i < count($output); $i++)
{ {
if($output[$i]->Key_name == $index_name) return true; if($output[$i]->Key_name == $index_name)
{
return true;
}
} }
return false; return false;
} }
@ -389,7 +461,10 @@ class DBMysql extends DB
*/ */
function createTableByXmlFile($file_name) function createTableByXmlFile($file_name)
{ {
if(!file_exists($file_name)) return; if(!file_exists($file_name))
{
return;
}
// read xml file // read xml file
$buff = FileHandler::readFile($file_name); $buff = FileHandler::readFile($file_name);
return $this->_createTable($buff); return $this->_createTable($buff);
@ -411,11 +486,20 @@ class DBMysql extends DB
$xml_obj = $oXml->parse($xml_doc); $xml_obj = $oXml->parse($xml_doc);
// Create a table schema // Create a table schema
$table_name = $xml_obj->table->attrs->name; $table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return; if($this->isTableExists($table_name))
{
return;
}
$table_name = $this->prefix . $table_name; $table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column; if(!is_array($xml_obj->table->column))
else $columns = $xml_obj->table->column; {
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
$primary_list = array(); $primary_list = array();
$unique_list = array(); $unique_list = array();
@ -433,18 +517,20 @@ class DBMysql extends DB
$default = $column->attrs->default; $default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment; $auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
$name,
$this->column_type[$type],
$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'',
$auto_increment?'auto_increment':''
);
if($primary_key) $primary_list[] = $name; if($primary_key)
else if($unique) $unique_list[$unique][] = $name; {
else if($index) $index_list[$index][] = $name; $primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
} }
if(count($primary_list)) if(count($primary_list))
@ -471,7 +557,8 @@ class DBMysql extends DB
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci"); $schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci");
$output = $this->_query($schema); $output = $this->_query($schema);
if(!$output) return false; if(!$output)
return false;
} }
/** /**
@ -484,7 +571,10 @@ class DBMysql extends DB
{ {
$query = $this->getInsertSql($queryObject, $with_values, true); $query = $this->getInsertSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query); return $this->_query($query);
} }
@ -498,7 +588,10 @@ class DBMysql extends DB
{ {
$query = $this->getUpdateSql($queryObject, $with_values, true); $query = $this->getUpdateSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query); return $this->_query($query);
} }
@ -512,7 +605,10 @@ class DBMysql extends DB
{ {
$query = $this->getDeleteSql($queryObject, $with_values, true); $query = $this->getDeleteSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return; if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query); return $this->_query($query);
} }
@ -530,17 +626,23 @@ class DBMysql extends DB
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
$result = NULL; $result = NULL;
if($limit && $limit->isPageHandler()) if($limit && $limit->isPageHandler())
{
return $this->queryPageLimit($queryObject, $result, $connection, $with_values); return $this->queryPageLimit($queryObject, $result, $connection, $with_values);
}
else else
{ {
$query = $this->getSelectSql($queryObject, $with_values); $query = $this->getSelectSql($queryObject, $with_values);
if(is_a($query, 'Object')) if(is_a($query, 'Object'))
{
return; return;
}
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : ''; $query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if($this->isError()) if($this->isError())
{
return $this->queryError($queryObject); return $this->queryError($queryObject);
}
$data = $this->_fetch($result); $data = $this->_fetch($result);
$buff = new Object (); $buff = new Object ();
@ -618,8 +720,10 @@ class DBMysql extends DB
return $buff; return $buff;
} }
else else
{
return; return;
} }
}
/** /**
* If select query execute, return page info * If select query execute, return page info
@ -659,17 +763,30 @@ class DBMysql extends DB
$total_count = (int) (isset($count_output->count) ? $count_output->count : NULL); $total_count = (int) (isset($count_output->count) ? $count_output->count : NULL);
$list_count = $limit->list_count->getValue(); $list_count = $limit->list_count->getValue();
if (!$list_count) $list_count = 20; if(!$list_count)
{
$list_count = 20;
}
$page_count = $limit->page_count->getValue(); $page_count = $limit->page_count->getValue();
if (!$page_count) $page_count = 10; if(!$page_count)
{
$page_count = 10;
}
$page = $limit->page->getValue(); $page = $limit->page->getValue();
if (!$page) $page = 1; if(!$page)
{
$page = 1;
}
// total pages // total pages
if($total_count) if($total_count)
{
$total_page = (int) (($total_count - 1) / $list_count) + 1; $total_page = (int) (($total_count - 1) / $list_count) + 1;
}
else else
{
$total_page = 1; $total_page = 1;
}
// check the page variables // check the page variables
if($page > $total_page) if($page > $total_page)
@ -690,7 +807,9 @@ class DBMysql extends DB
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if($this->isError()) if($this->isError())
{
return $this->queryError($queryObject); return $this->queryError($queryObject);
}
$virtual_no = $total_count - ($page - 1) * $list_count; $virtual_no = $total_count - ($page - 1) * $list_count;
$data = $this->_fetch($result, $virtual_no); $data = $this->_fetch($result, $virtual_no);
@ -715,27 +834,46 @@ class DBMysql extends DB
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0)
{ {
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '')
{
return new Object(-1, "Invalid query");
}
$select = 'SELECT ' . $select; $select = 'SELECT ' . $select;
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '')
{
return new Object(-1, "Invalid query");
}
$from = ' FROM ' . $from; $from = ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '')
{
$where = ' WHERE ' . $where;
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '')
{
$groupBy = ' GROUP BY ' . $groupBy;
}
$orderBy = $query->getOrderByString(); $orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '')
{
$orderBy = ' ORDER BY ' . $orderBy;
}
$limit = $query->getLimitString(); $limit = $query->getLimitString();
if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count); if($limit != '')
{
$limit = sprintf(' LIMIT %d, %d', $start_count, $list_count);
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
} }
/* End of file DBMysql.class.php */ /* End of file DBMysql.class.php */
/* Location: ./classes/db/DBMysql.class.php */ /* Location: ./classes/db/DBMysql.class.php */

View file

@ -1,5 +1,7 @@
<?php <?php
require_once('DBMysql.class.php'); require_once('DBMysql.class.php');
/** /**
* Class to use MySQL innoDB DBMS * Class to use MySQL innoDB DBMS
* mysql innodb handling class * mysql innodb handling class
@ -92,7 +94,10 @@ class DBMysql_innodb extends DBMysql
// Run the query statement // Run the query statement
$result = @mysql_query($query, $connection); $result = @mysql_query($query, $connection);
// Error Check // Error Check
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection)); if(mysql_error($connection))
{
$this->setError(mysql_errno($connection), mysql_error($connection));
}
// Return result // Return result
return $result; return $result;
} }
@ -113,11 +118,20 @@ class DBMysql_innodb extends DBMysql
$xml_obj = $oXml->parse($xml_doc); $xml_obj = $oXml->parse($xml_doc);
// Create a table schema // Create a table schema
$table_name = $xml_obj->table->attrs->name; $table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return; if($this->isTableExists($table_name))
{
return;
}
$table_name = $this->prefix . $table_name; $table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column; if(!is_array($xml_obj->table->column))
else $columns = $xml_obj->table->column; {
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
foreach($columns as $column) foreach($columns as $column)
{ {
@ -131,18 +145,20 @@ class DBMysql_innodb extends DBMysql
$default = $column->attrs->default; $default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment; $auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
$name,
$this->column_type[$type],
$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'',
$auto_increment?'auto_increment':''
);
if($primary_key) $primary_list[] = $name; if($primary_key)
else if($unique) $unique_list[$unique][] = $name; {
else if($index) $index_list[$index][] = $name; $primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
} }
if(count($primary_list)) if(count($primary_list))
@ -169,8 +185,12 @@ class DBMysql_innodb extends DBMysql
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci"); $schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
$output = $this->_query($schema); $output = $this->_query($schema);
if(!$output) return false; if(!$output)
{
return false;
} }
} }
}
/* End of file DBMysql_innodb.class.php */ /* End of file DBMysql_innodb.class.php */
/* Location: ./classes/db/DBMysql_innodb.class.php */ /* Location: ./classes/db/DBMysql_innodb.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
require_once('DBMysql.class.php'); require_once('DBMysql.class.php');
/** /**
@ -11,7 +12,8 @@ require_once('DBMysql.class.php');
* @package /classes/db * @package /classes/db
* @version 0.1 * @version 0.1
*/ */
class DBMysqli extends DBMysql { class DBMysqli extends DBMysql
{
/** /**
* Constructor * Constructor
@ -30,7 +32,10 @@ class DBMysqli extends DBMysql {
*/ */
function isSupported() function isSupported()
{ {
if(!function_exists('mysqli_connect')) return false; if(!function_exists('mysqli_connect'))
{
return false;
}
return true; return true;
} }
@ -95,7 +100,10 @@ class DBMysqli extends DBMysql {
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string)) if(!is_numeric($string))
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
@ -138,20 +146,23 @@ class DBMysqli extends DBMysql {
// 2. Bind parameters // 2. Bind parameters
$status = call_user_func_array('mysqli_stmt_bind_param', $args); $status = call_user_func_array('mysqli_stmt_bind_param', $args);
if(!$status) if(!$status)
{
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true)); $this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
} }
}
// 3. Execute query // 3. Execute query
$status = mysqli_stmt_execute($stmt); $status = mysqli_stmt_execute($stmt);
if(!$status) if(!$status)
{
$this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true)); $this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
}
// Return stmt for other processing - like retrieving resultset (_fetch) // Return stmt for other processing - like retrieving resultset (_fetch)
return $stmt; return $stmt;
// mysqli_stmt_close($stmt); // mysqli_stmt_close($stmt);
} }
} }
// Run the query statement // Run the query statement
$result = mysqli_query($connection, $query); $result = mysqli_query($connection, $query);
@ -176,7 +187,10 @@ class DBMysqli extends DBMysql {
{ {
$types = ''; $types = '';
$params = array(); $params = array();
if(!$this->param) return; if(!$this->param)
{
return;
}
foreach($this->param as $k => $o) foreach($this->param as $k => $o)
{ {
@ -184,7 +198,10 @@ class DBMysqli extends DBMysql {
$type = $o->getType(); $type = $o->getType();
// Skip column names -> this should be concatenated to query string // Skip column names -> this should be concatenated to query string
if($o->isColumnName()) continue; if($o->isColumnName())
{
continue;
}
switch($type) switch($type)
{ {
@ -211,9 +228,6 @@ class DBMysqli extends DBMysql {
$params[] = $value; $params[] = $value;
$types .= $type; $types .= $type;
} }
} }
} }
@ -230,7 +244,10 @@ class DBMysqli extends DBMysql {
return parent::_fetch($result, $arrayIndexEndValue); return parent::_fetch($result, $arrayIndexEndValue);
} }
$output = array(); $output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output; if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
// Prepared stements: bind result variable and fetch data // Prepared stements: bind result variable and fetch data
$stmt = $result; $stmt = $result;
@ -246,13 +263,18 @@ class DBMysqli extends DBMysql {
foreach($fields as $field) foreach($fields as $field)
{ {
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
{
$field->name = 'repeat_' . $field->name; $field->name = 'repeat_' . $field->name;
}
// Array passed needs to contain references, not values // Array passed needs to contain references, not values
$row[$field->name] = ""; $row[$field->name] = "";
$resultArray[$field->name] = &$row[$field->name]; $resultArray[$field->name] = &$row[$field->name];
if($field->type == 252) $longtext_exists = true; if($field->type == 252)
{
$longtext_exists = true;
}
} }
$resultArray = array_merge(array($stmt), $resultArray); $resultArray = array_merge(array($stmt), $resultArray);
@ -270,8 +292,14 @@ class DBMysqli extends DBMysql {
foreach($resultArray as $key => $value) foreach($resultArray as $key => $value)
{ {
if($key === 0) continue; // Skip stmt object if($key === 0)
if(strpos($key, 'repeat_')) $key = substr($key, 6); {
continue; // Skip stmt object
}
if(strpos($key, 'repeat_'))
{
$key = substr($key, 6);
}
$resultObject->$key = $value; $resultObject->$key = $value;
} }
@ -294,8 +322,14 @@ class DBMysqli extends DBMysql {
if(count($output) == 1) if(count($output) == 1)
{ {
if(isset($arrayIndexEndValue)) return $output; if(isset($arrayIndexEndValue))
else return $output[0]; {
return $output;
}
else
{
return $output[0];
}
} }
return $output; return $output;
@ -307,7 +341,8 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values * @param boolean $with_values
* @return resource * @return resource
*/ */
function _executeInsertAct($queryObject, $with_values = false){ function _executeInsertAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
return parent::_executeInsertAct($queryObject); return parent::_executeInsertAct($queryObject);
@ -324,7 +359,8 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values * @param boolean $with_values
* @return resource * @return resource
*/ */
function _executeUpdateAct($queryObject, $with_values = false) { function _executeUpdateAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
return parent::_executeUpdateAct($queryObject); return parent::_executeUpdateAct($queryObject);
@ -341,7 +377,8 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values * @param boolean $with_values
* @return resource * @return resource
*/ */
function _executeDeleteAct($queryObject, $with_values = false) { function _executeDeleteAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
return parent::_executeDeleteAct($queryObject); return parent::_executeDeleteAct($queryObject);
@ -404,6 +441,7 @@ class DBMysqli extends DBMysql {
{ {
return mysqli_free_result($result); return mysqli_free_result($result);
} }
} }
/* End of file DBMysqli.class.php */ /* End of file DBMysqli.class.php */
/* Location: ./classes/db/DBMysqli.class.php */ /* Location: ./classes/db/DBMysqli.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts * @package /classes/db/queryparts
@ -6,16 +7,19 @@
*/ */
class Query extends Object class Query extends Object
{ {
/** /**
* Query id, defined in query xml file * Query id, defined in query xml file
* @var string * @var string
*/ */
var $queryID; var $queryID;
/** /**
* DML type, ex) INSERT, DELETE, UPDATE, SELECT * DML type, ex) INSERT, DELETE, UPDATE, SELECT
* @var string * @var string
*/ */
var $action; var $action;
/** /**
* priority level ex)LOW_PRIORITY, HIGHT_PRIORITY * priority level ex)LOW_PRIORITY, HIGHT_PRIORITY
* @var string * @var string
@ -27,26 +31,31 @@ class Query extends Object
* @var string|array * @var string|array
*/ */
var $columns; var $columns;
/** /**
* table list * table list
* @var string|array * @var string|array
*/ */
var $tables; var $tables;
/** /**
* condition list * condition list
* @var string|array * @var string|array
*/ */
var $conditions; var $conditions;
/** /**
* group list * group list
* @var string|array * @var string|array
*/ */
var $groups; var $groups;
/** /**
* order list * order list
* @var array * @var array
*/ */
var $orderby; var $orderby;
/** /**
* limit count * limit count
* @var int * @var int
@ -98,7 +107,11 @@ class Query extends Object
$this->action = $action; $this->action = $action;
$this->priority = $priority; $this->priority = $priority;
if(!isset($tables)) return; if(!isset($tables))
{
return;
}
$this->columns = $this->setColumns($columns); $this->columns = $this->setColumns($columns);
$this->tables = $this->setTables($tables); $this->tables = $this->setTables($tables);
$this->conditions = $this->setConditions($conditions); $this->conditions = $this->setConditions($conditions);
@ -153,7 +166,10 @@ class Query extends Object
return; return;
} }
if(!is_array($columns)) $columns = array($columns); if(!is_array($columns))
{
$columns = array($columns);
}
$this->columns = $columns; $this->columns = $columns;
} }
@ -167,7 +183,10 @@ class Query extends Object
return; return;
} }
if(!is_array($tables)) $tables = array($tables); if(!is_array($tables))
{
$tables = array($tables);
}
$this->tables = $tables; $this->tables = $tables;
} }
@ -180,34 +199,58 @@ class Query extends Object
function setConditions($conditions) function setConditions($conditions)
{ {
$this->conditions = array(); $this->conditions = array();
if(!isset($conditions) || count($conditions) === 0) return; if(!isset($conditions) || count($conditions) === 0)
if(!is_array($conditions)) $conditions = array($conditions); {
return;
}
if(!is_array($conditions))
{
$conditions = array($conditions);
}
foreach($conditions as $conditionGroup) foreach($conditions as $conditionGroup)
{ {
if($conditionGroup->show()) $this->conditions[] = $conditionGroup; if($conditionGroup->show())
{
$this->conditions[] = $conditionGroup;
}
} }
} }
function setGroups($groups) function setGroups($groups)
{ {
if(!isset($groups) || count($groups) === 0) return; if(!isset($groups) || count($groups) === 0)
if(!is_array($groups)) $groups = array($groups); {
return;
}
if(!is_array($groups))
{
$groups = array($groups);
}
$this->groups = $groups; $this->groups = $groups;
} }
function setOrder($order) function setOrder($order)
{ {
if(!isset($order) || count($order) === 0) return; if(!isset($order) || count($order) === 0)
if(!is_array($order)) $order = array($order); {
return;
}
if(!is_array($order))
{
$order = array($order);
}
$this->orderby = $order; $this->orderby = $order;
} }
function setLimit($limit = NULL) function setLimit($limit = NULL)
{ {
if(!isset($limit)) return; if(!isset($limit))
{
return;
}
$this->limit = $limit; $this->limit = $limit;
} }
@ -278,6 +321,7 @@ class Query extends Object
$this->setLimit($limit); $this->setLimit($limit);
return $this; return $this;
} }
// END Fluent interface // END Fluent interface
function getAction() function getAction()
@ -304,10 +348,13 @@ class Query extends Object
function getClickCountColumns() function getClickCountColumns()
{ {
$click_count_columns = array(); $click_count_columns = array();
foreach($this->columns as $column){ foreach($this->columns as $column)
{
if($column->show() && is_a($column, 'ClickCountExpression')) if($column->show() && is_a($column, 'ClickCountExpression'))
{
$click_count_columns[] = $column; $click_count_columns[] = $column;
} }
}
return $click_count_columns; return $click_count_columns;
} }
@ -321,13 +368,17 @@ class Query extends Object
foreach($this->columns as $column) foreach($this->columns as $column)
{ {
if($column->show()) if($column->show())
{
if($column->isSubquery()) if($column->isSubquery())
{ {
$select[] = $column->toString($with_values) . ' as ' . $column->getAlias(); $select[] = $column->toString($with_values) . ' as ' . $column->getAlias();
} }
else else
{
$select[] = $column->getExpression($with_values); $select[] = $column->getExpression($with_values);
} }
}
}
return trim(implode($select, ', ')); return trim(implode($select, ', '));
} }
@ -341,8 +392,10 @@ class Query extends Object
foreach($this->columns as $column) foreach($this->columns as $column)
{ {
if($column->show()) if($column->show())
{
$update[] = $column->getExpression($with_values); $update[] = $column->getExpression($with_values);
} }
}
return trim(implode($update, ', ')); return trim(implode($update, ', '));
} }
@ -401,14 +454,26 @@ class Query extends Object
$simple_table_count = 0; $simple_table_count = 0;
foreach($this->tables as $table) foreach($this->tables as $table)
{ {
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' '; if($table->isJoinTable() || !$simple_table_count)
else $from .= ', '.$table->toString($with_values) . ' '; {
$from .= $table->toString($with_values) . ' ';
}
else
{
$from .= ', ' . $table->toString($with_values) . ' ';
}
if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' '; if(is_a($table, 'Subquery'))
{
$from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
}
$simple_table_count++; $simple_table_count++;
} }
if(trim($from) == '') return ''; if(trim($from) == '')
{
return '';
}
return $from; return $from;
} }
@ -439,7 +504,9 @@ class Query extends Object
{ {
if($condition_count !== 0) if($condition_count !== 0)
{
$where = '(' . $where . ') '; $where = '(' . $where . ') ';
}
foreach($this->orderby as $order) foreach($this->orderby as $order)
{ {
@ -448,7 +515,9 @@ class Query extends Object
{ {
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and'); $opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if($condition_count === 0) if($condition_count === 0)
{
$opt_condition->setPipe(""); $opt_condition->setPipe("");
}
$where .= $opt_condition->toString($with_values) . ' '; $where .= $opt_condition->toString($with_values) . ' ';
$condition_count++; $condition_count++;
} }
@ -465,8 +534,13 @@ class Query extends Object
function getGroupByString() function getGroupByString()
{ {
$groupBy = ''; $groupBy = '';
if($this->groups) if($this->groups[0] !== "") if($this->groups)
{
if($this->groups[0] !== "")
{
$groupBy = implode(', ', $this->groups); $groupBy = implode(', ', $this->groups);
}
}
return $groupBy; return $groupBy;
} }
@ -478,7 +552,10 @@ class Query extends Object
{ {
if(!$this->_orderByString) if(!$this->_orderByString)
{ {
if(count($this->orderby) === 0) return ''; if(count($this->orderby) === 0)
{
return '';
}
$orderBy = ''; $orderBy = '';
foreach($this->orderby as $order) foreach($this->orderby as $order)
{ {
@ -533,7 +610,10 @@ class Query extends Object
if($table->isJoinTable() || is_a($table, 'Subquery')) if($table->isJoinTable() || is_a($table, 'Subquery'))
{ {
$args = $table->getArguments(); $args = $table->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args); if($args)
{
$this->arguments = array_merge($this->arguments, $args);
}
} }
} }
} }
@ -547,30 +627,43 @@ class Query extends Object
if($column->show()) if($column->show())
{ {
$args = $column->getArguments(); $args = $column->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args); if($args)
{
$this->arguments = array_merge($this->arguments, $args);
}
} }
} }
} }
// Condition arguments // Condition arguments
if(count($this->conditions) > 0) if(count($this->conditions) > 0)
{
foreach($this->conditions as $conditionGroup) foreach($this->conditions as $conditionGroup)
{ {
$args = $conditionGroup->getArguments(); $args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args); if(count($args) > 0)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
} }
// Navigation arguments // Navigation arguments
if(count($this->orderby) > 0) if(count($this->orderby) > 0)
{
foreach($this->orderby as $order) foreach($this->orderby as $order)
{ {
$args = $order->getArguments(); $args = $order->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args); if(count($args) > 0)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
} }
} }
return $this->arguments; return $this->arguments;
} }
}
}
/* End of file Query.class.php */ /* End of file Query.class.php */
/* Location: ./classes/db/queryparts/Query.class.php */ /* Location: ./classes/db/queryparts/Query.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts * @package /classes/db/queryparts
@ -6,11 +7,13 @@
*/ */
class Subquery extends Query class Subquery extends Query
{ {
/** /**
* table alias * table alias
* @var string * @var string
*/ */
var $alias; var $alias;
/** /**
* join type * join type
* @var string * @var string
@ -52,7 +55,10 @@ class Subquery extends Query
function isJoinTable() function isJoinTable()
{ {
if($this->join_type) return true; if($this->join_type)
{
return true;
}
return false; return false;
} }
@ -61,13 +67,13 @@ class Subquery extends Query
$oDB = &DB::getInstance(); $oDB = &DB::getInstance();
return '(' . $oDB->getSelectSql($this, $with_values) . ')'; return '(' . $oDB->getSelectSql($this, $with_values) . ')';
} }
function isSubquery() function isSubquery()
{ {
return true; return true;
} }
} }
/* End of file Subquery.class.php */ /* End of file Subquery.class.php */
/* Location: ./classes/db/queryparts/Subquery.class.php */ /* Location: ./classes/db/queryparts/Subquery.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition * @package /classes/db/queryparts/condition
@ -6,12 +7,14 @@
*/ */
class Condition class Condition
{ {
/** /**
* column name * column name
* @var string * @var string
*/ */
var $column_name; var $column_name;
var $argument; var $argument;
/** /**
* operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail', * operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail',
* 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between' * 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between'
@ -19,14 +22,13 @@ class Condition
* @var string * @var string
*/ */
var $operation; var $operation;
/** /**
* pipe can use 'and', 'or'... * pipe can use 'and', 'or'...
* @var string * @var string
*/ */
var $pipe; var $pipe;
var $_value; var $_value;
var $_show; var $_show;
var $_value_to_string; var $_value_to_string;
@ -135,17 +137,34 @@ class Condition
case 'not': case 'not':
case 'notequal' : case 'notequal' :
// if variable is not set or is not string or number, return // if variable is not set or is not string or number, return
if(!isset($this->_value)) { $this->_show = false; break;} if(!isset($this->_value))
if($this->_value === '') { $this->_show = false; break; } {
$this->_show = false;
break;
}
if($this->_value === '')
{
$this->_show = false;
break;
}
$tmpArray = array('string' => 1, 'integer' => 1); $tmpArray = array('string' => 1, 'integer' => 1);
if(!isset($tmpArray[gettype($this->_value)])) if(!isset($tmpArray[gettype($this->_value)]))
{ {
$this->_show = false; break; $this->_show = false;
break;
} }
break; break;
case 'between' : case 'between' :
if(!is_array($this->_value)) { $this->_show = false; break;} if(!is_array($this->_value))
if(count($this->_value)!=2) {$this->_show = false; break;} {
$this->_show = false;
break;
}
if(count($this->_value) != 2)
{
$this->_show = false;
break;
}
case 'null': case 'null':
case 'notnull': case 'notnull':
break; break;
@ -232,6 +251,7 @@ class Condition
break; break;
} }
} }
} }
/* End of file Condition.class.php */ /* End of file Condition.class.php */
/* Location: ./classes/db/queryparts/condition/Condition.class.php */ /* Location: ./classes/db/queryparts/condition/Condition.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition * @package /classes/db/queryparts/condition
@ -6,17 +7,18 @@
*/ */
class ConditionGroup class ConditionGroup
{ {
/** /**
* condition list * condition list
* @var array * @var array
*/ */
var $conditions; var $conditions;
/** /**
* pipe can use 'and', 'or'... * pipe can use 'and', 'or'...
* @var string * @var string
*/ */
var $pipe; var $pipe;
var $_group; var $_group;
var $_show; var $_show;
@ -32,10 +34,18 @@ class ConditionGroup
foreach($conditions as $condition) foreach($conditions as $condition)
{ {
if($condition->show()) if($condition->show())
{
$this->conditions[] = $condition; $this->conditions[] = $condition;
} }
if(count($this->conditions) === 0) $this->_show = false; }
else $this->_show = true; if(count($this->conditions) === 0)
{
$this->_show = false;
}
else
{
$this->_show = true;
}
$this->pipe = $pipe; $this->pipe = $pipe;
} }
@ -47,7 +57,10 @@ class ConditionGroup
function setPipe($pipe) function setPipe($pipe)
{ {
if($this->pipe !== $pipe) $this->_group = null; if($this->pipe !== $pipe)
{
$this->_group = null;
}
$this->pipe = $pipe; $this->pipe = $pipe;
} }
@ -65,7 +78,10 @@ class ConditionGroup
foreach($this->conditions as $condition) foreach($this->conditions as $condition)
{ {
if($cond_indx === 0) $condition->setPipe(""); if($cond_indx === 0)
{
$condition->setPipe("");
}
$group .= $condition->toString($with_value) . ' '; $group .= $condition->toString($with_value) . ' ';
$cond_indx++; $cond_indx++;
} }
@ -90,10 +106,14 @@ class ConditionGroup
foreach($this->conditions as $condition) foreach($this->conditions as $condition)
{ {
$arg = $condition->getArgument(); $arg = $condition->getArgument();
if($arg) $args[] = $arg; if($arg)
{
$args[] = $arg;
}
} }
return $args; return $args;
} }
} }
/* End of file ConditionGroup.class.php */ /* End of file ConditionGroup.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionGroup.class.php */ /* Location: ./classes/db/queryparts/condition/ConditionGroup.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition * @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/ */
class ConditionSubquery extends Condition class ConditionSubquery extends Condition
{ {
/** /**
* constructor * constructor
* @param string $column_name * @param string $column_name
@ -19,6 +21,7 @@ class ConditionSubquery extends Condition
parent::Condition($column_name, $argument, $operation, $pipe); parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $this->argument->toString(); $this->_value = $this->argument->toString();
} }
} }
/* End of file ConditionSubquery.class.php */ /* End of file ConditionSubquery.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionSubquery.class.php */ /* Location: ./classes/db/queryparts/condition/ConditionSubquery.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition * @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/ */
class ConditionWithArgument extends Condition class ConditionWithArgument extends Condition
{ {
/** /**
* constructor * constructor
* @param string $column_name * @param string $column_name
@ -16,14 +18,19 @@ class ConditionWithArgument extends Condition
*/ */
function ConditionWithArgument($column_name, $argument, $operation, $pipe = "") function ConditionWithArgument($column_name, $argument, $operation, $pipe = "")
{ {
if($argument === null) { $this->_show = false; return; } if($argument === null)
{
$this->_show = false;
return;
}
parent::Condition($column_name, $argument, $operation, $pipe); parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $argument->getValue(); $this->_value = $argument->getValue();
} }
function getArgument() function getArgument()
{ {
if(!$this->show()) return; if(!$this->show())
return;
return $this->argument; return $this->argument;
} }
@ -38,8 +45,14 @@ class ConditionWithArgument extends Condition
if(is_array($value)) if(is_array($value))
{ {
$q = ''; $q = '';
foreach ($value as $v) $q .= '?,'; foreach($value as $v)
if($q !== '') $q = substr($q, 0, -1); {
$q .= '?,';
}
if($q !== '')
{
$q = substr($q, 0, -1);
}
$q = '(' . $q . ')'; $q = '(' . $q . ')';
} }
else else
@ -64,8 +77,14 @@ class ConditionWithArgument extends Condition
{ {
if(!isset($this->_show)) if(!isset($this->_show))
{ {
if(!$this->argument->isValid()) $this->_show = false; if(!$this->argument->isValid())
if($this->_value === '\'\'') $this->_show = false; {
$this->_show = false;
}
if($this->_value === '\'\'')
{
$this->_show = false;
}
if(!isset($this->_show)) if(!isset($this->_show))
{ {
return parent::show(); return parent::show();
@ -73,6 +92,7 @@ class ConditionWithArgument extends Condition
} }
return $this->_show; return $this->_show;
} }
} }
/* End of file ConditionWithArgument.class.php */ /* End of file ConditionWithArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithArgument.class.php */ /* Location: ./classes/db/queryparts/condition/ConditionWithArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition * @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/ */
class ConditionWithoutArgument extends Condition class ConditionWithoutArgument extends Condition
{ {
/** /**
* constructor * constructor
* @param string $column_name * @param string $column_name
@ -20,13 +22,18 @@ class ConditionWithoutArgument extends Condition
$tmpArray = array('in' => 1, 'notin' => 1, 'not_in' => 1); $tmpArray = array('in' => 1, 'notin' => 1, 'not_in' => 1);
if(isset($tmpArray[$operation])) if(isset($tmpArray[$operation]))
{ {
if(is_array($argument)) $argument = implode($argument, ','); if(is_array($argument))
{
$argument = implode($argument, ',');
}
$this->_value = '(' . $argument . ')'; $this->_value = '(' . $argument . ')';
} }
else else
{
$this->_value = $argument; $this->_value = $argument;
} }
} }
}
/* End of file ConditionWithoutArgument.class.php */ /* End of file ConditionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithoutArgument.class.php */ /* Location: ./classes/db/queryparts/condition/ConditionWithoutArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* ClickCountExpression * ClickCountExpression
* @author Arnia Software * @author Arnia Software
@ -7,6 +8,7 @@
*/ */
class ClickCountExpression extends SelectExpression class ClickCountExpression extends SelectExpression
{ {
/** /**
* click count * click count
* @var bool * @var bool
@ -53,7 +55,7 @@ class ClickCountExpression extends SelectExpression
return "$this->column_name"; return "$this->column_name";
} }
} }
}
}
/* End of file ClickCountExpression.class.php */ /* End of file ClickCountExpression.class.php */
/* Location: ./classes/db/queryparts/expression/ClickCountExpression.class.php */ /* Location: ./classes/db/queryparts/expression/ClickCountExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* DeleteExpression * DeleteExpression
* *
@ -9,6 +10,7 @@
*/ */
class DeleteExpression extends Expression class DeleteExpression extends Expression
{ {
/** /**
* column value * column value
* @var mixed * @var mixed
@ -39,15 +41,22 @@ class DeleteExpression extends Expression
function getValue() function getValue()
{ {
// TODO Escape value according to column type instead of variable type // TODO Escape value according to column type instead of variable type
if(!is_numeric($this->value)) return "'".$this->value."'"; if(!is_numeric($this->value))
{
return "'" . $this->value . "'";
}
return $this->value; return $this->value;
} }
function show() function show()
{ {
if(!$this->value) return false; if(!$this->value)
{
return false;
}
return true; return true;
} }
} }
/* End of file DeleteExpression.class.php */ /* End of file DeleteExpression.class.php */
/* Location: ./classes/db/queryparts/expression/DeleteExpression.class.php */ /* Location: ./classes/db/queryparts/expression/DeleteExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Expression * Expression
* Represents an expression used in select/update/insert/delete statements * Represents an expression used in select/update/insert/delete statements
@ -13,6 +14,7 @@
*/ */
class Expression class Expression
{ {
/** /**
* column name * column name
* @var string * @var string
@ -45,8 +47,9 @@ class Expression
*/ */
function getExpression() function getExpression()
{ {
}
} }
}
/* End of file Expression.class.php */ /* End of file Expression.class.php */
/* Location: ./classes/db/queryparts/expression/Expression.class.php */ /* Location: ./classes/db/queryparts/expression/Expression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* InsertExpression * InsertExpression
* *
@ -8,6 +9,7 @@
*/ */
class InsertExpression extends Expression class InsertExpression extends Expression
{ {
/** /**
* argument * argument
* @var object * @var object
@ -29,15 +31,23 @@ class InsertExpression extends Expression
function getValue($with_values = true) function getValue($with_values = true)
{ {
if($with_values) if($with_values)
{
return $this->argument->getValue(); return $this->argument->getValue();
}
return '?'; return '?';
} }
function show() function show()
{ {
if(!$this->argument) return false; if(!$this->argument)
{
return false;
}
$value = $this->argument->getValue(); $value = $this->argument->getValue();
if(!isset($value)) return false; if(!isset($value))
{
return false;
}
return true; return true;
} }
@ -49,10 +59,15 @@ class InsertExpression extends Expression
function getArguments() function getArguments()
{ {
if($this->argument) if($this->argument)
{
return array($this->argument); return array($this->argument);
}
else else
{
return array(); return array();
} }
} }
}
/* End of file InsertExpression.class.php */ /* End of file InsertExpression.class.php */
/* Location: ./classes/db/queryparts/expression/InsertExpression.class.php */ /* Location: ./classes/db/queryparts/expression/InsertExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* SelectExpression * SelectExpression
* Represents an expresion that appears in the select clause * Represents an expresion that appears in the select clause
@ -15,6 +16,7 @@
*/ */
class SelectExpression extends Expression class SelectExpression extends Expression
{ {
/** /**
* column alias name * column alias name
* @var string * @var string
@ -61,6 +63,7 @@ class SelectExpression extends Expression
{ {
return false; return false;
} }
} }
/* End of file SelectExpression.class.php */ /* End of file SelectExpression.class.php */
/* Location: ./classes/db/queryparts/expression/SelectExpression.class.php */ /* Location: ./classes/db/queryparts/expression/SelectExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* StarExpression * StarExpression
* Represents the * in 'select * from ...' statements * Represents the * in 'select * from ...' statements
@ -9,6 +10,7 @@
*/ */
class StarExpression extends SelectExpression class StarExpression extends SelectExpression
{ {
/** /**
* constructor, set the column to asterisk * constructor, set the column to asterisk
* @return void * @return void
@ -28,6 +30,7 @@ class StarExpression extends SelectExpression
// StarExpression has no arguments // StarExpression has no arguments
return array(); return array();
} }
} }
/* End of file StarExpression.class.php */ /* End of file StarExpression.class.php */
/* Location: ./classes/db/queryparts/expression/StarExpression.class.php */ /* Location: ./classes/db/queryparts/expression/StarExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* UpdateExpression * UpdateExpression
* *
@ -8,6 +9,7 @@
*/ */
class UpdateExpression extends Expression class UpdateExpression extends Expression
{ {
/** /**
* argument * argument
* @var object * @var object
@ -33,7 +35,9 @@ class UpdateExpression extends Expression
function getExpression($with_value = true) function getExpression($with_value = true)
{ {
if($with_value) if($with_value)
{
return $this->getExpressionWithValue(); return $this->getExpressionWithValue();
}
return $this->getExpressionWithoutValue(); return $this->getExpressionWithoutValue();
} }
@ -46,7 +50,9 @@ class UpdateExpression extends Expression
$value = $this->argument->getValue(); $value = $this->argument->getValue();
$operation = $this->argument->getColumnOperation(); $operation = $this->argument->getColumnOperation();
if(isset($operation)) if(isset($operation))
{
return "$this->column_name = $this->column_name $operation $value"; return "$this->column_name = $this->column_name $operation $value";
}
return "$this->column_name = $value"; return "$this->column_name = $value";
} }
@ -59,7 +65,9 @@ class UpdateExpression extends Expression
{ {
$operation = $this->argument->getColumnOperation(); $operation = $this->argument->getColumnOperation();
if(isset($operation)) if(isset($operation))
{
return "$this->column_name = $this->column_name $operation ?"; return "$this->column_name = $this->column_name $operation ?";
}
return "$this->column_name = ?"; return "$this->column_name = ?";
} }
@ -67,15 +75,24 @@ class UpdateExpression extends Expression
{ {
// TODO Escape value according to column type instead of variable type // TODO Escape value according to column type instead of variable type
$value = $this->argument->getValue(); $value = $this->argument->getValue();
if(!is_numeric($value)) return "'".$value."'"; if(!is_numeric($value))
{
return "'" . $value . "'";
}
return $value; return $value;
} }
function show() function show()
{ {
if(!$this->argument) return false; if(!$this->argument)
{
return false;
}
$value = $this->argument->getValue(); $value = $this->argument->getValue();
if(!isset($value)) return false; if(!isset($value))
{
return false;
}
return true; return true;
} }
@ -87,11 +104,15 @@ class UpdateExpression extends Expression
function getArguments() function getArguments()
{ {
if($this->argument) if($this->argument)
{
return array($this->argument); return array($this->argument);
}
else else
{
return array(); return array();
} }
} }
}
/* End of file UpdateExpression.class.php */ /* End of file UpdateExpression.class.php */
/* Location: ./classes/db/queryparts/expression/UpdateExpression.class.php */ /* Location: ./classes/db/queryparts/expression/UpdateExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* UpdateExpression * UpdateExpression
* *
@ -8,6 +9,7 @@
*/ */
class UpdateExpressionWithoutArgument extends UpdateExpression class UpdateExpressionWithoutArgument extends UpdateExpression
{ {
/** /**
* argument * argument
* @var object * @var object
@ -35,15 +37,24 @@ class UpdateExpressionWithoutArgument extends UpdateExpression
{ {
// TODO Escape value according to column type instead of variable type // TODO Escape value according to column type instead of variable type
$value = $this->argument; $value = $this->argument;
if(!is_numeric($value)) return "'".$value."'"; if(!is_numeric($value))
{
return "'" . $value . "'";
}
return $value; return $value;
} }
function show() function show()
{ {
if(!$this->argument) return false; if(!$this->argument)
{
return false;
}
$value = $this->argument; $value = $this->argument;
if(!isset($value)) return false; if(!isset($value))
{
return false;
}
return true; return true;
} }
@ -56,6 +67,7 @@ class UpdateExpressionWithoutArgument extends UpdateExpression
{ {
return array(); return array();
} }
} }
/* End of file UpdateExpressionWithoutArgument.class.php */ /* End of file UpdateExpressionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php */ /* Location: ./classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/limit * @package /classes/db/queryparts/limit
@ -6,21 +7,25 @@
*/ */
class Limit class Limit
{ {
/** /**
* start number * start number
* @var int * @var int
*/ */
var $start; var $start;
/** /**
* list count * list count
* @var int * @var int
*/ */
var $list_count; var $list_count;
/** /**
* page count * page count
* @var int * @var int
*/ */
var $page_count; var $page_count;
/** /**
* current page * current page
* @var int * @var int
@ -53,8 +58,14 @@ class Limit
*/ */
function isPageHandler() function isPageHandler()
{ {
if ($this->page)return true; if($this->page)
else return false; {
return true;
}
else
{
return false;
}
} }
function getOffset() function getOffset()
@ -69,9 +80,16 @@ class Limit
function toString() function toString()
{ {
if ($this->page) return $this->start . ' , ' . $this->list_count->getValue(); if($this->page)
else return $this->list_count->getValue(); {
return $this->start . ' , ' . $this->list_count->getValue();
} }
else
{
return $this->list_count->getValue();
}
}
} }
/* End of file Limit.class.php */ /* End of file Limit.class.php */
/* Location: ./classes/db/limit/Limit.class.php */ /* Location: ./classes/db/limit/Limit.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/order * @package /classes/db/queryparts/order
@ -6,11 +7,13 @@
*/ */
class OrderByColumn class OrderByColumn
{ {
/** /**
* column name * column name
* @var string * @var string
*/ */
var $column_name; var $column_name;
/** /**
* sort order * sort order
* @var string * @var string
@ -46,10 +49,15 @@ class OrderByColumn
{ {
$args = array(); $args = array();
if(is_a($this->column_name, 'Argument')) if(is_a($this->column_name, 'Argument'))
{
$args[] = $this->column_name; $args[] = $this->column_name;
}
if(is_a($this->sort_order, 'Argument')) if(is_a($this->sort_order, 'Argument'))
{
$args[] = $this->sort_order; $args[] = $this->sort_order;
} }
} }
}
/* End of file OrderByColumn.class.php */ /* End of file OrderByColumn.class.php */
/* Location: ./classes/db/order/OrderByColumn.class.php */ /* Location: ./classes/db/order/OrderByColumn.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table * @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/ */
class CubridTableWithHint extends Table class CubridTableWithHint extends Table
{ {
/** /**
* table name * table name
* @var string * @var string
*/ */
var $name; var $name;
/** /**
* table alias * table alias
* @var string * @var string
*/ */
var $alias; var $alias;
/** /**
* index hint list * index hint list
* @var array * @var array
@ -61,6 +65,7 @@ class CubridTableWithHint extends Table
$result = substr($result, 0, -2); $result = substr($result, 0, -2);
return $result; return $result;
} }
} }
/* End of file CubridTableWithHint.class.php */ /* End of file CubridTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/CubridTableWithHint.class.php */ /* Location: ./classes/db/queryparts/table/CubridTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table * @package /classes/db/queryparts/table
@ -6,11 +7,13 @@
*/ */
class IndexHint class IndexHint
{ {
/** /**
* index name * index name
* @var string * @var string
*/ */
var $index_name; var $index_name;
/** /**
* index hint type, ex) IGNORE, FORCE, USE... * index hint type, ex) IGNORE, FORCE, USE...
* @var string * @var string
@ -38,6 +41,7 @@ class IndexHint
{ {
return $this->index_hint_type; return $this->index_hint_type;
} }
} }
/* End of file IndexHint.class.php */ /* End of file IndexHint.class.php */
/* Location: ./classes/db/queryparts/table/IndexHint.class.php */ /* Location: ./classes/db/queryparts/table/IndexHint.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* class JoinTable * class JoinTable
* $conditions in an array of Condition objects * $conditions in an array of Condition objects
@ -9,11 +10,13 @@
*/ */
class JoinTable extends Table class JoinTable extends Table
{ {
/** /**
* join type * join type
* @var string * @var string
*/ */
var $join_type; var $join_type;
/** /**
* condition list * condition list
* @var array * @var array
@ -41,7 +44,9 @@ class JoinTable extends Table
$part .= $this->alias ? ' as ' . $this->alias : ''; $part .= $this->alias ? ' as ' . $this->alias : '';
$part .= ' on '; $part .= ' on ';
foreach($this->conditions as $conditionGroup) foreach($this->conditions as $conditionGroup)
{
$part .= $conditionGroup->toString($with_value); $part .= $conditionGroup->toString($with_value);
}
return $part; return $part;
} }
@ -54,9 +59,12 @@ class JoinTable extends Table
{ {
$args = array(); $args = array();
foreach($this->conditions as $conditionGroup) foreach($this->conditions as $conditionGroup)
{
$args = array_merge($args, $conditionGroup->getArguments()); $args = array_merge($args, $conditionGroup->getArguments());
}
return $args; return $args;
} }
} }
/* End of file JoinTable.class.php */ /* End of file JoinTable.class.php */
/* Location: ./classes/db/queryparts/table/JoinTable.class.php */ /* Location: ./classes/db/queryparts/table/JoinTable.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table * @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/ */
class MssqlTableWithHint extends Table class MssqlTableWithHint extends Table
{ {
/** /**
* table name * table name
* @var string * @var string
*/ */
var $name; var $name;
/** /**
* table alias * table alias
* @var string * @var string
*/ */
var $alias; var $alias;
/** /**
* index hint type, ex) IGNORE, FORCE, USE... * index hint type, ex) IGNORE, FORCE, USE...
* @var array * @var array
@ -45,14 +49,17 @@ class MssqlTableWithHint extends Table
{ {
$index_hint_type = $index_hint->getIndexHintType(); $index_hint_type = $index_hint->getIndexHintType();
if(isset($indexTypeList[$index_hint_type])) if(isset($indexTypeList[$index_hint_type]))
{
$index_hint_string .= 'INDEX(' . $index_hint->getIndexName() . '), '; $index_hint_string .= 'INDEX(' . $index_hint->getIndexName() . '), ';
} }
}
if($index_hint_string != '') if($index_hint_string != '')
{ {
$result .= ' WITH(' . substr($index_hint_string, 0, -2) . ') '; $result .= ' WITH(' . substr($index_hint_string, 0, -2) . ') ';
} }
return $result; return $result;
} }
} }
/* End of file MssqlTableWithHint.class.php */ /* End of file MssqlTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/MssqlTableWithHint.class.php */ /* Location: ./classes/db/queryparts/table/MssqlTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table * @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/ */
class MysqlTableWithHint extends Table class MysqlTableWithHint extends Table
{ {
/** /**
* table name * table name
* @var string * @var string
*/ */
var $name; var $name;
/** /**
* table alias * table alias
* @var string * @var string
*/ */
var $alias; var $alias;
/** /**
* index hint type, ex) IGNORE, FORCE, USE... * index hint type, ex) IGNORE, FORCE, USE...
* @var array * @var array
@ -39,13 +43,24 @@ class MysqlTableWithHint extends Table
{ {
$result = parent::toString(); $result = parent::toString();
$use_index_hint = ''; $force_index_hint = ''; $ignore_index_hint = ''; $use_index_hint = '';
$force_index_hint = '';
$ignore_index_hint = '';
foreach($this->index_hints_list as $index_hint) foreach($this->index_hints_list as $index_hint)
{ {
$index_hint_type = $index_hint->getIndexHintType(); $index_hint_type = $index_hint->getIndexHintType();
if($index_hint_type == 'USE') $use_index_hint .= $index_hint->getIndexName() . ', '; if($index_hint_type == 'USE')
else if($index_hint_type == 'FORCE') $force_index_hint .= $index_hint->getIndexName() . ', '; {
else if($index_hint_type == 'IGNORE') $ignore_index_hint .= $index_hint->getIndexName() . ', '; $use_index_hint .= $index_hint->getIndexName() . ', ';
}
else if($index_hint_type == 'FORCE')
{
$force_index_hint .= $index_hint->getIndexName() . ', ';
}
else if($index_hint_type == 'IGNORE')
{
$ignore_index_hint .= $index_hint->getIndexName() . ', ';
}
} }
if($use_index_hint != '') if($use_index_hint != '')
{ {
@ -61,6 +76,7 @@ class MysqlTableWithHint extends Table
} }
return $result; return $result;
} }
} }
/* End of file MysqlTableWithHint.class.php */ /* End of file MysqlTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/MysqlTableWithHint.class.php */ /* Location: ./classes/db/queryparts/table/MysqlTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table * @package /classes/db/queryparts/table
@ -6,11 +7,13 @@
*/ */
class Table class Table
{ {
/** /**
* table name * table name
* @var string * @var string
*/ */
var $name; var $name;
/** /**
* table alias * table alias
* @var string * @var string
@ -49,7 +52,7 @@ class Table
{ {
return false; return false;
} }
}
}
/* End of file Table.class.php */ /* End of file Table.class.php */
/* Location: ./classes/db/queryparts/table/Table.class.php */ /* Location: ./classes/db/queryparts/table/Table.class.php */