SysQuery::countTotal vs SysQuery::countLoops

Why sometime I have to use SysQuery::countTotal and sometime SysQuery::countLoops

and the result is not the same.

EX:

conteur= SysQuery::countTotal(inventtable_ds.queryRun()) ;

conteur= SysQuery::countLoops(inventtable_ds.queryRun()) ;

Thanks

It gives you the count if the query has single data source, otherwise it will give you the sum of recId’s.

it gives the number of loops performed by while (queryRun.next())

See \Classes\SysQuery\countPrim

Because they calculate different things. You can look how they’re are implemented (which is usually a good way to learn what a method does :)), you’ll see that they both call SysQuery::countPrim() but they take different return values from there. And this is in countPrim():

…
if (countQuery.dataSourceCount() == 1)
    qbds.addSelectionField(fieldNum(Common,RecId),SelectionField::Count);

countQueryRun   = new QueryRun(countQuery);

while (countQueryRun.next())
{
    common  = countQueryRun.get(countQuery.dataSourceNo(1).table());
    counter += common.RecId;
    loops++;
}

return [counter,loops];

If the query has one data source, it’s changed to use count() aggregation method - there is exactly one loop and counter is the result of count(). If there are more data sources, loops is increased by one for every (joined) record returned from database (counter contains some rubbish in this case).

I don’t know why it’s implemented in this way (I can imagine a better implementation), but it should answer your question why you have to use different count* methods in different situations.

So can I use this in any scenario ? SysQuery::countLoops even if I have 1 or many datasource?

.

Yes, that looks strange. I think we can go with countLoops method in case the query has multiple data sources.

Martin do you agree that we can always use countLoop? or sometime it can give you the wrong result and you have to use the countTotal instead ?

Thanks

No, I don’t agree (Kranthi didn’t say that either). If a query has just a single data source, there will be always a single loop only, because the SQL command will be select count(RecId) from …, which obviously returns a single row.

Can we close this POST by saying:

SysQuery::countLoopsfor multiple datasources

SysQuery::countTotal for single datasource

or I’m missing something…

Thanks

For a single data source, this will give you the count of Records, we cannot consider this in case if the query has multiple data source (As the count will not be applied on recid, which then return the recId and they are added)

For a single data source (As Martin said), there will be only one loop irrespective of number records returned by the queryRun.

So we need consider this in case of multiple data source, where the count will not be applied on the recId and the queryRun will loop all the satisfied records.