BaseDataModule.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. unit BaseDataModule;
  2. interface
  3. uses
  4. System.SysUtils, System.Classes, FireDAC.Stan.ExprFuncs, FireDAC.Stan.Intf,
  5. FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Stan.Option, FireDAC.Stan.Param,
  6. FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  7. FireDAC.Stan.Async, FireDAC.DApt, FireDAC.UI.Intf, FireDAC.Stan.Def,
  8. FireDAC.Stan.Pool, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, Data.DB,
  9. FireDAC.Comp.Client, FireDAC.Comp.DataSet, InterfaceDataProvider, mybean.core.objects;
  10. type
  11. TBaseDataModel = class(TDataModule)
  12. driverLink: TFDPhysSQLiteDriverLink;
  13. FDGUIxWaitCursor1: TFDGUIxWaitCursor;
  14. ConnectionManager: TFDManager;
  15. private
  16. FLoginName: string;
  17. { Private declarations }
  18. public
  19. function GetConnection: TFDCustomConnection;
  20. procedure GiveBackConnection(AConnection: TFDCustomConnection);
  21. function GetQuery: TFDQuery;
  22. procedure GiveBackQuery(AQuery: TFDQuery);
  23. procedure Uninstall;
  24. procedure Install(ALoginName, ADir: string);
  25. end;
  26. var
  27. BaseDataModel: TBaseDataModel;
  28. implementation
  29. uses
  30. Dialogs, MD5_32, LoggerImport, DataProviderImport;
  31. {%CLASSGROUP 'System.Classes.TPersistent'}
  32. {$R *.dfm}
  33. const
  34. SQLITE_CONNECTIONDEFNAME = 'SQLitePool';
  35. { TBaseDataModel }
  36. function TBaseDataModel.GetConnection: TFDCustomConnection;
  37. begin
  38. if ConnectionManager.Active then
  39. begin
  40. Result := TFDConnection.Create(nil);
  41. Result.ConnectionDefName := SQLITE_CONNECTIONDEFNAME;
  42. end;
  43. end;
  44. function TBaseDataModel.GetQuery: TFDQuery;
  45. begin
  46. if ConnectionManager.Active then
  47. begin
  48. Result := TFDQuery.Create(nil);
  49. Result.Connection := GetConnection;
  50. end;
  51. end;
  52. procedure TBaseDataModel.GiveBackConnection(AConnection: TFDCustomConnection);
  53. begin
  54. if AConnection = nil then
  55. Exit;
  56. AConnection.Close;
  57. FreeAndNil(AConnection);
  58. end;
  59. procedure TBaseDataModel.GiveBackQuery(AQuery: TFDQuery);
  60. begin
  61. if AQuery = nil then
  62. Exit;
  63. AQuery.Close;
  64. GiveBackConnection(AQuery.Connection);
  65. FreeAndNil(AQuery);
  66. end;
  67. procedure TBaseDataModel.Install(ALoginName, ADir: string);
  68. var
  69. AParams: TStringList;
  70. begin
  71. if ALoginName = '' then
  72. Exit;
  73. try
  74. if (ConnectionManager <> nil) then
  75. begin
  76. if SameText(FLoginName, ALoginName) and ConnectionManager.Active then
  77. Exit;
  78. Uninstall;
  79. FLoginName := UpperCase(ALoginName);
  80. AParams := TStringList.Create;
  81. AParams.Add('DriverID=SQLite');
  82. AParams.Add('Database='+ADir + FLoginName+'.sdb');
  83. AParams.Add('User_Name='+FLoginName);
  84. AParams.Add('Password=' + MD5Print(MD5String(FLoginName + 'yy')));
  85. AParams.Add('LockingMode=Normal');
  86. AParams.Add('Pooled=True');
  87. ConnectionManager.AddConnectionDef(SQLITE_CONNECTIONDEFNAME, 'SQLite', AParams);
  88. FreeAndNil(AParams);
  89. ConnectionManager.Active := True;
  90. Debug('Password=' + MD5Print(MD5String(FLoginName + 'yy')), 'TBaseDataModel.Install')
  91. end;
  92. except
  93. on E: Exception do
  94. begin
  95. if AParams <> nil then
  96. FreeAndNil(AParams);
  97. ShowMessage(E.Message);
  98. end;
  99. end;
  100. end;
  101. procedure TBaseDataModel.Uninstall;
  102. begin
  103. try
  104. if (FLoginName = '') or not ConnectionManager.Active then
  105. begin
  106. FLoginName := '';
  107. ConnectionManager.Close;
  108. Exit;
  109. end;
  110. GetAppIconProvider.Uninstall;
  111. GetMapTeamUsersProvider.Uninstall;
  112. GetUsersHashProvider.Uninstall;
  113. FLoginName := '';
  114. ConnectionManager.Close;
  115. if ConnectionManager.IsConnectionDef(SQLITE_CONNECTIONDEFNAME) then
  116. ConnectionManager.DeleteConnectionDef(SQLITE_CONNECTIONDEFNAME);
  117. except
  118. on E: Exception do
  119. begin
  120. ShowMessage(E.Message);
  121. end;
  122. end;
  123. end;
  124. initialization
  125. finalization
  126. if (BaseDataModel <> nil) then
  127. begin
  128. FreeAndNil(BaseDataModel);
  129. end;
  130. end.