33interface
44
55uses
6- fpJSON, SysUtils, Classes,
7- DB, ZDataSet;
6+ fpJSON, SysUtils, Classes, DB,
7+ ZDataSet;
88
99type
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 ;
196202end ;
197203
198- constructor TCRUDBase.Create(DataSet: TZQuery ; aTableName, aPK: string);
204+ constructor TCRUDBase.Create(DataSet: TQuery ; aTableName: string; aPK: string);
199205begin
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 ;
303314end ;
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 ;
461470end ;
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+
463504procedure TCRUDBase.SetTableName (const Value : string);
464505begin
465506 FTableName := Value ;
0 commit comments