sqldblib.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. unit SQLDBLib;
  2. {
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2022 by Michael van Canney and other members of the
  5. Free Pascal development team
  6. SQLDB Library Loader
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$mode objfpc}{$H+}
  14. interface
  15. uses
  16. Classes, SysUtils, db, sqldb;
  17. Type
  18. { TSQLDBLibraryLoader }
  19. TSQLDBLibraryLoader = Class(TComponent)
  20. private
  21. FCType: String;
  22. FEnabled: Boolean;
  23. FLibraryName: String;
  24. procedure CheckDisabled;
  25. procedure SetCType(const AValue: String);
  26. procedure SetEnabled(AValue: Boolean);
  27. procedure SetLibraryName(const AValue: String);
  28. Protected
  29. Function GetConnectionDef : TConnectionDef;
  30. Procedure Loaded; override;
  31. Procedure SetDefaultLibraryName; virtual;
  32. Public
  33. Procedure LoadLibrary;
  34. Procedure UnloadLibrary;
  35. Published
  36. Property Enabled : Boolean Read FEnabled Write SetEnabled;
  37. Property ConnectionType : String Read FCType Write SetCType;
  38. Property LibraryName : String Read FLibraryName Write SetLibraryName;
  39. end;
  40. implementation
  41. Resourcestring
  42. SErrConnnected = 'This operation is not allowed while the datatabase is loaded';
  43. SErrInvalidConnectionType = 'Invalid connection type : "%s"';
  44. { TSQLDBLibraryLoader }
  45. procedure TSQLDBLibraryLoader.CheckDisabled;
  46. begin
  47. If not (csLoading in ComponentState) and Enabled then
  48. DatabaseError(SErrConnnected,Self);
  49. end;
  50. procedure TSQLDBLibraryLoader.SetCType(const AValue: String);
  51. begin
  52. if FCType=AValue then Exit;
  53. CheckDisabled;
  54. FCType:=AValue;
  55. if (FCType<>'') then
  56. SetDefaultLibraryName;
  57. end;
  58. procedure TSQLDBLibraryLoader.SetEnabled(aValue: Boolean);
  59. begin
  60. if FEnabled=AValue then Exit;
  61. if (csLoading in ComponentState) then
  62. FEnabled:=AValue
  63. else
  64. If AValue then
  65. LoadLibrary
  66. else
  67. UnloadLibrary;
  68. end;
  69. procedure TSQLDBLibraryLoader.SetLibraryName(const AValue: String);
  70. begin
  71. if FLibraryName=AValue then Exit;
  72. CheckDisabled;
  73. FLibraryName:=AValue;
  74. end;
  75. function TSQLDBLibraryLoader.GetConnectionDef: TConnectionDef;
  76. begin
  77. Result:=sqldb.GetConnectionDef(ConnectionType);
  78. if (Result=Nil) then
  79. DatabaseErrorFmt(SErrInvalidConnectionType,[FCType],Self)
  80. end;
  81. procedure TSQLDBLibraryLoader.Loaded;
  82. begin
  83. inherited;
  84. If FEnabled and (FCType<>'') and (FLibraryName<>'') then
  85. LoadLibrary;
  86. end;
  87. procedure TSQLDBLibraryLoader.SetDefaultLibraryName;
  88. Var
  89. D : TConnectionDef;
  90. begin
  91. D:=GetConnectionDef;
  92. LibraryName:=D.DefaultLibraryName;
  93. end;
  94. procedure TSQLDBLibraryLoader.LoadLibrary;
  95. Var
  96. D : TConnectionDef;
  97. l : TLibraryLoadFunction;
  98. begin
  99. D:=GetConnectionDef;
  100. L:=D.LoadFunction();
  101. if (L<>Nil) then
  102. L(LibraryName);
  103. FEnabled:=True;
  104. end;
  105. procedure TSQLDBLibraryLoader.UnloadLibrary;
  106. Var
  107. D : TConnectionDef;
  108. l : TLibraryUnLoadFunction;
  109. begin
  110. D:=GetConnectionDef;
  111. L:=D.UnLoadFunction;
  112. if L<>Nil then
  113. L;
  114. FEnabled:=False;
  115. end;
  116. end.