unit BaseDataModule; interface uses System.SysUtils, System.Classes, FireDAC.Stan.ExprFuncs, FireDAC.Stan.Intf, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, Data.DB, FireDAC.Comp.Client, FireDAC.Comp.DataSet, InterfaceDataProvider, mybean.core.objects; type TBaseDataModel = class(TDataModule) driverLink: TFDPhysSQLiteDriverLink; FDGUIxWaitCursor1: TFDGUIxWaitCursor; ConnectionManager: TFDManager; private FLoginName: string; { Private declarations } public function GetConnection: TFDCustomConnection; procedure GiveBackConnection(AConnection: TFDCustomConnection); function GetQuery: TFDQuery; procedure GiveBackQuery(AQuery: TFDQuery); procedure Uninstall; procedure Install(ALoginName, ADir: string); end; var BaseDataModel: TBaseDataModel; implementation uses Dialogs, MD5_32, LoggerImport, DataProviderImport; {%CLASSGROUP 'System.Classes.TPersistent'} {$R *.dfm} const SQLITE_CONNECTIONDEFNAME = 'SQLitePool'; { TBaseDataModel } function TBaseDataModel.GetConnection: TFDCustomConnection; begin if ConnectionManager.Active then begin Result := TFDConnection.Create(nil); Result.ConnectionDefName := SQLITE_CONNECTIONDEFNAME; end; end; function TBaseDataModel.GetQuery: TFDQuery; begin if ConnectionManager.Active then begin Result := TFDQuery.Create(nil); Result.Connection := GetConnection; end; end; procedure TBaseDataModel.GiveBackConnection(AConnection: TFDCustomConnection); begin if AConnection = nil then Exit; AConnection.Close; FreeAndNil(AConnection); end; procedure TBaseDataModel.GiveBackQuery(AQuery: TFDQuery); begin if AQuery = nil then Exit; AQuery.Close; GiveBackConnection(AQuery.Connection); FreeAndNil(AQuery); end; procedure TBaseDataModel.Install(ALoginName, ADir: string); var AParams: TStringList; begin if ALoginName = '' then Exit; try if (ConnectionManager <> nil) then begin if SameText(FLoginName, ALoginName) and ConnectionManager.Active then Exit; Uninstall; FLoginName := UpperCase(ALoginName); AParams := TStringList.Create; AParams.Add('DriverID=SQLite'); AParams.Add('Database='+ADir + FLoginName+'.sdb'); AParams.Add('User_Name='+FLoginName); AParams.Add('Password=' + MD5Print(MD5String(FLoginName + 'yy'))); AParams.Add('LockingMode=Normal'); AParams.Add('Pooled=True'); ConnectionManager.AddConnectionDef(SQLITE_CONNECTIONDEFNAME, 'SQLite', AParams); FreeAndNil(AParams); ConnectionManager.Active := True; Debug('Password=' + MD5Print(MD5String(FLoginName + 'yy')), 'TBaseDataModel.Install') end; except on E: Exception do begin if AParams <> nil then FreeAndNil(AParams); ShowMessage(E.Message); end; end; end; procedure TBaseDataModel.Uninstall; begin try if (FLoginName = '') or not ConnectionManager.Active then begin FLoginName := ''; ConnectionManager.Close; Exit; end; GetAppIconProvider.Uninstall; GetMapTeamUsersProvider.Uninstall; GetUsersHashProvider.Uninstall; FLoginName := ''; ConnectionManager.Close; if ConnectionManager.IsConnectionDef(SQLITE_CONNECTIONDEFNAME) then ConnectionManager.DeleteConnectionDef(SQLITE_CONNECTIONDEFNAME); except on E: Exception do begin ShowMessage(E.Message); end; end; end; initialization finalization if (BaseDataModel <> nil) then begin FreeAndNil(BaseDataModel); end; end.