Skip to content

Commit a7ba0b8

Browse files
committed
[CONFIG]
- Atualização para Zeos8 [DAC] - Atualização para Zeos8 - Ajuste para sqldb [DAO] - Atualização de CRUDBase para Zeos8
1 parent f95c38e commit a7ba0b8

4 files changed

Lines changed: 106 additions & 70 deletions

File tree

config/lazarus/config.sqlite.zeos.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ procedure TSQLiteConfig.ClearDatabase;
5252
constructor TSQLiteConfig.Create(aFileName: string);
5353
begin
5454
FConn := TZConnection.Create(nil);
55-
FConn.Protocol := 'sqlite-3';
55+
FConn.Protocol := 'sqlite';
5656
FConn.Database := ExtractFilePath(ParamStr(0)) + aFileName;
5757
FConn.Properties.Add('LockingMode=normal');
5858
FConn.LibraryLocation := GetDefaultDir('sqlite3.dll');

dac/lazarus/dac.postgres.sqldb.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ destructor TDAC.Destroy;
116116
begin
117117
if Assigned(FQuery) then
118118
begin
119+
FQuery.Close;
119120
FQuery.DataBase := nil;
120121
FreeAndNil(FQuery);
121122
end;

dac/lazarus/dac.postgres.zeos.pas

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ interface
1717

1818
TDAC = class
1919
private
20-
FConnection: TZConnection;
21-
FQuery: TZQuery;
20+
FConnection: TConnection;
21+
FQuery: TQuery;
2222
FSchema: string;
2323
function GetDefaultLibDir: string;
2424
public
@@ -44,20 +44,17 @@ constructor TDAC.Create(aJSON: TJSONObject);
4444
begin
4545
LoginPrompt := False;
4646
HostName := aJSON.Get('dbserver', '');
47-
Port := aJSON.Get('dbport', 0);
47+
Port := aJSON.Get('dbport', 5432);
4848
User := aJSON.Get('dbuser', '');
4949
Password := aJSON.Get('dbpassword', '');
5050
Protocol := 'postgresql';
51+
Database := aJSON.Get('banco', '');
5152
LibraryLocation := GetDefaultLibDir;
52-
53-
if aJSON.Get('banco', '') <> '' then
54-
Database := aJSON.Get('banco', '');
5553
end;
5654
FQuery := TQuery.Create(nil);
5755
FQuery.Connection := FConnection;
58-
59-
if aJSON.Get('schema', '') <> '' then
60-
FSchema := aJSON.Get('schema', '');
56+
FSchema := aJSON.Get('schema', '');
57+
FConnection.Connect;
6158
except
6259
// log
6360
end;
@@ -66,10 +63,8 @@ constructor TDAC.Create(aJSON: TJSONObject);
6663
destructor TDAC.Destroy;
6764
begin
6865
FQuery.Connection := nil;
69-
if Assigned(FQuery) then
70-
FreeAndNil(FQuery);
71-
if Assigned(FConnection) then
72-
FreeAndNil(FConnection);
66+
if Assigned(FQuery) then FreeAndNil(FQuery);
67+
if Assigned(FConnection) then FreeAndNil(FConnection);
7368
inherited;
7469
end;
7570

@@ -105,25 +100,24 @@ function TDAC.getConnectionStatus: string;
105100

106101
function TDAC.GetDefaultLibDir: string;
107102
var
108-
DefaultDir, temp: string;
103+
DefaultDir: string;
109104
begin
110105
Result := '';
111106
DefaultDir := ExtractFileDir(ParamStr(0));
112107

113-
temp := DefaultDir + '\lib\libpq.dll';
114-
if FileExists(temp) then
115-
Result := temp
108+
if FileExists(DefaultDir + '\lib\libpq.dll') then
109+
Result := DefaultDir + '\lib\libpq.dll'
110+
else if FileExists(DefaultDir + '\lib\libpq.dll') then
111+
Result := DefaultDir + '\lib\libpq.dll'
116112
else
117-
begin
118-
temp := DefaultDir + 'libpq.dll';
119-
if FileExists(temp) then
120-
Result := temp;
121-
end;
113+
raise Exception.Create('libpq.dll' +
114+
' precisa estar na raiz do executável ou na pasta \lib\');
122115
end;
123116

124117
function TDAC.getQuery: TQuery;
125118
begin
126119
Result := FQuery;
120+
FQuery.Properties.Add('emulate_prepares=true');
127121
end;
128122

129123
end.

dao/lazarus/CRUDBase.Zeos.pas

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
interface
44

55
uses
6-
fpJSON, SysUtils, Classes,
7-
DB, ZDataSet;
6+
fpJSON, SysUtils, Classes, DB,
7+
ZDataSet;
88

99
type
10+
TQuery = TZQuery;
11+
12+
{ TCRUDBase }
13+
1014
TCRUDBase = class
1115
private
1216
FPK: string;
@@ -17,19 +21,21 @@ TCRUDBase = class
1721
procedure SetPK(const Value: string);
1822
procedure SetTableName(const Value: string);
1923
procedure SetSchema(const Value: string);
24+
function Create(aQuery: string): string;
25+
function Read(aQuery: string): string;
26+
function Update(aQuery: string): string;
27+
function Delete(aQuery: string): string;
2028
public
21-
FDataSet: TZQuery;
29+
FDataSet: TQuery;
30+
constructor Create(DataSet: TQuery; aTableName: string = ''; aPK: string = '');
31+
2232
function Atualizar(aJSON: TJSONObject; out StatusCode: integer;
2333
ValidaConcorrente: boolean): string; overload;
2434
function Atualizar(aQuery: string; out StatusCode: integer): string; overload;
25-
constructor Create(DataSet: TZQuery; aTableName: string = ''; aPK: string = '');
26-
function Excluir(aJSON: TJSONObject; out StatusCode: integer): string;
27-
overload;
35+
function Excluir(aJSON: TJSONObject; out StatusCode: integer): string; overload;
2836
function Excluir(aQuery: string; out StatusCode: integer): string; overload;
29-
function getDados(aJSON: TJSONObject; out StatusCode: integer): string;
30-
overload;
31-
function getDados(aQuery: string; out StatusCode: integer): string;
32-
overload;
37+
function getDados(aJSON: TJSONObject; out StatusCode: integer): string; overload;
38+
function getDados(aQuery: string; out StatusCode: integer): string; overload;
3339
function getDados(aQuery: string; aFilterParams: TJSONObject;
3440
out StatusCode: integer): string; overload;
3541
function getDados(out StatusCode: integer): string; overload;
@@ -68,11 +74,11 @@ function TCRUDBase.Atualizar(aJSON: TJSONObject): string;
6874
Close;
6975
SQL.Clear;
7076
if not Schema.IsEmpty then
71-
SQL.Add('SET search_path = ' + FSchema + ';');
77+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
7278
SQL.Add('SELECT *');
73-
SQL.Add(' FROM "' + TableName + '"');
74-
SQL.Add(' WHERE "' + PK + '" = :pkv');
75-
ParamByName('pkv').Value := StrToIntDef(pkv, 0);
79+
SQL.Add(' FROM "%s"', [TableName]);
80+
SQL.Add(' WHERE "%s" = :pkv', [PK]);
81+
ParamByName('pkv').Value := pkv;
7682
Open;
7783
if not IsEmpty then
7884
Edit
@@ -92,7 +98,7 @@ function TCRUDBase.Atualizar(aJSON: TJSONObject): string;
9298
if pkv = '' then
9399
begin
94100
SQL.Clear;
95-
SQL.Text := 'SELECT MAX("' + PK + '") FROM "' + TableName + '"';
101+
SQL.Text := Format('SELECT MAX("%s") FROM "%s"', [PK, TableName]);
96102
Open;
97103
retorno.Add(PK, Fields.Fields[0].AsString);
98104
Close;
@@ -132,25 +138,25 @@ function TCRUDBase.Atualizar(aJSON: TJSONObject; out StatusCode: integer;
132138
begin
133139
SQL.Clear;
134140
if not Schema.IsEmpty then
135-
SQL.Add('SET search_path = ' + FSchema + ';');
136-
SQL.Add('SELECT * FROM "' + TableName + '" WHERE "' + PK + '" is null');
141+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
142+
SQL.Add('SELECT * FROM "%s" WHERE "%s" is null', [TableName, PK]);
137143
Open;
138144
for I := 0 to pred(aJSON.Count) do
139145
begin
140146
if not (FindField(aJSON.Names[I]) = nil) then
141147
JSONFields.Add(FindField(aJSON.Names[I]).FieldName, aJSON.Items[I].AsString);
142148
end;
143149
Close;
150+
144151
SQL.Clear;
145152
if not Schema.IsEmpty then
146-
SQL.Add('SET search_path = ' + FSchema + ';');
153+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
147154
SQL.Add('SELECT *');
148-
SQL.Add(' FROM "' + TableName + '"');
155+
SQL.Add(' FROM "%s"', [TableName]);
149156
SQL.Add(' WHERE 1=1');
150157
for I := 0 to pred(JSONFields.Count) do
151-
SQL.Add(' AND CAST("' + JSONFields.Names[I] +
152-
'" AS varchar) ILIKE CAST(''' + JSONFields.Items[I].AsString +
153-
''' AS varchar)');
158+
SQL.Add(' AND CAST("%s" AS varchar) ILIKE CAST(''%s'' AS varchar)',
159+
[JSONFields.Names[I], JSONFields.Items[I].AsString]);
154160
Open;
155161
if not IsEmpty then
156162
begin
@@ -179,7 +185,7 @@ function TCRUDBase.Atualizar(aQuery: string; out StatusCode: integer): string;
179185
Close;
180186
SQL.Clear;
181187
if not Schema.IsEmpty then
182-
SQL.Add('SET search_path = ' + FSchema + ';');
188+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
183189
SQL.Add(aQuery);
184190
ExecSQL;
185191
StatusCode := 200;
@@ -195,7 +201,7 @@ function TCRUDBase.Atualizar(aQuery: string; out StatusCode: integer): string;
195201
end;
196202
end;
197203

198-
constructor TCRUDBase.Create(DataSet: TZQuery; aTableName, aPK: string);
204+
constructor TCRUDBase.Create(DataSet: TQuery; aTableName: string; aPK: string);
199205
begin
200206
FSchema := '';
201207
FDataSet := DataSet;
@@ -229,7 +235,7 @@ function TCRUDBase.Excluir(aQuery: string; out StatusCode: integer): string;
229235
Close;
230236
SQL.Clear;
231237
if not Schema.IsEmpty then
232-
SQL.Add('SET search_path = ' + FSchema + ';');
238+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
233239
SQL.Add(aQuery);
234240
ExecSQL;
235241
StatusCode := 200;
@@ -257,13 +263,14 @@ function TCRUDBase.Excluir(aJSON: TJSONObject; out StatusCode: integer): string;
257263
Close;
258264
SQL.Clear;
259265
if not Schema.IsEmpty then
260-
SQL.Add('SET search_path = ' + FSchema + ';');
266+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
261267
SQL.Add('DELETE ');
262-
SQL.Add(' FROM "' + TableName + '"');
268+
SQL.Add(' FROM "%s"', [TableName]);
263269
SQL.Add(' WHERE 1=1');
264270
for I := 0 to pred(aJSON.Count) do
265-
SQL.Add(' AND CAST("' + aJSON.Names[I] + '" AS varchar) = CAST(''' +
266-
aJSON.Items[I].AsString + ''' AS varchar)');
271+
SQL.Add(' AND CAST("%s" AS varchar) ILIKE CAST(''%s'' AS varchar)',
272+
[aJSON.Names[I], aJSON.Items[I].AsString]);
273+
267274
ExecSQL;
268275
StatusCode := 200;
269276
Result := Fields.Fields[0].Value;
@@ -288,7 +295,7 @@ function TCRUDBase.getDados(aQuery: string; out StatusCode: integer): string;
288295
Close;
289296
SQL.Clear;
290297
if not Schema.IsEmpty then
291-
SQL.Add('set search_path = ' + QuotedStr(Schema) + ';');
298+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
292299
SQL.Add(aQuery);
293300
Open;
294301

@@ -297,8 +304,12 @@ function TCRUDBase.getDados(aQuery: string; out StatusCode: integer): string;
297304

298305
Close;
299306
end;
300-
finally
301-
307+
except
308+
on e: Exception do
309+
begin
310+
StatusCode := 500;
311+
Result := Format('{"erro":"%s"}', [e.Message]);
312+
end;
302313
end;
303314
end;
304315

@@ -315,8 +326,8 @@ function TCRUDBase.getDados(aJSON: TJSONObject; out StatusCode: integer): string
315326
begin
316327
SQL.Clear;
317328
if not Schema.IsEmpty then
318-
SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
319-
SQL.Add('SELECT * FROM "' + TableName + '" LIMIT 1');
329+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
330+
SQL.Add('SELECT * FROM "%s" LIMIT 1', [TableName]);
320331
Open;
321332
for I := 0 to pred(aJSON.Count) do
322333
if not (FindField(aJSON.Names[I]) = nil) then
@@ -326,14 +337,13 @@ function TCRUDBase.getDados(aJSON: TJSONObject; out StatusCode: integer): string
326337
Close;
327338
SQL.Clear;
328339
if not Schema.IsEmpty then
329-
SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
340+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
330341
SQL.Add('SELECT *');
331-
SQL.Add(' FROM "' + TableName + '"');
342+
SQL.Add(' FROM "%s"', [TableName]);
332343
SQL.Add(' WHERE 1=1');
333344
for I := 0 to pred(JSONFields.Count) do
334-
SQL.Add(' AND CAST(' + JSONFields.Names[I] +
335-
' AS varchar) ILIKE CAST(' + QuotedStr('%' +
336-
JSONFields.Items[I].AsString + '%') + ' AS varchar)');
345+
SQL.Add(' AND CAST(%s AS varchar) ILIKE CAST(%s AS varchar)',
346+
[JSONFields.Names[I], QuotedStr('%' + JSONFields.Items[I].AsString + '%')]);
337347
Open;
338348

339349
StatusCode := 200;
@@ -355,8 +365,8 @@ function TCRUDBase.getDados(out StatusCode: integer): string;
355365
Close;
356366
SQL.Clear;
357367
if not Schema.IsEmpty then
358-
SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
359-
SQL.Add('SELECT * FROM "' + FTableName + '"');
368+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
369+
SQL.Add('SELECT * FROM "%s"', [FTableName]);
360370
Open;
361371

362372
StatusCode := 200;
@@ -374,7 +384,7 @@ function TCRUDBase.getValue(aQuery: string; out StatusCode: integer): string;
374384
Close;
375385
SQL.Clear;
376386
if not Schema.IsEmpty then
377-
SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
387+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
378388
SQL.Add(aQuery);
379389
Open;
380390

@@ -407,8 +417,8 @@ function TCRUDBase.getDados(aQuery: string; aFilterParams: TJSONObject;
407417
Close;
408418
SQL.Clear;
409419
if not Schema.IsEmpty then
410-
SQL.Add('SET search_path = ' + QuotedStr(FSchema) + ';');
411-
SQL.Add('SELECT * FROM (' + aQuery + ') as r LIMIT 1');
420+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
421+
SQL.Add('SELECT * FROM (%s) as r LIMIT 1', [aQuery]);
412422
Open;
413423

414424
for I := 0 to pred(aFilterParams.Count) do
@@ -432,9 +442,8 @@ function TCRUDBase.getDados(aQuery: string; aFilterParams: TJSONObject;
432442
J := J + 1;
433443
for I := 0 to pred(JSONFields.Count) do
434444
begin
435-
SQLInput.Insert(J, ' AND CAST(' + JSONFields.Names[I] +
436-
' AS varchar) ILIKE CAST(' + QuotedStr('%' +
437-
JSONFields.Items[I].AsString + '%') + ' AS varchar)');
445+
SQLInput.Insert(J, Format(' AND CAST(%s AS varchar) ILIKE CAST(%s AS varchar)',
446+
[JSONFields.Names[I], QuotedStr('%' + JSONFields.Items[I].AsString + '%')]));
438447
J := J + 1;
439448
end;
440449
SQL.Text := SQLInput.Text;
@@ -460,6 +469,38 @@ procedure TCRUDBase.SetSchema(const Value: string);
460469
FSchema := Value;
461470
end;
462471

472+
function TCRUDBase.Create(aQuery: string): string;
473+
begin
474+
475+
end;
476+
477+
function TCRUDBase.Read(aQuery: string): string;
478+
begin
479+
with FDataSet do
480+
begin
481+
Close;
482+
SQL.Clear;
483+
if not Schema.IsEmpty then
484+
Connection.ExecuteDirect(Format('SET search_path = ''%s'';', [FSchema]));
485+
SQL.Add(aQuery);
486+
Open;
487+
488+
Result := FDataSet.ToJSON;
489+
490+
Close;
491+
end;
492+
end;
493+
494+
function TCRUDBase.Update(aQuery: string): string;
495+
begin
496+
497+
end;
498+
499+
function TCRUDBase.Delete(aQuery: string): string;
500+
begin
501+
502+
end;
503+
463504
procedure TCRUDBase.SetTableName(const Value: string);
464505
begin
465506
FTableName := Value;

0 commit comments

Comments
 (0)