Page 1 of 1

Fielddefs.count

Posted: Thu 22 Aug 2013 12:32
by Ludek
Hi devart team,
I have a query, that selects one table and does not contain all PK columns of the selected table (as I don't need them selected). all selected fields are made persistent in delphi ide.
After opening the query, the property "fielddefs" contains more items than the property "fields" and I can see, the fielddefs contain also the missing PK column, that I did not select and don't need.

Why is this happening? It makes big trouble, if the dataset is used in datasnap server (EArgumentOfOfRangeException in function TDBXDataSetTable.GetColumns), as there is following code

Code: Select all

    SetLength(FValueTypes, FTable.FieldDefs.Count);
    for Ordinal := Low(FValueTypes) to High(FValueTypes) do
    begin
      FieldDef                := FTable.FieldDefs[Ordinal];
      Field                   := FTable.Fields[Ordinal];
you can see, the Ftable.fields[ordinal] will generate an exception, if fields.count < fielddefs.count.

Why is SDAC adding this nonselected PK column to fielddefs?! Can I avoid this somehow? It's really annoying to add all PK fields of all joined tables (nr, nr_1, nr_2 etc), when the query is more complicated :(

Or, do you think, is it a bug in the delphi sources?

Thanks, Ludek.

Re: Fielddefs.count

Posted: Thu 22 Aug 2013 13:53
by AndreyZ
Hello,

The point is that the fields of Primary Key are implicitly returned by the SQL Server even if you do not specify them in the query. For hidden fields, fielddefs are created with the faHiddenCol attribute, but fields are not created. To avoid the problem, you should use the following code:

Code: Select all

SetLength(FValueTypes, FTable.Fields.Count);
for Ordinal := Low(FValueTypes) to High(FValueTypes) do
begin
  FieldDef                := FTable.FieldDefs[Ordinal];
  Field                   := FTable.Fields[Ordinal];

Re: Fielddefs.count

Posted: Thu 22 Aug 2013 14:05
by Ludek
So if i understand correctly, embarcadero's unit Data.DBXDBReaders has to be changed? would you make new report to embarcaderos quality central or should I?

Re: Fielddefs.count

Posted: Fri 23 Aug 2013 08:47
by AndreyZ
There are two workarounds that you can use to avoid this problem:
1. set the TMSQuery.Options.UniqueRecords property to False. In this case, SDAC forces SQL Server not to return additional fields.
2. set the TMSQuery.FieldDefs.HiddenFields property to True. In this case, fields will be created for all fields, hidden or not.
Please note that it is a correct situation to have fielddefs more than fields. As an example, you can take a look at the TDataSet.CreateFields method (the DB unit). There you can see that fields may not be created for all fielddefs.
We are not going to report this issue to Embarcadero Quality Central, you can do this on your own.

Re: Fielddefs.count

Posted: Fri 23 Aug 2013 09:05
by Ludek
ok, thanks for info, i'll use the property.

Re: Fielddefs.count

Posted: Fri 23 Aug 2013 09:30
by AndreyZ
Feel free to contact us if you have any further questions about SDAC.