Delphi和C++ Builder中的Hibernate開發(四)


持久(Persistent)的實例在數據庫中有對應的記錄,並擁有一個持久化標識(identifier)。

有時一條查詢語句返回多個記錄,對應多個持久對象,因此需要定義一個持久對象的容器類。

unit UnitBaseTableList;

interface

uses
  SysUtils, Windows, Messages, Classes, Contnrs, TypInfo,
  DB, Variants, MyUtils, UnitBaseTable;

type

  TTableDataClass = class of TTableData;
  TTableClassArray = array of TTableDataClass;

  TTableDataList = class(TPersistent)
  private
    FItemType: TTableDataClass;
    FList: TObjectList;
    FOnRefresh: TNotifyEvent;
    function GetCount: Integer;
    function GetModified: Boolean;
  protected
    function GetItem(Index: Integer): TTableData; virtual;
    procedure SetItem(Index: Integer; Value: TTableData); virtual;
  public
    constructor Create(AItemType: TTableDataClass); reintroduce;
    destructor Destroy; override;
    procedure Add(AValue: TTableData);
    procedure Append(ASource: TTableDataList);
    procedure Clear;
    function Extract(AObject: TTableData): TTableData;
    function FindByKeyValue(Value: Variant): TTableData; overload;
    function FindByPropertyValue(APropertyName: string; AValue: Variant)
      : TTableData;
    function FindByKeyValue(AOperator: TPersistent; Value: Variant)
      : TTableData; overload;
    function FindByValues(AOperator: TPersistent; ANames: array of string;
      AValues: array of Variant): TTableData;
    procedure Refresh; virtual;
    procedure Remove(AObject: TTableData);
    property Count: Integer read GetCount;
    property Items[Index: Integer]: TTableData read GetItem
      write SetItem; default;
    property ItemType: TTableDataClass read FItemType write FItemType;
    property Modified: Boolean read GetModified;
    property OnRefresh: TNotifyEvent read FOnRefresh write FOnRefresh;
  end;

implementation

{
  ******************************** TTableDataList ********************************
}

uses unitDataOperator;

constructor TTableDataList.Create(AItemType: TTableDataClass);
begin
  FList := TObjectList.Create(False);
  FItemType := AItemType;
end;

destructor TTableDataList.Destroy;
begin
  FList.Free;
  inherited;
end;

procedure TTableDataList.Add(AValue: TTableData);
begin
  FList.Add(AValue);
end;

procedure TTableDataList.Append(ASource: TTableDataList);
var
  I: Integer;
begin
  for I := 0 to ASource.Count - 1 do // Iterate
  begin
    Add(ASource[I]);
  end; // for

end;

procedure TTableDataList.Clear;
begin
  FList.Clear;
end;

function TTableDataList.Extract(AObject: TTableData): TTableData;
begin
  Result := FList.Extract(AObject) as TTableData;
end;

function TTableDataList.FindByKeyValue(Value: Variant): TTableData;
var
  I: Integer;
begin
  for I := 0 to Count - 1 do // Iterate
  begin
    Result := Items[I];
    if Result.KeyValue = Value then
      Exit;
  end; // for
  Result := nil;
end;

function TTableDataList.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TTableDataList.GetItem(Index: Integer): TTableData;
begin
  Result := TTableData(FList.Items[Index]);
end;

procedure TTableDataList.Refresh;
begin
  Clear;
  if Assigned(FOnRefresh) then
    FOnRefresh(Self);
end;

procedure TTableDataList.Remove(AObject: TTableData);
begin
  FList.Remove(AObject);
end;

procedure TTableDataList.SetItem(Index: Integer; Value: TTableData);
begin
  FList.Items[Index] := Value;
end;

function TTableDataList.GetModified: Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to Count - 1 do // Iterate
  begin
    Result := Items[I].Modified or (Items[I].DeleteFlag);
    if Result then
      Exit;
  end; // for
end;

function TTableDataList.FindByKeyValue(AOperator: TPersistent; Value: Variant)
  : TTableData;
begin
  Result := FindByKeyValue(Value);
  if (Result = nil) and (AOperator is TDataOperator) then
    Result := TDataOperator(AOperator).Load(FItemType, Value);
end;

function TTableDataList.FindByPropertyValue(APropertyName: string;
  AValue: Variant): TTableData;
begin

end;

function TTableDataList.FindByValues(AOperator: TPersistent;
  ANames: array of string; AValues: array of Variant): TTableData;
var
  I, J: Integer;
  Found: Boolean;
begin
  Result := nil;
  for I := 0 to Count - 1 do // Iterate
  begin
    Result := Items[I];
    Found := False;
    for J := Low(ANames) to High(ANames) do // Iterate
    begin
      Found := Result.Values[ANames[I]] = AValues[I];
      if not Found then
        Break;
    end; // for
    if Found then
      Exit;
  end; // for
  if AOperator is TDataOperator then
    Result := TDataOperator(AOperator).Load(ItemType, ANames, AValues);
end;

end.

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com