Skip to content

Commit 17b0d90

Browse files
committed
+ Adição de informações de cabeçalho das units
- Correção de Schema para DAC.Postgres.Zeos * Remoção de alguns Hints em FMXFormat + Início de unit de consumo de APIs REST
1 parent e1f4442 commit 17b0d90

9 files changed

Lines changed: 261 additions & 36 deletions

config/Config.SQLite.FireDAC.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
14
unit Config.SQLite.FireDAC;
25

36
interface

config/Config.SQLite.Zeos.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
14
unit Config.SQLite.Zeos;
25

36
interface

dac/DAC.Firebird.FireDAC.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
14
unit DAC.Firebird.FireDAC;
25

36
interface

dac/DAC.Postgres.FireDAC.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
14
unit DAC.Postgres.FireDAC;
25

36
interface

dac/DAC.Postgres.Zeos.pas

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Maiores Informações
2+
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3+
14
unit DAC.Postgres.Zeos;
25

36
interface
@@ -14,6 +17,7 @@ TDAC = class
1417
private
1518
FConnection: TZConnection;
1619
FQuery: TZQuery;
20+
FSchema: string;
1721
function GetDefaultDir(aFileName: string): string;
1822
public
1923
constructor Create(aJSON: TJSONObject);
@@ -31,6 +35,9 @@ implementation
3135

3236
constructor TDAC.Create(aJSON: TJSONObject);
3337
begin
38+
FConnection := nil;
39+
FQuery := nil;
40+
FSchema := '';
3441
FConnection := TZConnection.Create(nil);
3542
try
3643
with FConnection do
@@ -50,21 +57,19 @@ constructor TDAC.Create(aJSON: TJSONObject);
5057
FQuery.Connection := FConnection;
5158

5259
if aJSON.GetValue('schema') <> nil then
53-
begin
54-
FQuery.SQL.Add('SET search_path = ' + aJSON.GetValue('schema').Value);
55-
FQuery.ExecSQL;
56-
end;
60+
FSchema := aJSON.GetValue('schema').Value;
5761
except
5862
// log
5963
end;
6064
end;
6165

6266
destructor TDAC.Destroy;
6367
begin
64-
if FQuery <> nil then
65-
FQuery.Free;
66-
if FConnection <> nil then
67-
FConnection.Free;
68+
FQuery.Connection := nil;
69+
if Assigned(FQuery) then
70+
FreeAndNil(FQuery);
71+
if Assigned(FConnection) then
72+
FreeAndNil(FConnection);
6873
inherited;
6974
end;
7075

@@ -116,6 +121,8 @@ function TDAC.GetDefaultDir(aFileName: string): string;
116121

117122
function TDAC.getQuery: TQuery;
118123
begin
124+
if not FSchema.IsEmpty then
125+
FQuery.SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
119126
Result := FQuery;
120127
end;
121128

dao/RESTDAO.pas

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
unit RESTDAO;
2+
3+
interface
4+
5+
uses
6+
Classes, SysUtils, Generics.Collections,
7+
REST.Types, REST.Client,
8+
System.JSON;
9+
10+
type
11+
TRESTDAO = class
12+
private
13+
FRESTClient: TRESTClient;
14+
FRESTRequest: TRESTRequest;
15+
16+
procedure SetBaseURL(const Value: string);
17+
function GetBaseURL: string;
18+
procedure SetResource(const Value: string);
19+
function GetResource: string;
20+
procedure DefineParams(aMethod: TRESTRequestMethod;
21+
aHeaders: TJSONObject = nil; aBody: TJSONObject = nil); overload;
22+
procedure DefineFileParams(aMethod: TRESTRequestMethod;
23+
aHeaders: TJSONObject = nil; aBody: TStream = nil);
24+
public
25+
constructor Create; overload;
26+
constructor Create(aBaseURL: string); overload;
27+
destructor Destroy; override;
28+
function Get(aParams: TJSONObject = nil): string;
29+
function GetFile(aParams: TJSONObject = nil): TStream;
30+
function Post(aHeaderParams: TJSONObject = nil; aBody: TJSONObject = nil)
31+
: string; overload;
32+
function Post(aHeaderParams: TJSONObject = nil; aBody: TStream = nil)
33+
: string; overload;
34+
function PostFile(aHeaderParams: TJSONObject = nil;
35+
aBody: TJSONObject = nil): TStream; overload;
36+
function PostFile(aHeaderParams: TJSONObject = nil; aBody: TStream = nil)
37+
: TStream; overload;
38+
function Delete(aParams: TJSONObject = nil): string;
39+
40+
property BaseURL: string read GetBaseURL write SetBaseURL;
41+
property Endpoint: string read GetResource write SetResource;
42+
end;
43+
44+
THelper = class helper for TCustomRESTResponse
45+
public
46+
procedure SaveToStream(aStream: TStream);
47+
end;
48+
49+
implementation
50+
51+
{ TRESTDAO }
52+
53+
constructor TRESTDAO.Create;
54+
begin
55+
FRESTClient := nil;
56+
FRESTRequest := nil;
57+
FRESTClient := TRESTClient.Create('');
58+
FRESTRequest := TRESTRequest.Create(nil);
59+
60+
FRESTRequest.Client := FRESTClient;
61+
end;
62+
63+
constructor TRESTDAO.Create(aBaseURL: string);
64+
begin
65+
Create;
66+
BaseURL := aBaseURL;
67+
end;
68+
69+
procedure TRESTDAO.DefineFileParams(aMethod: TRESTRequestMethod;
70+
aHeaders: TJSONObject; aBody: TStream);
71+
var
72+
I: integer;
73+
begin
74+
FRESTRequest.Method := aMethod;
75+
FRESTRequest.Params.Clear;
76+
if aHeaders <> nil then
77+
for I := 0 to pred(aHeaders.Count) do
78+
FRESTRequest.AddParameter(aHeaders.Pairs[I].JsonString.Value,
79+
aHeaders.Pairs[I].JsonValue.Value, pkQUERY);
80+
81+
FRESTRequest.Body.ClearBody;
82+
if aBody <> nil then
83+
FRESTRequest.AddBody(aBody);
84+
FRESTRequest.Response := nil;
85+
end;
86+
87+
procedure TRESTDAO.DefineParams(aMethod: TRESTRequestMethod;
88+
aHeaders, aBody: TJSONObject);
89+
var
90+
I: integer;
91+
begin
92+
FRESTRequest.Method := aMethod;
93+
FRESTRequest.Params.Clear;
94+
if aHeaders <> nil then
95+
for I := 0 to pred(aHeaders.Count) do
96+
FRESTRequest.AddParameter(aHeaders.Pairs[I].JsonString.Value,
97+
aHeaders.Pairs[I].JsonValue.Value, pkQUERY);
98+
99+
FRESTRequest.Body.ClearBody;
100+
if aBody <> nil then
101+
FRESTRequest.AddBody(aBody);
102+
FRESTRequest.Response := nil;
103+
end;
104+
105+
function TRESTDAO.Delete(aParams: TJSONObject): string;
106+
begin
107+
Result := '';
108+
DefineParams(rmDELETE, aParams);
109+
FRESTRequest.Execute;
110+
Result := FRESTRequest.Response.Content;
111+
end;
112+
113+
destructor TRESTDAO.Destroy;
114+
begin
115+
if assigned(FRESTRequest) then
116+
FreeAndNil(FRESTRequest);
117+
118+
if assigned(FRESTClient) then
119+
FreeAndNil(FRESTClient);
120+
121+
inherited;
122+
end;
123+
124+
function TRESTDAO.Get(aParams: TJSONObject): string;
125+
begin
126+
Result := '';
127+
DefineParams(rmGET, aParams);
128+
FRESTRequest.Execute;
129+
Result := FRESTRequest.Response.Content;
130+
end;
131+
132+
function TRESTDAO.GetFile(aParams: TJSONObject): TStream;
133+
begin
134+
Result := nil;
135+
DefineFileParams(rmGET, aParams);
136+
FRESTRequest.Execute;
137+
FRESTRequest.Response.SaveToStream(Result);
138+
end;
139+
140+
function TRESTDAO.PostFile(aHeaderParams, aBody: TJSONObject): TStream;
141+
begin
142+
Result := nil;
143+
DefineParams(rmPOST, aHeaderParams, aBody);
144+
FRESTRequest.Execute;
145+
FRESTRequest.Response.SaveToStream(Result);
146+
end;
147+
148+
function TRESTDAO.Post(aHeaderParams, aBody: TJSONObject): string;
149+
begin
150+
Result := '';
151+
DefineParams(rmPOST, aHeaderParams, aBody);
152+
FRESTRequest.Execute;
153+
Result := FRESTRequest.Response.Content;
154+
end;
155+
156+
function TRESTDAO.Post(aHeaderParams: TJSONObject; aBody: TStream): string;
157+
begin
158+
Result := '';
159+
DefineFileParams(rmPOST, aHeaderParams, aBody);
160+
FRESTRequest.Execute;
161+
Result := FRESTRequest.Response.Content;
162+
end;
163+
164+
function TRESTDAO.PostFile(aHeaderParams: TJSONObject; aBody: TStream): TStream;
165+
begin
166+
Result := nil;
167+
DefineFileParams(rmPOST, aHeaderParams, aBody);
168+
FRESTRequest.Execute;
169+
FRESTRequest.Response.SaveToStream(Result);
170+
end;
171+
172+
function TRESTDAO.GetBaseURL: string;
173+
begin
174+
Result := FRESTClient.BaseURL;
175+
end;
176+
177+
function TRESTDAO.GetResource: string;
178+
begin
179+
Result := FRESTRequest.Resource;
180+
end;
181+
182+
procedure TRESTDAO.SetBaseURL(const Value: string);
183+
begin
184+
FRESTClient.BaseURL := Value;
185+
end;
186+
187+
procedure TRESTDAO.SetResource(const Value: string);
188+
begin
189+
FRESTRequest.Resource := Value;
190+
end;
191+
192+
{ THelper }
193+
194+
procedure THelper.SaveToStream(aStream: TStream);
195+
begin
196+
if aStream <> nil then
197+
begin
198+
aStream.Write(Self.RawBytes, length(Self.RawBytes));
199+
aStream.Position := 0;
200+
end;
201+
end;
202+
203+
end.

0 commit comments

Comments
 (0)