Skip to content

Commit 35bd290

Browse files
committed
* Melhorias de destruição de objetos DAC
+ Novo DAC.MySQL.Zeos para Delphi e Lazarus + Novo DAC.Firebird.Zeos para Delphi e Lazarus - Correção de DriverName em DAC.MySQL.FireDAC.pas
1 parent 8778f09 commit 35bd290

9 files changed

Lines changed: 444 additions & 49 deletions

dac/delphi/DAC.Firebird.FireDAC.pas

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ interface
1010
FireDAC.Comp.Client, FireDAC.Phys.FB;
1111

1212
type
13+
TConnection = TFDConnection;
14+
TQuery = TFDQuery;
15+
1316
TDAC = class
1417
private
1518
FDriver: TFDPhysFBDriverLink;
16-
FConnection: TFDConnection;
17-
FQuery: TFDQuery;
19+
FConnection: TConnection;
20+
FQuery: TQuery;
1821
function GetDefaultLibDir: string;
1922
public
2023
constructor Create(aJSON: TJSONObject);
2124
destructor Destroy; override;
22-
function getConnection: TFDConnection;
23-
function getQuery: TFDQuery;
25+
function getConnection: TConnection;
26+
function getQuery: TQuery;
2427
end;
2528

2629
implementation
@@ -31,7 +34,7 @@ constructor TDAC.Create(aJSON: TJSONObject);
3134
FDriver.DriverID := 'FB';
3235
FDriver.VendorLib := GetDefaultLibDir;
3336

34-
FConnection := TFDConnection.Create(nil);
37+
FConnection := TConnection.Create(nil);
3538
try
3639
with FConnection do
3740
begin
@@ -43,7 +46,8 @@ constructor TDAC.Create(aJSON: TJSONObject);
4346
Params.Add('Port=' + aJSON.GetValue('dbport').Value);
4447
if aJSON.GetValue('banco') <> nil then
4548
Params.Add('Database=' + aJSON.GetValue('banco').Value);
46-
FQuery := TFDQuery.Create(nil);
49+
50+
FQuery := TQuery.Create(nil);
4751
FQuery.Connection := FConnection;
4852
FQuery.ResourceOptions.SilentMode := true;
4953
end;
@@ -54,16 +58,13 @@ constructor TDAC.Create(aJSON: TJSONObject);
5458

5559
destructor TDAC.Destroy;
5660
begin
57-
if FDriver <> nil then
58-
FDriver.Free;
59-
if FQuery <> nil then
60-
FQuery.Free;
61-
if FConnection <> nil then
62-
FConnection.Free;
61+
if Assigned(FDriver) then FreeAndNil(FDriver);
62+
if Assigned(FQuery) then FreeAndNil(FQuery);
63+
if Assigned(FConnection) then FreeAndNil(FConnection);
6364
inherited;
6465
end;
6566

66-
function TDAC.getConnection: TFDConnection;
67+
function TDAC.getConnection: TConnection;
6768
begin
6869
Result := FConnection;
6970
end;
@@ -85,10 +86,10 @@ function TDAC.GetDefaultLibDir: string;
8586
Result := DefaultDir + '\lib\fbembed.dll';
8687
end;
8788

88-
function TDAC.getQuery: TFDQuery;
89+
function TDAC.getQuery: TQuery;
8990
begin
9091
if not Assigned(FQuery) then
91-
FQuery := TFDQuery.Create(nil);
92+
FQuery := TQuery.Create(nil);
9293
Result := FQuery;
9394
end;
9495

dac/delphi/DAC.Firebird.Zeos.pas

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
4+
unit DAC.Firebird.Zeos;
5+
6+
interface
7+
8+
uses
9+
JSON, SysUtils,
10+
ZConnection, ZDataset;
11+
12+
type
13+
TConnection = TZConnection;
14+
TQuery = TZQuery;
15+
16+
TDAC = class
17+
private
18+
FConnection: TConnection;
19+
FQuery: TQuery;
20+
function GetDefaultLibDir: string;
21+
public
22+
constructor Create(aJSON: TJSONObject);
23+
destructor Destroy; override;
24+
function getConnection: TConnection;
25+
function getQuery: TQuery;
26+
end;
27+
28+
implementation
29+
30+
constructor TDAC.Create(aJSON: TJSONObject);
31+
begin
32+
FConnection := nil;
33+
FQuery := nil;
34+
FConnection := TConnection.Create(nil);
35+
try
36+
with FConnection do
37+
begin
38+
LoginPrompt := False;
39+
HostName := aJSON.GetValue('dbserver').Value;
40+
Port := aJSON.GetValue('dbport').Value.ToInteger;
41+
User := aJSON.GetValue('dbuser').Value;
42+
Password := aJSON.GetValue('dbpassword').Value;
43+
Protocol := 'firebird';
44+
LibraryLocation := GetDefaultLibDir;
45+
46+
if aJSON.GetValue('banco') <> nil then
47+
Database := aJSON.GetValue('banco').Value;
48+
end;
49+
FQuery := TQuery.Create(nil);
50+
FQuery.Connection := FConnection;
51+
except
52+
// log
53+
end;
54+
end;
55+
56+
destructor TDAC.Destroy;
57+
begin
58+
if Assigned(FQuery) then FreeAndNil(FQuery);
59+
if Assigned(FConnection) then FreeAndNil(FConnection);
60+
inherited;
61+
end;
62+
63+
function TDAC.getConnection: TConnection;
64+
begin
65+
Result := FConnection;
66+
end;
67+
68+
function TDAC.GetDefaultLibDir: string;
69+
var
70+
DefaultDir: string;
71+
begin
72+
Result := '';
73+
DefaultDir := ExtractFileDir(ParamStr(0));
74+
// Firebird depende de fbembed.dll ou fbclient.dll
75+
if FileExists(DefaultDir + 'fbclient.dll') then
76+
Result := DefaultDir + 'fbclient.dll'
77+
else if FileExists(DefaultDir + 'fbembed.dll') then
78+
Result := DefaultDir + 'fbembed.dll'
79+
else if FileExists(DefaultDir + '\lib\fbclient.dll') then
80+
Result := DefaultDir + '\lib\fbclient.dll'
81+
else if FileExists(DefaultDir + '\lib\fbembed.dll') then
82+
Result := DefaultDir + '\lib\fbembed.dll';
83+
end;
84+
85+
function TDAC.getQuery: TQuery;
86+
begin
87+
if not Assigned(FQuery) then
88+
FQuery := TQuery.Create(nil);
89+
Result := FQuery;
90+
end;
91+
92+
end.

dac/delphi/DAC.MySQL.FireDAC.pas

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ interface
1010
FireDAC.Comp.Client, FireDAC.Phys.MySQL;
1111

1212
type
13+
TConnection = TFDConnection;
14+
TQuery = TFDQuery;
15+
1316
TDAC = class
1417
private
1518
FDriver: TFDPhysMySQLDriverLink;
16-
FConnection: TFDConnection;
17-
FQuery: TFDQuery;
19+
FConnection: TConnection;
20+
FQuery: TQuery;
1821
function GetDefaultLibDir: string;
1922
public
2023
constructor Create(aJSON: TJSONObject);
2124
destructor Destroy; override;
22-
function getConnection: TFDConnection;
23-
function getQuery: TFDQuery;
25+
function getConnection: TConnection;
26+
function getQuery: TQuery;
2427
end;
2528

2629
implementation
@@ -31,20 +34,20 @@ constructor TDAC.Create(aJSON: TJSONObject);
3134
FDriver.DriverID := 'MySQL';
3235
FDriver.VendorLib := GetDefaultLibDir;
3336

34-
FConnection := TFDConnection.Create(nil);
37+
FConnection := TConnection.Create(nil);
3538
try
3639
with FConnection do
3740
begin
3841
LoginPrompt := false;
39-
Params.Add('DriverID=FB');
42+
Params.Add('DriverID=MySQL');
4043
Params.Add('Server=' + aJSON.GetValue('dbserver').Value);
4144
Params.Add('User_Name=' + aJSON.GetValue('dbuser').Value);
4245
Params.Add('Password=' + aJSON.GetValue('dbpassword').Value);
4346
Params.Add('Port=' + aJSON.GetValue('dbport').Value);
4447
if aJSON.GetValue('banco') <> nil then
4548
Params.Add('Database=' + aJSON.GetValue('banco').Value);
4649

47-
FQuery := TFDQuery.Create(nil);
50+
FQuery := TQuery.Create(nil);
4851
FQuery.Connection := FConnection;
4952
FQuery.ResourceOptions.SilentMode := true;
5053
end;
@@ -55,16 +58,13 @@ constructor TDAC.Create(aJSON: TJSONObject);
5558

5659
destructor TDAC.Destroy;
5760
begin
58-
if FDriver <> nil then
59-
FDriver.Free;
60-
if FQuery <> nil then
61-
FQuery.Free;
62-
if FConnection <> nil then
63-
FConnection.Free;
61+
if Assigned(FDriver) then FreeAndNil(FDriver);
62+
if Assigned(FQuery) then FreeAndNil(FQuery);
63+
if Assigned(FConnection) then FreeAndNil(FConnection);
6464
inherited;
6565
end;
6666

67-
function TDAC.getConnection: TFDConnection;
67+
function TDAC.getConnection: TConnection;
6868
begin
6969
Result := FConnection;
7070
end;
@@ -95,10 +95,10 @@ function TDAC.GetDefaultLibDir: string;
9595
' precisam estar na raiz do executável ou na pasta \lib\');
9696
end;
9797

98-
function TDAC.getQuery: TFDQuery;
98+
function TDAC.getQuery: TQuery;
9999
begin
100100
if not Assigned(FQuery) then
101-
FQuery := TFDQuery.Create(nil);
101+
FQuery := TQuery.Create(nil);
102102
Result := FQuery;
103103
end;
104104

dac/delphi/DAC.MySQL.Zeos.pas

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
4+
unit DAC.MySQL.Zeos;
5+
6+
interface
7+
8+
uses
9+
JSON, SysUtils,
10+
ZConnection, ZDataset;
11+
12+
type
13+
TConnection = TZConnection;
14+
TQuery = TZQuery;
15+
16+
{ TDAC }
17+
18+
TDAC = class
19+
private
20+
FConnection: TConnection;
21+
FQuery: TQuery;
22+
function GetDefaultLibDir: string;
23+
public
24+
constructor Create(aJSON: TJSONObject);
25+
destructor Destroy; override;
26+
function getConnection: TConnection;
27+
function getQuery: TQuery;
28+
end;
29+
30+
implementation
31+
32+
constructor TDAC.Create(aJSON: TJSONObject);
33+
begin
34+
FConnection := nil;
35+
FQuery := nil;
36+
FConnection := TConnection.Create(nil);
37+
try
38+
with FConnection do
39+
begin
40+
LoginPrompt := False;
41+
HostName := aJSON.GetValue('dbserver').Value;
42+
Port := aJSON.GetValue('dbport').Value.ToInteger;
43+
User := aJSON.GetValue('dbuser').Value;
44+
Password := aJSON.GetValue('dbpassword').Value;
45+
Protocol := 'mysql';
46+
LibraryLocation := GetDefaultLibDir;
47+
48+
if aJSON.GetValue('banco') <> nil then
49+
Database := aJSON.GetValue('banco').Value;
50+
end;
51+
FQuery := TQuery.Create(nil);
52+
FQuery.Connection := FConnection;
53+
except
54+
// log
55+
end;
56+
end;
57+
58+
destructor TDAC.Destroy;
59+
begin
60+
if Assigned(FQuery) then FreeAndNil(FQuery);
61+
if Assigned(FConnection) then FreeAndNil(FConnection);
62+
inherited;
63+
end;
64+
65+
function TDAC.getConnection: TConnection;
66+
begin
67+
Result := FConnection;
68+
end;
69+
70+
function TDAC.GetDefaultLibDir: string;
71+
var
72+
DefaultDir: string;
73+
begin
74+
Result := '';
75+
DefaultDir := ExtractFileDir(ParamStr(0));
76+
// libmysql.dll, libmariadb or libmysqld.dll
77+
// procurando no diretório do exe primeiro, depois no diretório \lib\
78+
79+
if FileExists(DefaultDir + 'libmysql.dll') then
80+
Result := DefaultDir + 'libmysql.dll'
81+
else if FileExists(DefaultDir + 'libmariadb.dll') then
82+
Result := DefaultDir + 'libmariadb.dll'
83+
else if FileExists(DefaultDir + 'libmysqld.dll') then
84+
Result := DefaultDir + 'libmysqld.dll'
85+
else if FileExists(DefaultDir + '\lib\libmysql.dll') then
86+
Result := DefaultDir + '\lib\libmysql.dll'
87+
else if FileExists(DefaultDir + '\lib\libmariadb.dll') then
88+
Result := DefaultDir + '\lib\libmariadb.dll'
89+
else if FileExists(DefaultDir + '\lib\libmysqld.dll') then
90+
Result := DefaultDir + '\lib\libmysqld.dll'
91+
else
92+
raise Exception.Create('libmysql.dll, libmariadb.dll ou libmysqld.dll' +
93+
' precisam estar na raiz do executável ou na pasta \lib\');
94+
end;
95+
96+
function TDAC.getQuery: TQuery;
97+
begin
98+
if not Assigned(FQuery) then
99+
FQuery := TQuery.Create(nil);
100+
Result := FQuery;
101+
end;
102+
103+
end.

dac/delphi/DAC.Postgres.FireDAC.pas

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ constructor TDAC.Create(aJSON: TJSONObject);
6464

6565
destructor TDAC.Destroy;
6666
begin
67-
if FDriver <> nil then
68-
FDriver.Free;
69-
if FQuery <> nil then
70-
FQuery.Free;
71-
if FConnection <> nil then
72-
FConnection.Free;
67+
if Assigned(FDriver) then FreeAndNil(FDriver);
68+
if Assigned(FQuery) then FreeAndNil(FQuery);
69+
if Assigned(FConnection) then FreeAndNil(FConnection);
7370
inherited;
7471
end;
7572

0 commit comments

Comments
 (0)