Browse Source

* Correct use of FieldDefs factory. Bind FieldDefs to fields.

git-svn-id: trunk@26776 -
michael 11 years ago
parent
commit
882eca6b09

+ 11 - 6
packages/fcl-db/src/base/dataset.inc

@@ -83,7 +83,7 @@ end;
 procedure TDataSet.BindFields(Binding: Boolean);
 
 var i, FieldIndex: Integer;
-    FieldDef: TFieldDef;
+
 begin
   { FieldNo is set to -1 for calculated/lookup fields, to 0 for unbound field
     and for bound fields it is set to FieldDef.FieldNo }
@@ -91,6 +91,7 @@ begin
   FBlobFieldCount := 0;
   for i := 0 to Fields.Count - 1 do
     with Fields[i] do begin
+      FFieldDef:=Nil;
       if Binding then begin
         if FieldKind in [fkCalculated, fkLookup] then begin
           FFieldNo := -1;
@@ -107,14 +108,14 @@ begin
             if FLookupCache then RefreshLookupList;
           end
         end else begin
-          FieldDef := nil;
+          FFieldDef := nil;
           FieldIndex := FieldDefs.IndexOf(Fields[i].FieldName);
           if FieldIndex <> -1 then begin
-            FieldDef := FieldDefs[FieldIndex];
-            FFieldNo := FieldDef.FieldNo;
+            FFieldDef := FieldDefs[FieldIndex];
+            FFieldNo := FFieldDef.FieldNo;
             if FieldDef.InternalCalcField then FInternalCalcFields := True;
             if IsBlob then begin
-              FSize := FieldDef.Size;
+              FSize := FFieldDef.Size;
               FOffset := FBlobFieldCount;
               Inc(FBlobFieldCount);
             end;
@@ -198,6 +199,7 @@ begin
   FreeFieldBuffers;
   ClearBuffers;
   SetBufListSize(0);
+  Fields.ClearFieldDefs;
   InternalClose;
   FInternalOpenComplete := False;
 end;
@@ -877,6 +879,7 @@ end;
 procedure TDataSet.SetFieldDefs(AFieldDefs: TFieldDefs);
 
 begin
+  Fields.ClearFieldDefs;
   FFieldDefs.Assign(AFieldDefs);
 end;
 
@@ -899,6 +902,7 @@ end;
 
 procedure TDataSet.InitFieldDefsFromfields;
 var i : integer;
+
 begin
   if FieldDefs.count = 0 then
     begin
@@ -907,7 +911,8 @@ begin
       for i := 0 to Fields.Count-1 do with fields[i] do
         if not (FieldKind in [fkCalculated,fkLookup]) then // Do not add fielddefs for calculated/lookup fields.
           begin
-          with TFieldDef.Create(FieldDefs,FieldName,DataType,Size,Required,FieldDefs.Count+1) do
+          FFieldDef:=FieldDefs.FieldDefClass.Create(FieldDefs,FieldName,DataType,Size,Required,FieldDefs.Count+1);
+          with FFieldDef do
             begin
             if Required then Attributes := attributes + [faRequired];
             if ReadOnly then Attributes := attributes + [faReadOnly];

+ 5 - 2
packages/fcl-db/src/base/db.pas

@@ -275,6 +275,7 @@ type
     FDisplayLabel : String;
     FDisplayWidth : Longint;
     FEditMask: TEditMask;
+    FFieldDef: TFieldDef;
     FFieldKind : TFieldKind;
     FFieldName : String;
     FFieldNo : Longint;
@@ -305,7 +306,7 @@ type
     function GetIndex : longint;
     function GetLookup: Boolean;
     procedure SetAlignment(const AValue: TAlignMent);
-    procedure SetIndex(const AValue: Integer);
+    procedure SetIndex(const AValue: Longint);
     function GetDisplayText: String;
     function GetEditText: String;
     procedure SetEditText(const AValue: string);
@@ -422,6 +423,7 @@ type
     property Value: variant read GetAsVariant write SetAsVariant;
     property OldValue: variant read GetOldValue;
     property LookupList: TLookupList read GetLookupList;
+    Property FieldDef : TFieldDef Read FFieldDef;
   published
     property Alignment : TAlignment read FAlignment write SetAlignment default taLeftJustify;
     property CustomConstraint: string read FCustomConstraint write FCustomConstraint;
@@ -1084,10 +1086,11 @@ type
       FOnChange : TNotifyEvent;
       FValidFieldKinds : TFieldKinds;
     Protected
+      Procedure ClearFieldDefs;
       Procedure Changed;
       Procedure CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
       Function GetCount : Longint;
-      Function GetField (Index : longint) : TField;
+      Function GetField (Index : Integer) : TField;
       Procedure SetField(Index: Integer; Value: TField);
       Procedure SetFieldIndex (Field : TField;Value : Integer);
       Property OnChange : TNotifyEvent Read FOnChange Write FOnChange;

+ 39 - 28
packages/fcl-db/src/base/fields.inc

@@ -91,6 +91,7 @@ begin
     DatabaseErrorFmt(SUnknownFieldType,[FName]);
   Result:=Thefield.Create(AOwner);
   Try
+    Result.FFieldDef:=Self;
     Result.Size:=FSize;
     Result.Required:=FRequired;
     Result.FFieldName:=FName;
@@ -197,7 +198,7 @@ begin
   // fieldno is 1 based !
   BeginUpdate;
   try
-    TFieldDef.Create(Self,AName,ADataType,ASize,Arequired,Count+1);
+    FieldDefClass.Create(Self,AName,ADataType,ASize,Arequired,Count+1);
   finally
     EndUpdate;
   end;
@@ -279,7 +280,7 @@ end;
 function TFieldDefs.AddFieldDef: TFieldDef;
 
 begin
-  Result:=TFieldDef.Create(Self,'',ftUnknown,0,False,Count+1);
+  Result:=FieldDefClass.Create(Self,'',ftUnknown,0,False,Count+1);
 end;
 
 { ---------------------------------------------------------------------
@@ -462,7 +463,7 @@ begin
   raise AccessError(SInteger);
 end;
 
-function TField.GetAsVariant: Variant;
+function TField.GetAsVariant: variant;
 
 begin
   raise AccessError(SVariant);
@@ -480,7 +481,7 @@ begin
   Result := GetAsString;
 end;
 
-function TField.GetOldValue: Variant;
+function TField.GetOldValue: variant;
 
 var SaveState : TDatasetState;
 
@@ -604,7 +605,7 @@ begin
     Result:=FFieldName;
 end;
 
-Function TField.IsDisplayStored : Boolean;
+function TField.IsDisplayStored: Boolean;
 
 begin
   Result:=(DisplayLabel<>FieldName);
@@ -625,7 +626,7 @@ begin
     Value := FLookupDataSet.Lookup(FLookupKeyfields, FDataSet.FieldValues[FKeyFields], FLookupresultField);
 end;
 
-function TField.getIndex : longint;
+function TField.GetIndex: longint;
 
 begin
   If Assigned(FDataset) then
@@ -658,7 +659,7 @@ begin
     end;
 end;
 
-procedure TField.SetIndex(const AValue: Integer);
+procedure TField.SetIndex(const AValue: Longint);
 begin
   if FFields <> nil then FFields.SetFieldIndex(Self, AValue)
 end;
@@ -785,7 +786,7 @@ begin
   Raise AccessError(SFloat);
 end;
 
-procedure TField.SetAsVariant(const AValue: Variant);
+procedure TField.SetAsVariant(const AValue: variant);
 
 begin
   if VarIsNull(AValue) then
@@ -842,7 +843,7 @@ begin
   FDataSet.SetFieldData(Self,Buffer, NativeFormat);
 end;
 
-Procedure TField.SetDataset (AValue : TDataset);
+procedure TField.SetDataset(AValue: TDataset);
 
 begin
 {$ifdef dsdebug}
@@ -915,7 +916,7 @@ begin
     end;
 end;
 
-class function Tfield.IsBlob: Boolean;
+class function TField.IsBlob: Boolean;
 
 begin
   Result:=False;
@@ -3218,7 +3219,7 @@ end;
 
 { TFields }
 
-Constructor TFields.Create(ADataset : TDataset);
+constructor TFields.Create(ADataset: TDataset);
 
 begin
   FDataSet:=ADataset;
@@ -3226,7 +3227,7 @@ begin
   FValidFieldKinds:=[fkData..fkInternalcalc];
 end;
 
-Destructor TFields.Destroy;
+destructor TFields.Destroy;
 
 begin
   if Assigned(FFieldList) then
@@ -3235,7 +3236,17 @@ begin
   inherited Destroy;
 end;
 
-Procedure Tfields.Changed;
+procedure TFields.ClearFieldDefs;
+
+Var
+  i : Integer;
+
+begin
+  For I:=0 to Count-1 do
+    Fields[i].FFieldDef:=Nil;
+end;
+
+procedure TFields.Changed;
 
 begin
   if (FDataSet <> nil) and not (csDestroying in FDataSet.ComponentState) and FDataset.Active then
@@ -3244,32 +3255,32 @@ begin
     FOnChange(Self);
 end;
 
-Procedure TFields.CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
+procedure TFields.CheckfieldKind(Fieldkind: TFieldKind; Field: TField);
 
 begin
   If Not (FieldKind in ValidFieldKinds) Then
     DatabaseErrorFmt(SInvalidFieldKind,[Field.FieldName]);
 end;
 
-Function Tfields.GetCount : Longint;
+function TFields.GetCount: Longint;
 
 begin
   Result:=FFieldList.Count;
 end;
 
 
-Function TFields.GetField (Index : longint) : TField;
+function TFields.GetField(Index: Integer): TField;
 
 begin
   Result:=Tfield(FFieldList[Index]);
 end;
 
-procedure Tfields.SetField(Index: Integer; Value: TField);
+procedure TFields.SetField(Index: Integer; Value: TField);
 begin
   Fields[Index].Assign(Value);
 end;
 
-Procedure TFields.SetFieldIndex (Field : TField;Value : Integer);
+procedure TFields.SetFieldIndex(Field: TField; Value: Integer);
 
 Var Old : Longint;
 
@@ -3289,7 +3300,7 @@ begin
     end;
 end;
 
-Procedure TFields.Add(Field : TField);
+procedure TFields.Add(Field: TField);
 
 begin
   CheckFieldName(Field.FieldName);
@@ -3298,14 +3309,14 @@ begin
   Changed;
 end;
 
-Procedure TFields.CheckFieldName (Const Value : String);
+procedure TFields.CheckFieldName(const Value: String);
 
 begin
   If FindField(Value)<>Nil then
     DataBaseErrorFmt(SDuplicateFieldName,[Value],FDataset);
 end;
 
-Procedure TFields.CheckFieldNames (Const Value : String);
+procedure TFields.CheckFieldNames(const Value: String);
 
 
 Var I : longint;
@@ -3322,7 +3333,7 @@ begin
   Until (T='');
 end;
 
-Procedure TFields.Clear;
+procedure TFields.Clear;
 var
   AField: TField;
 begin
@@ -3336,7 +3347,7 @@ begin
   Changed;
 end;
 
-Function TFields.FindField (Const Value : String) : TField;
+function TFields.FindField(const Value: String): TField;
 
 Var S : String;
     I : longint;
@@ -3355,7 +3366,7 @@ begin
       end;
 end;
 
-Function TFields.FieldByName (Const Value : String) : TField;
+function TFields.FieldByName(const Value: String): TField;
 
 begin
   Result:=FindField(Value);
@@ -3363,7 +3374,7 @@ begin
     DatabaseErrorFmt(SFieldNotFound,[Value],FDataset);
 end;
 
-Function TFields.FieldByNumber(FieldNo : Integer) : TField;
+function TFields.FieldByNumber(FieldNo: Integer): TField;
 
 Var i : Longint;
 
@@ -3377,13 +3388,13 @@ begin
       end;
 end;
 
-Function TFields.GetEnumerator: TFieldsEnumerator;
+function TFields.GetEnumerator: TFieldsEnumerator;
 
 begin
   Result:=TFieldsEnumerator.Create(Self);
 end;
 
-Procedure TFields.GetFieldNames (Values : TStrings);
+procedure TFields.GetFieldNames(Values: TStrings);
 
 Var i : longint;
 
@@ -3393,7 +3404,7 @@ begin
     Values.Add(Tfield(FFieldList[I]).FieldName);
 end;
 
-Function TFields.IndexOf(Field : TField) : Longint;
+function TFields.IndexOf(Field: TField): Longint;
 
 begin
   Result:=FFieldList.IndexOf(Field);