Posts Tagged ‘firedac’

FireDAC MsSQL server compound field/column

Thursday, March 17th, 2016

So I bumped into this problem with firedac (8.0.x) where I had a table like


CREATE TABLE test(
col1 integer,
col2 integer,
col3 AS ISNULL(col1, col2)
)

and when inserting or updating the col1 or col2 fields, the dataset would not update the col3.
I did quite some debugging and found that there is no support in FireDAC for mssql server for compound fields. There is some in firebird though.
So spending some more time in debugging, I managed to find a workaround by subclassing the dataset like this:


type
TADQuery = class(uADCompClient.TADQuery)
private
FOnAfterInitFieldDefs: TNotifyEvent;
protected
procedure InternalInitFieldDefs; override;
public
property OnAfterInitFieldDefs: TNotifyEvent read FOnAfterInitFieldDefs write FOnAfterInitFieldDefs;
end;

procedure TADQuery.InternalInitFieldDefs;
begin
inherited;

if Assigned(FOnAfterInitFieldDefs) then
FOnAfterInitFieldDefs(self);
end;

... in formcreate or wherever ...

qrySomething.OnAfterInitFieldDefs := DoAfterInitFieldDefs;

procedure TForm1.DoAfterInitFieldDefs(Sender: TObject);
var i: integer;
col: TADDatSColumn;
begin
for i := 0 to TDataSet(Sender).FieldDefs.Count - 1 do
if TDataSet(Sender).FieldDefs[i].Name = 'col3' then
begin
col := TADDataSet(Sender).Table.Columns.ColumnByName('col3');
col.Options := col.Options + [coAfterInsChanges, coAfterUpdChanged];
col.Attributes := col.Attributes + [caDefault];
end;
end;

Enjoy.

Related posts