@@ -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;
8989end ;
9090
9191function TSQLiteConfig.getValue (pKey: string): string;
92+ var
93+ SQL: TStringList;
94+ Idx: Integer;
95+ JSON: TJSONObject;
9296begin
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 ;
110134end ;
111135
@@ -132,9 +156,9 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
132156procedure TSQLiteConfig.LoadForm (aForm: TForm);
133157var
134158 { $IFNDEF HAS_FMX}
135- J: integer ;
159+ J: Integer ;
136160 { $ENDIF}
137- I: integer ;
161+ I: Integer ;
138162 JSONTela, JSONItem: TJSONObject;
139163begin
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);
188212procedure TSQLiteConfig.SaveForm (aForm: TForm);
189213var
190214 { $IFNDEF HAS_FMX}
191- J: integer ;
215+ J: Integer ;
192216 { $ENDIF}
193- I: integer ;
217+ I: Integer ;
194218 JSONTela, JSONItem: TJSONObject;
195219begin
196220 JSONTela := TJSONObject.Create;
@@ -241,7 +265,7 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
241265
242266procedure TSQLiteConfig.UpdateConfig (aJSON: TJSONObject);
243267var
244- I: integer ;
268+ I: Integer ;
245269begin
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