@@ -40,7 +40,7 @@ TSQLiteConfig = class
4040 function Validate : boolean;
4141 function GetDefaultDir (aFileName: string): string;
4242 public
43- constructor Create;
43+ constructor Create(aFileName: string = ' config.db ' ) ;
4444 destructor Destroy; override;
4545 function getValue (pKey: string): string;
4646 procedure UpdateConfig (aJSON: TJSONObject); overload;
@@ -58,7 +58,7 @@ implementation
5858
5959{ TSQLiteConfig }
6060
61- constructor TSQLiteConfig.Create;
61+ constructor TSQLiteConfig.Create(aFileName: string = ' config.db ' ) ;
6262begin
6363 { $IFDEF MSWINDOWS} // android já possui a dll instalada
6464 FDriver := TFDPhysSQLiteDriverLink.Create(nil );
@@ -69,11 +69,10 @@ constructor TSQLiteConfig.Create;
6969 FConn.Params.Clear;
7070 FConn.Params.Add(' DriverID=SQLite' );
7171 { $IFDEF Android}
72- FConn.Params.Add(' Database=' + TPath.Combine(TPath.GetDocumentsPath,
73- ' config.db' ));
72+ FConn.Params.Add(' Database=' + TPath.Combine(TPath.GetDocumentsPath, aFileName));
7473 { $ENDIF}
7574 { $IFDEF MSWINDOWS}
76- FConn.Params.Add(' Database=' + ExtractFilePath(ParamStr(0 )) + ' config.db ' );
75+ FConn.Params.Add(' Database=' + ExtractFilePath(ParamStr(0 )) + aFileName );
7776 { $ENDIF}
7877 FConn.Params.Add(' LockingMode=normal' );
7978
@@ -125,8 +124,7 @@ function TSQLiteConfig.getValue(pKey: string): string;
125124 SQL.Add(' FROM Config' );
126125 SQL.Add(' WHERE CFG_Key = :CFG_Key' );
127126 if Idx > 0 then
128- SQL.Text := SQL.Text.Replace(' :CFG_Key' ,
129- QuotedStr(Copy(pKey, 0 , Idx - 1 )))
127+ SQL.Text := SQL.Text.Replace(' :CFG_Key' , QuotedStr(Copy(pKey, 0 , Idx - 1 )))
130128 else
131129 SQL.Text := SQL.Text.Replace(' :CFG_Key' , QuotedStr(pKey));
132130
@@ -175,9 +173,9 @@ function TSQLiteConfig.LoadConfig: TJSONObject;
175173procedure TSQLiteConfig.LoadForm (aForm: TForm);
176174var
177175 { $IFNDEF HAS_FMX}
178- J: integer ;
176+ J: Integer ;
179177 { $ENDIF}
180- I: integer ;
178+ I: Integer ;
181179 JSONTela, JSONItem: TJSONObject;
182180begin
183181 JSONTela := LoadConfig;
@@ -211,14 +209,11 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
211209 begin
212210 JSONItem :=
213211 TJSONObject(TJSONObject.ParseJSONValue
214- (JSONTela.getValue(TValueListEditor(aForm.Components[I])
215- .Name ).ToJSON));
212+ (JSONTela.getValue(TValueListEditor(aForm.Components[I]).Name ).ToJSON));
216213 if JSONItem <> nil then
217- for J := 1 to pred(TValueListEditor(aForm.Components[I])
218- .RowCount) do
214+ for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
219215 TValueListEditor(aForm.Components[I]).Cells[1 , J] :=
220- JSONItem.getValue(TValueListEditor(aForm.Components[I])
221- .Keys[J]).Value ;
216+ JSONItem.getValue(TValueListEditor(aForm.Components[I]).Keys[J]).Value ;
222217 JSONItem.Free;
223218 end
224219 { $ENDIF}
@@ -231,17 +226,16 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
231226procedure TSQLiteConfig.SaveForm (aForm: TForm);
232227var
233228 { $IFNDEF HAS_FMX}
234- J: integer ;
229+ J: Integer ;
235230 { $ENDIF}
236- I: integer ;
231+ I: Integer ;
237232 JSONTela, JSONItem: TJSONObject;
238233begin
239234 JSONTela := TJSONObject.Create;
240235 try
241236 for I := 0 to pred(aForm.ComponentCount) do
242237 if aForm.Components[I] is TEdit then
243- JSONTela.AddPair(TEdit(aForm.Components[I]).Name ,
244- TEdit(aForm.Components[I]).Text)
238+ JSONTela.AddPair(TEdit(aForm.Components[I]).Name , TEdit(aForm.Components[I]).Text)
245239 else if aForm.Components[I] is TComboBox then
246240 JSONTela.AddPair(TComboBox(aForm.Components[I]).Name ,
247241 TComboBox(aForm.Components[I]).ItemIndex)
@@ -253,8 +247,8 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
253247 JSONTela.AddPair(TDateEdit(aForm.Components[I]).Name ,
254248 TDateEdit(aForm.Components[I]).Text)
255249 else if aForm.Components[I] is TSwitch then
256- JSONTela.AddPair(TSwitch(aForm.Components[I]).Name ,
257- TSwitch(aForm.Components[I]) .IsChecked)
250+ JSONTela.AddPair(TSwitch(aForm.Components[I]).Name , TSwitch(aForm.Components[I])
251+ .IsChecked)
258252 { $ENDIF}
259253 else if aForm.Components[I] is TCheckBox then
260254 JSONTela.AddPair(TCheckBox(aForm.Components[I]).Name ,
@@ -284,30 +278,19 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
284278
285279procedure TSQLiteConfig.UpdateConfig (aJSON: TJSONObject);
286280var
287- I: integer ;
281+ I: Integer ;
288282begin
289283 // exemplo entrada
290284 // {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5"}
291285 // aJSON.Pairs[i].JSONString.tostring = "key1",
292286 // aJSON.Pairs[i].JSONValue.tostring = "value1";
293- for I := 0 to aJSON.Count - 1 do
294- with FDataSet do
295- begin
296- Close;
297- SQL.Clear;
298- SQL.Add(' SELECT CFG_Key, CFG_Value' );
299- SQL.Add(' FROM Config' );
300- SQL.Add(' WHERE CFG_Key = :CFG_Key' );
301- ParamByName(' CFG_Key' ).AsString := aJSON.Pairs[I].JsonString.Value ;
302- Open;
303- Edit;
304- Fields.Fields[0 ].AsString := aJSON.Pairs[I].JsonString.Value ;
305- Fields.Fields[1 ].AsString := aJSON.Pairs[I].JsonValue.ToString;
306- Post;
307- if FDataSet.CachedUpdates then
308- ApplyUpdates;
309- Close;
310- end ;
287+ for I := 0 to pred(aJSON.Count) do
288+ begin
289+ if aJSON.Pairs[I].JsonValue is TJSONObject then
290+ UpdateConfig(aJSON.Pairs[I].JsonString.Value , aJSON.Pairs[I].JsonValue.ToJSON)
291+ else
292+ UpdateConfig(aJSON.Pairs[I].JsonString.Value , aJSON.Pairs[I].JsonValue.Value );
293+ end ;
311294end ;
312295
313296procedure TSQLiteConfig.UpdateConfig (aKey, aValue: string);
@@ -316,17 +299,11 @@ procedure TSQLiteConfig.UpdateConfig(aKey, aValue: string);
316299 begin
317300 Close;
318301 SQL.Clear;
319- SQL.Add(' SELECT CFG_Key, CFG_Value' );
320- SQL.Add(' FROM Config' );
321- SQL.Add(' WHERE CFG_Key = :CFG_Key' );
322- ParamByName(' CFG_Key' ).AsString := aKey;
323- Open;
324- Edit;
325- Fields.Fields[0 ].AsString := aKey;
326- Fields.Fields[1 ].AsString := aValue;
327- Post;
328- if FDataSet.CachedUpdates then
329- ApplyUpdates;
302+ SQL.Add(' INSERT INTO Config (CFG_KEY, CFG_VALUE) ' );
303+ SQL.Add(' VALUES (' + QuotedStr(aKey) + ' , ' + QuotedStr(aValue) + ' ) ' );
304+ SQL.Add(' ON CONFLICT (CFG_KEY) DO UPDATE ' );
305+ SQL.Add(' SET CFG_VALUE = excluded.CFG_VALUE;' );
306+ ExecSQL;
330307 Close;
331308 end ;
332309end ;
@@ -335,11 +312,15 @@ function TSQLiteConfig.ValidaBanco: boolean;
335312begin
336313 Result := False;
337314 try
338- FDataSet.Open(' select count(*) from config' );
315+ try
316+ FDataSet.SQL.Text := ' PRAGMA table_info("Config")' ;
317+ FDataSet.ExecSQL;
318+ Result := true;
319+ except
320+ Result := False;
321+ end ;
322+ finally
339323 FDataSet.Close;
340- Result := true;
341- except
342- Result := False;
343324 end ;
344325end ;
345326
@@ -351,13 +332,12 @@ function TSQLiteConfig.Validate: boolean;
351332 begin
352333 Close;
353334 Open(' PRAGMA table_info(Config)' );
354- if isEmpty then
335+ if IsEmpty then
355336 begin
356337 Close;
357338 SQL.Clear;
358339 SQL.Add(' CREATE TABLE Config(' );
359- SQL.Add(' CFG_ID integer primary key' );
360- SQL.Add(' , CFG_Key varchar' );
340+ SQL.Add(' CFG_Key varchar not null primary key' );
361341 SQL.Add(' , CFG_Value varchar' );
362342 SQL.Add(' );' );
363343 ExecSQL;
0 commit comments