Issue 1778 - DB Classes: Add prepared statements support - First version of mysqli support

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10480 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2012-03-26 16:10:28 +00:00
parent 1bc9b8af10
commit f613ff3b88
2 changed files with 214 additions and 45 deletions

View file

@ -367,13 +367,8 @@
/**
* @brief Handle the insertAct
**/
function _executeInsertAct($queryObject) {
// TODO See what priority does
//priority setting
//$priority = '';
//if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getInsertSql($queryObject, true, true);
function _executeInsertAct($queryObject, $with_values = true) {
$query = $this->getInsertSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return;
return $this->_query($query);
}
@ -381,13 +376,8 @@
/**
* @brief Handle updateAct
**/
function _executeUpdateAct($queryObject) {
// TODO See what proiority does
//priority setting
//$priority = '';
//if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getUpdateSql($queryObject, true, true);
function _executeUpdateAct($queryObject, $with_values = true) {
$query = $this->getUpdateSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return;
return $this->_query($query);
}
@ -395,15 +385,9 @@
/**
* @brief Handle deleteAct
**/
function _executeDeleteAct($queryObject) {
$query = $this->getDeleteSql($queryObject, true, true);
function _executeDeleteAct($queryObject, $with_values = true) {
$query = $this->getDeleteSql($queryObject, $with_values, true);
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);
}
@ -413,24 +397,26 @@
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
**/
function _executeSelectAct($queryObject, $connection = null) {
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler())
return $this->queryPageLimit($queryObject, $result, $connection);
else {
$query = $this->getSelectSql($queryObject);
if(is_a($query, 'Object')) return;
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler())
return $this->queryPageLimit($queryObject, $result, $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) : '';
$result = $this->_query ($query, $connection);
if ($this->isError ()) return $this->queryError($queryObject);
$result = $this->_query($query, $connection);
if ($this->isError())
return $this->queryError($queryObject);
$data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
return $buff;
}
}
$data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
return $buff;
}
}
function db_insert_id()
{
@ -465,16 +451,16 @@
return;
}
function queryPageLimit($queryObject, $result, $connection){
function queryPageLimit($queryObject, $result, $connection, $with_values = true){
$limit = $queryObject->getLimit();
// Total count
$temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
$temp_where = $queryObject->getWhereString($with_values, false);
$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
$temp_select = $queryObject->getSelectString();
$temp_select = $queryObject->getSelectString($with_values);
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;
}
@ -514,7 +500,7 @@
}
$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):'';
$result = $this->_query ($query, $connection);

View file

@ -91,6 +91,42 @@
* return\n
**/
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
call_user_func_array('mysqli_stmt_bind_param',$args);
}
// 3. Execute query
$status = mysqli_stmt_execute($stmt);
if(!$status)
$this->setError(-1, "Prepared statement failed: $query");
// Return stmt for other processing - like retrieving resultset (_fetch)
return $stmt;
// mysqli_stmt_close($stmt);
}
}
// Run the query statement
$result = mysqli_query($connection,$query);
// Error Check
@ -101,10 +137,157 @@
// 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();
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()
{
$connection = $this->_getConnection('master');
$connection = $this->_getConnection('master');
return mysqli_insert_id($connection);
}