sqldblib.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. unit sqldblib;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, db, sqldb;
  6. Type
  7. { TSQLDBLibraryLoader }
  8. TSQLDBLibraryLoader = Class(TComponent)
  9. private
  10. FCType: String;
  11. FEnabled: Boolean;
  12. FLibraryName: String;
  13. procedure CheckDisabled;
  14. procedure SetCType(AValue: String);
  15. procedure SetEnabled(AValue: Boolean);
  16. procedure SetLibraryName(AValue: String);
  17. Protected
  18. Function GetConnectionDef : TConnectionDef;
  19. Procedure Loaded; override;
  20. Procedure SetDefaultLibraryName; virtual;
  21. Public
  22. Procedure LoadLibrary;
  23. Procedure UnloadLibrary;
  24. Published
  25. Property Enabled : Boolean Read FEnabled Write SetEnabled;
  26. Property ConnectionType : String Read FCType Write SetCType;
  27. Property LibraryName : String Read FLibraryName Write SetLibraryName;
  28. end;
  29. implementation
  30. Resourcestring
  31. SErrConnnected = 'This operation is not allowed while the datatabase is loaded';
  32. SErrInvalidConnectionType = 'Invalid connection type : "%s"';
  33. { TSQLDBLibraryLoader }
  34. procedure TSQLDBLibraryLoader.CheckDisabled;
  35. begin
  36. If Enabled then
  37. DatabaseError(SErrConnnected,Self);
  38. end;
  39. procedure TSQLDBLibraryLoader.SetCType(AValue: String);
  40. begin
  41. if FCType=AValue then Exit;
  42. CheckDisabled;
  43. FCType:=AValue;
  44. if (FCType<>'') then
  45. SetDefaultLibraryName;
  46. end;
  47. procedure TSQLDBLibraryLoader.SetEnabled(AValue: Boolean);
  48. begin
  49. if FEnabled=AValue then Exit;
  50. if (csLoading in ComponentState) then
  51. FEnabled:=AValue
  52. else
  53. If AValue then
  54. LoadLibrary
  55. else
  56. UnloadLibrary;
  57. end;
  58. procedure TSQLDBLibraryLoader.SetLibraryName(AValue: String);
  59. begin
  60. if FLibraryName=AValue then Exit;
  61. CheckDisabled;
  62. FLibraryName:=AValue;
  63. end;
  64. function TSQLDBLibraryLoader.GetConnectionDef: TConnectionDef;
  65. begin
  66. Result:=sqldb.GetConnectionDef(ConnectionType);
  67. if (Result=Nil) then
  68. DatabaseErrorFmt(SErrInvalidConnectionType,[FCType],Self)
  69. end;
  70. procedure TSQLDBLibraryLoader.Loaded;
  71. begin
  72. inherited;
  73. If FEnabled and (FCType<>'') and (FLibraryName<>'') then
  74. LoadLibrary;
  75. end;
  76. procedure TSQLDBLibraryLoader.SetDefaultLibraryName;
  77. Var
  78. D : TConnectionDef;
  79. begin
  80. D:=GetConnectionDef;
  81. LibraryName:=D.DefaultLibraryName;
  82. end;
  83. procedure TSQLDBLibraryLoader.LoadLibrary;
  84. Var
  85. D : TConnectionDef;
  86. l : TLibraryLoadFunction;
  87. begin
  88. D:=GetConnectionDef;
  89. L:=D.LoadFunction();
  90. if (L<>Nil) then
  91. L(LibraryName);
  92. FEnabled:=True;
  93. end;
  94. procedure TSQLDBLibraryLoader.UnloadLibrary;
  95. Var
  96. D : TConnectionDef;
  97. l : TLibraryUnLoadFunction;
  98. begin
  99. D:=GetConnectionDef;
  100. L:=D.UnLoadFunction;
  101. if L<>Nil then
  102. L;
  103. FEnabled:=False;
  104. end;
  105. end.