De acuerdo con os posts de Al Gonzalez em su blog RescatandoDelphi,
Implemente los eventos BeforeFieldChange y AfterFieldChange en Delphi 2010 utilizando la query de UNIDAC como herencia y obtuve éxito.
Sin embargo, no funcionó con Delphi Berlin.
He intentado debugar y me di cuenta de que la función NativeValue no devuelve los valores correctos.
¿Alguien sabría decirme cuál sería la solución?
Sigue el código del componente que he creado heredando de UNIDAC Query:
( perdón pero parece que no tengo permiso para insertar enlaces ni syntaxhilight en el foro )
Código Delphi
[-]
unit MxUniQuery;
interface
uses
SysUtils, Classes, DB, MemDS, DBAccess, Uni;
type
TFieldAfterChangeEvent = Procedure (Field :TField; PrevValue :Variant) Of Object;
TFieldBeforeChangeEvent = Procedure (Field :TField; NewValue :Variant) Of Object;
TMxUniQuery = class(TUniQuery)
private
FAfterFieldChange :TFieldAfterChangeEvent;
FBeforeFieldChange :TFieldBeforeChangeEvent;
protected
TempFieldData :PPointer;
Procedure SetFieldData (Field :TField; Buffer :Pointer); Override;
Function NativeValue (Const Field :TField; Const Buffer :Pointer) :Variant;
public
Function GetFieldData (Field :TField; Buffer :Pointer) :Boolean; Override;
published
Property AfterFieldChange:TFieldAfterChangeEvent Read FAfterFieldChange Write FAfterFieldChange;
Property BeforeFieldChange:TFieldBeforeChangeEvent Read FBeforeFieldChange Write FBeforeFieldChange;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MxDatabase', [TMxUniQuery]);
end;
Type
TFieldAccess = Class (TField);
function TMxUniQuery.GetFieldData(Field: TField; Buffer: Pointer): Boolean;
begin
If TempFieldData = Nil Then
Result := Inherited GetFieldData (Field,Buffer)
Else
Begin
Result := TempFieldData^ <> Nil;
If Result And (Buffer <> Nil) Then
TFieldAccess (Field).CopyData (TempFieldData^, Buffer);
End;
end;
function TMxUniQuery.NativeValue(const Field: TField;
const Buffer: Pointer): Variant;
begin
TempFieldData := @Buffer;
Try
Result := Field.Value;
Finally
TempFieldData := Nil;
End;
end;
procedure TMxUniQuery.SetFieldData(Field: TField; Buffer: Pointer);
var
Value: Variant;
begin
If State In [dsEdit, dsInsert] Then
Begin
Value := NativeValue (Field, Buffer);
If Value <> Field.Value Then Begin
If Assigned (BeforeFieldChange) Then
BeforeFieldChange (Field, Value);
Value := Field.Value; Inherited SetFieldData (Field, Buffer);
If Assigned (AfterFieldChange) Then
AfterFieldChange (Field, Value);
Exit;
End;
End;
Inherited SetFieldData (Field, Buffer);
end;
end.