|
From: Ruffaldi E. <pi...@ss...> - 2005-02-22 19:22:14
|
Hello,
In the application I'm writing I need to print only the
number of matches resulting from and RDQL query.
In the case of the RDQL engine based on the SQL
database, its very easy to implement that by using
COUNT(*) when postfiltering is not present. It is much
much more efficient than obtaining the result and
counting the value.
Now I can do something like
model->queryCount(expression)
The changes I've made are:
RdqlDbEngine.php
// 1) generateSql augmented with the count
function generateSql($modelID,$count = false) {
$sql = $this->generateSql_SelectClause($count);
$sql .= $this->generateSql_FromClause();
$sql .= $this->generateSql_WhereClause($modelID);
return $sql;
}
// 2) select clause optimized to count(*) !!!! in the
case of no filters
function generateSql_SelectClause($count = false) {
global $_generateSql_SelectVar_index;
$sql_select = 'SELECT';
$_generateSql_SelectVar_index = 0;
$this->rsIndexes = array();
if($count && !isset($this->parsedQuery['filters']))
$sql_select .= " count(*) ";
else
// 3) RDFS model: new function rdqlQueryCount or modify
rdqlQuery with new param
function rdqlQueryCount($queryString) {
....
$sql=$engine->generateSql($this->getModelIds(), true);
$recordSet=&$this->dbConn->execute($sql);
return $engine->filterQueryResult($recordSet,true);
// 4) filterQueryResult
// declaration and cleanup of the count
function filterQueryResult(&$recordSet, $justcount =
false) {
$count = 0;
// increment the count if filtered ok
....
if($justcount)
$count++;
else
$queryResult[] =
$this->_convertRsRowToQueryResultRow($recordSet->fields);
// get the entry from count(*) in the case of query
without filtering
....
}else
if($justcount)
{
$count = $recordSet->fields[0] + 0;
}
else
// return the count
....
return $justcount ? $count : $queryResult;
Emanuele
|