Skip to content

Commit 8bb4259

Browse files
committed
+ Criação de classes para Lazarus.
* Ajuste de diretórios separando Delphi e Lazarus. + Log para Lazarus usando Zeos
1 parent 73ab61c commit 8bb4259

10 files changed

Lines changed: 1212 additions & 45 deletions
Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface
1111
{$ENDIF}
1212

1313
uses
14-
System.JSON, System.SysUtils, System.Generics.Collections,
14+
System.JSON, System.SysUtils, System.Generics.Collections, System.Classes,
1515
Data.DB,
1616
{$IF CompilerVersion > 33.0}
1717
FireDAC.Phys.SQLiteWrapper.Stat, FireDAC.Stan.ExprFuncs,
@@ -108,19 +108,47 @@ function TSQLiteConfig.GetDefaultDir(aFileName: string): string;
108108
end;
109109

110110
function TSQLiteConfig.getValue(pKey: string): string;
111+
var
112+
SQL: TStringList;
113+
Idx: Integer;
114+
JSON: TJSONObject;
111115
begin
112116
Result := '';
113-
with FDataSet do
114-
begin
115-
Close;
116-
SQL.Clear;
117-
SQL.Add('SELECT CFG_Value');
118-
SQL.Add(' FROM Config');
119-
SQL.Add(' WHERE CFG_Key = :CFG_Key');
120-
ParamByName('CFG_Key').AsString := pKey;
121-
Open;
122-
Result := Fields.Fields[0].AsString;
123-
Close;
117+
Idx := 0;
118+
if pos('.', pKey) > 0 then
119+
Idx := pos('.', pKey);
120+
121+
SQL := TStringList.Create;
122+
try
123+
try
124+
SQL.Add('SELECT CFG_Value');
125+
SQL.Add(' FROM Config');
126+
SQL.Add(' WHERE CFG_Key = :CFG_Key');
127+
if Idx > 0 then
128+
SQL.Text := SQL.Text.Replace(':CFG_Key',
129+
QuotedStr(Copy(pKey, 0, Idx - 1)))
130+
else
131+
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(pKey));
132+
133+
FDataSet.Close;
134+
FDataSet.SQL.Text := SQL.Text;
135+
FDataSet.Open;
136+
137+
if (Idx > 0) and (not FDataSet.IsEmpty) then
138+
begin
139+
JSON := TJSONObject(TJSONObject.ParseJSONValue(FDataSet.Fields.Fields[0]
140+
.AsString));
141+
Result := JSON.getValue(Copy(pKey, Idx + 1, length(pKey))).Value;
142+
JSON.Free;
143+
end
144+
else
145+
Result := FDataSet.Fields.Fields[0].AsString.Replace('"', '');
146+
FDataSet.Close;
147+
except
148+
Result := '';
149+
end;
150+
finally
151+
SQL.Free;
124152
end;
125153
end;
126154

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface
1414
{$IFDEF Android}
1515
System.IOUtils,
1616
{$ENDIF}
17-
System.JSON, System.SysUtils, System.Generics.Collections,
17+
System.JSON, System.SysUtils, System.Generics.Collections, Classes,
1818
Data.DB,
1919
ZConnection, ZDataSet
2020

@@ -89,23 +89,47 @@ function TSQLiteConfig.GetDefaultDir(aFileName: string): string;
8989
end;
9090

9191
function TSQLiteConfig.getValue(pKey: string): string;
92+
var
93+
SQL: TStringList;
94+
Idx: Integer;
95+
JSON: TJSONObject;
9296
begin
9397
Result := '';
98+
Idx := 0;
99+
if pos('.', pKey) > 0 then
100+
Idx := pos('.', pKey);
101+
102+
SQL := TStringList.Create;
94103
try
95-
with FDataSet do
96-
begin
97-
Close;
98-
SQL.Clear;
104+
try
99105
SQL.Add('SELECT CFG_Value');
100106
SQL.Add(' FROM Config');
101107
SQL.Add(' WHERE CFG_Key = :CFG_Key');
102-
ParamByName('CFG_Key').AsString := pKey;
103-
Open;
104-
Result := Fields.Fields[0].AsString;
105-
Close;
108+
if Idx > 0 then
109+
SQL.Text := SQL.Text.Replace(':CFG_Key',
110+
QuotedStr(Copy(pKey, 0, Idx - 1)))
111+
else
112+
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(pKey));
113+
114+
FDataSet.Close;
115+
FDataSet.SQL.Text := SQL.Text;
116+
FDataSet.Open;
117+
118+
if (Idx > 0) and (not FDataSet.IsEmpty) then
119+
begin
120+
JSON := TJSONObject(TJSONObject.ParseJSONValue(FDataSet.Fields.Fields[0]
121+
.AsString));
122+
Result := JSON.getValue(Copy(pKey, Idx + 1, length(pKey))).Value;
123+
JSON.Free;
124+
end
125+
else
126+
Result := FDataSet.Fields.Fields[0].AsString.Replace('"', '');
127+
FDataSet.Close;
128+
except
129+
Result := '';
106130
end;
107-
except
108-
Result := '';
131+
finally
132+
SQL.Free;
109133
end;
110134
end;
111135

@@ -132,9 +156,9 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
132156
procedure TSQLiteConfig.LoadForm(aForm: TForm);
133157
var
134158
{$IFNDEF HAS_FMX}
135-
J: integer;
159+
J: Integer;
136160
{$ENDIF}
137-
I: integer;
161+
I: Integer;
138162
JSONTela, JSONItem: TJSONObject;
139163
begin
140164
JSONTela := LoadConfig;
@@ -146,20 +170,20 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
146170
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
147171
else if (aForm.Components[I] is TComboBox) then
148172
TComboBox(aForm.Components[I]).ItemIndex :=
149-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
173+
JSONTela.getValue(TComboBox(aForm.Components[I]).Name).Value.ToInteger
150174
{$IFDEF HAS_FMX}
151175
else if (aForm.Components[I] is TComboEdit) then
152176
TComboEdit(aForm.Components[I]).ItemIndex :=
153-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToInteger
177+
JSONTela.getValue(TComboEdit(aForm.Components[I]).Name).Value.ToInteger
154178
else if (aForm.Components[I] is TDateEdit) then
155179
TDateEdit(aForm.Components[I]).Text :=
156-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value
180+
JSONTela.getValue(TDateEdit(aForm.Components[I]).Name).Value
157181
else if (aForm.Components[I] is TSwitch) then
158182
TSwitch(aForm.Components[I]).IsChecked :=
159-
JSONTela.getValue(TEdit(aForm.Components[I]).Name).Value.ToBoolean
183+
JSONTela.getValue(TSwitch(aForm.Components[I]).Name).Value.ToBoolean
160184
{$ENDIF}
161185
else if (aForm.Components[I] is TCheckBox) then
162-
TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF} := JSONTela.Get(TEdit(aForm.Components[I]).Name).Value.ToBoolean
186+
TCheckBox(aForm.Components[I]).{$IFDEF HAS_FMX}IsChecked{$ELSE}Checked{$ENDIF} := JSONTela.Get(TCheckBox(aForm.Components[I]).Name).Value.ToBoolean
163187
{$IFNDEF HAS_FMX}
164188
else if aForm.Components[I] is TLabeledEdit then
165189
TLabeledEdit(aForm.Components[I]).Text :=
@@ -188,9 +212,9 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
188212
procedure TSQLiteConfig.SaveForm(aForm: TForm);
189213
var
190214
{$IFNDEF HAS_FMX}
191-
J: integer;
215+
J: Integer;
192216
{$ENDIF}
193-
I: integer;
217+
I: Integer;
194218
JSONTela, JSONItem: TJSONObject;
195219
begin
196220
JSONTela := TJSONObject.Create;
@@ -241,7 +265,7 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
241265

242266
procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
243267
var
244-
I: integer;
268+
I: Integer;
245269
begin
246270
// exemplo entrada
247271
// {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5"}
@@ -310,7 +334,7 @@ function TSQLiteConfig.Validate: boolean;
310334
Close;
311335
SQL.Text := 'PRAGMA table_info("Config")';
312336
Open;
313-
if isEmpty then
337+
if IsEmpty then
314338
begin
315339
Close;
316340
SQL.Clear;

0 commit comments

Comments
 (0)