Advertisement
HEX0x29A

uSQLiteIni

Jul 7th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.13 KB | None | 0 0
  1. unit uSQLiteIni;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, System.SysUtils, System.UITypes, Vcl.Forms, System.Classes, System.Variants,
  7.   FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Stan.Def,
  8.   FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Stan.ExprFuncs,
  9.   FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt, Data.DB, FireDAC.Comp.Client;
  10.  
  11. const
  12.   DBJOURNAL_POSTFIX = '-journal';
  13.  
  14. type
  15.   TSQLiteIni = class
  16.   private
  17.     fConnection: TFDConnection;
  18.   public
  19.     constructor Create(const FileName: string; const Password: string = '');
  20.     destructor Destroy; override;
  21.     function Read(const Section, Variable: string; const DefaultValue: Variant): Variant;
  22.     procedure Write(const Section, Variable: string; const Value: Variant);
  23.   end;
  24.  
  25. implementation
  26.  
  27. uses
  28.   uCommon;
  29.  
  30. procedure nop;
  31. asm
  32. end;
  33.  
  34. constructor TSQLiteIni.Create(const FileName: string; const Password: string = '');
  35. begin
  36.   if FileExists(FileName + DBJOURNAL_POSTFIX) then
  37.     DeleteFile(FileName + DBJOURNAL_POSTFIX);
  38.   if not DirectoryExists(ExtractFileDir(FileName)) then
  39.     ForceDirectories(ExtractFileDir(FileName));
  40.   fConnection := TFDConnection.Create(nil);
  41.   try
  42.     with fConnection do
  43.     begin
  44.       Params.Database := FileName;
  45.       Params.DriverID := 'SQLite';
  46.       Params.Password := Password;
  47.       LoginPrompt := False;
  48.       Connected := True;
  49.       try
  50.         ExecSQL('VACUUM');
  51.       except
  52.         nop;
  53.       end;
  54.     end;
  55.   except
  56.     if Password.IsEmpty then
  57.       Application.MessageBox(PChar(Format('Невозможно открыть файл "%s". Возможно программа уже запущена.',
  58.         [FileName])), 'Ошибка', MB_OK or MB_ICONERROR)
  59.     else
  60.       Application.MessageBox(PChar(Format('Файл конфигурации не предназначен для данного устройства.'#13#10
  61.         + 'Удалите файл "%s" и повторите попытку.', [IniFileName])), 'Ошибка', MB_OK or
  62.         MB_ICONERROR);
  63.     Halt(0);
  64.   end;
  65. end;
  66.  
  67. destructor TSQLiteIni.Destroy;
  68. begin
  69.   with fConnection do
  70.   begin
  71.     if Connected then
  72.     try
  73.       ExecSQL('VACUUM');
  74.     except
  75.       nop;
  76.     end;
  77.     Connected := False;
  78.   end;
  79.   FreeAndNil(fConnection);
  80.   inherited;
  81. end;
  82.  
  83. function TSQLiteIni.Read(const Section, Variable: string; const DefaultValue: Variant):
  84.   Variant;
  85. begin
  86.   Result := DefaultValue;
  87.   with TFDQuery.Create(nil) do
  88.   try
  89.     Connection := fConnection;
  90.     SQL.Text := Format('SELECT * FROM [sqlite_master] WHERE [name] = ''%s'' and [type] = ''table''',
  91.       [Section]);
  92.     Active := True;
  93.     if RecordCount > 0 then
  94.     begin
  95.       Active := False;
  96.       SQL.Text := Format('SELECT [Value] FROM [%s] WHERE [Variable] = ''%s''', [Section,
  97.         Variable]);
  98.       Active := True;
  99.       if RecordCount > 0 then
  100.       begin
  101.         Result := FieldByName('Value').AsVariant;
  102.       end;
  103.     end;
  104.   finally
  105.     Free;
  106.   end;
  107. end;
  108.  
  109. procedure TSQLiteIni.Write(const Section, Variable: string; const Value: Variant);
  110. begin
  111.   with TFDQuery.Create(nil) do
  112.   try
  113.     Connection := fConnection;
  114.     SQL.Text := Format('SELECT * FROM [sqlite_master] WHERE [name] =''%s'' and [type]=''table''',
  115.       [Section]);
  116.     Active := True;
  117.     if RecordCount <= 0 then
  118.     try
  119.       fConnection.ExecSQL(Format('CREATE TABLE IF NOT EXISTS [%s]([Variable] TEXT PRIMARY KEY NOT NULL, [Value] TEXT);',
  120.         [Section]));
  121.     except
  122.       nop;
  123.     end;
  124.     Active := False;
  125.     SQL.Text := Format('SELECT [Value] FROM [%s] WHERE [Variable] =''%s''', [Section,
  126.       Variable]);
  127.     Active := True;
  128.     if RecordCount > 0 then
  129.     begin
  130.       Active := False;
  131.       SQL.Text := Format('UPDATE [%s] SET [Value] = ''%s'' WHERE [Variable] = ''%s''', [Section,
  132.         Value, Variable]);
  133.       ExecSQL;
  134.     end
  135.     else
  136.     begin
  137.       Active := False;
  138.       SQL.Text := Format('INSERT INTO [%s]([Variable], [Value]) VALUES(''%s'', ''%s'')', [Section,
  139.         Variable, Value]);
  140.       ExecSQL;
  141.     end;
  142.   finally
  143.     Free;
  144.   end;
  145. end;
  146.  
  147. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement