Advertisement
jpfassis

Model.DAO.SQL Delphi

Jun 19th, 2023
1,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.03 KB | None | 0 0
  1. unit UModel.DAO.SQL;
  2.  
  3. interface
  4.  
  5. uses
  6.   Data.DB,
  7.   System.Generics.Collections,
  8.   System.SysUtils,
  9.   SimpleInterface,
  10.   SimpleDAO,
  11.   SimpleAttributes,
  12.   SimpleQueryFireDac,
  13.   UModel.DAOGeneric.Interfaces,
  14.   UModel.Connection.Firedac,
  15.   UModel.FactoryConnectionFireDac,
  16.   FireDAC.Comp.Client;
  17.  
  18. type
  19.   TModelDAOSQL<T : class, constructor>  = class(TInterfacedObject, iDAOGeneric<T>)
  20.     private
  21.       [weak]
  22.       FQuery : iSimpleQuery;
  23.       FConn : TFDConnection;
  24.       FDAO : iSimpleDAO<T>;
  25.       FDataSource : TDataSource;
  26.       i : integer;
  27.       FObject : T;
  28.     public
  29.       constructor Create;
  30.       destructor Destroy; override;
  31.       class function New : iDAOGeneric<T>;
  32.       function Insert : iDAOGeneric<T>;
  33.       function Update : iDAOGeneric<T>;
  34.       function Find : iDAOGeneric<T>; overload;
  35.       function Find(const aID : String) : iDAOGeneric<T>; overload;
  36.       function Find(var aList : TObjectList<T>) : iDAOGeneric<T>; overload;
  37.       function Find(aParam : String; aValue : String) : iDAOGeneric<T>; overload;
  38.       function Fields(aFields : String) : iDAOGeneric<T>;
  39.       function Where(aWhere : String) : iDAOGeneric<T>;
  40.       function OrderBy(aField : String) : iDAOGeneric<T>;
  41.       function Delete : iDAOGeneric<T>;
  42.       function DataSource(aValue : TDataSource) : iDAOGeneric<T>;
  43.       function Current : T;
  44.       function NewObject : T;
  45.       function DataSet : TDataSet;
  46. end;
  47.  
  48. implementation
  49.  
  50. { TModelDAOSQL<T> }
  51.  
  52.  
  53. constructor TModelDAOSQL<T>.Create;
  54. begin
  55.   FDataSource := TDataSource.Create(nil);
  56.  
  57.   FConn := TModelFactoryConnectionFireDac.New
  58.               .Connection
  59.               .Parameters
  60.                 .DriveName('FB')
  61.                 .DriveID('FB')
  62.                 .DataBase('C:\Trabalho\Dados\TABELAS_SISCOM.FDB')
  63.                 .UserName('SYSDBA')
  64.                 .Password('masterkey')
  65.                 .Server('localhost')
  66.                 .Porta(3050)
  67.               .&EndParams
  68.               .Connect
  69.               .GetConnection;
  70.  
  71.   FQuery := TSimplequeryFireDac.New(FConn);
  72.  
  73.   FDAO := TSimpleDAO<T>.New(FQuery).DataSource(FDataSource);
  74. end;
  75.  
  76. function TModelDAOSQL<T>.Current: T;
  77. begin
  78.   Result := FObject;
  79. end;
  80.  
  81. function TModelDAOSQL<T>.DataSet: TDataSet;
  82. begin
  83.   Result := FDataSource.DataSet;
  84. end;
  85.  
  86. function TModelDAOSQL<T>.DataSource(aValue: TDataSource): iDAOGeneric<T>;
  87. begin
  88.   Result := Self;
  89.   aValue.DataSet := FDataSource.DataSet;
  90. end;
  91.  
  92. function TModelDAOSQL<T>.Delete: iDAOGeneric<T>;
  93. begin
  94.   Result := Self;
  95.   FDAO.Delete(FObject);
  96. end;
  97.  
  98. destructor TModelDAOSQL<T>.Destroy;
  99. begin
  100.   FDataSource.Free;
  101.  
  102.   if Assigned(FObject) then
  103.     FObject.Free;
  104.  
  105.   inherited;
  106. end;
  107.  
  108. function TModelDAOSQL<T>.Fields(aFields: String): iDAOGeneric<T>;
  109. begin
  110.   Result := Self;
  111.   FDAO.SQL.Fields(aFields);
  112. end;
  113.  
  114. function TModelDAOSQL<T>.Find(aParam, aValue: String): iDAOGeneric<T>;
  115. begin
  116.   Result := Self;
  117.   FDAO.SQL.Where(aParam + ' = ' + aValue).&End.Find;
  118. end;
  119.  
  120. function TModelDAOSQL<T>.Find(var aList: TObjectList<T>): iDAOGeneric<T>;
  121. begin
  122.   Result := Self;
  123.   FDAO.Find(aList);
  124. end;
  125.  
  126. function TModelDAOSQL<T>.Find(const aID: String): iDAOGeneric<T>;
  127. begin
  128.   Result := Self;
  129.  
  130.   FDAO.Find(StrToInt(aID));
  131. end;
  132.  
  133. function TModelDAOSQL<T>.Find: iDAOGeneric<T>;
  134. begin
  135.   Result := Self;
  136.   FDAO.Find;
  137. end;
  138.  
  139. function TModelDAOSQL<T>.Insert: iDAOGeneric<T>;
  140. begin
  141.   Result := Self;
  142.   FDAO.Insert(FObject);
  143. end;
  144.  
  145. class function TModelDAOSQL<T>.New: iDAOGeneric<T>;
  146. begin
  147.   Result := Self.Create;
  148. end;
  149.  
  150. function TModelDAOSQL<T>.NewObject: T;
  151. begin
  152.   if Assigned(FObject) then
  153.     FObject.Free;
  154.  
  155.   FObject := T.Create;
  156.   Result := FObject;
  157. end;
  158.  
  159. function TModelDAOSQL<T>.OrderBy(aField: String): iDAOGeneric<T>;
  160. begin
  161.   Result := Self;
  162.   FDAO.SQL.OrderBy(aField).&End;
  163. end;
  164.  
  165. function TModelDAOSQL<T>.Update: iDAOGeneric<T>;
  166. begin
  167.   Result := Self;
  168.  
  169.   FDAO.Update(FObject);
  170. end;
  171.  
  172.  
  173. function TModelDAOSQL<T>.Where(aWhere: String): iDAOGeneric<T>;
  174. begin
  175.   Result := Self;
  176.   FDAO.SQL.Where(aWhere).&End;
  177. end;
  178.  
  179. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement