5

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

Marc Guillot
  • 6,090
  • 1
  • 15
  • 42
  • 3
    Why not specify the columns that you want to be returned from the query rather than *? – Keith Miller Oct 24 '16 at 12:23
  • Yes, this is the best way to avoid this error, but I'm not the only one writing queries on the server. If someone forgets to do so I would like to return a helpful message instead of an AV. – Marc Guillot Oct 24 '16 at 12:27

1 Answers1

6

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.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130