Issue 1778: DB Classes: Add prepared statements support - Added CUBRID first version

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10490 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2012-03-28 14:51:30 +00:00
parent 12d4e13480
commit 4065581cb3

View file

@ -164,6 +164,53 @@
**/ **/
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();
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
@ -185,6 +232,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);
@ -542,9 +594,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):'';
@ -553,22 +610,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;
} }
@ -576,15 +638,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;
} }
@ -594,25 +662,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){
@ -629,11 +704,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);
} }
@ -672,11 +747,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);
@ -687,11 +762,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) {