unit DatasetData;
interface
uses
DB, Variants, TypInfo;
function VarDatasetCreate( ds : TDataset ) : Variant;
implementation
uses
SysUtils;
type
TDatasetData =
packed record
VType : TVarType;
Reserved1, Reserved2, Reserved3 : Word;
VDataset : TDataset;
Reserved4 : LongWord;
end;
type
TDatasetVariant =
class( TPublishableVariantType )
protected
function GetInstance(
const V : TVarData ) : TObject;
override;
public
procedure Clear(
var V : TVarData );
override;
procedure Copy(
var Dest : TVarData;
const Source : TVarData;
const Indirect : Boolean );
override;
function GetProperty(
var Dest : TVarData;
const V : TVarData;
const Name :
string ) : Boolean;
override;
function SetProperty(
const V : TVarData;
const Name :
string;
const Value : TVarData ) : Boolean;
override;
end;
var
DatasetVariant : TDatasetVariant;
procedure TDatasetVariant.Clear(
var V : TVarData );
begin
SimplisticClear( V );
end;
// procedure TDatasetVariant.Clear( var V : TVarData );
// begin
// TDatasetData( V ).VType := varEmpty;
// TDatasetData( V ).VDataset := 0;
// end;
procedure TDatasetVariant.Copy(
var Dest : TVarData;
const Source : TVarData;
const Indirect : Boolean );
begin
SimplisticCopy( Dest, Source, Indirect );
end;
function TDatasetVariant.GetProperty(
var Dest : TVarData;
const V : TVarData;
const Name :
string ) : Boolean;
var
ds : TDataset;
fld : TField;
begin
ds := TDatasetData( V ).VDataset;
fld := ds.FindField(
Name );
if ( Assigned( fld ) )
then
begin
// If the property name is a field name from the dataset, return a
// reference to the field.
Result := True;
Variant( Dest ) := fld.Value;
end
else
// If the name of the property is not a field name, return the value
// of one of the published properties.
Result :=
inherited GetProperty( Dest, V,
Name );
end;
function TDatasetVariant.SetProperty(
const V : TVarData;
const Name :
string;
const Value : TVarData ) : Boolean;
var
ds : TDataset;
fld : TField;
begin
ds := TDatasetData( V ).VDataset;
fld := ds.FindField(
Name );
if ( Assigned( fld ) )
then
begin
fld.Value := Variant( Value );
Result := True;
end
else
// If the name of the property is not a field name, set the value
// of one of the published properties.
Result :=
inherited SetProperty( V,
Name, Value );
end;
function TDatasetVariant.GetInstance(
const V : TVarData ) : TObject;
begin
Result := TDatasetData( V ).VDataset;
end;
function VarDatasetCreate( ds : TDataset ) : Variant;
begin
VarClear( Result );
TDatasetData( Result ).VType := DatasetVariant.VarType;
TDatasetData( Result ).VDataset := ds;
end;
initialization
DatasetVariant := TDatasetVariant.Create;
finalization
FreeAndNil( DatasetVariant );
end.