Fix for DELETE statement. MySql class refactoring to use new DBClasses.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8449 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-03 14:50:12 +00:00
parent 9ef9069ba2
commit 69bca759f6
6 changed files with 89 additions and 20 deletions

View file

@ -484,6 +484,11 @@
function getDeleteSql($query){ function getDeleteSql($query){
$sql = 'DELETE '; $sql = 'DELETE ';
// TODO Add support for deleting based on alias, for both simple FROM and multi table join FROM clause
$tables = $query->getTables();
$sql .= $tables[0]->getAlias();
$from = $query->getFromString(); $from = $query->getFromString();
if($from == '') return new Object(-1, "Invalid query"); if($from == '') return new Object(-1, "Invalid query");
$sql .= ' FROM '.$from; $sql .= ' FROM '.$from;

View file

@ -574,6 +574,7 @@
function _executeInsertAct($queryObject) function _executeInsertAct($queryObject)
{ {
$query = $this->getInsertSql($queryObject); $query = $this->getInsertSql($queryObject);
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):'';
@ -591,6 +592,8 @@
function _executeUpdateAct($queryObject) function _executeUpdateAct($queryObject)
{ {
$query = $this->getUpdateSql($queryObject); $query = $this->getUpdateSql($queryObject);
if(is_a($query, 'Object')) return;
$result = $this->_query($query); $result = $this->_query($query);
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd); if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
@ -605,6 +608,8 @@
function _executeDeleteAct($queryObject) function _executeDeleteAct($queryObject)
{ {
$query = $this->getDeleteSql($queryObject); $query = $this->getDeleteSql($queryObject);
if(is_a($query, 'Object')) return;
$result = $this->_query ($query); $result = $this->_query ($query);
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd); if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
@ -621,6 +626,7 @@
// TODO Rewrite with Query object as input // TODO Rewrite with Query object as input
function _executeSelectAct($queryObject){ function _executeSelectAct($queryObject){
$query = $this->getSelectSql($queryObject); $query = $this->getSelectSql($queryObject);
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):'';

View file

@ -152,7 +152,7 @@
// Notify to start a query execution // Notify to start a query execution
$this->actStart($query); $this->actStart($query);
// Run the query statement // Run the query statement
$result = @mysql_query($query, $this->fd); $result = mysql_query($query, $this->fd);
// Error Check // Error Check
if(mysql_error($this->fd)) $this->setError(mysql_errno($this->fd), mysql_error($this->fd)); if(mysql_error($this->fd)) $this->setError(mysql_errno($this->fd), mysql_error($this->fd));
// Notify to complete a query execution // Notify to complete a query execution
@ -164,12 +164,16 @@
/** /**
* @brief Fetch results * @brief Fetch results
**/ **/
function _fetch($result) { function _fetch($result, $arrayIndexEndValue = NULL) {
if(!$this->isConnected() || $this->isError() || !$result) return; if(!$this->isConnected() || $this->isError() || !$result) return;
while($tmp = $this->db_fetch_object($result)) { while($tmp = $this->db_fetch_object($result)) {
$output[] = $tmp; if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
else $output[] = $tmp;
}
if(count($output)==1){
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
} }
if(count($output)==1) return $output[0];
return $output; return $output;
} }
@ -384,6 +388,7 @@
//if($output->priority) $priority = $output->priority['type'].'_priority'; //if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getInsertSql($queryObject); $query = $this->getInsertSql($queryObject);
if(is_a($query, 'Object')) return;
return $this->_query($query); return $this->_query($query);
} }
@ -397,6 +402,7 @@
//if($output->priority) $priority = $output->priority['type'].'_priority'; //if($output->priority) $priority = $output->priority['type'].'_priority';
$query = $this->getUpdateSql($queryObject); $query = $this->getUpdateSql($queryObject);
if(is_a($query, 'Object')) return;
return $this->_query($query); return $this->_query($query);
} }
@ -406,6 +412,8 @@
function _executeDeleteAct($queryObject) { function _executeDeleteAct($queryObject) {
$query = $this->getDeleteSql($queryObject); $query = $this->getDeleteSql($queryObject);
if(is_a($query, 'Object')) return;
//priority setting //priority setting
// TODO Check what priority does // TODO Check what priority does
//$priority = ''; //$priority = '';
@ -421,17 +429,60 @@
**/ **/
function _executeSelectAct($queryObject) { function _executeSelectAct($queryObject) {
$query = $this->getSelectSql($queryObject); $query = $this->getSelectSql($queryObject);
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
$result = $this->_query($query); if(is_a($query, 'Object')) return;
if($this->isError()) return;
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
// TODO Add support for click count // TODO Add support for click count
// TODO Add code for pagination // TODO Add code for pagination
$data = $this->_fetch($result);
$buff = new Object(); $result = $this->_query ($query);
if ($this->isError ()) {
if ($limit && $output->limit->isPageHandler()){
$buff = new Object ();
$buff->total_count = 0;
$buff->total_page = 0;
$buff->page = 1;
$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;
}
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):'';
$result_count = $this->_query($count_query);
$count_output = $this->_fetch($result_count);
$total_count = (int)$count_output->count;
// 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;
$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->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
}else{
$data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
}
return $buff; return $buff;
} }

View file

@ -136,6 +136,10 @@
return "($columnsList) \n VALUES ($valuesList)"; return "($columnsList) \n VALUES ($valuesList)";
} }
function getTables(){
return $this->tables;
}
function getFromString(){ function getFromString(){
$from = ''; $from = '';
$simple_table_count = 0; $simple_table_count = 0;

View file

@ -17,6 +17,10 @@
return $this->name; return $this->name;
} }
function getAlias(){
return $this->alias;
}
function isJoinTable(){ function isJoinTable(){
if(in_array($tableName,array('left join','left outer join','right join','right outer join'))) return true; if(in_array($tableName,array('left join','left outer join','right join','right outer join'))) return true;
return false; return false;

View file

@ -32,9 +32,8 @@
function &getDBParser(){ function &getDBParser(){
static $dbParser; static $dbParser;
if(!$dbParser){ if(!$dbParser){
//$oDB = &DB::getParser(); $oDB = &DB::getInstance();
//self::$dbParser = $oDB->getParser(); $dbParser = $oDB->getParser();
$dbParser = new DBParser('"');
} }
return $dbParser; return $dbParser;
} }