[fpc-devel] patch to tdataset

Alexandrov Alexandru alexandru.alexandrov at gmail.com
Sun Apr 16 19:46:05 CEST 2006


added GetIndexDefs and InitFieldDefsFromFields,
implemented Filed.GetIsIndexField.


Alex
-------------- next part --------------
Index: dataset.inc
===================================================================
--- dataset.inc	(revision 3232)
+++ dataset.inc	(working copy)
@@ -246,91 +246,91 @@
 
 begin
  If assigned(FAfterCancel) then
-   FAfterCancel(Self);
+ FAfterCancel(Self);
 end;
 
 Procedure TDataset.DoAfterClose;
 
 begin
  If assigned(FAfterClose) then
-   FAfterClose(Self);
+ FAfterClose(Self);
 end;
 
 Procedure TDataset.DoAfterDelete;
 
 begin
  If assigned(FAfterDelete) then
-   FAfterDelete(Self);
+ FAfterDelete(Self);
 end;
 
 Procedure TDataset.DoAfterEdit;
 
 begin
  If assigned(FAfterEdit) then
-   FAfterEdit(Self);
+ FAfterEdit(Self);
 end;
 
 Procedure TDataset.DoAfterInsert;
 
 begin
  If assigned(FAfterInsert) then
-   FAfterInsert(Self);
+ FAfterInsert(Self);
 end;
 
 Procedure TDataset.DoAfterOpen;
 
 begin
  If assigned(FAfterOpen) then
-   FAfterOpen(Self);
+ FAfterOpen(Self);
 end;
 
 Procedure TDataset.DoAfterPost;
 
 begin
  If assigned(FAfterPost) then
-   FAfterPost(Self);
+ FAfterPost(Self);
 end;
 
 Procedure TDataset.DoAfterScroll;
 
 begin
  If assigned(FAfterScroll) then
-   FAfterScroll(Self);
+ FAfterScroll(Self);
 end;
 
 Procedure TDataset.DoAfterRefresh;
 
 begin
  If assigned(FAfterRefresh) then
-   FAfterRefresh(Self);
+ FAfterRefresh(Self);
 end;
 
 Procedure TDataset.DoBeforeCancel;
 
 begin
  If assigned(FBeforeCancel) then
-   FBeforeCancel(Self);
+ FBeforeCancel(Self);
 end;
 
 Procedure TDataset.DoBeforeClose;
 
 begin
  If assigned(FBeforeClose) then
-   FBeforeClose(Self);
+ FBeforeClose(Self);
 end;
 
 Procedure TDataset.DoBeforeDelete;
 
 begin
  If assigned(FBeforeDelete) then
-   FBeforeDelete(Self);
+ FBeforeDelete(Self);
 end;
 
 Procedure TDataset.DoBeforeEdit;
 
 begin
  If assigned(FBeforeEdit) then
-   FBeforeEdit(Self);
+ FBeforeEdit(Self);
 end;
 
 Procedure TDataset.DoBeforeInsert;
@@ -344,28 +344,28 @@
 
 begin
  If assigned(FBeforeOpen) then
-   FBeforeOpen(Self);
+ FBeforeOpen(Self);
 end;
 
 Procedure TDataset.DoBeforePost;
 
 begin
  If assigned(FBeforePost) then
-   FBeforePost(Self);
+ FBeforePost(Self);
 end;
 
 Procedure TDataset.DoBeforeScroll;
 
 begin
  If assigned(FBeforeScroll) then
-   FBeforeScroll(Self);
+ FBeforeScroll(Self);
 end;
 
 Procedure TDataset.DoBeforeRefresh;
 
 begin
  If assigned(FBeforeRefresh) then
-   FBeforeRefresh(Self);
+ FBeforeRefresh(Self);
 end;
 
 Procedure TDataset.DoInternalOpen;
@@ -719,6 +719,21 @@
   Result := -1;
 end;
 
+function TDataSet.GetStateFieldValue(State: TDataSetState; Field: TField): Variant;
+
+var OldState: TDataSetState;
+begin
+  Result := NULL;
+  if Not (Field.FieldKind in [fkData, fkInternalCalc]) then Exit;
+  OldState := FState;
+  FState := State;
+  try
+    Result := Field.AsVariant;
+  finally
+  FState := OldState;
+  end;
+end;
+
 Function TDataset.GetRecordCount: Longint;
 
 begin
@@ -737,6 +752,48 @@
   end;
 end;
 
+procedure TDataSet.InitFieldDefsFromFields;
+
+  procedure CreateFieldDefs(Fields: TFields; FieldDefs: TFieldDefs);
+  var
+    I: Integer;
+    F: TField;
+    FieldDef: TFieldDef;
+  begin
+    for I := 0 to Fields.Count - 1 do
+    begin
+      F := Fields[I];
+      with F do
+      if FieldKind = fkData then
+      begin
+        FieldDef := FieldDefs.AddFieldDef;
+        FieldDef.Name := FieldName;
+        FieldDef.DataType := DataType;
+        FieldDef.Size := Size;
+        if Required then
+          FieldDef.Attributes := [faRequired];
+        if ReadOnly then
+          FieldDef.Attributes := FieldDef.Attributes + [faReadonly];
+        if (DataType = ftBCD) and (F is TBCDField) then
+          FieldDef.Precision := TBCDField(F).Precision;
+      end;
+    end;
+  end;
+
+begin
+  { Create FieldDefs from persistent fields if needed }
+  if FieldDefs.Count = 0 then
+  begin
+    FieldDefs.BeginUpdate;
+    try
+      CreateFieldDefs(FFieldList, FieldDefs);
+    finally
+      FieldDefs.EndUpdate;
+    end;
+  end;
+end;
+
+
 Procedure TDataset.InitRecord(Buffer: PChar);
 
 begin
@@ -1081,6 +1138,22 @@
     end;
 end;
 
+procedure TDataSet.SetStateFieldValue(State: TDataSetState; Field: TField;
+  const Value: Variant);
+
+var
+  OldState: TDataSetState;
+begin
+  if Field.FieldKind <> fkData then Exit;
+  OldState := FState;
+  FState := State;
+  try
+    Field.AsVariant := Value;
+  finally
+    FState := OldState;
+  end;
+end;
+
 Function TDataset.Tempbuffer: PChar;
 
 begin
@@ -1975,6 +2048,37 @@
     Result := FieldByName(FieldName).Value
 end;
 
+function TDataSet.GetIndexDefs ( IndexDefs: TIndexDefs;
+  IndexTypes: TIndexOptions ) : TIndexDefs;
+
+var i: Integer;
+begin
+  Result := nil;
+  try
+    IndexDefs.Update;
+    if IndexDefs.Count = 0 then Exit;
+    Result := TIndexDefs.Create(TDataset(nil));
+    Result.Assign(IndexDefs);
+    for i := Result.Count - 1 downto 0 do
+      if (IndexTypes <> []) and ((Result[i].Options * IndexTypes) = []) then
+        Result[i].Free
+      else
+      try
+        GetFieldList(nil, Result[i].Fields);
+      except
+        Result[i].Free;
+      end;
+  except
+    if Assigned(Result) then
+      Result.Clear;
+  end;
+  if Result.Count = 0 then
+  begin
+    Result.Free;
+    Result := nil;
+  end;
+end;
+
 procedure TDataset.SetFieldValues(Fieldname: string; Value: Variant);
 
 var i: Integer;
Index: db.pp
===================================================================
--- db.pp	(revision 3232)
+++ db.pp	(working copy)
@@ -268,6 +268,7 @@
     function IsDisplayStored : Boolean;
     function GetLookupList: TLookupList;
     procedure CalcLookupValue;
+    function GetIsIndexField: boolean;
   protected
     function AccessError(const TypeName: string): EDatabaseError;
     procedure CheckInactive;
@@ -345,7 +346,7 @@
     property DisplayName: String Read GetDisplayName;
     property DisplayText: String read GetDisplayText;
     property FieldNo: Longint read FFieldNo;
-    property IsIndexField: Boolean read FIsIndexField;
+    property IsIndexField: Boolean read GetIsIndexField;
     property IsNull: Boolean read GetIsNull;
     property NewValue: Variant read GetNewValue write SetNewValue;
     property Offset: word read FOffset;
@@ -1024,6 +1025,7 @@
     function  GetFieldClass(FieldType: TFieldType): TFieldClass; virtual;
     Function  GetfieldCount : Integer;
     function  GetFieldValues(fieldname : string) : Variant; virtual;
+    function  GetIndexDefs(IndexDefs: TIndexDefs; IndexTypes: TIndexOptions): TIndexDefs;
     function  GetIsIndexField(Field: TField): Boolean; virtual;
     function  GetNextRecords: Longint; virtual;
     function  GetNextRecord: Boolean; virtual;
@@ -1031,7 +1033,9 @@
     function  GetPriorRecord: Boolean; virtual;
     function  GetRecordCount: Longint; virtual;
     function  GetRecNo: Longint; virtual;
+    function  GetStateFieldValue(State: TDataSetState; Field: TField): Variant; virtual;
     procedure InitFieldDefs; virtual;
+    procedure InitFieldDefsFromFields;
     procedure InitRecord(Buffer: PChar); virtual;
     procedure InternalCancel; virtual;
     procedure InternalEdit; virtual;
@@ -1055,6 +1059,7 @@
     procedure SetOnFilterRecord(const Value: TFilterRecordEvent); virtual;
     procedure SetRecNo(Value: Longint); virtual;
     procedure SetState(Value: TDataSetState);
+    procedure SetStateFieldValue(State: TDataSetState; Field: TField; const Value: Variant); virtual;
     function SetTempState(const Value: TDataSetState): TDataSetState;
     Function Tempbuffer: PChar;
     procedure UpdateIndexDefs; virtual;
@@ -1696,15 +1701,15 @@
   TMasterParamsDataLink = Class(TMasterDataLink)
   Private
     FParams : TParams;
-    Procedure SetParams(AVAlue : TParams);  
-  Protected  
+    Procedure SetParams(AVAlue : TParams);
+  Protected
     Procedure DoMasterDisable; override;
     Procedure DoMasterChange; override;
   Public
     constructor Create(ADataSet: TDataSet); override;
     Procedure RefreshParamNames; virtual;
     Procedure CopyParamsFromMaster(CopyBound : Boolean); virtual;
-    Property Params : TParams Read FParams Write SetParams;  
+    Property Params : TParams Read FParams Write SetParams;
   end;
 
 const
Index: fields.inc
===================================================================
--- fields.inc	(revision 3232)
+++ fields.inc	(working copy)
@@ -478,58 +478,26 @@
 
 function TField.GetOldValue: Variant;
 
-var SaveState : TDatasetState;
-
 begin
-  SaveState := FDataset.State;
-  try
-    FDataset.SetTempState(dsOldValue);
-    Result := GetAsVariant;
-  finally
-    FDataset.RestoreState(SaveState);
-  end;
+  Result := DataSet.GetStateFieldValue(dsOldValue, Self);
 end;
 
 function TField.GetNewValue: Variant;
 
-var SaveState : TDatasetState;
-
 begin
-  SaveState := FDataset.State;
-  try
-    FDataset.SetTempState(dsNewValue);
-    Result := GetAsVariant;
-  finally
-    FDataset.RestoreState(SaveState);
-  end;
+  Result := DataSet.GetStateFieldValue(dsNewValue, Self);
 end;
 
 procedure TField.SetNewValue(const AValue: Variant);
 
-var SaveState : TDatasetState;
-
 begin
-  SaveState := FDataset.State;
-  try
-    FDataset.SetTempState(dsNewValue);
-    SetAsVariant(AValue);
-  finally
-    FDataset.RestoreState(SaveState);
-  end;
+  DataSet.SetStateFieldValue(dsNewValue, Self, Value);
 end;
 
 function TField.GetCurValue: Variant;
 
-var SaveState : TDatasetState;
-
 begin
-  SaveState := FDataset.State;
-  try
-    FDataset.SetTempState(dsCurValue);
-    Result := GetAsVariant;
-  finally
-    FDataset.RestoreState(SaveState);
-  end;
+  Result := DataSet.GetStateFieldValue(dsCurValue, Self);
 end;
 
 function TField.GetCanModify: Boolean;
@@ -608,6 +576,13 @@
       FDataSet.FieldValues[FKeyFields], FLookupResultField);
 end;
 
+function TField.GetIsIndexField: boolean;
+begin
+  if (FDataSet <> nil) then
+    Result := DataSet.GetIsIndexField(Self) else
+    Result := False;
+end;
+
 function TField.getIndex : longint;
 
 begin




More information about the fpc-devel mailing list