Skip to content

Commit d81f3ad

Browse files
committed
- Correção de Leitura de parâmetros em Config quando possuir caracteres especiais unicode
1 parent 24f8507 commit d81f3ad

2 files changed

Lines changed: 68 additions & 79 deletions

File tree

config/delphi/Config.SQLite.FireDAC.pas

Lines changed: 58 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Maiores Informações
22
// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki
3-
43
unit Config.SQLite.FireDAC;
54

65
interface
@@ -13,22 +12,21 @@ interface
1312
uses
1413
System.JSON, System.SysUtils, System.Generics.Collections, System.Classes,
1514
Data.DB,
16-
{$IF CompilerVersion > 33.0}
15+
{$IF CompilerVersion > 33.0}
1716
FireDAC.Phys.SQLiteWrapper.Stat, FireDAC.Stan.ExprFuncs,
1817
FireDAC.Phys.SQLiteDef, FireDAC.Stan.Intf, FireDAC.Phys, FireDAC.Phys.SQLite,
1918
FireDAC.Stan.Param,
20-
{$IFEND}
21-
{$IFDEF Android}
19+
{$IFEND}
20+
{$IFDEF Android}
2221
System.IOUtils,
23-
{$ENDIF}
22+
{$ENDIF}
2423
FireDAC.Comp.Client
25-
26-
{$IFDEF HAS_FMX}
24+
{$IFDEF HAS_FMX}
2725
, FMX.Forms, FMX.Edit, FMX.ComboEdit, FMX.StdCtrls, FMX.ExtCtrls,
2826
FMX.Controls, FMX.ListBox, FMX.DateTimeCtrls
29-
{$ELSE}
27+
{$ELSE}
3028
, VCL.Forms, VCL.StdCtrls, VCL.ExtCtrls, VCL.ValEdit
31-
{$IFEND}
29+
{$IFEND}
3230
;
3331

3432
type
@@ -39,6 +37,7 @@ TSQLiteConfig = class
3937
FDriver: TFDPhysSQLiteDriverLink;
4038
function Validate: boolean;
4139
function GetDefaultDir(aFileName: string): string;
40+
function isJSON(aJSON: string): boolean;
4241
public
4342
constructor Create(aFileName: string = 'config.db');
4443
destructor Destroy; override;
@@ -57,41 +56,37 @@ TSQLiteConfig = class
5756
implementation
5857

5958
{ TSQLiteConfig }
60-
6159
constructor TSQLiteConfig.Create(aFileName: string = 'config.db');
6260
begin
63-
{$IFDEF MSWINDOWS} // android já possui a dll instalada
61+
{$IFDEF MSWINDOWS} // android já possui a dll instalada
6462
FDriver := TFDPhysSQLiteDriverLink.Create(nil);
6563
FDriver.DriverID := 'SQLite';
6664
FDriver.VendorLib := GetDefaultDir('sqlite3.dll');
67-
{$ENDIF}
65+
{$ENDIF}
6866
FConn := TFDConnection.Create(nil);
6967
FConn.Params.Clear;
7068
FConn.Params.Add('DriverID=SQLite');
71-
{$IFDEF Android}
69+
{$IFDEF Android}
7270
FConn.Params.Add('Database=' + TPath.Combine(TPath.GetDocumentsPath, aFileName));
73-
{$ENDIF}
74-
{$IFDEF MSWINDOWS}
71+
{$ENDIF}
72+
{$IFDEF MSWINDOWS}
7573
FConn.Params.Add('Database=' + ExtractFilePath(ParamStr(0)) + aFileName);
76-
{$ENDIF}
74+
{$ENDIF}
7775
FConn.Params.Add('LockingMode=normal');
78-
7976
FDataSet := TFDQuery.Create(nil);
8077
FDataSet.Connection := FConn;
8178
FDataSet.ResourceOptions.SilentMode := true;
82-
8379
if not Validate then
84-
raise Exception.Create
85-
('sqlite3.dll precisa estar na raiz do projeto ou na pasta /lib');
80+
raise Exception.Create('sqlite3.dll precisa estar na raiz do projeto ou na pasta /lib');
8681
end;
8782

8883
destructor TSQLiteConfig.Destroy;
8984
begin
9085
FDataSet.Free;
9186
FConn.Free;
92-
{$IFDEF MSWINDOWS}
87+
{$IFDEF MSWINDOWS}
9388
FDriver.Free;
94-
{$ENDIF}
89+
{$ENDIF}
9590
inherited;
9691
end;
9792

@@ -116,7 +111,6 @@ function TSQLiteConfig.getValue(pKey: string): string;
116111
Idx := 0;
117112
if pos('.', pKey) > 0 then
118113
Idx := pos('.', pKey);
119-
120114
SQL := TStringList.Create;
121115
try
122116
try
@@ -127,15 +121,12 @@ function TSQLiteConfig.getValue(pKey: string): string;
127121
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(Copy(pKey, 0, Idx - 1)))
128122
else
129123
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(pKey));
130-
131124
FDataSet.Close;
132125
FDataSet.SQL.Text := SQL.Text;
133126
FDataSet.Open;
134-
135127
if (Idx > 0) and (not FDataSet.IsEmpty) then
136128
begin
137-
JSON := TJSONObject(TJSONObject.ParseJSONValue(FDataSet.Fields.Fields[0]
138-
.AsString));
129+
JSON := TJSONObject(TJSONObject.ParseJSONValue(FDataSet.Fields.Fields[0].AsString));
139130
Result := JSON.getValue(Copy(pKey, Idx + 1, length(pKey))).Value;
140131
JSON.Free;
141132
end
@@ -150,6 +141,11 @@ function TSQLiteConfig.getValue(pKey: string): string;
150141
end;
151142
end;
152143

144+
function TSQLiteConfig.isJSON(aJSON: string): boolean;
145+
begin
146+
Result := ((pos('{', aJSON) > 0) or (pos('[', aJSON) > 0)) and (pos('"', aJSON) > 0);
147+
end;
148+
153149
function TSQLiteConfig.LoadConfig: TJSONObject;
154150
begin
155151
Result := TJSONObject.Create;
@@ -158,12 +154,14 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
158154
Close;
159155
SQL.Clear;
160156
SQL.Add('SELECT CFG_Key, CFG_Value');
161-
SQL.Add(' FROM Config');
157+
SQL.Add(' FROM Config');
162158
Open;
163159
while not Eof do
164160
begin
165-
Result.AddPair(Fields.Fields[0].AsString,
166-
TJSONObject.ParseJSONValue(Fields.Fields[1].AsString));
161+
if isJSON(Fields.Fields[1].AsString) then
162+
Result.AddPair(Fields.Fields[0].AsString, TJSONObject.ParseJSONValue(Fields.Fields[1].AsString))
163+
else
164+
Result.AddPair(Fields.Fields[0].AsString, Fields.Fields[1].AsString);
167165
Next;
168166
end;
169167
Close;
@@ -172,9 +170,9 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
172170

173171
procedure TSQLiteConfig.LoadForm(aForm: TForm);
174172
var
175-
{$IFNDEF HAS_FMX}
173+
{$IFNDEF HAS_FMX}
176174
J: Integer;
177-
{$ENDIF}
175+
{$ENDIF}
178176
I: Integer;
179177
JSONTela, JSONItem: TJSONObject;
180178
begin
@@ -183,40 +181,32 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
183181
for I := 0 to pred(aForm.ComponentCount) do
184182
if JSONTela.getValue(aForm.Components[I].Name) <> nil then
185183
if (aForm.Components[I] is TEdit) then
186-
TEdit(aForm.Components[I]).Text :=
187-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
184+
TEdit(aForm.Components[I]).Text := JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
188185
else if (aForm.Components[I] is TComboBox) then
189-
TComboBox(aForm.Components[I]).ItemIndex :=
190-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
191-
{$IFDEF HAS_FMX}
186+
TComboBox(aForm.Components[I]).ItemIndex := JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
187+
{$IFDEF HAS_FMX}
192188
else if (aForm.Components[I] is TComboEdit) then
193-
TComboEdit(aForm.Components[I]).ItemIndex :=
194-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
189+
TComboEdit(aForm.Components[I]).ItemIndex := JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
195190
else if (aForm.Components[I] is TDateEdit) then
196-
TDateEdit(aForm.Components[I]).Text :=
197-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
191+
TDateEdit(aForm.Components[I]).Text := JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
198192
else if (aForm.Components[I] is TSwitch) then
199-
TSwitch(aForm.Components[I]).IsChecked :=
200-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToBoolean
201-
{$ENDIF}
193+
TSwitch(aForm.Components[I]).IsChecked := JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToBoolean
194+
{$ENDIF}
202195
else if (aForm.Components[I] is TCheckBox) then
203-
TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF} := JSONTela.Get(TEdit(aForm.Components[I]).Name).Value.ToBoolean
204-
{$IFNDEF HAS_FMX}
196+
TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF} := JSONTela.Get(TEdit(aForm.Components[I]).Name)
197+
.Value.ToBoolean
198+
{$IFNDEF HAS_FMX}
205199
else if aForm.Components[I] is TLabeledEdit then
206-
TLabeledEdit(aForm.Components[I]).Text :=
207-
JSONTela.getValue(TLabeledEdit(aForm.Components[I]).Name).Value
200+
TLabeledEdit(aForm.Components[I]).Text := JSONTela.getValue(TLabeledEdit(aForm.Components[I]).Name).Value
208201
else if (aForm.Components[I] is TValueListEditor) then
209202
begin
210-
JSONItem :=
211-
TJSONObject(TJSONObject.ParseJSONValue
212-
(JSONTela.getValue(TValueListEditor(aForm.Components[I]).Name).ToJSON));
203+
JSONItem := TJSONObject(TJSONObject.ParseJSONValue(JSONTela.getValue(TValueListEditor(aForm.Components[I]).Name).ToJSON));
213204
if JSONItem <> nil then
214205
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
215-
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
216-
JSONItem.getValue(TValueListEditor(aForm.Components[I]).Keys[J]).Value;
206+
TValueListEditor(aForm.Components[I]).Cells[1, J] := JSONItem.getValue(TValueListEditor(aForm.Components[I]).Keys[J]).Value;
217207
JSONItem.Free;
218208
end
219-
{$ENDIF}
209+
{$ENDIF}
220210
;
221211
finally
222212
JSONTela.Free;
@@ -225,9 +215,9 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
225215

226216
procedure TSQLiteConfig.SaveForm(aForm: TForm);
227217
var
228-
{$IFNDEF HAS_FMX}
218+
{$IFNDEF HAS_FMX}
229219
J: Integer;
230-
{$ENDIF}
220+
{$ENDIF}
231221
I: Integer;
232222
JSONTela, JSONItem: TJSONObject;
233223
begin
@@ -237,39 +227,30 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
237227
if aForm.Components[I] is TEdit then
238228
JSONTela.AddPair(TEdit(aForm.Components[I]).Name, TEdit(aForm.Components[I]).Text)
239229
else if aForm.Components[I] is TComboBox then
240-
JSONTela.AddPair(TComboBox(aForm.Components[I]).Name,
241-
TComboBox(aForm.Components[I]).ItemIndex)
242-
{$IFDEF HAS_FMX}
230+
JSONTela.AddPair(TComboBox(aForm.Components[I]).Name, TComboBox(aForm.Components[I]).ItemIndex)
231+
{$IFDEF HAS_FMX}
243232
else if aForm.Components[I] is TComboEdit then
244-
JSONTela.AddPair(TComboEdit(aForm.Components[I]).Name,
245-
TComboEdit(aForm.Components[I]).ItemIndex)
233+
JSONTela.AddPair(TComboEdit(aForm.Components[I]).Name, TComboEdit(aForm.Components[I]).ItemIndex)
246234
else if aForm.Components[I] is TDateEdit then
247-
JSONTela.AddPair(TDateEdit(aForm.Components[I]).Name,
248-
TDateEdit(aForm.Components[I]).Text)
235+
JSONTela.AddPair(TDateEdit(aForm.Components[I]).Name, TDateEdit(aForm.Components[I]).Text)
249236
else if aForm.Components[I] is TSwitch then
250-
JSONTela.AddPair(TSwitch(aForm.Components[I]).Name, TSwitch(aForm.Components[I])
251-
.IsChecked)
252-
{$ENDIF}
237+
JSONTela.AddPair(TSwitch(aForm.Components[I]).Name, TSwitch(aForm.Components[I]).IsChecked)
238+
{$ENDIF}
253239
else if aForm.Components[I] is TCheckBox then
254-
JSONTela.AddPair(TCheckBox(aForm.Components[I]).Name,
255-
TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF})
256-
{$IFNDEF HAS_FMX}
240+
JSONTela.AddPair(TCheckBox(aForm.Components[I]).Name, TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF})
241+
{$IFNDEF HAS_FMX}
257242
else if aForm.Components[I] is TLabeledEdit then
258-
JSONTela.AddPair(TLabeledEdit(aForm.Components[I]).Name,
259-
TLabeledEdit(aForm.Components[I]).Text)
243+
JSONTela.AddPair(TLabeledEdit(aForm.Components[I]).Name, TLabeledEdit(aForm.Components[I]).Text)
260244
else if aForm.Components[I] is TValueListEditor then
261245
begin
262246
JSONItem := TJSONObject.Create;
263247
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
264-
JSONItem.AddPair(TValueListEditor(aForm.Components[I]).Keys[J],
265-
TValueListEditor(aForm.Components[I]).Cells[1, J]);
266-
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Name,
267-
TJSONObject.ParseJSONValue(JSONItem.ToJSON));
248+
JSONItem.AddPair(TValueListEditor(aForm.Components[I]).Keys[J], TValueListEditor(aForm.Components[I]).Cells[1, J]);
249+
JSONTela.AddPair(TValueListEditor(aForm.Components[I]).Name, TJSONObject.ParseJSONValue(JSONItem.ToJSON));
268250
JSONItem.Free;
269251
end
270-
{$ENDIF}
252+
{$ENDIF}
271253
;
272-
273254
UpdateConfig(JSONTela);
274255
finally
275256
JSONTela.Free;

config/delphi/Config.SQLite.Zeos.pas

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ TSQLiteConfig = class
3333
FDataSet: TZQuery;
3434
function Validate: boolean;
3535
function GetDefaultDir(aFileName: string): string;
36+
function isJSON(aJSON: string): boolean;
3637
public
3738
constructor Create(aFileName: string = 'config.db');
3839
destructor Destroy; override;
@@ -132,6 +133,11 @@ function TSQLiteConfig.getValue(pKey: string): string;
132133
end;
133134
end;
134135

136+
function TSQLiteConfig.isJSON(aJSON: string): boolean;
137+
begin
138+
Result := ((pos('{', aJSON) > 0) or (pos('[', aJSON) > 0)) and (pos('"', aJSON) > 0);
139+
end;
140+
135141
function TSQLiteConfig.LoadConfig: TJSONObject;
136142
begin
137143
Result := TJSONObject.Create;
@@ -144,8 +150,10 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
144150
Open;
145151
while not Eof do
146152
begin
147-
Result.AddPair(Fields.Fields[0].AsString,
148-
TJSONObject.ParseJSONValue(Fields.Fields[1].AsString));
153+
if isJSON(Fields.Fields[1].AsString) then
154+
Result.AddPair(Fields.Fields[0].AsString, TJSONObject.ParseJSONValue(Fields.Fields[1].AsString))
155+
else
156+
Result.AddPair(Fields.Fields[0].AsString, Fields.Fields[1].AsString);
149157
Next;
150158
end;
151159
Close;

0 commit comments

Comments
 (0)