Issue 667: 게시판 목록 가져오는 쿼리의 검토 필요

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9737 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-10-25 16:11:24 +00:00
parent 2e87a9461f
commit 0c5f501692
2 changed files with 114 additions and 115 deletions

View file

@ -585,16 +585,24 @@
* a method, navigation, is used * a method, navigation, is used
**/ **/
function _executeSelectAct($queryObject, $connection = null){ function _executeSelectAct($queryObject, $connection = null){
$query = $this->getSelectSql($queryObject); $limit = $queryObject->getLimit();
if(is_a($query, 'Object')) return; 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):''; $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);
else
return $this->queryPageLimit($queryObject, $result, $connection); $data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
return $buff;
}
} }
function queryError($queryObject){ function queryError($queryObject){
@ -612,9 +620,6 @@
} }
function queryPageLimit($queryObject, $result, $connection){ function queryPageLimit($queryObject, $result, $connection){
$limit = $queryObject->getLimit();
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));
@ -661,11 +666,6 @@
$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{
$data = $this->_fetch($result);
$buff = new Object ();
$buff->data = $data;
}
return $buff; return $buff;
} }

View file

@ -156,6 +156,7 @@
if(isset($arrayIndexEndValue)) return $output; if(isset($arrayIndexEndValue)) return $output;
else return $output[0]; else return $output[0];
} }
mysql_free_result($result);
return $output; return $output;
} }
@ -410,128 +411,126 @@
* 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); $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):'';
if(is_a($query, 'Object')) return; $result = $this->_query ($query, $connection);
if ($this->isError ()) return $this->queryError($queryObject);
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):''; $data = $this->_fetch($result);
$buff = new Object ();
// TODO Add support for click count $buff->data = $data;
// TODO Add code for pagination return $buff;
}
$result = $this->_query ($query, $connection);
if ($this->isError ()) return $this->queryError($queryObject);
else return $this->queryPageLimit($queryObject, $result, $connection);
} }
function db_insert_id() function db_insert_id()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
return mysql_insert_id($connection); return mysql_insert_id($connection);
} }
function db_fetch_object(&$result) function db_fetch_object(&$result)
{ {
return mysql_fetch_object($result); return mysql_fetch_object($result);
} }
function getParser(){ function getParser(){
return new DBParser('`', '`', $this->prefix); return new DBParser('`', '`', $this->prefix);
} }
function queryError($queryObject){ function queryError($queryObject){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){ if ($limit && $limit->isPageHandler()){
$buff = new Object (); $buff = new Object ();
$buff->total_count = 0; $buff->total_count = 0;
$buff->total_page = 0; $buff->total_page = 0;
$buff->page = 1; $buff->page = 1;
$buff->data = array (); $buff->data = array ();
$buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values $buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values
return $buff; return $buff;
}else }else
return; return;
} }
function queryPageLimit($queryObject, $result, $connection){ function queryPageLimit($queryObject, $result, $connection){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
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 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):''; $count_query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
$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) $list_count = 20;
$page_count = $limit->page_count->getValue(); $page_count = $limit->page_count->getValue();
if (!$page_count) $page_count = 10; if (!$page_count) $page_count = 10;
$page = $limit->page->getValue(); $page = $limit->page->getValue();
if (!$page) $page = 1; 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 else
$total_page = 1; $total_page = 1;
// check the page variables // check the page variables
if ($page > $total_page) $page = $total_page; if ($page > $total_page) $page = $total_page;
$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, true, $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);
$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 = $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{ return $buff;
$data = $this->_fetch($result); }
$buff = new Object ();
$buff->data = $data;
}
return $buff;
}
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values); function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
if($select == '') return new Object(-1, "Invalid query"); $select = $query->getSelectString($with_values);
$select = 'SELECT ' .$select; if($select == '') return new Object(-1, "Invalid query");
$select = 'SELECT ' .$select;
$from = $query->getFromString($with_values); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '') return new Object(-1, "Invalid query");
$from = ' FROM '.$from; $from = ' FROM '.$from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '') $where = ' WHERE ' . $where;
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
$orderBy = $query->getOrderByString(); $orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
$limit = $query->getLimitString(); $limit = $query->getLimitString();
if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count); if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count);
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
} }
return new DBMysql; return new DBMysql;