Subject RE: [IBO] Calculated Fields - still didn´t got them
Author IBO Support List
Sorry for my belated response.

There are a few things you can try to beef up your performance.

Since you will get an aRow that could differ between the Fields[] array and
the BufferFields[] array you cannot just use a direct link to persistent
field objects. But, instead of doing a call to aRow.ByName[] you can store
the index offset for each of the columns you are working with such that you
can do something like this:

type
TMyForm = class ( TCustomForm )
...
Private
MyFieldA_Idx: integer;
MyFieldB_Idx: integer;
MyFieldC_Idx: integer;
MyFieldD_Idx: integer; ...

Procedure TMyForm.MyQueryAfterPrepare( ... )
Begin
MyFieldA_Idx := MyQuery.FieldByName( 'FIELD_A' ).Index;
MyFieldB_Idx := MyQuery.FieldByName( 'FIELD_B' ).Index;
MyFieldC_Idx := MyQuery.FieldByName( 'FIELD_C' ).Index;
MyFieldD_Idx := MyQuery.FieldByName( 'FIELD_D' ).Index;
End;

Procedure TMyForm.MyQueryCalculateFields(
Sender: TIB_Statement;
ARow: TIB_Row;
AField: TIB_Column );
Begin
If not Assigned( Afield ) or ( AField.FieldName = 'FIELD_A' ) then
aRow[MyFieldA_Idx].AsString :=
MyCalculationFunction1( aRow[MyFieldB_Idx].AsString );
If not Assigned( Afield ) or ( AField.FieldName = 'FIELD_C' ) then
aRow[MyFieldC_Idx].AsString :=
MyCalculationFunction2( aRow[MyFieldD_Idx].AsString );
End;

This should improve your performance.

Also, please note, the values for the calculated columns remains stored in
your buffers so they are only calculated when the record is first fetched
and then afterwards if values in the record are changed. Thus, you should
not have a significant performance degredation from using them as I have
designed for them to work.

I hope this helps you get the boost you are looking for.

Jason

-----Original Message-----
From: IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com] On Behalf
Of Andrei Luís
Sent: 26 September 2012 08:28 AM
To: ibobjects@yahoogroups.com
Subject: [IBO] Calculated Fields - still didn´t got them

Hello folks,

I'm dealing with a performance problem for a long time, and I know that
part of it has been the calculated fields I'm using.

I'm doing them by 'my way', and by this I mean 'any way', not the best.

I'm searching about this topic here in the list, and I found something like
that:

1 procedure Tdm.qrTestCalculateField(Sender: TIB_Statement; ARow: TIB_Row;
AField: TIB_Column);
2 begin
3 if (AField.FieldName='MYCALC1') then
4 begin
5 AField.AsCurrency := ARow.ByName('FIELD_A').AsCurrency -
ARow.ByName('FIELD_B').asCurrency;
6 end;
7
8 if (AField.FieldName='MYCALC2') then
9 begin
10 AField.AsCurrency := ARow.ByName('FIELD_A').AsCurrency +
ARow.ByName('FIELD_B').asCurrency;
11 end;
12
13 if (AField.FieldName='MYCALC3') then
14 begin
15 AField.AsCurrency := ARow.ByName('FIELD_A').AsCurrency *
ARow.ByName('FIELD_B').asCurrency;
16 end;
17 end;


My questions:
1 - Is this the best way to use calculated fields in IBO? I mean, is it the
fastest way thinking about performance?
2 - I tried this code, but AField is always NIL!??? So if I run it like
that, I get an access violation exception at line 3. So I tried to isolate
this with a 'If AField = nil then exit', but every time the event is
called, AField is nil, then nothing is calculated.
3 - How about the code below? This is what I'm doing today:
1 procedure Tdm.qrTestCalculateField(Sender: TIB_Statement; ARow:
TIB_Row; AField: TIB_Column);
2 begin
3 ARow.ByName('MYCALC1').asCurrency :=
ARow.ByName('FIELD_A').AsCurrency - ARow.ByName('FIELD_B').asCurrency;
4 ARow.ByName('MYCALC1').asCurrency :=
ARow.ByName('FIELD_A').AsCurrency + ARow.ByName('FIELD_B').asCurrency;
5 ARow.ByName('MYCALC1').asCurrency :=
ARow.ByName('FIELD_A').AsCurrency * ARow.ByName('FIELD_B').asCurrency;
6 end;

[]s
Andrei


[Non-text portions of this message have been removed]



------------------------------------

___________________________________________________________________________
IB Objects - direct, complete, custom connectivity to Firebird or InterBase
without the need for BDE, ODBC or any other layer.
___________________________________________________________________________
http://www.ibobjects.com - your IBO community resource for Tech Info papers,
keyword-searchable FAQ, community code contributions and more !
Yahoo! Groups Links