EntityDAC

DemoClasses.pas

unit DemoClasses;

interface

uses
  SysUtils, Classes,
  EntityDAC.EntityAttributes,
  EntityDAC.EntityContext,
  EntityDAC.MetaData,
  EntityDAC.MetaEntity,
  EntityDAC.NullableTypes,
  EntityDAC.Types;

type
  TDept = class;
  TEmp = class;
  TDeptEmps = class;

  [Table('DEPT')]
  [Model('Demo')]
  [Key('FDeptno')]
  TDept = class(TMappedEntity)
  private
    [Column('DEPTNO', [ReadOnly])]
    FDeptno: TIntegerAttribute;
    [Column('DNAME', 14)]
    FDname: TStringAttribute;
    [Column('LOC', 13)]
    FLoc: TStringAttribute;

    [Column]
    [Collection('TEmp', 'FDept', 'FDeptno', 'FDeptno', srNone, drNone)]
    FEmps: TDeptEmps;

    function GetDeptno: Integer;
    function GetDname: String;
    procedure SetDname(const Value: String);
    function GetLoc: String;
    procedure SetLoc(const Value: String);
  protected
    constructor Create(AMetaType: TMetaType); overload; override;

  public
    constructor Create; overload; override;

    property Deptno: Integer read GetDeptno;
    property Dname: String read GetDname write SetDname;
    property Loc: String read GetLoc write SetLoc;

    property Emps: TDeptEmps read FEmps;
  end;

  [Table('EMP')]
  [Model('Demo')]
  [Key('FEmpno')]
  TEmp = class(TMappedEntity)
  private
    [Column('EMPNO', [ReadOnly])]
    FEmpno: TIntegerAttribute;
    [Column('ENAME', 10)]
    FEname: TStringAttribute;
    [Column('JOB', 9)]
    FJob: TStringAttribute;
    [Column('MGR', [CanBeNull])]
    FMgr: TIntegerNullableAttribute;
    [Column('HIREDATE')]
    FHiredate: TDateTimeAttribute;
    [Column('SAL', [CanBeNull])]
    FSal: TDoubleNullableAttribute;
    [Column('COMM', [CanBeNull])]
    FComm: TDoubleNullableAttribute;
    [Column('DEPTNO', [CanBeNull])]
    FDeptno: TIntegerNullableAttribute;

    [Column]
    [Reference('TDept', 'FEmps', 'FDeptno', 'FDeptno', srNone, drNone)]
    FDept: TMappedReference;

    function GetEmpno: Integer;
    function GetEname: String;
    procedure SetEname(const Value: String);
    function GetJob: String;
    procedure SetJob(const Value: String);
    function GetMgr: IntegerNullable;
    procedure SetMgr(const Value: IntegerNullable);
    function GetHiredate: TDateTime;
    procedure SetHiredate(const Value: TDateTime);
    function GetSal: DoubleNullable;
    procedure SetSal(const Value: DoubleNullable);
    function GetComm: DoubleNullable;
    procedure SetComm(const Value: DoubleNullable);
    function GetDeptno: IntegerNullable;
    procedure SetDeptno(const Value: IntegerNullable);
  protected
    constructor Create(AMetaType: TMetaType); overload; override;


    function GetDept: TDept;
    procedure SetDept(const Value: TDept);
  public
    constructor Create; overload; override;

    property Empno: Integer read GetEmpno;
    property Ename: String read GetEname write SetEname;
    property Job: String read GetJob write SetJob;
    property Mgr: IntegerNullable read GetMgr write SetMgr;
    property Hiredate: TDateTime read GetHiredate write SetHiredate;
    property Sal: DoubleNullable read GetSal write SetSal;
    property Comm: DoubleNullable read GetComm write SetComm;
    property Deptno: IntegerNullable read GetDeptno write SetDeptno;

    property Dept: TDept read GetDept write SetDept;
  end;


  TDeptEmps = class(TMappedCollection<TEmp>)
  end;

implementation

uses
  EntityDAC.Utils;

{ TDept }

constructor TDept.Create(AMetaType: TMetaType);
begin
  inherited Create(AMetaType);

  FDeptno := TIntegerAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Deptno'));
  FDname := TStringAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Dname'));
  FLoc := TStringAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Loc'));
  FEmps := TDeptEmps.Create(Self, MetaType.MetaCollections.Get('Emps'));
end;

constructor TDept.Create;
begin
  Create(Models.GetMetaType(Self.ClassType));
end;

function TDept.GetDeptno: Integer;
begin
  Result := FDeptno.Value;
end;

function TDept.GetDname: String;
begin
  Result := FDname.Value;
end;

procedure TDept.SetDname(const Value: String);
begin
  FDname.Value := Value;
end;

function TDept.GetLoc: String;
begin
  Result := FLoc.Value;
end;

procedure TDept.SetLoc(const Value: String);
begin
  FLoc.Value := Value;
end;

{ TEmp }

constructor TEmp.Create(AMetaType: TMetaType);
begin
  inherited Create(AMetaType);

  FEmpno := TIntegerAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Empno'));
  FEname := TStringAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Ename'));
  FJob := TStringAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Job'));
  FMgr := TIntegerNullableAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Mgr'));
  FHiredate := TDateTimeAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Hiredate'));
  FSal := TDoubleNullableAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Sal'));
  FComm := TDoubleNullableAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Comm'));
  FDeptno := TIntegerNullableAttribute.Create(Attributes, MetaType.MetaAttributes.Get('Deptno'));
  FDept := TMappedReference.Create(Self, MetaType.MetaReferences.Get('Dept'));
end;

constructor TEmp.Create;
begin
  Create(Models.GetMetaType(Self.ClassType));
end;

function TEmp.GetEmpno: Integer;
begin
  Result := FEmpno.Value;
end;

function TEmp.GetEname: String;
begin
  Result := FEname.Value;
end;

procedure TEmp.SetEname(const Value: String);
begin
  FEname.Value := Value;
end;

function TEmp.GetJob: String;
begin
  Result := FJob.Value;
end;

procedure TEmp.SetJob(const Value: String);
begin
  FJob.Value := Value;
end;

function TEmp.GetMgr: IntegerNullable;
begin
  Result := FMgr.Value;
end;

procedure TEmp.SetMgr(const Value: IntegerNullable);
begin
  FMgr.Value := Value;
end;

function TEmp.GetHiredate: TDateTime;
begin
  Result := FHiredate.Value;
end;

procedure TEmp.SetHiredate(const Value: TDateTime);
begin
  FHiredate.Value := Value;
end;

function TEmp.GetSal: DoubleNullable;
begin
  Result := FSal.Value;
end;

procedure TEmp.SetSal(const Value: DoubleNullable);
begin
  FSal.Value := Value;
end;

function TEmp.GetComm: DoubleNullable;
begin
  Result := FComm.Value;
end;

procedure TEmp.SetComm(const Value: DoubleNullable);
begin
  FComm.Value := Value;
end;

function TEmp.GetDeptno: IntegerNullable;
begin
  Result := FDeptno.Value;
end;

procedure TEmp.SetDeptno(const Value: IntegerNullable);
begin
  FDeptno.Value := Value;
end;

function TEmp.GetDept: TDept;
begin
  Result := FDept.Value as TDept;
end;

procedure TEmp.SetDept(const Value: TDept);
begin
  FDept.Value := Value;
end;

{******************************************************************************}
{  The following code is used for automatic entity mapping                     }
{******************************************************************************}

initialization
  ForceRtti(TDept);
  ForceRtti(TEmp);

end.
© 1997-2017 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback