mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-04 17:44:38 +09:00
Issue 1778: DB Classes: Add prepared statements support - MSSQL
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10492 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
f1a80e7bbd
commit
0665b24aee
1 changed files with 79 additions and 40 deletions
|
|
@ -152,19 +152,58 @@
|
||||||
|
|
||||||
// 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 +478,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 +521,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 +553,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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue