home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / HTML / dpoBase.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-08  |  1.7 KB  |  86 lines

  1. unit dpoBase;
  2.  
  3. interface
  4.  
  5. uses
  6.   Forms, DBTables, Classes, Db;
  7.  
  8. type
  9.   TDataObject = class;
  10.   TDataObjectClass = class of TDataObject;
  11.   
  12.   TDataObject = class(TDataModule)
  13.     qryMain: TQuery;
  14.   private
  15.   protected
  16.     OIDFieldNames: TStringList;
  17.     function GetOID: string;
  18.     procedure InternalSetOID(aOID: string); virtual;
  19.   public
  20.     constructor Create(aDB: TDatabase); reintroduce; virtual;
  21.     destructor Destroy; override;
  22.     procedure GetByOID(aOID: string);
  23.     function PropertyByName(aName: string): TField;
  24.     property OID: string read GetOID;
  25.   end;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30.  
  31. { TDataObject }
  32.  
  33. constructor TDataObject.Create(aDB: TDatabase);
  34. begin
  35.   inherited Create(nil); { do not set owner }
  36.   qryMain.DatabaseName := aDB.DatabaseName;
  37.   OIDFieldNames := TStringList.Create;
  38. end;
  39.  
  40. destructor TDataObject.Destroy;
  41. begin
  42.   OIDFieldNames.Free;
  43.   inherited;
  44. end;
  45.  
  46. procedure TDataObject.GetByOID(aOID: string);
  47. begin
  48.   with qryMain do
  49.   begin
  50.     if Active then Close;
  51.     InternalSetOID(aOID);
  52.     Open;
  53.     FetchAll;
  54.   end;
  55. end;
  56.  
  57. function TDataObject.GetOID: string;
  58. var
  59.   I: Integer;
  60. begin
  61.   Result := '';
  62.   for I := 0 to OIDFieldNames.Count - 1 do
  63.     Result := Result + VarAsType(qryMain.FieldByName(OIDFieldNames[I]).Value, varString);
  64. end;
  65.  
  66. procedure TDataObject.InternalSetOID(aOID: string);
  67. var
  68.   I: Integer;
  69. begin
  70.   with TStringList.Create do
  71.     try
  72.       CommaText := aOID;
  73.       for I := 0 to OIDFieldNames.Count - 1 do
  74.         qryMain.ParamByName(OIDFieldNames[I]).Value := Strings[I];
  75.     finally
  76.       Free;
  77.     end;
  78. end;
  79.  
  80. function TDataObject.PropertyByName(aName: string): TField;
  81. begin
  82.   Result := qryMain.FindField(aName);
  83. end;
  84.  
  85. end.
  86.