-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuclassexportjson.pas
79 lines (63 loc) · 1.58 KB
/
uclassexportjson.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
unit uclassExportJson;
{$mode objfpc}{$H+}
interface
uses
Db, SqlDb, ibconnection,
fpjson, jsonparser, httpDefs, custcgi,
Classes, SysUtils;
Type
TClassExportJson = class
Private
FConn: TSqlConnection;
FQuery: TSqlQuery;
FTransaction: TSqlTransaction;
AResponse: TResponse;
// Get
function GetQuery: TSqlQuery;
// Set
procedure SetQuery( const Value: TSqlQuery );
Public
function Execute(): TStringList;
property Query : TSQLQuery read GetQuery write SetQuery;
end;
implementation
function TClassExportJson.Execute(): TStringList;
var
lJsonArray: TJSONArray;
lJson: TJSONObject;
lRow: TJSONObject;
slResposta: TStringList;
i: integer;
begin
lJsonArray := TJSONArray.Create;
lJson := TJSONObject.Create;
try
while not FQuery.Eof do
begin
lRow := TJSONObject.Create;
For i:= 0 to FQuery.FieldDefs.Count-1 do
begin
//TJSONIntegerNumber. TJsonString.
lRow.Add(FQuery.FieldDefs.Items[i].Name,
TJsonString.Create( FQuery.FieldByName( FQuery.FieldDefs.Items[i].Name ).asString ) );
end;
FQuery.Next;
lJsonArray.Add(lRow);
end;
lJson.Add('rows', lJsonArray);
slResposta:= TStringList.create;
slResposta.Text:= lJson.AsJSON;
result:= slResposta;
finally
lJson.Free;
end;
end;
procedure TClassExportJson.SetQuery( const Value: TSQLQuery);
begin
FQuery:= Value;
end;
function TClassExportJson.GetQuery(): TSQLQuery;
begin
result:= FQuery;
end;
end.