sqldbini.pp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. unit sqldbini;
  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 ini file support
  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. {$modeswitch typehelpers}
  15. interface
  16. uses
  17. Classes, SysUtils, sqldb, inifiles, strutils;
  18. Type
  19. TSQLDBIniOption = (sioClearOnRead, // Clear values first
  20. sioSkipPassword, // Do not save/load password
  21. sioSkipMaskPassword, // do not mask the password
  22. sioUserNameAsMask, // use the username as mask for password
  23. sioSkipParams // Do not read/write params.
  24. );
  25. TSQLDBIniOptions = set of TSQLDBIniOption;
  26. { TSQLDBIniHelper }
  27. TSQLDBIniHelper = class helper for TSQLConnection
  28. Private
  29. Procedure ClearValues;
  30. Public
  31. Procedure LoadFromIni(Const aIni: TCustomIniFile; aOptions : TSQLDBIniOptions = []); overload;
  32. Procedure LoadFromIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TSQLDBIniOptions); overload;
  33. Procedure LoadFromFile(Const aFileName : String; aOptions : TSQLDBIniOptions = []); overload;
  34. Procedure LoadFromFile(Const aFileName : String; Const ASection : String; aOptions : TSQLDBIniOptions); overload;
  35. Procedure SaveToFile(Const aFileName : String; aOptions : TSQLDBIniOptions = []);overload;
  36. Procedure SaveToFile(Const aFileName : String; Const ASection : String; aOptions : TSQLDBIniOptions = []);overload;
  37. Procedure SaveToIni(Const aIni: TCustomIniFile; aOptions : TSQLDBIniOptions = []); overload;
  38. Procedure SaveToIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TSQLDBIniOptions); overload;
  39. end;
  40. Var
  41. TrivialEncryptKey : String = 'SQLDB';
  42. DefaultSection : String = 'Connection';
  43. implementation
  44. { TSQLDBIniHelper }
  45. procedure TSQLDBIniHelper.ClearValues;
  46. begin
  47. HostName:='';
  48. DatabaseName:='';
  49. UserName:='';
  50. Password:='';
  51. CharSet:='';
  52. Params.Clear;
  53. Port:=0;
  54. end;
  55. Const
  56. KeyHost = 'Host';
  57. KeyDatabaseName = 'DatabaseName';
  58. KeyUserName = 'UserName';
  59. KeyPassword = 'Password';
  60. KeyPort = 'Port';
  61. keyParams = 'Params';
  62. KeyCharset = 'Charset';
  63. KeyRole = 'Role';
  64. Const
  65. ForbiddenParamKeys : Array[1..8] of unicodestring
  66. = (keyHost,KeyDatabaseName,KeyUserName,KeyPassword,KeyPort,keyParams,keyCharSet,keyRole);
  67. ParamSeps = [',',';',' '];
  68. procedure TSQLDBIniHelper.LoadFromIni(const aIni: TCustomIniFile; ASection: String; aOptions: TSQLDBIniOptions);
  69. Var
  70. M,N,P : String;
  71. I : integer;
  72. begin
  73. With aIni do
  74. begin
  75. if (sioClearOnRead in aOptions) then
  76. ClearValues;
  77. HostName:=ReadString(ASection,KeyHost,HostName);
  78. DatabaseName:=ReadString(ASection,KeyDatabaseName,DatabaseName);
  79. UserName:=ReadString(ASection,KeyUserName,UserName);
  80. CharSet:=ReadString(ASection,KeyCharset,CharSet);
  81. Role:=ReadString(ASection,KeyRole,Role);
  82. Port:=ReadInteger(ASection,KeyPort,Port);
  83. // optional parts
  84. if not (sioSkipPassword in aOptions) then
  85. begin
  86. if sioSkipMaskPassword in aOptions then
  87. P:=ReadString(ASection,KeyPassword,Password)
  88. else
  89. begin
  90. P:=ReadString(ASection,KeyPassword,'');
  91. if (P<>'') then
  92. begin
  93. if sioUserNameAsMask in aOptions then
  94. M:=UserName
  95. else
  96. M:=TrivialEncryptKey;
  97. P:=XorDecode(M,P);
  98. end;
  99. end;
  100. Password:=P;
  101. end;
  102. if not (sioSkipParams in aOptions) then
  103. begin
  104. M:=ReadString(ASection,keyParams,'');
  105. For I:=1 to WordCount(M,ParamSeps) do
  106. begin
  107. N:=ExtractWord(I,M,ParamSeps);
  108. if IndexStr(Utf8Decode(N),ForbiddenParamKeys)=-1 then
  109. begin
  110. P:=ReadString(ASection,N,'');
  111. Params.Values[N]:=P;
  112. end;
  113. end;
  114. end;
  115. end;
  116. end;
  117. procedure TSQLDBIniHelper.LoadFromIni(const aIni: TCustomIniFile; aOptions: TSQLDBIniOptions);
  118. begin
  119. LoadFromIni(aIni,Defaultsection,aOptions);
  120. end;
  121. procedure TSQLDBIniHelper.LoadFromFile(const aFileName: String; aOptions: TSQLDBIniOptions);
  122. begin
  123. Loadfromfile(aFileName,DefaultSection,aOptions);
  124. end;
  125. procedure TSQLDBIniHelper.LoadFromFile(const aFileName: String; const ASection: String; aOptions: TSQLDBIniOptions);
  126. Var
  127. Ini : TCustomIniFile;
  128. begin
  129. Ini:=TMeminiFile.Create(aFileName);
  130. try
  131. LoadFromIni(Ini,aSection,aOptions);
  132. finally
  133. Ini.Free;
  134. end;
  135. end;
  136. procedure TSQLDBIniHelper.SaveToFile(const aFileName: String; aOptions: TSQLDBIniOptions);
  137. begin
  138. SaveToFile(aFileName,DefaultSection,aOptions);
  139. end;
  140. procedure TSQLDBIniHelper.SaveToFile(const aFileName: String; const ASection: String; aOptions: TSQLDBIniOptions);
  141. Var
  142. Ini : TCustomIniFile;
  143. begin
  144. Ini:=TMeminiFile.Create(aFileName);
  145. try
  146. SaveToini(Ini,aSection,aOptions);
  147. finally
  148. Ini.Free;
  149. end;
  150. end;
  151. procedure TSQLDBIniHelper.SaveToIni(const aIni: TCustomIniFile; aOptions: TSQLDBIniOptions);
  152. begin
  153. SaveToIni(aIni,DefaultSection,aOptions);
  154. end;
  155. procedure TSQLDBIniHelper.SaveToIni(const aIni: TCustomIniFile; ASection: String; aOptions: TSQLDBIniOptions);
  156. Var
  157. M,N,P : String;
  158. I : integer;
  159. begin
  160. With aIni do
  161. begin
  162. WriteString(ASection,KeyHost,HostName);
  163. WriteString(ASection,KeyDatabaseName,DatabaseName);
  164. WriteString(ASection,KeyUserName,UserName);
  165. WriteString(ASection,KeyCharset,CharSet);
  166. WriteString(ASection,KeyRole,Role);
  167. WriteInteger(ASection,KeyPort,Port);
  168. if not (sioSkipPassword in aOptions) then
  169. begin
  170. P:=Password;
  171. if Not (sioSkipMaskPassword in aOptions) then
  172. begin
  173. if sioUserNameAsMask in aOptions then
  174. M:=UserName
  175. else
  176. M:=TrivialEncryptKey;
  177. P:=XorEncode(M,P);
  178. end;
  179. WriteString(ASection,KeyPassword,P);
  180. end;
  181. if not (sioSkipParams in aOptions) then
  182. begin
  183. M:='';
  184. for I:=0 to Params.Count-1 do
  185. begin
  186. Params.GetNameValue(I,N,P);
  187. if (N<>'') and (IndexStr(Utf8Decode(N),ForbiddenParamKeys)=-1) then
  188. begin
  189. WriteString(ASection,N,P);
  190. if (M<>'') then
  191. M:=M+',';
  192. M:=M+N;
  193. end;
  194. end;
  195. WriteString(ASection,KeyParams,M);
  196. end;
  197. end;
  198. end;
  199. end.