Page 1 of 1

[bug_fixed] "List out of bounds" in Metadata

Posted: 08.09.2008, 12:08
by niek
I made a function DefineFieldIndexMeta to escape from a bug "List out of bounds" in Metadata. This is because calculated fields do not exist in the metadata (my assumption). It is only suppost to work as a temp solution, because i lack knowledge about zeos.

gr,
Niek

SOLUTION: add/replace code in unit ZAbstractRODataset:

{**
Defines an original field index in the metadata of the dataset, since calcfields and
@param FieldsLookupTable a lookup table to define original index.
@param Field a TDataset field object.
@returns an original fields index or -1 otherwise.
}
function DefineFieldIndexMeta(const FieldsLookupTable: TIntegerDynArray; Field: TField): Integer;

var
I, IndexCorrection: Integer;
begin
Result := -1; IndexCorrection:= 1;
for I := 0 to High(FieldsLookupTable) do
begin
if TField(FieldsLookupTable).FieldKind<>fkData then dec(IndexCorrection);
if FieldsLookupTable = Integer(Field) then
begin
Result := I + IndexCorrection;
Break;
end;
end;
end;

procedure TZAbstractRODataset.InternalPost;
Procedure Checkrequired;

Var I : longint;
columnindex : integer;

begin
For I:=0 to Fields.Count-1 do
With Fields do
Case State of
dsEdit:
if Required and not ReadOnly and (FieldKind=fkData) and IsNull then
raise EZDatabaseError.Create(Format(SNeedField,[DisplayName]));
dsInsert:
if Required and not ReadOnly and (FieldKind=fkData) and IsNull then
begin
// allow autoincrement and defaulted fields to be null;
columnindex := DefineFieldIndexMeta(FieldsLookupTable,Fields);
if not Resultset.GetMetadata.HasDefaultValue(columnIndex) and
not Resultset.GetMetadata.IsAutoIncrement(columnIndex) then
raise EZDatabaseError.Create(Format(SNeedField,[DisplayName]));
end;

End;
end;

begin.........................

Posted: 08.09.2008, 12:39
by mdaems
Hi Niek,

Could you please tell us what the original problem was that you encountered? As you say yourself it's a workaround, we need to know what the real bug to fix is.
You can describe the bug here, but you should post it to http://zeosbugs.firmos.at/ . Please do provide a small demonstration program for the bug. (Project files + a db creation script + eventual steps to show the bug using the demo program)

Mark

Posted: 13.09.2008, 18:19
by niek
problem: add a calculated field to a dataset and open it, then call insert or append, then call post and you get a "list out of bounds" exception. the calculated field needs to be the first field to generate the exception. if you can't get the error i can mail you a test project.

Niek

Posted: 17.09.2008, 20:26
by mdaems
Niek,

I committed following change to avoid this problem. I'm not sure it's as performant as your proposed patch, but I think it's safer in case one changes the order of the fields more than just by adding a calculated field. (Imagine select f1,f2, f3 from y, ordered in the columns editor as calc1, f3,calc2, f1,f2) Not sure if the returned indices will correspond to the expected fields in the metadata.

The patch

Code: Select all

Index: ZAbstractRODataset.pas
===================================================================
--- ZAbstractRODataset.pas	(revision 451)
+++ ZAbstractRODataset.pas	(working copy)
@@ -2079,9 +2079,10 @@
           if Required and not ReadOnly and (FieldKind=fkData) and IsNull then
             begin
            // allow autoincrement and defaulted fields to be null;
-              columnindex := DefineFieldIndex(FieldsLookupTable,Fields[i]);
-              if not Resultset.GetMetadata.HasDefaultValue(columnIndex) and
-                 not Resultset.GetMetadata.IsAutoIncrement(columnIndex) then
+              columnindex := Resultset.FindColumn(Fields[i].FieldName);
+              if (Columnindex = 0) or
+                 (not Resultset.GetMetadata.HasDefaultValue(columnIndex) and
+                  not Resultset.GetMetadata.IsAutoIncrement(columnIndex)) then
                 raise EZDatabaseError.Create(Format(SNeedField,[DisplayName]));
             end;
         End;

Please confirm if this works. It's something that can be included in the 6.6-patches branch (and that way into 6.6.4 release), but before I commit to that branch I'd like to have a 'second opinion'.

Mark

Posted: 25.09.2008, 21:16
by mdaems
Merged my solution to 6.6-patches branch (SVN rev. 460).

Mark