Skip to content

Commit 821825a

Browse files
committed
- Correção de erro de gravação em Config.SQLite.Zeos.pas no Lazarus.
* Melhoria de gravação de dados em todas as units Config, ficando mais rápido em caso de regravação. * Otimização e Redução de código total das units + Capacidade de alterar o nome do arquivo físico de todas as configs
1 parent c277f9e commit 821825a

3 files changed

Lines changed: 89 additions & 146 deletions

File tree

config/delphi/Config.SQLite.FireDAC.pas

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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');
6262
begin
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;
175173
procedure TSQLiteConfig.LoadForm(aForm: TForm);
176174
var
177175
{$IFNDEF HAS_FMX}
178-
J: integer;
176+
J: Integer;
179177
{$ENDIF}
180-
I: integer;
178+
I: Integer;
181179
JSONTela, JSONItem: TJSONObject;
182180
begin
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);
231226
procedure TSQLiteConfig.SaveForm(aForm: TForm);
232227
var
233228
{$IFNDEF HAS_FMX}
234-
J: integer;
229+
J: Integer;
235230
{$ENDIF}
236-
I: integer;
231+
I: Integer;
237232
JSONTela, JSONItem: TJSONObject;
238233
begin
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

285279
procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
286280
var
287-
I: integer;
281+
I: Integer;
288282
begin
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;
311294
end;
312295

313296
procedure 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;
332309
end;
@@ -335,11 +312,15 @@ function TSQLiteConfig.ValidaBanco: boolean;
335312
begin
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;
344325
end;
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;

config/delphi/Config.SQLite.Zeos.pas

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TSQLiteConfig = class
3434
function Validate: boolean;
3535
function GetDefaultDir(aFileName: string): string;
3636
public
37-
constructor Create;
37+
constructor Create(aFileName: string = 'config.db');
3838
destructor Destroy; override;
3939
function getValue(pKey: string): string;
4040
procedure UpdateConfig(aJSON: TJSONObject); overload;
@@ -49,15 +49,15 @@ implementation
4949

5050
{ TSQLiteConfig }
5151

52-
constructor TSQLiteConfig.Create;
52+
constructor TSQLiteConfig.Create(aFileName: string = 'config.db');
5353
begin
5454
FConn := TZConnection.Create(nil);
5555
FConn.Protocol := 'sqlite-3';
5656
{$IFDEF Android}
57-
FConn.Database := TPath.Combine(TPath.GetDocumentsPath, 'config.db');
57+
FConn.Database := TPath.Combine(TPath.GetDocumentsPath, aFileName);
5858
{$ENDIF}
5959
{$IFDEF MSWINDOWS}
60-
FConn.Database := ExtractFilePath(ParamStr(0)) + 'config.db';
60+
FConn.Database := ExtractFilePath(ParamStr(0)) + aFileName;
6161
{$ENDIF}
6262
FConn.Properties.Add('LockingMode=normal');
6363

@@ -106,8 +106,7 @@ function TSQLiteConfig.getValue(pKey: string): string;
106106
SQL.Add(' FROM Config');
107107
SQL.Add(' WHERE CFG_Key = :CFG_Key');
108108
if Idx > 0 then
109-
SQL.Text := SQL.Text.Replace(':CFG_Key',
110-
QuotedStr(Copy(pKey, 0, Idx - 1)))
109+
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(Copy(pKey, 0, Idx - 1)))
111110
else
112111
SQL.Text := SQL.Text.Replace(':CFG_Key', QuotedStr(pKey));
113112

@@ -192,14 +191,11 @@ procedure TSQLiteConfig.LoadForm(aForm: TForm);
192191
begin
193192
JSONItem :=
194193
TJSONObject(TJSONObject.ParseJSONValue
195-
(JSONTela.getValue(TValueListEditor(aForm.Components[I])
196-
.Name).ToJSON));
194+
(JSONTela.getValue(TValueListEditor(aForm.Components[I]).Name).ToJSON));
197195
if JSONItem <> nil then
198-
for J := 1 to pred(TValueListEditor(aForm.Components[I])
199-
.RowCount) do
196+
for J := 1 to pred(TValueListEditor(aForm.Components[I]).RowCount) do
200197
TValueListEditor(aForm.Components[I]).Cells[1, J] :=
201-
JSONItem.getValue(TValueListEditor(aForm.Components[I])
202-
.Keys[J]).Value;
198+
JSONItem.getValue(TValueListEditor(aForm.Components[I]).Keys[J]).Value;
203199
JSONItem.Free;
204200
end
205201
{$ENDIF}
@@ -221,8 +217,7 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
221217
try
222218
for I := 0 to pred(aForm.ComponentCount) do
223219
if aForm.Components[I] is TEdit then
224-
JSONTela.AddPair(TEdit(aForm.Components[I]).Name,
225-
TEdit(aForm.Components[I]).Text)
220+
JSONTela.AddPair(TEdit(aForm.Components[I]).Name, TEdit(aForm.Components[I]).Text)
226221
else if aForm.Components[I] is TComboBox then
227222
JSONTela.AddPair(TComboBox(aForm.Components[I]).Name,
228223
TComboBox(aForm.Components[I]).ItemIndex)
@@ -234,8 +229,8 @@ procedure TSQLiteConfig.SaveForm(aForm: TForm);
234229
JSONTela.AddPair(TDateEdit(aForm.Components[I]).Name,
235230
TDateEdit(aForm.Components[I]).Text)
236231
else if aForm.Components[I] is TSwitch then
237-
JSONTela.AddPair(TSwitch(aForm.Components[I]).Name,
238-
TSwitch(aForm.Components[I]).IsChecked)
232+
JSONTela.AddPair(TSwitch(aForm.Components[I]).Name, TSwitch(aForm.Components[I])
233+
.IsChecked)
239234
{$ENDIF}
240235
else if aForm.Components[I] is TCheckBox then
241236
JSONTela.AddPair(TCheckBox(aForm.Components[I]).Name,
@@ -271,24 +266,14 @@ procedure TSQLiteConfig.UpdateConfig(aJSON: TJSONObject);
271266
// {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5"}
272267
// aJSON.Pairs[i].JSONString.Value = "key1",
273268
// aJSON.Pairs[i].JSONValue.Value = "value1";
274-
for I := 0 to aJSON.Count - 1 do
275-
with FDataSet do
276-
begin
277-
Close;
278-
SQL.Clear;
279-
SQL.Add('SELECT CFG_Key, CFG_Value');
280-
SQL.Add(' FROM Config');
281-
SQL.Add(' WHERE CFG_Key = :CFG_Key');
282-
ParamByName('CFG_Key').AsString := aJSON.Pairs[I].JsonString.Value;
283-
Open;
284-
Edit;
285-
Fields.Fields[0].AsString := aJSON.Pairs[I].JsonString.Value;
286-
Fields.Fields[1].AsString := aJSON.Pairs[I].JsonValue.ToString;
287-
Post;
288-
if FDataSet.CachedUpdates then
289-
ApplyUpdates;
290-
Close;
291-
end;
269+
270+
for I := 0 to pred(aJSON.Count) do
271+
begin
272+
if aJSON.Pairs[I].JsonValue is TJSONObject then
273+
UpdateConfig(aJSON.Pairs[I].JsonString.Value, aJSON.Pairs[I].JsonValue.ToJSON)
274+
else
275+
UpdateConfig(aJSON.Pairs[I].JsonString.Value, aJSON.Pairs[I].JsonValue.Value);
276+
end;
292277
end;
293278

294279
procedure TSQLiteConfig.UpdateConfig(aKey, aValue: string);
@@ -297,17 +282,11 @@ procedure TSQLiteConfig.UpdateConfig(aKey, aValue: string);
297282
begin
298283
Close;
299284
SQL.Clear;
300-
SQL.Add('SELECT CFG_Key, CFG_Value');
301-
SQL.Add(' FROM Config');
302-
SQL.Add(' WHERE CFG_Key = :CFG_Key');
303-
ParamByName('CFG_Key').AsString := aKey;
304-
Open;
305-
Edit;
306-
Fields.Fields[0].AsString := aKey;
307-
Fields.Fields[1].AsString := aValue;
308-
Post;
309-
if FDataSet.CachedUpdates then
310-
ApplyUpdates;
285+
SQL.Add('INSERT INTO Config (CFG_KEY, CFG_VALUE) ');
286+
SQL.Add('VALUES (' + QuotedStr(aKey) + ', ' + QuotedStr(aValue) + ') ');
287+
SQL.Add('ON CONFLICT (CFG_KEY) DO UPDATE ');
288+
SQL.Add('SET CFG_VALUE = excluded.CFG_VALUE;');
289+
ExecSQL;
311290
Close;
312291
end;
313292
end;
@@ -316,12 +295,15 @@ function TSQLiteConfig.ValidaBanco: boolean;
316295
begin
317296
Result := False;
318297
try
319-
FDataSet.SQL.Text := 'select count(*) from config';
320-
FDataSet.Open;
298+
try
299+
FDataSet.SQL.Text := 'PRAGMA table_info("Config")';
300+
FDataSet.ExecSQL;
301+
Result := True;
302+
except
303+
Result := False;
304+
end;
305+
finally
321306
FDataSet.Close;
322-
Result := true;
323-
except
324-
Result := False;
325307
end;
326308
end;
327309

@@ -339,14 +321,13 @@ function TSQLiteConfig.Validate: boolean;
339321
Close;
340322
SQL.Clear;
341323
SQL.Add('CREATE TABLE Config(');
342-
SQL.Add(' CFG_ID integer primary key');
343-
SQL.Add(', CFG_Key varchar');
324+
SQL.Add(' CFG_Key varchar not null primary key');
344325
SQL.Add(', CFG_Value varchar');
345326
SQL.Add(');');
346327
ExecSQL;
347328
end;
348329
end;
349-
Result := true;
330+
Result := True;
350331
except
351332
Result := False;
352333
end;

0 commit comments

Comments
 (0)