jpfassis

DAO Generic - Delphi

Aug 27th, 2020
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.02 KB | None | 0 0
  1. unit Model.DAO.Generic;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.JSON,
  7.   REST.JSon,
  8.   SimpleInterface,
  9.   SimpleDAO,
  10.   SimpleAttributes,
  11.   SimpleQueryFiredac,
  12.   Data.DB,
  13.   DataSetConverter4D,
  14.   DataSetConverter4D.Impl,
  15.   DataSetConverter4D.Helper,
  16.   DataSetConverter4D.Util,
  17.   System.SysUtils;
  18.  
  19. type
  20.  
  21. iDAOGeneric<T : Class> = interface
  22.   ['{D808BA54-457B-4E80-ABC7-45016EAD857D}']
  23.   function Find : TJsonArray; overload;
  24.   function Find(const aID : String; var aObject : T) : iDAOGeneric<T>; overload;
  25.   function Find(const aID : String) : TJsonObject; overload;
  26.   function Insert (const aJsonObject : TJsonObject) : TJsonObject;
  27.   function Update (const aJsonObject : TJsonObject) : TJsonObject; overload;
  28.   function Update (const aObject : T) : iDAOGeneric<T>; overload;
  29.   function Delete (aField : String; aValue : String) : TJsonObject;
  30.   function DAO : iSimpleDAO<T>;
  31.   function DataSetAsJsonArray : TJsonArray;
  32.   function DataSetAsJsonObject : TJsonObject;
  33.   function DataSet : TDataSet;
  34. end;
  35.  
  36. TDAOGeneric<T : class, constructor> = class(TInterfacedObject, iDAOGeneric<T>)
  37.   private
  38.     FIndexConn : Integer;
  39.     FConn : iSimpleQuery;
  40.     FDAO : iSimpleDAO<T>;
  41.     FDataSource : TDataSource;
  42.   public
  43.     constructor Create;
  44.     destructor Destroy; override;
  45.     class function New : iDAOGeneric<T>;
  46.     function Find : TJsonArray; overload;
  47.     function Find(const aID : String; var aObject : T) : iDAOGeneric<T>; overload;
  48.     function Find(const aID : String) : TJsonObject; overload;
  49.     function Insert (const aJsonObject : TJsonObject) : TJsonObject;
  50.     function Update (const aJsonObject : TJsonObject) : TJsonObject; overload;
  51.     function Update (const aObject : T) : iDAOGeneric<T>; overload;
  52.     function Delete (aField : String; aValue : String) : TJsonObject;
  53.     function DAO : ISimpleDAO<T>;
  54.     function DataSetAsJsonArray : TJsonArray;
  55.     function DataSetAsJsonObject : TJsonObject;
  56.     function DataSet : TDataSet;
  57. end;
  58.  
  59. implementation
  60.  
  61. { TDAOGeneric<T> }
  62.  
  63. uses
  64.   Model.Connection;
  65.  
  66. constructor TDAOGeneric<T>.Create;
  67. begin
  68.   FDataSource := TDataSource.Create(nil);
  69.   FIndexConn := Model.Connection.Connected;
  70.   FConn := TSimpleQueryFireDAC.New(Model.Connection.FConnList.Items[FIndexConn]);
  71.   FDAO := TSimpleDAO<T>.New(FConn).DataSource(FDataSource);
  72. end;
  73.  
  74. function TDAOGeneric<T>.DAO: ISimpleDAO<T>;
  75. begin
  76.   Result := FDAO;
  77. end;
  78.  
  79. function TDAOGeneric<T>.DataSet: TDataSet;
  80. begin
  81.   Result := FDataSource.DataSet;
  82. end;
  83.  
  84. function TDAOGeneric<T>.DataSetAsJsonArray: TJsonArray;
  85. begin
  86.   Result := FDataSource.DataSet.AsJSONArray;
  87. end;
  88.  
  89. function TDAOGeneric<T>.DataSetAsJsonObject: TJsonObject;
  90. begin
  91.   Result := FDataSource.DataSet.AsJSONObject;
  92. end;
  93.  
  94. function TDAOGeneric<T>.Delete(aField, aValue: String): TJsonObject;
  95. begin
  96.   FDAO.Delete(aField, aValue);
  97.   Result := FDataSource.DataSet.AsJSONObject;
  98. end;
  99.  
  100. destructor TDAOGeneric<T>.Destroy;
  101. begin
  102.   FDataSource.Free;
  103.   Model.Connection.Disconnected(FIndexConn);
  104.   inherited;
  105. end;
  106.  
  107. function TDAOGeneric<T>.Find(const aID: String; var aObject: T): iDAOGeneric<T>;
  108. begin
  109.   Result := Self;
  110.   aObject := FDAO.Find(StrToInt(aID));
  111. end;
  112.  
  113. function TDAOGeneric<T>.Find(const aID: String): TJsonObject;
  114. begin
  115.    FDAO.Find(StrToInt(aID));
  116.    Result := FDataSource.DataSet.AsJSONObject;
  117. end;
  118.  
  119. function TDAOGeneric<T>.Find: TJsonArray;
  120. begin
  121.   FDAO.Find;
  122.   Result := FDataSource.DataSet.AsJSONArray;
  123. end;
  124.  
  125. function TDAOGeneric<T>.Insert(const aJsonObject: TJsonObject): TJsonObject;
  126. begin
  127.   FDAO.Insert(TJson.JsonToObject<T>(aJSonObject));
  128.   Result := FDataSource.DataSet.AsJSONObject;
  129. end;
  130.  
  131. class function TDAOGeneric<T>.New: iDAOGeneric<T>;
  132. begin
  133.   Result := Self.Create;
  134. end;
  135.  
  136. function TDAOGeneric<T>.Update(const aJsonObject: TJsonObject): TJsonObject;
  137. begin
  138.   FDAO.Update(TJson.JsonToObject<T>(aJsonObject));
  139.   Result := FDataSource.DataSet.AsJSONObject;
  140. end;
  141.  
  142. function TDAOGeneric<T>.Update(const aObject: T): iDAOGeneric<T>;
  143. begin
  144.   FDAO.Update(aObject);
  145.   Result := FDataSource.DataSet.AsJSONObject;
  146. end;
  147.  
  148. end.
  149.  
Add Comment
Please, Sign In to add comment