Skip to content

Commit cef663c

Browse files
committed
+ Criação de units DAC para o componente SQLDB
1 parent eec82ca commit cef663c

3 files changed

Lines changed: 442 additions & 0 deletions

File tree

dac/lazarus/dac.firebird.sqldb.pas

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
4+
unit DAC.Firebird.SQLDB;
5+
6+
{$mode ObjFPC}{$H+}
7+
8+
interface
9+
10+
uses
11+
{$IFDEF Windows}windows,{$ENDIF}
12+
fpJSON, SysUtils, Classes,
13+
IBConnection, SQLDB;
14+
15+
type
16+
TConnection = TIBConnection;
17+
TQuery = TSQLQuery;
18+
19+
{ TDAC }
20+
21+
TDAC = class
22+
private
23+
FConnection: TConnection;
24+
FQuery: TQuery;
25+
FTransaction: TSQLTransaction;
26+
function GetDefaultLibDir: string;
27+
procedure SetEnvironmentPath;
28+
public
29+
constructor Create(aJSON: TJSONObject);
30+
destructor Destroy; override;
31+
function getConnection: TConnection;
32+
function getQuery: TQuery;
33+
end;
34+
35+
implementation
36+
37+
constructor TDAC.Create(aJSON: TJSONObject);
38+
begin
39+
FConnection := nil;
40+
FQuery := nil;
41+
FConnection := TConnection.Create(nil);
42+
FTransaction := TSQLTransaction.Create(nil);
43+
try
44+
SetEnvironmentPath;
45+
with FConnection do
46+
begin
47+
Transaction := FTransaction;
48+
LoginPrompt := False;
49+
CharSet := 'utf-8';
50+
HostName := aJSON.Get('dbserver', '');
51+
Port := aJSON.Get('dbport', 0);
52+
DatabaseName := aJSON.Get('banco', '');
53+
UserName := aJSON.Get('dbuser', '');
54+
Password := aJSON.Get('dbpassword', '');
55+
end;
56+
FQuery := TQuery.Create(nil);
57+
FQuery.DataBase := FConnection;
58+
FConnection.Open;
59+
except
60+
// log
61+
end;
62+
end;
63+
64+
destructor TDAC.Destroy;
65+
begin
66+
if Assigned(FQuery) then
67+
begin
68+
FQuery.DataBase := nil;
69+
FreeAndNil(FQuery);
70+
end;
71+
72+
if Assigned(FConnection) then FreeAndNil(FConnection);
73+
if Assigned(FTransaction) then FreeAndNil(FTransaction);
74+
inherited;
75+
end;
76+
77+
function TDAC.getConnection: TConnection;
78+
begin
79+
Result := FConnection;
80+
end;
81+
82+
function TDAC.GetDefaultLibDir: string;
83+
var
84+
DefaultDir: string;
85+
begin
86+
Result := '';
87+
DefaultDir := ExtractFileDir(ParamStr(0));
88+
// Firebird depende de fbembed.dll ou fbclient.dll
89+
if FileExists(DefaultDir + '\lib\fbclient.dll') then
90+
Result := DefaultDir + '\lib\fbclient.dll'
91+
else if FileExists(DefaultDir + '\lib\fbembed.dll') then
92+
Result := DefaultDir + '\lib\fbembed.dll'
93+
else if FileExists(DefaultDir + 'fbclient.dll') then
94+
Result := DefaultDir + 'fbclient.dll'
95+
else if FileExists(DefaultDir + 'fbembed.dll') then
96+
Result := DefaultDir + 'fbembed.dll'
97+
else
98+
raise Exception.Create('fbclient.dll ou fbembed.dll' +
99+
' precisa estar na raiz do executável ou na pasta \lib\');
100+
end;
101+
102+
procedure TDAC.SetEnvironmentPath;
103+
var
104+
envpath: TStringList;
105+
I: integer;
106+
begin
107+
envpath := TStringList.Create;
108+
try
109+
// get environment path
110+
envpath.AddDelimitedText(GetEnvironmentVariable('path'), ';', True);
111+
envpath.Sorted := True;
112+
113+
// detect if the dblib is defined on the path
114+
if not envpath.Find(GetDefaultLibDir, I) then
115+
begin
116+
envpath.Add(ExtractFileDir(GetDefaultLibDir));
117+
envpath.Delimiter := ';';
118+
119+
// save new path
120+
{$IF Defined(Windows)}
121+
SetEnvironmentVariable(Pchar('Path'), PChar(envpath.DelimitedText));
122+
{$IFEND}
123+
end;
124+
finally
125+
envpath.Free;
126+
end;
127+
end;
128+
129+
function TDAC.getQuery: TQuery;
130+
begin
131+
if not Assigned(FQuery) then
132+
FQuery := TFDQuery.Create(nil);
133+
Result := FQuery;
134+
end;
135+
136+
end.

dac/lazarus/dac.mysql.sqldb.pas

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
4+
unit DAC.MySQL.SQLDB;
5+
6+
{$mode ObjFPC}{$H+}
7+
8+
interface
9+
10+
uses
11+
{$IFDEF Windows}windows,{$ENDIF}
12+
fpJSON, SysUtils, Classes,
13+
mysql80conn, SQLDB;
14+
15+
type
16+
TConnection = TSQLConnector;
17+
TQuery = TSQLQuery;
18+
19+
{ TDAC }
20+
21+
TDAC = class
22+
private
23+
FConnection: TConnection;
24+
FQuery: TQuery;
25+
FTransaction: TSQLTransaction;
26+
function GetDefaultLibDir: string;
27+
procedure SetEnvironmentPath;
28+
public
29+
constructor Create(aJSON: TJSONObject);
30+
destructor Destroy; override;
31+
function getConnection: TConnection;
32+
function getQuery: TQuery;
33+
end;
34+
35+
implementation
36+
37+
constructor TDAC.Create(aJSON: TJSONObject);
38+
begin
39+
FConnection := nil;
40+
FQuery := nil;
41+
FConnection := TConnection.Create(nil);
42+
FTransaction := TSQLTransaction.Create(nil);
43+
try
44+
SetEnvironmentPath;
45+
with FConnection do
46+
begin
47+
ConnectorType := 'MySQL 8.0';
48+
Transaction := FTransaction;
49+
LoginPrompt := False;
50+
CharSet := 'utf-8';
51+
52+
Params.Add('port=%d', [aJSON.Get('dbport', 3306)]);
53+
HostName := aJSON.Get('dbserver', '');
54+
DatabaseName := aJSON.Get('banco', '');
55+
UserName := aJSON.Get('dbuser', '');
56+
Password := aJSON.Get('dbpassword', '');
57+
end;
58+
FQuery := TQuery.Create(nil);
59+
FQuery.DataBase := FConnection;
60+
FConnection.Open;
61+
except
62+
// log
63+
end;
64+
end;
65+
66+
destructor TDAC.Destroy;
67+
begin
68+
if Assigned(FQuery) then
69+
begin
70+
FQuery.DataBase := nil;
71+
FreeAndNil(FQuery);
72+
end;
73+
74+
if Assigned(FConnection) then FreeAndNil(FConnection);
75+
if Assigned(FTransaction) then FreeAndNil(FTransaction);
76+
inherited;
77+
end;
78+
79+
function TDAC.getConnection: TConnection;
80+
begin
81+
Result := FConnection;
82+
end;
83+
84+
function TDAC.GetDefaultLibDir: string;
85+
var
86+
DefaultDir: string;
87+
begin
88+
Result := '';
89+
DefaultDir := ExtractFileDir(ParamStr(0));
90+
// libmysql.dll, libmariadb or libmysqld.dll
91+
92+
if FileExists(DefaultDir + '\lib\libmysql.dll') then
93+
Result := DefaultDir + '\lib\libmysql.dll'
94+
else if FileExists(DefaultDir + '\lib\libmariadb.dll') then
95+
Result := DefaultDir + '\lib\libmariadb.dll'
96+
else if FileExists(DefaultDir + '\lib\libmysqld.dll') then
97+
Result := DefaultDir + '\lib\libmysqld.dll'
98+
else if FileExists(DefaultDir + 'libmysql.dll') then
99+
Result := DefaultDir + 'libmysql.dll'
100+
else if FileExists(DefaultDir + 'libmariadb.dll') then
101+
Result := DefaultDir + 'libmariadb.dll'
102+
else if FileExists(DefaultDir + 'libmysqld.dll') then
103+
Result := DefaultDir + 'libmysqld.dll'
104+
else
105+
raise Exception.Create('libmysql.dll, libmariadb.dll ou libmysqld.dll' +
106+
' precisam estar na raiz do executável ou na pasta \lib\');
107+
end;
108+
109+
procedure TDAC.SetEnvironmentPath;
110+
var
111+
envpath: TStringList;
112+
I: integer;
113+
begin
114+
envpath := TStringList.Create;
115+
try
116+
// get environment path
117+
envpath.AddDelimitedText(GetEnvironmentVariable('path'), ';', True);
118+
envpath.Sorted := True;
119+
120+
// detect if the dblib is defined on the path
121+
if not envpath.Find(GetDefaultLibDir, I) then
122+
begin
123+
envpath.Add(ExtractFileDir(GetDefaultLibDir));
124+
envpath.Delimiter := ';';
125+
126+
// save new path
127+
{$IF Defined(Windows)}
128+
SetEnvironmentVariable(Pchar('Path'), PChar(envpath.DelimitedText));
129+
{$IFEND}
130+
end;
131+
finally
132+
envpath.Free;
133+
end;
134+
end;
135+
136+
function TDAC.getQuery: TQuery;
137+
begin
138+
if not Assigned(FQuery) then
139+
FQuery := TFDQuery.Create(nil);
140+
Result := FQuery;
141+
end;
142+
143+
end.

0 commit comments

Comments
 (0)