[bug_fixed] "List out of bounds" in Metadata

Code patches written by our users to solve certain "problems" that were not solved, yet.

Moderators: gto, cipto_kh, EgonHugeist, mdaems

Post Reply
niek
Fresh Boarder
Fresh Boarder
Posts: 4
Joined: 08.09.2008, 11:55

[bug_fixed] "List out of bounds" in Metadata

Post 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.........................
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post 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
Image
niek
Fresh Boarder
Fresh Boarder
Posts: 4
Joined: 08.09.2008, 11:55

Post 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
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post 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
Image
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Merged my solution to 6.6-patches branch (SVN rev. 460).

Mark
Image
Post Reply