Advertisement
AtlasSoft

Classe de conexao FireDAC

Dec 10th, 2023
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 10.68 KB | None | 0 0
  1. unit SyncMaster.Model.Conexao.Firedac;
  2. interface
  3. uses
  4.   Data.DB,
  5.  
  6.   Datasnap.DBClient,
  7.  
  8.   FireDAC.Comp.Client,
  9.   FireDAC.Comp.UI,
  10.   FireDAC.Phys,
  11.   FireDAC.Phys.FB,
  12.   FireDAC.Phys.FBDef,
  13.   FireDAC.Phys.IBBase,
  14.   FireDAC.Phys.Intf,
  15.   FireDAC.Stan.Async,
  16.   FireDAC.Stan.Def,
  17.   FireDAC.Stan.Error,
  18.   FireDAC.Stan.Intf,
  19.   FireDAC.Stan.Option,
  20.   FireDAC.Stan.Pool,
  21.   FireDAC.UI.Intf,
  22.   FireDAC.VCLUI.Wait,
  23.  
  24.   SyncMaster.Model.Interfaces,
  25.   SyncMaster.Model.Query.Firedac,
  26.   SyncMaster.Model.Tipificacoes,
  27.  
  28.   System.Classes,
  29.   System.Generics.Collections;
  30. type
  31.   TModelConexao = class(TInterfacedObject, IModelConexao)
  32.   private
  33.     FConexao: TFDConnection;
  34.     FQuery: TList<IModelQuery>;
  35.     FID : Integer;
  36.     FTableReceive: String;
  37.     FTableSend: String;
  38.     FDescrition: String;
  39.     FSend : Boolean;
  40.     FReceive : Boolean;
  41.     FDPhysFBDriverLink: TFDPhysFBDriverLink;
  42.     FTipsConexao: TTipsConexao; //é um record que carrego com os parametros para conexao de um arquivo .ini
  43.     procedure SetConexao(const Value: TFDConnection);
  44.     function GetConexao: TFDConnection;
  45.     function GetID: Integer;
  46.     procedure SetID(const Value: Integer);
  47.     procedure SetDescrition(const Value: String);
  48.     procedure SetTableReceive(const Value: String);
  49.     procedure SetTableSend(const Value: String);
  50.     function GetDescrition : String;
  51.     function GetTableReceive : String;
  52.     function GetTableSend : String;
  53.     function GetConnectionStatus: Boolean;
  54.     procedure SetConnectionStatus(const Value: Boolean);
  55.     function GetReceive: Boolean;
  56.     function GetSend: Boolean;
  57.     procedure SetReceive(const Value: Boolean);
  58.     procedure SetSend(const Value: Boolean);
  59.     function GetTables(Tipo : String): TStringList;
  60.     function fnc_buscarOrdemTabelas(Arquivo : String) : TStringList;
  61.     function StartTransaction : IModelConexao;
  62.     function Commit : IModelConexao;
  63.     function Rollback : IModelConexao;
  64.     function Connected(Value : Boolean) : iModelConexao;
  65.     function Objecto : TObject;
  66.     function Query(Value : Integer) : iModelQuery;
  67.     function IdNewQuery : Integer;
  68.     procedure FDBeforeConnect(Sender: TObject);
  69.     function GetTipsConexao: TTipsConexao;
  70.  
  71.   public
  72.     constructor Create(TipsConexao : TTipsConexao);
  73.     destructor Destroy; override;
  74.     class function New(TipsConexao : TTipsConexao) : IModelConexao;
  75.     function GetPrimaryKeys(Table : String) : TStringList;
  76.     class procedure GravarLog(aString : String);
  77.     property Conexao: TFDConnection read GetConexao write SetConexao;
  78.     property ID : Integer read GetID write SetID;
  79.     property Descrition : String read GetDescrition write SetDescrition;
  80.     property TableReceive : String read GetTableReceive write SetTableReceive;
  81.     property TableSend : String read GetTableSend write SetTableSend;
  82.     property ConnectionStatus : Boolean read GetConnectionStatus write SetConnectionStatus;
  83.     property Send : Boolean read GetSend write SetSend;
  84.     property Receive : Boolean read GetReceive write SetReceive;
  85.   end;
  86. implementation
  87. uses
  88.   System.IniFiles,
  89.   System.SysUtils;
  90. { TConexao }
  91.  
  92. function TModelConexao.Commit: IModelConexao;
  93. begin
  94.   Result := Self;
  95.   FConexao.Commit;
  96. end;
  97. function TModelConexao.Connected(Value: Boolean): iModelConexao;
  98. begin
  99.   Result := Self;
  100.   FConexao.Connected := Value;
  101. end;
  102. constructor TModelConexao.Create(TipsConexao : TTipsConexao);
  103. begin
  104.   Conexao := TFDConnection.Create(nil);
  105.   FTipsConexao := TipsConexao;
  106.   FDPhysFBDriverLink:= TFDPhysFBDriverLink.Create(nil);
  107.   try
  108.     FConexao.BeforeConnect := FDBeforeConnect;
  109.     FQuery := TList<IModelQuery>.Create;
  110.     ID := TipsConexao.ID;
  111.     Descrition := TipsConexao.Descrition;
  112.     TableReceive := TipsConexao.TableReceive;
  113.     TableSend := TipsConexao.TableSend;
  114.     Send := TipsConexao.Send;
  115.     Receive := TipsConexao.Receive;
  116.     Conexao.Connected := true;
  117.   except
  118.     raise Exception.Create('Não foi possível conectar a base de dados');
  119.   end;
  120. end;
  121. function TModelConexao.Objecto: TObject;
  122. begin
  123.   Result := FConexao;
  124. end;
  125. function TModelConexao.Query(Value : Integer) : iModelQuery;
  126. begin
  127.   while Pred(FQuery.Count) < Value do
  128.   begin
  129.     FQuery.Add(TModelQuery.New(Self));
  130.   end;
  131.   Result := FQuery[Value];
  132. end;
  133. function TModelConexao.Rollback: IModelConexao;
  134. begin
  135.   Result := Self;
  136.   FConexao.Rollback;
  137. end;
  138. destructor TModelConexao.Destroy;
  139. begin
  140.   if Assigned(FDPhysFBDriverLink) then
  141.     FreeAndNil(FDPhysFBDriverLink);
  142.  
  143.   if Assigned(FQuery) then
  144.     FreeAndNil(FQuery);
  145.  
  146.   if Assigned(Conexao) then
  147.     Conexao.Free;
  148.  
  149.  
  150.   inherited;
  151. end;
  152.  
  153. procedure TModelConexao.FDBeforeConnect(Sender: TObject);
  154. begin
  155.   var TipsConexao := GetTipsConexao;
  156.  
  157. //  if not Assigned(TipsConexao) then
  158. //    raise Exception.Create('Dados da configurações não carregados!');
  159.   try
  160.     FDPhysFBDriverLink.VendorLib := TipsConexao.vendorlib;
  161.     Conexao.Params.Values['DriverID'  ] := TipsConexao.DriverID;
  162.     Conexao.Params.Values['Database'  ] := TipsConexao.Database;
  163.     Conexao.Params.Values['UserName'  ] := TipsConexao.UserName;
  164.     Conexao.Params.Values['Password'  ] := TipsConexao.Password;
  165.     Conexao.Params.Values['Server'    ] := TipsConexao.Server;
  166.     Conexao.Params.Values['Port'      ] := TipsConexao.Port;
  167.     Conexao.Params.Values['Protocol'  ] := 'TCPIP';
  168.     Conexao.Params.Values['Collation' ] := 'WIN_PTBR';
  169.     Conexao.Params.Values['Charset'   ] := 'WIN1252';
  170.     Conexao.Params.Values['Dialect'   ] := '3';
  171.     Conexao.Params.Values['CreateDatabase'] := BoolToStr(not FileExists(TipsConexao.Database), true);
  172.   finally
  173.  
  174.   end;
  175. end;
  176.  
  177. function TModelConexao.GetConexao: TFDConnection;
  178. begin
  179.   Result := FConexao;
  180. end;
  181. function TModelConexao.GetConnectionStatus: Boolean;
  182. begin
  183.   Result := FConexao.Connected;
  184. end;
  185. function TModelConexao.GetDescrition: String;
  186. begin
  187.   Result := FDescrition;
  188. end;
  189. function TModelConexao.GetID: Integer;
  190. begin
  191.   Result := FID;
  192. end;
  193. function TModelConexao.GetPrimaryKeys(Table : String) : TStringList;
  194. var
  195.   SQL: string;
  196.   intQuery : integer;
  197. begin
  198.   intQuery := FQuery.Count;
  199.   Result := TStringList.Create;
  200.   SQL := 'select                                                                        ' +
  201.        '     s.rdb$field_name AS PK,                                                     ' +
  202.        '     i.rdb$relation_name as TABELA                                               ' +
  203.        ' from                                                                            ' +
  204.        '     rdb$indices i                                                               ' +
  205.        ' left join rdb$index_segments s on i.rdb$index_name = s.rdb$index_name           ' +
  206.        ' left join rdb$relation_constraints rc on rc.rdb$index_name = i.rdb$index_name   ' +
  207.        ' where                                                                           ' +
  208.        '     rc.rdb$constraint_type = ''PRIMARY KEY'' AND                                ' +
  209.        '     i.rdb$relation_name = ' + QuotedStr(Table) + '                              ';
  210.     Query(intQuery).SQL.Text := SQL;
  211.     Query(intQuery).Open;
  212.     while not Query(intQuery).Eof do
  213.     begin
  214.       Result.Add(Query(intQuery).FieldByName('PK').AsString);
  215.       Query(intQuery).Next;
  216.     end;
  217. end;
  218. function TModelConexao.GetReceive: Boolean;
  219. begin
  220.   Result := FReceive;
  221. end;
  222. function TModelConexao.GetSend: Boolean;
  223. begin
  224.   Result := FSend;
  225. end;
  226.  
  227. function TModelConexao.GetTableReceive: String;
  228. begin
  229.   Result := FTableReceive;
  230. end;
  231. function TModelConexao.GetTables(Tipo : String): TStringList;
  232. begin
  233.   try
  234.     Result := TStringList.Create;
  235.     if Tipo = 'I' then
  236.     begin
  237.       Result := fnc_buscarOrdemTabelas('TABLE_INSERT_ORDER');
  238.     end
  239.     else if Tipo = 'D' then
  240.       Result := fnc_buscarOrdemTabelas('TABLE_DELETE_ORDER');
  241.   except on E : Exception do
  242.     Self.GravarLog(E.Message);
  243.   end;
  244. end;
  245.  
  246. function TModelConexao.GetTableSend: String;
  247. begin
  248.   Result := FTableSend;
  249. end;
  250.  
  251. function TModelConexao.GetTipsConexao: TTipsConexao;
  252. begin
  253.   Result := FTipsConexao;
  254. end;
  255.  
  256. class procedure TModelConexao.GravarLog(aString: String);
  257. begin
  258.   TThread.Queue(TThread.CurrentThread,
  259.   procedure
  260.   var
  261.     arq: TextFile;
  262.     NomeArq: string;
  263.   begin
  264.     if not DirectoryExists(ExtractFilePath(ParamStr(0)) + 'LOG') then
  265.     CreateDir(ExtractFilePath(ParamStr(0)) + 'LOG');
  266.     NomeArq := ExtractFilePath(ParamStr(0)) + 'LOG\LOG_' + FormatDateTime('dd_mm_yyyy', now) + '.txt';
  267.     AssignFile(arq, NomeArq);
  268.     if not FileExists(NomeArq) then Rewrite(arq) else Append(arq);
  269.     Writeln(arq, FormatDateTime('hh:nn:ss', now) + ' - ' + aString);
  270.     CloseFile(arq);
  271.   end);
  272. end;
  273. function TModelConexao.IdNewQuery: Integer;
  274. begin
  275.   Result := FQuery.Count;
  276. end;
  277. function TModelConexao.fnc_buscarOrdemTabelas(Arquivo : String) : TStringList;
  278. var
  279.   I: Integer;
  280.   Lista: TStringList;
  281.   Ini: TIniFile;
  282.   aSQL: string;
  283.   intQuery: Integer;
  284. begin
  285.   Result := TStringList.Create;
  286.   if FileExists(ExtractFileDir(ParamStr(0)) + '\'+Arquivo+'.ini') then
  287.   begin
  288.     Lista := TStringList.Create;
  289.     Ini := TIniFile.Create(ExtractFileDir(ParamStr(0)) + '\'+Arquivo+'.ini');
  290.     try
  291.       Ini.ReadSection(Self.GetDescrition, Lista);
  292.       for I := 0 to Pred(Lista.Count) do
  293.         Result.Add(Ini.ReadString(Self.GetDescrition, Lista[I], ''));
  294.     finally
  295.       Ini.Free;
  296.     end;
  297.   end
  298.   else
  299.   begin
  300.     intQuery := FQuery.Count;
  301.     aSQL := 'select rdb$relation_name as tabela from rdb$relations where rdb$system_flag = 0';
  302.     Query(intQuery).SQL.Text := aSQL;
  303.     Query(intQuery).Open;
  304.     while not Query(intQuery).eof do
  305.     begin
  306.       Result.Add(Query(intQuery).FieldByName('tabela').AsString);
  307.       Query(intQuery).Next;
  308.     end;
  309.   end;
  310. end;
  311. class function TModelConexao.New(TipsConexao : TTipsConexao) : IModelConexao;
  312. begin
  313.   Result := TModelConexao.Create(TipsConexao);
  314. end;
  315. procedure TModelConexao.SetConexao(const Value: TFDConnection);
  316. begin
  317.   FConexao := Value;
  318. end;
  319. procedure TModelConexao.SetConnectionStatus(const Value: Boolean);
  320. begin
  321. //
  322. end;
  323. procedure TModelConexao.SetDescrition(const Value: String);
  324. begin
  325.   FDescrition := Value;
  326. end;
  327. procedure TModelConexao.SetID(const Value: Integer);
  328. begin
  329.   FID := Value;
  330. end;
  331. procedure TModelConexao.SetReceive(const Value: Boolean);
  332. begin
  333.   FReceive := Value;
  334. end;
  335. procedure TModelConexao.SetSend(const Value: Boolean);
  336. begin
  337.   FSend := Value;
  338. end;
  339. procedure TModelConexao.SetTableReceive(const Value: String);
  340. begin
  341.   FTableReceive := Value;
  342. end;
  343. procedure TModelConexao.SetTableSend(const Value: String);
  344. begin
  345.   FTableSend := Value;
  346. end;
  347. function TModelConexao.StartTransaction: IModelConexao;
  348. begin
  349.   Result := Self;
  350.   FConexao.StartTransaction;
  351. end;
  352. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement