Aurelius, returning a non-Entity dataset

I am really enjoying using Aurelius.

I am confident with code like this:

AureliusDataset1.Close;
FreeAndNil(Artists);
Artists := XClient.List;
AureliusDataset1.SetSourceList(Artists);
AureliusDataset1.Open;

In this case the Aurelius dataset is bound to an Aurelius Entity.

However in my App I frequently pass back "vanilla" data from the server in the form of a dataset.

This will include multiple rows and columns, but not connected to any one entity, made to be displayed to the user.

I want to write a Service on my Aurelius Server which returns something which is close to a dataset, but without any relationship to entities

On the server-side I am using code like this:

var
AStatement: IDBStatement;
AData: IDBResultSet;
lParams: TObjectList;
begin
lParams := TObjectList.Create;
try
lParams.Add(TDBParam.Create('ID', TFieldType.ftInteger, aID));
AStatement := Manager.Connection.CreateStatement;
AStatement.SetSQLCommand(aSQLStatement);
AStatement.SetParams(lParams);
AData := AStatement.ExecuteQuery;
end;

However if I try to pass back "AData" I get an error saying IDBResultSet can't be passed.

What is the most suitable object-type to pass back for this purpose?

In this case you should return an object or a JSON value (TJSONObject).
You should then fill such object properties from the IDBResultSet value.

Is there an example showing how to read IDBResultSet into TJSONObject?

Also, I can see the TJSONObject declared in 2 places:

System.JSON (from EMBA) and WEBLib.JSON (from TMS)

Are these 2 classes identical? Which one should I reference for this purpose?

You can find a working example in XData demo XDataFireDacSqlServer in folder demos\firedac-sql. Here is a snippet:

function TCustomerService.GetCustomer(Id: Integer): TCustomer;
begin
  Query.SQL.Text := 'SELECT * FROM CUSTOMERS WHERE ID = :id';
  Query.ParamByName('id').AsInteger := Id;
  Query.Open;
  if Query.IsEmpty then Exit(nil);

  Result := TCustomer.Create;
  TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);
  Result.Id := Query.FieldByName('ID').AsInteger;
  Result.Name := Query.FieldByName('NAME').AsString;
  Result.Country := Query.FieldByName('COUNTRY').AsString;
end;

Another example:

    IResultSet := IStatement.ExecuteQuery;
    ResultArray := TJSONArray.Create;
    TXDataOperationContext.Current.Handler.ManagedObjects.Add(ResultArray);
    while IResultSet.Next do
    begin
      ResultObject := TJSONObject.Create;
      ResultObject.AddPair('Firstname', IResultSet.GetFieldValue('Firstname') );
      ResultObject.AddPair('Lasttname', IResultSet.GetFieldValue('Lastname') );
      ResultArray.AddElement(ResultObject);
    end;
  Result := ResultArray;

No, they are different. Use the native Delphi one from System.JSON, which you can find plenty of documentation and examples by the way. The one in WEBLib.JSON is just for TMS Web Core applications.

Brilliant Wagner. Thank you. Especially the second example is really useful, as it shows working with generic data that is not connected to an entity.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.