DBCubrid class: removed duplicate fetch code from pagination, updates to update, delete, insert queries.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8442 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-02 17:36:31 +00:00
parent 251555245d
commit 2e122219f1
5 changed files with 99 additions and 181 deletions

View file

@ -214,34 +214,37 @@
/**
* @brief Fetch the result
**/
function _fetch($result)
function _fetch($result, $arrayIndexEndValue = NULL)
{
if (!$this->isConnected() || $this->isError() || !$result) return;
// TODO Improve this piece of code
// This code trims values from char type columns
$col_types = cubrid_column_types ($result);
$col_names = cubrid_column_names ($result);
$max = count ($col_types);
for ($count = 0; $count < $max; $count++) {
if (preg_match ("/^char/", $col_types[$count]) > 0) {
$char_type_fields[] = $col_names[$count];
}
}
while ($tmp = cubrid_fetch ($result, CUBRID_OBJECT)) {
if (is_array ($char_type_fields)) {
foreach ($char_type_fields as $val) {
$tmp->{$val} = rtrim ($tmp->{$val});
}
}
$output[] = $tmp;
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
else $output[] = $tmp;
}
unset ($char_type_fields);
if ($result) cubrid_close_request($result);
if (count ($output) == 1) return $output[0];
return $output;
}
@ -563,30 +566,22 @@
}
function getInsertSql($query){
$tableName = $query->getFirstTableName();
$values = $query->getInsertString();
return "INSERT INTO $tableName \n $values";
}
/**
* @brief handles insertAct
**/
// TODO Rewrite with Query object as input
function _executeInsertAct ($output)
function _executeInsertAct($queryObject)
{
$query = '';
$tableName = $output->tables[0]->getName();
$columnsList = '';
$valuesList = '';
foreach($output->columns as $column){
if($column->show()){
$columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue() . ', ';
}
}
$columnsList = substr($columnsList, 0, -2);
$valuesList = substr($valuesList, 0, -2);
$query = "INSERT INTO $tableName \n ($columnsList) \n VALUES ($valuesList)";
$query = $this->getInsertSql($queryObject);
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
$result = $this->_query ($query);
if ($result && !$this->transaction_started) {
@cubrid_commit ($this->fd);
@ -595,69 +590,53 @@
return $result;
}
function getUpdateSql($query){
$columnsList = $query->getSelectString();
if($columnsList == '') return new Object(-1, "Invalid query");
$tableName = $query->getFirstTableName();
if($tableName == '') return new Object(-1, "Invalid query");
$where = $query->getWhereString();
if($where != '') $where = ' WHERE ' . $where;
return "UPDATE $tableName SET $columnsList ".$where;
}
/**
* @brief handles updateAct
**/
// TODO Rewrite with Query object as input
function _executeUpdateAct ($output)
function _executeUpdateAct($queryObject)
{
$query = '';
$query = $this->getUpdateSql($queryObject);
$result = $this->_query($query);
$tableName = $output->tables[0]->getName();
$columnsList = '';
foreach($output->columns as $column){
if($column->show()){
$columnsList .= $column->getExpression() . ', ';
}
}
$columnsList = substr($columnsList, 0, -2);
$where = '';
if(count($output->conditions) > 0){
$where = 'WHERE ';
foreach($output->conditions as $conditionGroup){
$where .= $conditionGroup->toString();
}
}
$query = "UPDATE $tableName SET $columnsList ".$where;
$result = $this->_query ($query);
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
return $result;
}
function getDeleteSql($query){
$sql = 'DELETE ';
$from = $query->getFromString();
if($from == '') return new Object(-1, "Invalid query");
$sql .= ' FROM '.$from;
$where = $query->getWhereString();
if($where != '') $sql .= ' WHERE ' . $where;
return $sql;
}
/**
* @brief handles deleteAct
**/
// TODO Rewrite with Query object as input
function _executeDeleteAct ($output)
{
$query = '';
$select = 'DELETE ';
$from = 'FROM ';
$simple_table_count = 0;
foreach($output->tables as $table){
if($simple_table_count > 0) $from .= ', ';
$from .= $table->toString() . ' ';
if(!$table->isJoinTable()) $simple_table_count++;
}
$where = '';
if(count($output->conditions) > 0){
$where = 'WHERE ';
foreach($output->conditions as $conditionGroup){
$where .= $conditionGroup->toString();
}
}
$query = $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
function _executeDeleteAct($queryObject)
{
$query = $this->getDeleteSql($queryObject);
$result = $this->_query ($query);
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
return $result;
@ -685,7 +664,7 @@
$limit = $query->getLimitString();
if($limit != '') $limit = ' LIMIT ' . $limit;
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;// . ' ' . $limit;
}
/**
@ -698,8 +677,8 @@
function _executeSelectAct($queryObject){
$query = $this->getSelectSql($queryObject);
//$query = sprintf ("select %s from %s %s %s %s", $columns, implode (',',$table_list), implode (' ',$left_join), $condition, //$groupby_query.$orderby_query);
//$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);
if ($this->isError ()) {
if ($limit && $output->limit->isPageHandler()){
@ -710,43 +689,40 @@
$buff->data = array ();
$buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values
return $buff;
}else return;
}else
return;
}
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
// Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString()));
if ($queryObject->getGroupByString() != '') {
$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):'';
$result_count = $this->_query($count_query);
$count_output = $this->_fetch($result_count);
$total_count = (int)$count_output->count;
// total pages
// Total pages
if ($total_count) {
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
} else $total_page = 1;
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
while ($tmp = cubrid_fetch ($result, CUBRID_OBJECT)) {
if ($tmp) {
foreach ($tmp as $k => $v) {
$tmp->{$k} = rtrim($v);
}
}
$data[$virtual_no--] = $tmp;
}
$data = $this->_fetch($result);
//$data = $this->_fetch($result, $virtual_no);
$buff = new Object ();
$buff->total_count = $total_count;
$buff->total_page = $total_page;
$buff->page = $queryObject->getLimit()->page;
$buff->data = $data;
$buff->page_navigation = new PageHandler ($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
}else{
$data = $this->_fetch ($result);
$data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
}

View file

@ -64,7 +64,7 @@
$this->orderby = $order;
}
function setLimit($limit){
function setLimit($limit = NULL){
if(!isset($limit)) return;
$this->limit = $limit;
}
@ -117,6 +117,25 @@
return $select;
}
function getUpdateString(){
return $this->getSelectString();
}
function getInsertString(){
$columnsList = '';
$valuesList = '';
foreach($this->columns as $column){
if($column->show()){
$columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue() . ', ';
}
}
$columnsList = substr($columnsList, 0, -2);
$valuesList = substr($valuesList, 0, -2);
return "($columnsList) \n VALUES ($valuesList)";
}
function getFromString(){
$from = '';
$simple_table_count = 0;
@ -127,9 +146,6 @@
}
if(trim($from) == '') return '';
return $from;
}
function getWhereString(){
@ -173,6 +189,10 @@
}
return $limit;
}
function getFirstTableName(){
return $this->tables[0]->getName();
}
}

View file

@ -4,7 +4,6 @@ require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/ColumnTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/UpdateColumnsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/DeleteColumnsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TablesTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/JoinConditionsTag.class.php');
@ -99,7 +98,7 @@ class QueryParser {
}else if($this->action == 'update') {
$columns = new UpdateColumnsTag($this->query->columns->column);
}else if($this->action == 'delete') {
$columns = new DeleteColumnsTag($this->query->columns->column);
$columns = null;
}
@ -111,7 +110,8 @@ class QueryParser {
$this->setTableColumnTypes($tables);
$arguments = array();
$arguments = array_merge($arguments, $columns->getArguments());
if($columns)
$arguments = array_merge($arguments, $columns->getArguments());
$arguments = array_merge($arguments, $conditions->getArguments());
$arguments = array_merge($arguments, $navigation->getArguments());
@ -127,12 +127,14 @@ class QueryParser {
$prebuff .= "\n";
$buff = '';
$buff .= '$query->setColumns(' . $columns->toString() . ');'.PHP_EOL;
if($columns)
$buff .= '$query->setColumns(' . $columns->toString() . ');'.PHP_EOL;
$buff .= '$query->setTables(' . $tables->toString() .');'.PHP_EOL;
$buff .= '$query->setConditions('.$conditions->toString() .');'.PHP_EOL;
$buff .= '$query->setGroups(' . $groups->toString() . ');'.PHP_EOL;
$buff .= '$query->setOrder(' . $navigation->getOrderByString() .');'.PHP_EOL;
$buff .= $navigation->getLimitString()?'$query->setLimit(' . $navigation->getLimitString() .');'.PHP_EOL:"";
$buff .= '$query->setLimit(' . $navigation->getLimitString() .');'.PHP_EOL;
return "<?php if(!defined('__ZBXE__')) exit();\n"
. '$query = new Query();'.PHP_EOL

View file

@ -1,32 +0,0 @@
<?php
/**
* @class DeleteColumnTag
* @author Arnia Software
* @brief Models the <column> tag inside an XML Query file whose action is 'delete'
*
**/
class DeleteColumnTag extends ColumnTag {
var $argument;
function DeleteColumnTag($column) {
parent::ColumnTag($column->attrs->name);
$dbParser = XmlQueryParser::getDBParser();
$this->name = $dbParser->parseColumnName($this->name);
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/QueryArgument.class.php');
$this->argument = new QueryArgument($column);
}
function getExpressionString(){
return sprintf('new DeleteExpression(\'%s\', $args->%s)'
, $this->name
, $this->argument->argument_name);
}
function getArgument(){
return $this->argument;
}
}
?>

View file

@ -1,48 +0,0 @@
<?php
/**
* @class DeleteColumnsTag
* @author Arnia Software
* @brief Models the <column> tag inside an XML Query file whose action is 'delete'
*
**/
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/ColumnTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/column/DeleteColumnTag.class.php');
class DeleteColumnsTag{
var $columns;
function DeleteColumnsTag($xml_columns) {
$this->columns = array();
if(!$xml_columns)
return;
if(!is_array($xml_columns)) $xml_columns = array($xml_columns);
foreach($xml_columns as $column){
$this->columns[] = new DeleteColumnTag($column);
}
}
function toString(){
$output_columns = 'array(' . PHP_EOL;
foreach($this->columns as $column){
$output_columns .= $column->getExpressionString() . PHP_EOL . ',';
}
$output_columns = substr($output_columns, 0, -1);
$output_columns .= ')';
return $output_columns;
}
function getArguments(){
$arguments = array();
foreach($this->columns as $column){
$arguments[] = $column->getArgument();
}
return $arguments;
}
}
?>