浏览代码

* Raise an exception when a TBufDataset is opened while there is no dataset created

git-svn-id: trunk@21268 -
joost 13 年之前
父节点
当前提交
f011d02833

+ 16 - 1
packages/fcl-db/src/base/bufdataset.pas

@@ -1116,9 +1116,9 @@ end;
 procedure TCustomBufDataset.InternalOpen;
 
 var IndexNr : integer;
+    i : integer;
 
 begin
-  InitDefaultIndexes;
   if not Assigned(FDatasetReader) and (FileName<>'') then
     begin
     FFileStream := TFileStream.Create(FileName,fmOpenRead);
@@ -1126,6 +1126,21 @@ begin
     FReadFromFile := True;
     end;
   if assigned(FDatasetReader) then IntLoadFielddefsFromFile;
+
+  // This is to check if the dataset is actually created (By calling CreateDataset,
+  // reading from a stream in some other way implemented by a descendent)
+  // If there are less fields then FieldDefs we know for sure that the dataset
+  // is not (correctly) created.
+  if Fields.Count<FieldDefs.Count then
+    DatabaseError(SErrNoDataset);
+  // If there is a field with FieldNo=0 then the fields are not found to the
+  // FieldDefs which is a sign that there is no dataset created. (Calculated and
+  // lookupfields have FielNo=-1)
+  for i := 0 to Fields.Count-1 do
+    if fields[i].FieldNo=0 then
+      DatabaseError(SErrNoDataset);
+
+  InitDefaultIndexes;
   CalcRecordSize;
 
   FBRecordcount := 0;

+ 1 - 0
packages/fcl-db/src/base/dbconst.pas

@@ -111,6 +111,7 @@ Resourcestring
   SRollBackRetaining       = 'Rollback and retaining transaction';
   SErrNoFieldsDefined      = 'Can not create a dataset when there are no fielddefinitions or fields defined';
   SErrApplyUpdBeforeRefresh= 'Must apply updates before refreshing data';
+  SErrNoDataset            = 'Missing underlying dataset, can not open';
 
 Implementation
 

+ 20 - 0
packages/fcl-db/tests/testspecifictbufdataset.pas

@@ -32,6 +32,7 @@ type
   published
     procedure CreateDatasetFromFielddefs;
     procedure CreateDatasetFromFields;
+    procedure TestOpeningNonExistingDataset;
   end;
 
 implementation
@@ -109,6 +110,25 @@ begin
   TestDataset(ds);
 end;
 
+procedure TTestSpecificTBufDataset.TestOpeningNonExistingDataset;
+var ds : TBufDataset;
+    f: TField;
+begin
+  ds := TBufDataset.Create(nil);
+  F := TIntegerField.Create(ds);
+  F.FieldName:='ID';
+  F.DataSet:=ds;
+
+  CheckException(ds.Open,EDatabaseError);
+  ds.Free;
+
+  ds := TBufDataset.Create(nil);
+  DS.FieldDefs.Add('ID',ftInteger);
+
+  CheckException(ds.Open,EDatabaseError);
+  ds.Free;
+end;
+
 initialization
 {$ifdef fpc}