Skip to content

Commit 73ab61c

Browse files
committed
- Ajustes e correções em Config.SQLite
+ Campo UserAgent em RESTDAO
1 parent 11f2cf3 commit 73ab61c

3 files changed

Lines changed: 89 additions & 42 deletions

File tree

config/Config.SQLite.FireDAC.pas

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
136136
Open;
137137
while not Eof do
138138
begin
139-
Result.AddPair(Fields.Fields[0].AsString, Fields.Fields[1].AsString);
139+
Result.AddPair(Fields.Fields[0].AsString,
140+
TJSONObject.ParseJSONValue(Fields.Fields[1].AsString));
140141
Next;
141142
end;
142143
Close;
@@ -149,12 +150,12 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
149150
J: integer;
150151
{$ENDIF}
151152
I: integer;
152-
JSONTela: TJSONObject;
153+
JSONTela, JSONItem: TJSONObject;
153154
begin
154155
JSONTela := LoadConfig;
155156
try
156157
for I := 0 to pred(aForm.ComponentCount) do
157-
if JSONTela.getValue(TEdit(aForm.Components[I]).Name) <> nil then
158+
if JSONTela.getValue(aForm.Components[I].Name) <> nil then
158159
if (aForm.Components[I] is TEdit) then
159160
TEdit(aForm.Components[I]).Text :=
160161
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
@@ -178,13 +179,22 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
178179
else if aForm.Components[I] is TLabeledEdit then
179180
TLabeledEdit(aForm.Components[I]).Text :=
180181
JSONTela.getValue(TLabeledEdit(aForm.Components[I]).Name).Value
181-
else if aForm.Components[I] is TValueListEditor then
182-
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
183-
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
184-
JSONTela.getValue(TValueListEditor(aForm.Components[I])
185-
.Keys[J]).Value
186-
{$ENDIF}
187-
;
182+
else if (aForm.Components[I] is TValueListEditor) then
183+
begin
184+
JSONItem :=
185+
TJSONObject(TJSONObject.ParseJSONValue
186+
(JSONTela.getValue(TValueListEditor(aForm.Components[I])
187+
.Name).ToJSON));
188+
if JSONItem <> nil then
189+
for J := 1 to pred(TValueListEditor(aForm.Components[I])
190+
.RowCount) do
191+
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
192+
JSONItem.getValue(TValueListEditor(aForm.Components[I])
193+
.Keys[J]).Value;
194+
JSONItem.Free;
195+
end
196+
{$ENDIF}
197+
;
188198
finally
189199
JSONTela.Free;
190200
end;
@@ -196,7 +206,7 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
196206
J: integer;
197207
{$ENDIF}
198208
I: integer;
199-
JSONTela: TJSONObject;
209+
JSONTela, JSONItem: TJSONObject;
200210
begin
201211
JSONTela := TJSONObject.Create;
202212
try
@@ -226,11 +236,17 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
226236
JSONTela.AddPair(TLabeledEdit(aForm.Components[I]).Name,
227237
TLabeledEdit(aForm.Components[I]).Text)
228238
else if aForm.Components[I] is TValueListEditor then
239+
begin
240+
JSONItem := TJSONObject.Create;
229241
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
230-
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Keys[J],
242+
JSONItem.AddPair(TValueListEditor(aForm.Components[I]).Keys[J],
231243
TValueListEditor(aForm.Components[I]).Cells[1, J]);
232-
{$ENDIF}
233-
;
244+
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Name,
245+
TJSONObject.ParseJSONValue(JSONItem.ToJSON));
246+
JSONItem.Free;
247+
end
248+
{$ENDIF}
249+
;
234250

235251
UpdateConfig(JSONTela);
236252
finally
@@ -254,14 +270,11 @@ procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
254270
SQL.Add('SELECT CFG_Key, CFG_Value');
255271
SQL.Add(' FROM Config');
256272
SQL.Add(' WHERE CFG_Key = :CFG_Key');
257-
ParamByName('CFG_Key').AsString := aJSON.Pairs[I].JsonString.ToString.Replace
258-
('"', '', [rfReplaceAll]);
273+
ParamByName('CFG_Key').AsString := aJSON.Pairs[I].JsonString.Value;
259274
Open;
260275
Edit;
261-
Fields.Fields[0].AsString := aJSON.Pairs[I].JsonString.ToString.Replace('"',
262-
'', [rfReplaceAll]);
263-
Fields.Fields[1].AsString := aJSON.Pairs[I].JsonValue.ToString.Replace('"',
264-
'', [rfReplaceAll]);
276+
Fields.Fields[0].AsString := aJSON.Pairs[I].JsonString.Value;
277+
Fields.Fields[1].AsString := aJSON.Pairs[I].JsonValue.ToString;
265278
Post;
266279
if FDataSet.CachedUpdates then
267280
ApplyUpdates;

config/Config.SQLite.Zeos.pas

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
121121
Open;
122122
while not Eof do
123123
begin
124-
Result.AddPair(Fields.Fields[0].AsString, Fields.Fields[1].AsString);
124+
Result.AddPair(Fields.Fields[0].AsString,
125+
TJSONObject.ParseJSONValue(Fields.Fields[1].AsString));
125126
Next;
126127
end;
127128
Close;
@@ -134,12 +135,12 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
134135
J: integer;
135136
{$ENDIF}
136137
I: integer;
137-
JSONTela: TJSONObject;
138+
JSONTela, JSONItem: TJSONObject;
138139
begin
139140
JSONTela := LoadConfig;
140141
try
141142
for I := 0 to pred(aForm.ComponentCount) do
142-
if JSONTela.getValue(TEdit(aForm.Components[I]).Name) <> nil then
143+
if JSONTela.getValue(aForm.Components[I].Name) <> nil then
143144
if (aForm.Components[I] is TEdit) then
144145
TEdit(aForm.Components[I]).Text :=
145146
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
@@ -163,13 +164,22 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
163164
else if aForm.Components[I] is TLabeledEdit then
164165
TLabeledEdit(aForm.Components[I]).Text :=
165166
JSONTela.getValue(TLabeledEdit(aForm.Components[I]).Name).Value
166-
else if aForm.Components[I] is TValueListEditor then
167-
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
168-
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
169-
JSONTela.getValue(TValueListEditor(aForm.Components[I])
170-
.Keys[J]).Value
171-
{$ENDIF}
172-
;
167+
else if (aForm.Components[I] is TValueListEditor) then
168+
begin
169+
JSONItem :=
170+
TJSONObject(TJSONObject.ParseJSONValue
171+
(JSONTela.getValue(TValueListEditor(aForm.Components[I])
172+
.Name).ToJSON));
173+
if JSONItem <> nil then
174+
for J := 1 to pred(TValueListEditor(aForm.Components[I])
175+
.RowCount) do
176+
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
177+
JSONItem.getValue(TValueListEditor(aForm.Components[I])
178+
.Keys[J]).Value;
179+
JSONItem.Free;
180+
end
181+
{$ENDIF}
182+
;
173183
finally
174184
JSONTela.Free;
175185
end;
@@ -181,7 +191,7 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
181191
J: integer;
182192
{$ENDIF}
183193
I: integer;
184-
JSONTela: TJSONObject;
194+
JSONTela, JSONItem: TJSONObject;
185195
begin
186196
JSONTela := TJSONObject.Create;
187197
try
@@ -211,11 +221,17 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
211221
JSONTela.AddPair(TLabeledEdit(aForm.Components[I]).Name,
212222
TLabeledEdit(aForm.Components[I]).Text)
213223
else if aForm.Components[I] is TValueListEditor then
224+
begin
225+
JSONItem := TJSONObject.Create;
214226
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
215-
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Keys[J],
227+
JSONItem.AddPair(TValueListEditor(aForm.Components[I]).Keys[J],
216228
TValueListEditor(aForm.Components[I]).Cells[1, J]);
217-
{$ENDIF}
218-
;
229+
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Name,
230+
TJSONObject.ParseJSONValue(JSONItem.ToJSON));
231+
JSONItem.Free;
232+
end
233+
{$ENDIF}
234+
;
219235

220236
UpdateConfig(JSONTela);
221237
finally
@@ -229,8 +245,8 @@ procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
229245
begin
230246
// exemplo entrada
231247
// {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5"}
232-
// aJSON.Pairs[i].JSONString.tostring = "key1",
233-
// aJSON.Pairs[i].JSONValue.tostring = "value1";
248+
// aJSON.Pairs[i].JSONString.Value = "key1",
249+
// aJSON.Pairs[i].JSONValue.Value = "value1";
234250
for I := 0 to aJSON.Count - 1 do
235251
with FDataSet do
236252
begin
@@ -239,14 +255,11 @@ procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
239255
SQL.Add('SELECT CFG_Key, CFG_Value');
240256
SQL.Add(' FROM Config');
241257
SQL.Add(' WHERE CFG_Key = :CFG_Key');
242-
ParamByName('CFG_Key').AsString := aJSON.Pairs[I].JsonString.ToString.Replace
243-
('"', '', [rfReplaceAll]);
258+
ParamByName('CFG_Key').AsString := aJSON.Pairs[I].JsonString.Value;
244259
Open;
245260
Edit;
246-
Fields.Fields[0].AsString := aJSON.Pairs[I].JsonString.ToString.Replace('"',
247-
'', [rfReplaceAll]);
248-
Fields.Fields[1].AsString := aJSON.Pairs[I].JsonValue.ToString.Replace('"',
249-
'', [rfReplaceAll]);
261+
Fields.Fields[0].AsString := aJSON.Pairs[I].JsonString.Value;
262+
Fields.Fields[1].AsString := aJSON.Pairs[I].JsonValue.ToString;
250263
Post;
251264
if FDataSet.CachedUpdates then
252265
ApplyUpdates;

dao/RESTDAO.pas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ TRESTDAO = class
2121
aHeaders: TJSONObject = nil; aBody: TJSONObject = nil); overload;
2222
procedure DefineFileParams(aMethod: TRESTRequestMethod;
2323
aHeaders: TJSONObject = nil; aBody: TStream = nil);
24+
function GetUserAgent: string;
25+
procedure SetUserAgent(const Value: string);
2426
public
2527
constructor Create; overload;
2628
constructor Create(aBaseURL: string); overload;
29+
constructor Create(aBaseURL, aUserAgent: string); overload;
2730
destructor Destroy; override;
2831
function Get(aParams: TJSONObject = nil): string;
2932
function GetFile(aParams: TJSONObject = nil): TStream;
@@ -39,6 +42,7 @@ TRESTDAO = class
3942

4043
property BaseURL: string read GetBaseURL write SetBaseURL;
4144
property Endpoint: string read GetResource write SetResource;
45+
property UserAgent: string read GetUserAgent write SetUserAgent;
4246
end;
4347

4448
THelper = class helper for TCustomRESTResponse
@@ -66,6 +70,13 @@ constructor TRESTDAO.Create(aBaseURL: string);
6670
BaseURL := aBaseURL;
6771
end;
6872

73+
constructor TRESTDAO.Create(aBaseURL, aUserAgent: string);
74+
begin
75+
Create;
76+
BaseURL := aBaseURL;
77+
UserAgent := aUserAgent;
78+
end;
79+
6980
procedure TRESTDAO.DefineFileParams(aMethod: TRESTRequestMethod;
7081
aHeaders: TJSONObject; aBody: TStream);
7182
var
@@ -179,6 +190,11 @@ function TRESTDAO.GetResource: string;
179190
Result := FRESTRequest.Resource;
180191
end;
181192

193+
function TRESTDAO.GetUserAgent: string;
194+
begin
195+
Result := FRESTClient.UserAgent;
196+
end;
197+
182198
procedure TRESTDAO.SetBaseURL(const Value: string);
183199
begin
184200
FRESTClient.BaseURL := Value;
@@ -189,6 +205,11 @@ procedure TRESTDAO.SetResource(const Value: string);
189205
FRESTRequest.Resource := Value;
190206
end;
191207

208+
procedure TRESTDAO.SetUserAgent(const Value: string);
209+
begin
210+
FRESTClient.UserAgent := Value;
211+
end;
212+
192213
{ THelper }
193214

194215
procedure THelper.SaveToStream(aStream: TStream);

0 commit comments

Comments
 (0)