merge branche 1.5.0 to trunk (~r10525)

git-svn-id: http://xe-core.googlecode.com/svn/trunk@10534 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2012-04-05 01:14:47 +00:00
parent 361cd64c16
commit c9047de30d
15 changed files with 643 additions and 195 deletions

View file

@ -239,7 +239,12 @@ class Context {
$oInstallController = &getController('install'); $oInstallController = &getController('install');
$oInstallController->makeConfigFile(); $oInstallController->makeConfigFile();
} }
if(!$db_info->use_prepared_statements)
{
$db_info->use_prepared_statements = 'Y';
}
if(!$db_info->time_zone) $db_info->time_zone = date('O'); if(!$db_info->time_zone) $db_info->time_zone = date('O');
$GLOBALS['_time_zone'] = $db_info->time_zone; $GLOBALS['_time_zone'] = $db_info->time_zone;

View file

@ -82,6 +82,8 @@
var $db_type; ///< stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set var $db_type; ///< stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set
var $use_prepared_statements; ///< flag to decide if class prepared statements or not (when supported); can be changed from db.config.info
/** /**
* @brief returns instance of certain db type * @brief returns instance of certain db type
* @param[in] $db_type type of db * @param[in] $db_type type of db
@ -762,6 +764,7 @@
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;
} }
function __connect(){ function __connect(){

View file

@ -169,6 +169,55 @@
**/ **/
function __query($query, $connection) function __query($query, $connection)
{ {
if($this->use_prepared_statements == 'Y')
{
$req = @cubrid_prepare($connection, $query);
$position = 0;
if($this->param)
{
foreach($this->param as $param)
{
$value = $param->getUnescapedValue();
$type = $param->getType();
if($param->isColumnName()) continue;
switch($type)
{
case 'number' :
$bind_type = 'numeric';
break;
case 'varchar' :
$bind_type = 'string';
break;
default:
$bind_type = 'string';
}
if(is_array($value)){
foreach($value as $v)
{
cubrid_bind($req, ++$position, $v, $bind_type);
}
}
else
{
cubrid_bind($req, ++$position, $value, $bind_type);
}
}
}
$result = @cubrid_execute($req);
if(!$result)
{
$code = cubrid_error_code ();
$msg = cubrid_error_msg ();
$this->setError ($code, $msg);
}
return $req;
}
// Execute the query // Execute the query
$result = @cubrid_execute ($connection, $query); $result = @cubrid_execute ($connection, $query);
// error check // error check
@ -190,6 +239,11 @@
$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')
{
}
// TODO Improve this piece of code // TODO Improve this piece of code
// This code trims values from char type columns // This code trims values from char type columns
$col_types = cubrid_column_types ($result); $col_types = cubrid_column_types ($result);
@ -547,9 +601,14 @@
/** /**
* @brief handles insertAct * @brief handles insertAct
**/ **/
function _executeInsertAct($queryObject) function _executeInsertAct($queryObject, $with_values = true)
{ {
$query = $this->getInsertSql($queryObject); if($this->use_prepared_statements == 'Y')
{
$this->param = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getInsertSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
$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):'';
@ -558,22 +617,27 @@
if ($result && !$this->transaction_started) { if ($result && !$this->transaction_started) {
$this->_commit(); $this->_commit();
} }
unset($this->param);
return $result; return $result;
} }
/** /**
* @brief handles updateAct * @brief handles updateAct
**/ **/
function _executeUpdateAct($queryObject) function _executeUpdateAct($queryObject, $with_values = true)
{ {
$query = $this->getUpdateSql($queryObject); if($this->use_prepared_statements == 'Y')
{
$this->param = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getUpdateSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
$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);
return $result; return $result;
} }
@ -581,15 +645,21 @@
/** /**
* @brief handles deleteAct * @brief handles deleteAct
**/ **/
function _executeDeleteAct($queryObject) function _executeDeleteAct($queryObject, $with_values = true)
{ {
$query = $this->getDeleteSql($queryObject); if($this->use_prepared_statements == 'Y')
{
$this->param = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getDeleteSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
$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);
return $result; return $result;
} }
@ -599,25 +669,32 @@
* to get a specific page list easily in select statement,\n * to get a specific page list easily in select statement,\n
* a method, navigation, is used * a method, navigation, is used
**/ **/
function _executeSelectAct($queryObject, $connection = null){ function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
$limit = $queryObject->getLimit(); if ($this->use_prepared_statements == 'Y') {
if ($limit && $limit->isPageHandler()) $this->param = $queryObject->getArguments();
return $this->queryPageLimit($queryObject, $result, $connection); $with_values = false;
else { }
$query = $this->getSelectSql($queryObject); $limit = $queryObject->getLimit();
if(is_a($query, 'Object')) return; if ($limit && $limit->isPageHandler())
return $this->queryPageLimit($queryObject, $connection, $with_values);
else {
$query = $this->getSelectSql($queryObject, $with_values);
if (is_a($query, 'Object'))
return;
$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);
$data = $this->_fetch($result); $data = $this->_fetch($result);
$buff = new Object (); $buff = new Object ();
$buff->data = $data; $buff->data = $data;
return $buff;
} unset($this->param);
return $buff;
}
} }
function queryError($queryObject){ function queryError($queryObject){
@ -634,11 +711,11 @@
return; return;
} }
function queryPageLimit($queryObject, $result, $connection){ function queryPageLimit($queryObject, $connection, $with_values){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
// Total count // Total count
$temp_where = $queryObject->getWhereString(true, false); $temp_where = $queryObject->getWhereString($with_values, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where)); $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
@ -677,11 +754,11 @@
} }
$start_count = ($page - 1) * $list_count; $start_count = ($page - 1) * $list_count;
$query = $this->getSelectPageSql($queryObject, true, $start_count, $list_count); $query = $this->getSelectPageSql($queryObject, $with_values, $start_count, $list_count);
$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);
@ -692,11 +769,12 @@
$buff->page = $page; $buff->page = $page;
$buff->data = $data; $buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
unset($this->param);
return $buff; return $buff;
} }
function getParser(){ function getParser(){
return new DBParser('"', '"', $this->prefix); return new DBParser('"', '"', $this->prefix);
} }
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) { function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {

View file

@ -135,36 +135,76 @@
if(count($this->param)){ if(count($this->param)){
foreach($this->param as $k => $o){ foreach($this->param as $k => $o){
if($o->getType() == 'number'){ if($o->isColumnName()) continue;
$value = $o->getUnescapedValue(); if($o->getType() == 'number'){
if(is_array($value)) $_param = array_merge($_param, $value); $value = $o->getUnescapedValue();
else $_param[] = $o->getUnescapedValue(); if(is_array($value)) $_param = array_merge($_param, $value);
}else{ else $_param[] = $o->getUnescapedValue();
$value = $o->getUnescapedValue(); }else{
if(is_array($value)) { $value = $o->getUnescapedValue();
foreach($value as $v) if(is_array($value)) {
$_param[] = array($v, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8')); foreach($value as $v)
} $_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'));
}
} }
} }
// Run the query statement // Run the query statement
$result = false; $result = false;
if(count($_param)){ if(count($_param)){
$result = @sqlsrv_query($connection, $query, $_param); $args = $this->_getParametersByReference($_param);
$stmt = sqlsrv_prepare($connection, $query, $args);
}else{ }else{
$result = @sqlsrv_query($connection, $query); $stmt = sqlsrv_prepare($connection, $query);
} }
if(!$stmt)
{
$result = false;
}
else
{
$result = sqlsrv_execute($stmt);
}
// 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();
return $result; return $stmt;
} }
/**
* Parameters to sqlsrv_prepare need to be references, and not literals
* Parameters are sent as an array, where each parameter can be:
* - a PHP variable (by reference)
* - a PHP array (containng param value, type and direction) -> also needs to be sent by reference
*/
function _getParametersByReference($_param)
{
$copy = array(); $args = array(); $i = 0;
foreach($_param as $key => $value) {
if(is_array($value))
{
$value_copy = $value;
$value_arg = array();
$value_arg[] = &$value_copy[0];
$value_arg[] = $value_copy[1];
$value_arg[] = $value_copy[2];
}
else
{
$value_arg = $value;
}
$copy[$key] = $value_arg;
$args[$i++] = &$copy[$key];
}
return $args;
}
/** /**
* @brief Fetch results * @brief Fetch results
@ -439,7 +479,7 @@
} }
function getSelectSql($query){ function getSelectSql($query){
$with_value = false; $with_values = false;
//$limitOffset = $query->getLimit()->getOffset(); //$limitOffset = $query->getLimit()->getOffset();
//if($limitOffset) //if($limitOffset)
@ -482,18 +522,18 @@
* it supports a method as navigation * it supports a method as navigation
**/ **/
function _executeSelectAct($queryObject, $connection = null) { function _executeSelectAct($queryObject, $connection = null) {
$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();
$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 ()) return $this->queryError($queryObject);
else return $this->queryPageLimit($queryObject, $result, $connection); else return $this->queryPageLimit($queryObject, $result, $connection);
} }
function getParser(){ function getParser(){
@ -514,63 +554,63 @@
return; return;
} }
function queryPageLimit($queryObject, $result, $connection){ function queryPageLimit($queryObject, $result, $connection) {
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$temp_where = $queryObject->getWhereString(true, false); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where)); $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):''; $count_query .= (__DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
$result_count = $this->_query($count_query, $connection); $result_count = $this->_query($count_query, $connection);
$count_output = $this->_fetch($result_count); $count_output = $this->_fetch($result_count);
$total_count = (int)$count_output->count; $total_count = (int) $count_output->count;
$list_count = $limit->list_count->getValue(); $list_count = $limit->list_count->getValue();
if (!$list_count) $list_count = 20; if (!$list_count)
$page_count = $limit->page_count->getValue(); $list_count = 20;
if (!$page_count) $page_count = 10; $page_count = $limit->page_count->getValue();
$page = $limit->page->getValue(); if (!$page_count)
if (!$page) $page = 1; $page_count = 10;
$page = $limit->page->getValue();
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 $total_page = 1; } else
$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 ();
$buff->total_count = $total_count; $buff->total_count = $total_count;
$buff->total_page = $total_page; $buff->total_page = $total_page;
$buff->page = $page; $buff->page = $page;
$buff->data = array(); $buff->data = array();
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
return $buff; return $buff;
} }
$start_count = ($page - 1) * $list_count; $start_count = ($page - 1) * $list_count;
$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) : '';
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
$result = $this->_query ($query, $connection); $virtual_no = $total_count - ($page - 1) * $list_count;
if ($this->isError ()) $data = $this->_fetch($result, $virtual_no);
return $this->queryError($queryObject);
$virtual_no = $total_count - ($page - 1) * $list_count; $buff = new Object ();
$data = $this->_fetch($result, $virtual_no);
$buff = new Object ();
$buff->total_count = $total_count; $buff->total_count = $total_count;
$buff->total_page = $total_page; $buff->total_page = $total_page;
$buff->page = $page; $buff->page = $page;
$buff->data = $data; $buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
}else{ }else {
$data = $this->_fetch($result); $data = $this->_fetch($result);
$buff = new Object (); $buff = new Object ();
$buff->data = $data; $buff->data = $data;

View file

@ -6,6 +6,8 @@
* @version 0.1 * @version 0.1
* *
* mysql handling class * mysql handling class
*
* Does not use prepared statements, since mysql driver does not support them
**/ **/
class DBMysql extends DB { class DBMysql extends DB {
@ -365,13 +367,8 @@
/** /**
* @brief Handle the insertAct * @brief Handle the insertAct
**/ **/
function _executeInsertAct($queryObject) { function _executeInsertAct($queryObject, $with_values = true) {
// TODO See what priority does $query = $this->getInsertSql($queryObject, $with_values, true);
//priority setting
//$priority = '';
//if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getInsertSql($queryObject, true, true);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
return $this->_query($query); return $this->_query($query);
} }
@ -379,13 +376,8 @@
/** /**
* @brief Handle updateAct * @brief Handle updateAct
**/ **/
function _executeUpdateAct($queryObject) { function _executeUpdateAct($queryObject, $with_values = true) {
// TODO See what proiority does $query = $this->getUpdateSql($queryObject, $with_values, true);
//priority setting
//$priority = '';
//if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getUpdateSql($queryObject, true, true);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
return $this->_query($query); return $this->_query($query);
} }
@ -393,15 +385,9 @@
/** /**
* @brief Handle deleteAct * @brief Handle deleteAct
**/ **/
function _executeDeleteAct($queryObject) { function _executeDeleteAct($queryObject, $with_values = true) {
$query = $this->getDeleteSql($queryObject, true, true); $query = $this->getDeleteSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
//priority setting
// TODO Check what priority does
//$priority = '';
//if($output->priority) $priority = $output->priority['type'].'_priority';
return $this->_query($query); return $this->_query($query);
} }
@ -411,24 +397,26 @@
* In order to get a list of pages easily when selecting \n * In order to get a list of pages easily when selecting \n
* it supports a method as navigation * it supports a method as navigation
**/ **/
function _executeSelectAct($queryObject, $connection = null) { function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) if ($limit && $limit->isPageHandler())
return $this->queryPageLimit($queryObject, $result, $connection); return $this->queryPageLimit($queryObject, $result, $connection, $with_values);
else { else {
$query = $this->getSelectSql($queryObject); $query = $this->getSelectSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if (is_a($query, 'Object'))
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):''; return;
$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 ()) return $this->queryError($queryObject); if ($this->isError())
return $this->queryError($queryObject);
$data = $this->_fetch($result); $data = $this->_fetch($result);
$buff = new Object (); $buff = new Object ();
$buff->data = $data; $buff->data = $data;
return $buff; return $buff;
} }
} }
function db_insert_id() function db_insert_id()
{ {
@ -463,16 +451,16 @@
return; return;
} }
function queryPageLimit($queryObject, $result, $connection){ function queryPageLimit($queryObject, $result, $connection, $with_values = true){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
// Total count // Total count
$temp_where = $queryObject->getWhereString(true, false); $temp_where = $queryObject->getWhereString($with_values, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where)); $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
// Check for distinct query and if found update count query structure // Check for distinct query and if found update count query structure
$temp_select = $queryObject->getSelectString(); $temp_select = $queryObject->getSelectString($with_values);
if(strpos(strtolower($temp_select), "distinct") !== false) { if(strpos(strtolower($temp_select), "distinct") !== false) {
$count_query = sprintf('select %s %s %s', 'FROM ' . $queryObject->getFromString(), $temp_select, ($temp_where === '' ? '' : ' WHERE '. $temp_where)); $count_query = sprintf('select %s %s %s', 'FROM ' . $queryObject->getFromString($with_values), $temp_select, ($temp_where === '' ? '' : ' WHERE '. $temp_where));
$uses_distinct = true; $uses_distinct = true;
} }
@ -512,7 +500,7 @@
} }
$start_count = ($page - 1) * $list_count; $start_count = ($page - 1) * $list_count;
$query = $this->getSelectPageSql($queryObject, true, $start_count, $list_count); $query = $this->getSelectPageSql($queryObject, $with_values, $start_count, $list_count);
$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);

View file

@ -8,6 +8,8 @@
* @version 0.1 * @version 0.1
* *
* mysql innodb handling class * mysql innodb handling class
*
* Does not use prepared statements since the mysql driver does not support them
**/ **/
class DBMysql_innodb extends DBMysql { class DBMysql_innodb extends DBMysql {

View file

@ -91,6 +91,44 @@
* return\n * return\n
**/ **/
function __query($query, $connection) { function __query($query, $connection) {
if($this->use_prepared_statements == 'Y')
{
// 1. Prepare query
$stmt = mysqli_prepare($connection, $query);
if($stmt){
$types = '';
$params = array();
$this->_prepareQueryParameters($types, $params);
if(!empty($params))
{
$args[0] = $stmt;
$args[1] = $types;
$i = 2;
foreach($params as $key => $param) {
$copy[$key] = $param;
$args[$i++] = &$copy[$key];
}
// 2. Bind parameters
$status = call_user_func_array('mysqli_stmt_bind_param',$args);
if(!$status)
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
}
// 3. Execute query
$status = mysqli_stmt_execute($stmt);
if(!$status)
$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;
// mysqli_stmt_close($stmt);
}
}
// Run the query statement // Run the query statement
$result = mysqli_query($connection,$query); $result = mysqli_query($connection,$query);
// Error Check // Error Check
@ -101,10 +139,161 @@
// Return result // Return result
return $result; return $result;
} }
function _prepareQueryParameters(&$types, &$params){
$types = '';
$params = array();
if(!$this->param) return;
foreach($this->param as $k => $o){
$value = $o->getUnescapedValue();
$type = $o->getType();
// Skip column names -> this should be concatenated to query string
if($o->isColumnName()) continue;
switch($type)
{
case 'number' :
$type = 'i';
break;
case 'varchar' :
$type = 's';
break;
default:
$type = 's';
}
if(is_array($value))
{
foreach($value as $v)
{
$params[] = $v;
$types .= $type;
}
}
else {
$params[] = $value;
$types .= $type;
}
}
}
/**
* @brief Fetch results
**/
function _fetch($result, $arrayIndexEndValue = NULL) {
if($this->use_prepared_statements != 'Y'){
return parent::_fetch($result, $arrayIndexEndValue);
}
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
// Prepared stements: bind result variable and fetch data
$stmt = $result;
$meta = mysqli_stmt_result_metadata($stmt);
$fields = mysqli_fetch_fields($meta);
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
$field->name = 'repeat_' . $field->name;
// Array passed needs to contain references, not values
$row[$field->name] = "";
$resultArray[$field->name] = &$row[$field->name];
}
$resultArray = array_merge(array($stmt), $resultArray);
call_user_func_array('mysqli_stmt_bind_result', $resultArray);
$rows = array();
while(mysqli_stmt_fetch($stmt))
{
$resultObject = new stdClass();
foreach($resultArray as $key => $value)
{
if($key === 0) continue; // Skip stmt object
if(strpos($key, 'repeat_')) $key = substr($key, 6);
$resultObject->$key = $value;
}
$rows[] = $resultObject;
}
mysqli_stmt_close($stmt);
if($arrayIndexEndValue)
{
foreach($rows as $row)
{
$output[$arrayIndexEndValue--] = $row;
}
}
else
{
$output = $rows;
}
if(count($output)==1){
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
}
return $output;
}
function _executeInsertAct($queryObject){
if($this->use_prepared_statements != 'Y')
{
return parent::_executeInsertAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeInsertAct($queryObject, false);
unset($this->param);
return $result;
}
function _executeUpdateAct($queryObject) {
if($this->use_prepared_statements != 'Y')
{
return parent::_executeUpdateAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeUpdateAct($queryObject, false);
unset($this->param);
return $result;
}
function _executeDeleteAct($queryObject) {
if($this->use_prepared_statements != 'Y')
{
return parent::_executeDeleteAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeDeleteAct($queryObject, false);
unset($this->param);
return $result;
}
function _executeSelectAct($queryObject, $connection = null) {
if($this->use_prepared_statements != 'Y')
{
return parent::_executeSelectAct($queryObject, $connection);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeSelectAct($queryObject, $connection, false);
unset($this->param);
return $result;
}
function db_insert_id() function db_insert_id()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
return mysqli_insert_id($connection); return mysqli_insert_id($connection);
} }

View file

@ -223,34 +223,36 @@
function getWhereString($with_values = true, $with_optimization = true){ function getWhereString($with_values = true, $with_optimization = true){
$where = ''; $where = '';
$condition_count = 0; $condition_count = 0;
foreach($this->conditions as $conditionGroup){ foreach ($this->conditions as $conditionGroup) {
if($condition_count === 0){ if ($condition_count === 0) {
$conditionGroup->setPipe(""); $conditionGroup->setPipe("");
}
$condition_string = $conditionGroup->toString($with_values);
$where .= $condition_string;
$condition_count++;
}
if($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))){
if($condition_count !== 0) $where = '(' . $where .') ';
foreach($this->orderby as $order){
$colName = $order->getColumnName();
if(strstr($colName, 'list_order') || strstr($colName, 'update_order')){
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if ($condition_count === 0) $opt_condition->setPipe("");
$where .= $opt_condition->toString($with_values).' ';
$condition_count++;
} }
} $condition_string = $conditionGroup->toString($with_values);
$where .= $condition_string;
$condition_count++;
} }
return trim($where); if ($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))) {
if ($condition_count !== 0)
$where = '(' . $where . ') ';
foreach ($this->orderby as $order) {
$colName = $order->getColumnName();
if (strstr($colName, 'list_order') || strstr($colName, 'update_order')) {
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if ($condition_count === 0)
$opt_condition->setPipe("");
$where .= $opt_condition->toString($with_values) . ' ';
$condition_count++;
}
}
}
return trim($where);
} }
function getGroupByString(){ function getGroupByString(){
@ -294,6 +296,19 @@
if(!isset($this->arguments)){ if(!isset($this->arguments)){
$this->arguments = array(); $this->arguments = array();
// Join table arguments
if(count($this->tables) > 0)
{
foreach($this->tables as $table)
{
if($table->isJoinTable())
{
$args = $table->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
}
}
// Column arguments // Column arguments
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
foreach($this->columns as $column){ foreach($this->columns as $column){

View file

@ -24,13 +24,21 @@
} }
function toString($withValue = true){ function toString($withValue = true){
if(!isset($this->_value_to_string)){ if (!isset($this->_value_to_string)) {
if(!$this->show()) { $this->_value_to_string = ''; } if (!$this->show())
else if($withValue) {
$this->_value_to_string = $this->toStringWithValue(); $this->_value_to_string = '';
else $this->_value_to_string = $this->toStringWithoutValue(); }
} else if ($withValue)
return $this->_value_to_string; {
$this->_value_to_string = $this->toStringWithValue();
}
else
{
$this->_value_to_string = $this->toStringWithoutValue();
}
}
return $this->_value_to_string;
} }
function toStringWithoutValue(){ function toStringWithoutValue(){

View file

@ -14,16 +14,27 @@
} }
function toStringWithoutValue(){ function toStringWithoutValue(){
$value = $this->argument->getUnescapedValue(); $value = $this->argument->getUnescapedValue();
if(is_array($value)){ if(is_array($value)){
$q = ''; $q = '';
foreach ($value as $v) $q .= '?,'; foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1); if($q !== '') $q = substr($q, 0, -1);
$q = '(' . $q . ')'; $q = '(' . $q . ')';
} }
else $q = '?'; else
return $this->pipe . ' ' . $this->getConditionPart($q); {
// Prepared statements: column names should not be sent as query arguments, but instead concatenated to query string
if($this->argument->isColumnName())
{
$q = $value;
}
else
{
$q = '?';
}
}
return $this->pipe . ' ' . $this->getConditionPart($q);
} }
function show(){ function show(){

View file

@ -32,6 +32,15 @@
function isJoinTable(){ function isJoinTable(){
return true; return true;
} }
function getArguments()
{
$args = array();
foreach($this->conditions as $conditionGroup)
$args = array_merge($args, $conditionGroup->getArguments());
return $args;
}
} }
?> ?>

View file

@ -20,7 +20,9 @@ class Argument {
function getType() { function getType() {
if (isset($this->type)) if (isset($this->type))
{
return $this->type; return $this->type;
}
if (is_string($this->value)) if (is_string($this->value))
return 'column_name'; return 'column_name';
return 'number'; return 'number';
@ -29,7 +31,7 @@ class Argument {
function setColumnType($value) { function setColumnType($value) {
$this->type = $value; $this->type = $value;
} }
function setColumnOperation($operation) { function setColumnOperation($operation) {
$this->column_operation = $operation; $this->column_operation = $operation;
} }
@ -92,7 +94,7 @@ class Argument {
if ($column_type == 'number') { if ($column_type == 'number') {
if (is_array($value)) { if (is_array($value)) {
foreach ($value AS $key => $val) { foreach ($value AS $key => $val) {
if (isset($val)) { if (isset($val) && $val !== '') {
$value[$key] = (int) $val; $value[$key] = (int) $val;
} }
} }
@ -113,6 +115,13 @@ class Argument {
function isValid() { function isValid() {
return $this->isValid; return $this->isValid;
} }
function isColumnName(){
$type = $this->getType();
if($type == 'column_name') return true;
if($type == 'number' && !is_numeric($this->value) && $this->uses_default_value) return true;
return false;
}
function getErrorMessage() { function getErrorMessage() {
return $this->errorMessage; return $this->errorMessage;

View file

@ -5,11 +5,11 @@
function ConditionArgument($name, $value, $operation){ function ConditionArgument($name, $value, $operation){
if(isset($value) && in_array($operation, array('in', 'notin', 'between')) && !is_array($value)){ if(isset($value) && in_array($operation, array('in', 'notin', 'between')) && !is_array($value) && $value != ''){
$value = str_replace(' ', '', $value); $value = str_replace(' ', '', $value);
$value = str_replace('\'', '', $value); $value = str_replace('\'', '', $value);
$value = explode(',', $value); $value = explode(',', $value);
} }
parent::Argument($name, $value); parent::Argument($name, $value);
$this->operation = $operation; $this->operation = $operation;
} }
@ -63,22 +63,33 @@
} }
} }
/** /**
* Since ConditionArgument is used in WHERE clause, * Since ConditionArgument is used in WHERE clause,
* where the argument value is compared to a table column, * where the argument value is compared to a table column,
* it is assumed that all arguments have type. There are cases though * it is assumed that all arguments have type. There are cases though
* where the column does not have any type - if it was removed from * where the column does not have any type - if it was removed from
* the XML schema for example - see the is_secret column in xe_documents table. * the XML schema for example - see the is_secret column in xe_documents table.
* In this case, the column type is retrieved according to argument * In this case, the column type is retrieved according to argument
* value type (using the PHP function is_numeric). * value type (using the PHP function is_numeric).
* *
* @return type string * @return type string
*/ */
function getType(){ function getType(){
return $this->type ? $this->type : (!is_numeric($this->value) ? "varchar" : ""); if($this->type)
{
return $this->type;
}
else if(!is_numeric($this->value))
{
return 'varchar';
}
else
{
return '';
}
} }
function setColumnType($column_type){ function setColumnType($column_type){
if(!isset($this->value)) return; if(!isset($this->value)) return;
if($column_type === '') return; if($column_type === '') return;

View file

@ -400,5 +400,45 @@
define('__CUBRID_VERSION__', '8.4.1'); define('__CUBRID_VERSION__', '8.4.1');
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function test_resource_getLatestItem(){
$xml_file = _TEST_PATH_ . "db/xml_query/cubrid/data/resource.getLatestItem.xml";
$expected = 'SELECT "package"."module_srl" as "module_srl"
, "package"."status" as "status"
, "package"."category_srl" as "category_srl"
, "package"."member_srl" as "member_srl"
, "package"."package_srl" as "package_srl"
, "package"."path" as "path"
, "package"."license" as "license"
, "package"."title" as "title"
, "package"."homepage" as "homepage"
, "package"."description" as "package_description"
, "package"."voter" as "package_voter"
, "package"."voted" as "package_voted"
, "package"."downloaded" as "package_downloaded"
, "package"."regdate" as "package_regdate"
, "package"."last_update" as "package_last_update"
, "member"."nick_name" as "nick_name"
, "member"."user_id" as "user_id"
, "item"."item_srl" as "item_srl"
, "item"."document_srl" as "document_srl"
, "item"."file_srl" as "item_file_srl"
, "item"."screenshot_url" as "item_screenshot_url"
, "item"."version" as "item_version"
, "item"."voter" as "item_voter"
, "item"."voted" as "item_voted"
, "item"."downloaded" as "item_downloaded"
, "item"."regdate" as "item_regdate"
FROM "xe_resource_packages" as "package"
, "xe_member" as "member"
, "xe_resource_items" as "item"
WHERE "package"."package_srl" = ?
and "package"."member_srl" = "member"."member_srl"
and "item"."item_srl" = "package"."latest_item_srl"';
$argsString = '$args->package_srl = 18325662;';
$expectedArgs = array(18325662);
$this->_testPreparedQuery($xml_file, $argsString, $expected, 'getSelectSql', $expectedArgs);
}
} }

View file

@ -0,0 +1,40 @@
<query id="getLatestItem" action="select">
<tables>
<table name="resource_packages" alias="package" />
<table name="member" alias="member" />
<table name="resource_items" alias="item" />
</tables>
<columns>
<column name="package.module_srl" alias="module_srl"/>
<column name="package.status" alias="status"/>
<column name="package.category_srl" alias="category_srl"/>
<column name="package.member_srl" alias="member_srl"/>
<column name="package.package_srl" alias="package_srl"/>
<column name="package.path" alias="path"/>
<column name="package.license" alias="license"/>
<column name="package.title" alias="title"/>
<column name="package.homepage" alias="homepage"/>
<column name="package.description" alias="package_description"/>
<column name="package.voter" alias="package_voter"/>
<column name="package.voted" alias="package_voted"/>
<column name="package.downloaded" alias="package_downloaded"/>
<column name="package.regdate" alias="package_regdate"/>
<column name="package.last_update" alias="package_last_update"/>
<column name="member.nick_name" alias="nick_name" />
<column name="member.user_id" alias="user_id" />
<column name="item.item_srl" alias="item_srl" />
<column name="item.document_srl" alias="document_srl" />
<column name="item.file_srl" alias="item_file_srl" />
<column name="item.screenshot_url" alias="item_screenshot_url" />
<column name="item.version" alias="item_version" />
<column name="item.voter" alias="item_voter" />
<column name="item.voted" alias="item_voted" />
<column name="item.downloaded" alias="item_downloaded" />
<column name="item.regdate" alias="item_regdate" />
</columns>
<conditions>
<condition operation="equal" column="package.package_srl" var="package_srl" filter="number" />
<condition operation="equal" column="package.member_srl" default="member.member_srl" filter="number" pipe="and" />
<condition operation="equal" column="item.item_srl" var="item_srl" default="package.latest_item_srl" filter="number" pipe="and" />
</conditions>
</query>