Search code examples
delphifiredacdelphi-10.1-berlin

To check if a query returns more Fields than when you created it and defined its persistent fields


When you have a select * from XXX query eventually you can get more fields than you expected, do you know if there is a way to check if new fields have been added since you created that query and defined its persistent fields ?.

Normally you can open your queries without having to worry if new fields have been added. If new fields are not within your persistent fields then you just won't see them. But on a Datasnap REST Server you get an AV when trying to return a Dataset from a query that now has more fields than when you created its persistent fields.

If would like to know if there is a quick check that I can do so I can return a more helpful error instead of an AV.

Something like :

MyQuery.Open;
if MyQuery.FieldDefs.Count <> MyQuery.Fields.Count then begin
  raise Exception.Create('The number of returned Fields doesn''t match the expected');
end;

Thank you


Solution

  • If the dataset has persistent fields and you want those additional fields to be created when the query opens, you have to set dataset.FieldOptions.AutoCreateMode to acCombineAlways first.

    Now after opening the query, you can check for the existance of additional fields with lcAutomatic in dataset.Fields.LifeCycles.

    In case you are interested in which fields are new, just iterate dataset.Fields and check for field.LifeCycle = lcAutomatic.

    BTW, using the setting above you probably might not need that check anymore.