fix pagination

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8704 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
lickawtl 2011-07-29 16:18:06 +00:00
parent 88638b4ba8
commit 18d0add0f9
3 changed files with 148 additions and 40 deletions

View file

@ -244,9 +244,13 @@
if ($result) cubrid_close_request($result);
if($arrayIndexEndValue !== null && count($output) == 1) return $output[$arrayIndexEndValue + 1];
else if (count($output) == 1) return $output[0];
return $output;
if(count($output)==1){
// If call is made for pagination, always return array
if(isset($arrayIndexEndValue)) return $output;
// Else return object instead of array
else return $output[0];
}
return $output;
}
/**
@ -650,32 +654,53 @@
}
function queryPageLimit($queryObject, $result){
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);
}
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 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
$result = $this->_query($count_query);
$count_output = $this->_fetch($result);
$total_count = (int)$count_output->count;
$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;
$list_count = $queryObject->getLimit()->list_count->getValue();
if (!$list_count) $list_count = 20;
$page_count = $queryObject->getLimit()->page_count->getValue();
if (!$page_count) $page_count = 10;
$page = $queryObject->getLimit()->page->getValue();
if (!$page) $page = 1;
// Total pages
if ($total_count) {
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
} else $total_page = 1;
// total pages
if ($total_count) {
$total_page = (int) (($total_count - 1) / $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);
// check the page variables
if ($page > $total_page) $page = $total_page;
$start_count = ($page - 1) * $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):'';
$result = $this->_query ($query);
if ($this->isError ())
return $this->queryError($queryObject);
$virtual_no = $total_count - ($page - 1) * $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->getValue();
$buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
$buff = new Object ();
$buff->total_count = $total_count;
$buff->total_page = $total_page;
$buff->page = $page;
$buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
}else{
$data = $this->_fetch($result);
$buff = new Object ();
@ -687,7 +712,48 @@
function getParser(){
return new DBParser('"', '"', $this->prefix);
}
}
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");
$select = 'SELECT ' .$select;
$from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query");
$from = ' FROM '.$from;
$where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where;
$groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
$orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
$limit = $query->getLimitString();
if ($limit != '') {
if ($query->getLimit()) {
if($orderBy != '')
$limit = sprintf (' for orderby_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else{
if($groupBy != '')
$limit = sprintf (' having groupby_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else{
if ($where != '')
$limit = sprintf (' and inst_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else
$limit = sprintf(' where inst_num() between %d and %d', $start_count + 1, $list_count + $start_count);
}
}
}
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
}
}
return new DBCubrid;
?>

View file

@ -482,20 +482,39 @@
$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;
$list_count = $queryObject->getLimit()->list_count->getValue();
if (!$list_count) $list_count = 20;
$page_count = $queryObject->getLimit()->page_count->getValue();
if (!$page_count) $page_count = 10;
$page = $queryObject->getLimit()->page->getValue();
if (!$page) $page = 1;
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
// total pages
if ($total_count)
$total_page = (int) (($total_count - 1) / $list_count) + 1;
else
$total_page = 1;
// check the page variables
if ($page > $total_page) $page = $total_page;
$start_count = ($page - 1) * $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):'';
$result = $this->_query ($query);
if ($this->isError ())
return $this->queryError($queryObject);
$virtual_no = $total_count - ($page - 1) * $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->getValue();
$buff->page = $page;
$buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
}else{
$data = $this->_fetch($result);
$buff = new Object ();
@ -503,6 +522,30 @@
}
return $buff;
}
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");
$select = 'SELECT ' .$select;
$from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query");
$from = ' FROM '.$from;
$where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where;
$groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
$orderBy = $query->getOrderByString();
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
$limit = $query->getLimitString();
if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count);
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
}
}
return new DBMysql;

View file

@ -10,10 +10,10 @@
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/QueryArgument.class.php');
if($index->page->attrs && $index->page_count->attrs){
$this->page = $index->page->attrs;
$this->page_count = $index->page_count->attrs;
$this->arguments[] = new QueryArgument($index->page);
$this->arguments[] = new QueryArgument($index->page_count);
$this->page = new QueryArgument($index->page);
$this->page_count = new QueryArgument($index->page_count);
$this->arguments[] = $this->page;
$this->arguments[] = $this->page_count;
}
$this->list_count = new QueryArgument($index->list_count);
@ -21,9 +21,8 @@
}
function toString(){
$name = $this->list_count->getArgumentName();
if ($this->page)return sprintf("new Limit(\$%s_argument, \$%s_argument, \$%s_argument)",$name, $name, $name);
else return sprintf("new Limit(\$%s_argument)", $name);
if ($this->page)return sprintf("new Limit(\$%s_argument, \$%s_argument, \$%s_argument)", $this->list_count->getArgumentName(), $this->page->getArgumentName(), $this->page_count->getArgumentName());
else return sprintf("new Limit(\$%s_argument)", $this->list_count->getArgumentName());
}
function getArguments(){