Browse Source

* Bind fields when needed in InternalOpen. Fixes issue #40450

Michaël Van Canneyt 1 year ago
parent
commit
ac5b96a0c9
1 changed files with 20 additions and 7 deletions
  1. 20 7
      packages/fcl-db/src/base/bufdataset.pas

+ 20 - 7
packages/fcl-db/src/base/bufdataset.pas

@@ -1407,6 +1407,7 @@ var
   i : integer;
   aPacketReader : TDataPacketReader;
   aStream : TFileStream;
+  doBind : boolean;
 
 begin
   aPacketReader:=Nil;
@@ -1421,8 +1422,23 @@ begin
         aPacketReader := GetPacketReader(dfDefault, aStream);
         end;
       IntLoadFieldDefsFromPacket(aPacketReader);
+      end
+    else
+      begin
+      // Issue 40450: At design time, create a dataset, set to active.
+      // At runtime, open is called, but fields are not bound (this happens in createdataset)
+      // So we check for unbound fields and bind them if needed.
+      // Do not call bindfields unconditonally, because descendants may have called it.
+      I:=0;
+      DoBind:=False;
+      While (Not DoBind) and (I<Fields.Count) do
+        begin
+        DoBind:=Fields[i].FieldNo=0;
+        Inc(I);
+        end;
+      if DoBind then
+        BindFields(True);
       end;
-
     // This checks if the dataset is actually created (by calling CreateDataset,
     // or reading from a stream in some other way implemented by a descendent)
     // If there are less fields than FieldDefs we know for sure that the dataset
@@ -1436,7 +1452,6 @@ begin
     //  if Fields.Count<FieldDefs.Count then
     if (Fields.Count = 0) or (FieldDefs.Count=0) then
       DatabaseError(SErrNoDataset);
-
     // search for autoinc field
     FAutoIncField:=nil;
     if FAutoIncValue>-1 then
@@ -3676,17 +3691,15 @@ var
 
 begin
   CheckInactive;
+  if ((Fields.Count=0) and (FieldDefs.Count=0)) then
+    raise Exception.Create(SErrNoFieldsDefined);
   if ((Fields.Count=0) or (FieldDefs.Count=0)) then
     begin
     if (FieldDefs.Count>0) then
       CreateFields
     else if (Fields.Count>0) then
-      begin
       InitFieldDefsFromFields;
-      BindFields(True);
-      end
-    else
-      raise Exception.Create(SErrNoFieldsDefined);
+    BindFields(True);
     end;
   if FAutoIncValue<0 then  
     FAutoIncValue:=1;