| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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.
|