You are not logged in.
Pages: 1
I would like to execute simple query in form:
SELECT COUNT(*) FROM Table WHERE <some-criteria>
assuming that the Table is included into the model and the criteria will be something according to the conventions, like:
T := TSQLTable.Create(Rest, 'Status=?',[requiredStatus]);
but I want to receive just the count of records satisfying the criteria.
Thanks,
Offline
Found workable solution (requires TSQLRestClient):
with Rest.ListFmt([TSQLTable], 'COUNT(*)', 'Status=%', [requiredStatus]), []) do
begin
Result := GetAsInteger(1, 0);
Free;
end;
Anything simpler?
Offline
Perhaps another way:
t:= TSQLTable.CreateAndFillPrepare(RestDB, 'Datum_von > ?', [DateToSql(Now-30)], 'count(*) as ID');
t.FillOne;
t.ID<-- this holds the value of RecordCount
Offline
@danielkuettner
Thanks, that leads to:
Rest.OneFieldValue(TSQLTable, 'COUNT(*)', 'Status=?', [], [requiredStatus], Count);
And no need to destroy anything. Nice!
In fact, there is a special handling in TSQLRest.MultiFieldValue:
if (n=1) and IdemPChar(pointer(FieldName[0]),'COUNT(*)') then ...
(found with the debugger )
Offline
I've forgotten OneFieldValue...
BTW, if you want "nothing to destroy" you can use a SmartPointer-Interface:
var
t: ISmartPointer<TSQLtablename>;
begin
t:= TSmartPointer<TSQLtablename>.Create(TSQLtablename.CreateAndFillPrepare(RestDB, 'Datum_von > ?', [DateToSql(Now-30)], 'count(*) as ID'));
t.FillOne;
//the recordcount is now in t.ID
Although OneFieldValue is much easier to declare it returns a RawUTF8. You have to convert it with UTF8ToInteger.
Offline
There is an overload that returns Int64.
Besides, I don't use Delphi for daily work, just Delphi7 to check whether some mORMot bugs are FPC specific or general.
ISmartPointer<T> is not an option.
Offline
Pages: 1