Browse Source

* Set Field.FieldNo to 0 for fields that are not bound to a fielddef, so that
they are distinguisable from calculated fields with a FieldNo of -1.
* Added test to check for an exception when the Fields do not correspond to
the underlying data.
* Improved exception message when the Fields do not correspond with the data

git-svn-id: trunk@21641 -

joost 13 years ago
parent
commit
f67a327240
2 changed files with 26 additions and 9 deletions
  1. 3 9
      packages/fcl-db/src/base/dataset.inc
  2. 23 0
      packages/fcl-db/tests/testdbbasics.pas

+ 3 - 9
packages/fcl-db/src/base/dataset.inc

@@ -85,14 +85,8 @@ Procedure TDataset.BindFields(Binding: Boolean);
 var i, FieldIndex: Integer;
     FieldDef: TFieldDef;
 begin
-  {
-     Here some magic will be needed later; for now just simply set
-     Just set fieldno from listindex...
-     Later we should take it from the fielddefs.
-     // ATM Set by CreateField ...
-  For I:=0 to FFieldList.Count-1 do
-    FFieldList[i].FFieldNo:=I;
-  }
+  { FieldNo is set to -1 for calculated/lookup fields, to 0 for unbound field
+    and for bound fields it is set to FieldDef.FieldNo }
   FCalcFieldsSize := 0;
   FBlobFieldCount := 0;
   for i := 0 to Fields.Count - 1 do
@@ -124,7 +118,7 @@ begin
               FOffset := FBlobFieldCount;
               Inc(FBlobFieldCount);
             end;
-          end else FFieldNo := FieldIndex;
+          end else FFieldNo := 0;
         end;
       end else FFieldNo := 0;
     end;

+ 23 - 0
packages/fcl-db/tests/testdbbasics.pas

@@ -58,6 +58,7 @@ type
     procedure TestdeFieldListChange;
     procedure TestExceptionLocateClosed;    // bug 13938
     procedure TestCanModifySpecialFields;
+    procedure TestDetectionNonMatchingDataset;
   end;
 
   { TTestBufDatasetDBBasics }
@@ -683,6 +684,28 @@ begin
     end;
 end;
 
+procedure TTestDBBasics.TestDetectionNonMatchingDataset;
+var
+  F: TField;
+  ds: tdataset;
+begin
+  // TDataset.Bindfields should detect problems when the underlying data does
+  // not reflect the fields of the dataset. This test is to check if this is
+  // really done.
+  ds := DBConnector.GetNDataset(true,6);
+  with ds do
+    begin
+    open;
+    close;
+
+    F := TStringField.Create(ds);
+    F.FieldName:='DOES_NOT_EXIST';
+    F.DataSet:=ds;
+    F.Size:=50;
+
+    CheckException(open,EDatabaseError);
+    end;
+end;
 
 procedure TTestCursorDBBasics.TestAppendInsertRecord;
 begin