Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

it's possible to create a fake data field in a delphi dataset?

Tags:

dataset

delphi

I want to create a 'fake' data field in a DataSet (not ClientDataSet):

  • the field should not be stored in the db
  • it's not a calculated field (the user should be allowed to enter input data)
  • the field has business logic meaning, so after the user updates its value it should update other fields (with the OnFieldChange event)

I know I can have a simple no-dbaware control, capture it's OnChange event and perform the calculations there (or call a DataModule function, where the DataSet resides) but I think it's more clean if I can reutilize the dataset automatic binding with db-ware controls and dataset events..

Also this way the unique connection between the Form (Presentation) and the DataModule (Model) it's the DataSet (less coupling)..

PD: I'm using fibplus, with I think the solution (if any) will be at the VCL level..

Thanks!

like image 319
pragmatic_programmer Avatar asked Nov 03 '11 15:11

pragmatic_programmer


3 Answers

Have you tried using an InternalCalc field? Your data aware controls will let you edit an InternalCalc field's value, and the value is stored in the dataset.

If you create an InternalCalc field in the dataset (TClientDataSet, TQuery, etc.) at design time, it's almost exactly what you're asking for.

like image 87
Marcus Adams Avatar answered Nov 08 '22 05:11

Marcus Adams


You could create an updatable view (with before insert/update/delete triggers) in your Interbase/Firebird database. This would look like a "virtual table" to the client (the select statement of the view's declaration can also contain "virtual fields") and it's totally up to you how you implement the triggers.

like image 45
Ondrej Kelle Avatar answered Nov 08 '22 05:11

Ondrej Kelle


You can make a Calcfield writeable:

type
  TCalcStringField = class(TWideStringField)
    function GetCanModify: Boolean; override;
  end;


function TCalcStringField.GetCanModify: Boolean;
begin
  // Makes Calcfield editable
//if FieldNo > 0 then
    if DataSet.State <> dsSetKey then
      Result := not ReadOnly and DataSet.CanModify
    else
      Result := IsIndexField
//else
//  Result := False;
end;

Set Calculated to true, and in OnSetText of this Field you can then write the Text to any other place

procedure TformXX.XXOnSetText(Sender: TField; const Text: string);
begin
   ...
end;
like image 39
stha68 Avatar answered Nov 08 '22 04:11

stha68