sqldbrestini.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019 by the Free Pascal development team
  4. SQLDB REST Dispatcher .ini file load/save support.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit sqldbrestini;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, sqldbrestio, sqldbrestauth, sqldbrestbridge, sqldbrestschema, inifiles;
  16. Type
  17. TConnectionIniOption = (scoClearOnRead, // Clear values first
  18. scoSkipPassword, // Do not save/load password
  19. scoSkipMaskPassword, // do not mask the password
  20. scoUserNameAsMask, // use the username as mask for password
  21. scoSkipParams // Do not read/write params.
  22. );
  23. TConnectionIniOptions = Set of TConnectionIniOption;
  24. TSQLDBRestConnectionHelper = class helper for TSQLDBRestConnection
  25. Private
  26. Procedure ClearValues;
  27. Public
  28. Procedure LoadFromIni(Const aIni: TCustomIniFile; aOptions : TConnectionIniOptions = []); overload;
  29. Procedure LoadFromIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TConnectionIniOptions); overload;
  30. Procedure LoadFromFile(Const aFileName : String; aOptions : TConnectionIniOptions = []); overload;
  31. Procedure LoadFromFile(Const aFileName : String; Const ASection : String; aOptions : TConnectionIniOptions); overload;
  32. Procedure SaveToFile(Const aFileName : String; aOptions : TConnectionIniOptions = []);overload;
  33. Procedure SaveToFile(Const aFileName : String; Const ASection : String; aOptions : TConnectionIniOptions = []);overload;
  34. Procedure SaveToIni(Const aIni: TCustomIniFile; aOptions : TConnectionIniOptions = []); overload;
  35. Procedure SaveToIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TConnectionIniOptions); overload;
  36. end;
  37. TDispatcherIniOption = (dioSkipReadConnections, // Do not Read connection definitions
  38. dioSkipExposeConnections, // Do not Expose connections defined in .ini file
  39. dioSkipReadSchemas, // Do not Read schema definitions
  40. dioDisableSchemas, // Do not enable schemas
  41. dioSkipWriteConnections, // Do not write connection definitions
  42. dioSkipWriteSchemas, // Do not Read schema definitions
  43. dioSkipBasicAuth, // Do not read/write basic auth data.
  44. dioSkipStringConfig // Do not read strings config
  45. );
  46. TDispatcherIniOptions = set of TDispatcherIniOption;
  47. { TSQLDBRestDispatcherHelper }
  48. TSQLDBRestDispatcherHelper = class helper for TSQLDBRestDispatcher
  49. private
  50. Public
  51. procedure ReadSchemas(const aIni: TCustomIniFile; ASection: String; aOptions: TDispatcherIniOptions);
  52. procedure ReadConnections(const aIni: TCustomIniFile; ASection: String);
  53. procedure WriteConnections(const aIni: TCustomIniFile; ASection: String; aOptions : TConnectionIniOptions);
  54. procedure WriteSchemas(const aIni: TCustomIniFile; ASection: String; SchemaFileDir : String);
  55. Procedure LoadFromIni(Const aIni: TCustomIniFile; aOptions : TDispatcherIniOptions = []); overload;
  56. Procedure LoadFromIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TDispatcherIniOptions); overload;
  57. Procedure LoadFromFile(Const aFileName : String; aOptions : TDispatcherIniOptions = []); overload;
  58. Procedure LoadFromFile(Const aFileName : String; Const ASection : String; aOptions : TDispatcherIniOptions); overload;
  59. Procedure SaveToFile(Const aFileName : String; aOptions : TDispatcherIniOptions = []);overload;
  60. Procedure SaveToFile(Const aFileName : String; Const ASection : String; aOptions : TDispatcherIniOptions = []);overload;
  61. Procedure SaveToIni(Const aIni: TCustomIniFile; aOptions : TDispatcherIniOptions = []); overload;
  62. Procedure SaveToIni(Const aIni: TCustomIniFile; ASection : String; aOptions : TDispatcherIniOptions); overload;
  63. end;
  64. { TRestStringsConfigHelper }
  65. TRestStringsConfigHelper = class helper for TRestStringsConfig
  66. Public
  67. Procedure LoadFromIni(Const aIni: TCustomIniFile); overload;
  68. Procedure LoadFromIni(Const aIni: TCustomIniFile; ASection : String); overload;
  69. Procedure LoadFromFile(Const aFileName : String); overload;
  70. Procedure LoadFromFile(Const aFileName : String; Const ASection : String); overload;
  71. Procedure SaveToFile(Const aFileName : String);overload;
  72. Procedure SaveToFile(Const aFileName : String; Const ASection : String);overload;
  73. Procedure SaveToIni(Const aIni: TCustomIniFile); overload;
  74. Procedure SaveToIni(Const aIni: TCustomIniFile; ASection : String); overload;
  75. end;
  76. Function StrToOutputOptions(S : String) : TRestOutputOptions;
  77. Function StrToDispatcherOptions(S : String) : TRestDispatcherOptions;
  78. Function StrToConnectionIniOptions(S : String) : TConnectionIniOptions;
  79. Function OutputOptionsToStr(Options : TRestOutputOptions): String;
  80. Function DispatcherOptionsToStr(Options: TRestDispatcherOptions) : String;
  81. Function ConnectionIniOptionsToStr(Options: TConnectionIniOptions): String;
  82. Var
  83. TrivialEncryptKey : String = 'SQLDB';
  84. DefaultConnectionSection : String = 'Connection';
  85. DefaultDispatcherSection : String = 'Dispatcher';
  86. DefaultStringsConfigSection : String = 'Dispatcher_strings';
  87. implementation
  88. uses typinfo,strutils, sqldbrestauthini;
  89. Const
  90. KeyHost = 'Host';
  91. KeyDatabaseName = 'DatabaseName';
  92. KeyUserName = 'UserName';
  93. KeyPassword = 'Password';
  94. KeyPort = 'Port';
  95. keyParams = 'Params';
  96. KeyCharset = 'Charset';
  97. KeyRole = 'Role';
  98. KeyType = 'Type';
  99. KeyConnections = 'Connections';
  100. KeySchemas = 'Schemas';
  101. keyDispatcherOptions = 'DispatcherOptions';
  102. keyOutputOptions = 'OutputOptions';
  103. KeyBasePath = 'BasePath';
  104. KeyDefaultConnection = 'DefaultConnection';
  105. KeyEnforceLimit = 'EnforceLimit';
  106. KeyCORSAllowedOrigins = 'CORSAllowedOrigins';
  107. KeyLoadOptions = 'LoadOptions';
  108. KeyMinFieldOptions = 'MinFieldOptions';
  109. KeyFileName = 'File';
  110. KeyEnabled = 'Enabled';
  111. KeyBasicAuth = 'BasicAuth';
  112. Function StrToOutputOptions(S : String) : TRestOutputOptions;
  113. var
  114. i : integer;
  115. begin
  116. I:=StringToSet(PTypeInfo(TypeInfo(TRestOutputOptions)),S);
  117. Result:=TRestOutputOptions(I);
  118. end;
  119. Function StrToDispatcherOptions(S : String) : TRestDispatcherOptions;
  120. var
  121. i : integer;
  122. begin
  123. I:=StringToSet(PTypeInfo(TypeInfo(TRestDispatcherOptions)),S);
  124. Result:=TRestDispatcherOptions(I);
  125. end;
  126. Function StrToConnectionIniOptions(S : String) : TConnectionIniOptions;
  127. var
  128. i : integer;
  129. begin
  130. I:=StringToSet(PTypeInfo(TypeInfo(TConnectionIniOptions)),S);
  131. Result:=TConnectionIniOptions(I);
  132. end;
  133. Function StrToRestFieldOptions(S : String) : TRestFieldOptions;
  134. var
  135. i : integer;
  136. begin
  137. I:=StringToSet(PTypeInfo(TypeInfo(TRestFieldOptions)),S);
  138. Result:=TRestFieldOptions(I);
  139. end;
  140. Function OutputOptionsToStr(Options : TRestOutputOptions): String;
  141. begin
  142. Result:=SetToString(PTypeInfo(TypeInfo(TRestOutputOptions)),Integer(Options),False);
  143. end;
  144. Function DispatcherOptionsToStr(Options : TRestDispatcherOptions) : String;
  145. begin
  146. Result:=SetToString(PTypeInfo(TypeInfo(TRestDispatcherOptions)),Integer(Options),false);
  147. end;
  148. Function ConnectionIniOptionsToStr(Options : TConnectionIniOptions): String;
  149. begin
  150. Result:=SetToString(PTypeInfo(TypeInfo(TConnectionIniOptions)),Integer(Options),false);
  151. end;
  152. { TRestStringsConfigHelper }
  153. procedure TRestStringsConfigHelper.LoadFromIni(const aIni: TCustomIniFile);
  154. begin
  155. LoadFromIni(aIni,DefaultStringsConfigSection);
  156. end;
  157. procedure TRestStringsConfigHelper.LoadFromIni(const aIni: TCustomIniFile; ASection: String);
  158. Var
  159. T : TRestStringProperty;
  160. N : String;
  161. S : UTF8String;
  162. begin
  163. For T in TRestStringProperty do
  164. begin
  165. Str(T,N);
  166. Delete(N,1,2);
  167. S:=aIni.ReadString(aSection, N, GetRestString(T));
  168. SetRestString(T,S);
  169. end;
  170. end;
  171. procedure TRestStringsConfigHelper.LoadFromFile(const aFileName: String);
  172. begin
  173. LoadFromFile(aFileName,DefaultStringsConfigSection);
  174. end;
  175. procedure TRestStringsConfigHelper.LoadFromFile(const aFileName: String; const ASection: String);
  176. Var
  177. Ini : TCustomIniFile;
  178. begin
  179. Ini:=TMeminiFile.Create(aFileName);
  180. try
  181. LoadFromIni(Ini,aSection);
  182. finally
  183. Ini.Free;
  184. end;
  185. end;
  186. procedure TRestStringsConfigHelper.SaveToFile(const aFileName: String);
  187. begin
  188. SaveToFile(aFileName,DefaultStringsConfigSection);
  189. end;
  190. procedure TRestStringsConfigHelper.SaveToFile(const aFileName: String; const ASection: String);
  191. Var
  192. Ini : TCustomIniFile;
  193. begin
  194. Ini:=TMeminiFile.Create(aFileName);
  195. try
  196. SaveToIni(Ini,aSection);
  197. Ini.UpdateFile;
  198. finally
  199. Ini.Free;
  200. end;
  201. end;
  202. procedure TRestStringsConfigHelper.SaveToIni(const aIni: TCustomIniFile);
  203. begin
  204. SaveToini(aIni,DefaultStringsConfigSection);
  205. end;
  206. procedure TRestStringsConfigHelper.SaveToIni(const aIni: TCustomIniFile; ASection: String);
  207. Var
  208. T : TRestStringProperty;
  209. N : String;
  210. begin
  211. For T in TRestStringProperty do
  212. begin
  213. Str(T,N);
  214. Delete(N,1,2);
  215. aIni.WriteString(aSection, N, GetRestString(T));
  216. end;
  217. end;
  218. { TSQLDBRestDispatcherHelper }
  219. procedure TSQLDBRestDispatcherHelper.LoadFromIni(const aIni: TCustomIniFile; aOptions: TDispatcherIniOptions);
  220. begin
  221. LoadFromIni(aIni,DefaultDispatcherSection,aOptions);
  222. end;
  223. procedure TSQLDBRestDispatcherHelper.ReadConnections(const aIni: TCustomIniFile; ASection: String);
  224. Var
  225. S,L : String;
  226. I : Integer;
  227. C : TSQLDBRestConnection;
  228. CIO : TConnectionIniOptions;
  229. begin
  230. // Read connections
  231. L:=aIni.ReadString(aSection,KeyConnections,'');
  232. For I:=1 to WordCount(L,[',']) do
  233. begin
  234. S:=ExtractWord(I,L,[',']);
  235. C:=Connections.AddConnection('','','','','');
  236. C.Name:=S;
  237. CIO:=StrToConnectionIniOptions(aIni.ReadString(S,KeyLoadOptions,''));
  238. C.LoadFromIni(aIni,S,CIO);
  239. end;
  240. end;
  241. procedure TSQLDBRestDispatcherHelper.WriteConnections(const aIni: TCustomIniFile; ASection: String; aOptions: TConnectionIniOptions);
  242. Var
  243. S,L : String;
  244. I : Integer;
  245. begin
  246. L:='';
  247. for I:=0 to Connections.Count-1 do
  248. begin
  249. if (L<>'') then
  250. L:=L+',';
  251. L:=L+Connections[i].Name;
  252. end;
  253. aIni.WriteString(aSection,KeyConnections,L);
  254. for I:=0 to Connections.Count-1 do
  255. begin
  256. S:=Connections[i].Name;
  257. L:=ConnectionIniOptionsToStr(aOptions);
  258. Connections[i].SaveToIni(aIni,S,aOptions);
  259. aIni.WriteString(S,KeyLoadOptions,L);
  260. end;
  261. end;
  262. procedure TSQLDBRestDispatcherHelper.WriteSchemas(const aIni: TCustomIniFile; ASection: String; SchemaFileDir : String);
  263. Var
  264. S,L,FN : String;
  265. I : Integer;
  266. Sch : TSQLDBRestSchema;
  267. begin
  268. // Read Schemas
  269. L:='';
  270. for I:=0 to Schemas.Count-1 do
  271. if Assigned(Schemas[i].Schema) then
  272. begin
  273. if (L<>'') then
  274. L:=L+',';
  275. L:=L+Schemas[i].Schema.Name;
  276. end;
  277. aIni.WriteString(aSection,KeySchemas,L);
  278. for I:=0 to Schemas.Count-1 do
  279. if Assigned(Schemas[i].Schema) then
  280. begin
  281. S:=Schemas[i].Schema.Name;
  282. Sch:=Schemas[i].Schema;
  283. if (SchemaFileDir<>'') then
  284. FN:=IncludeTrailingPathDelimiter(SchemaFileDir)+S+'.json'
  285. else
  286. FN:='';
  287. aIni.WriteString(S,KeyFileName,FN);
  288. aIni.WriteBool(S,KeyEnabled,Schemas[i].Enabled);
  289. if (FN<>'') then
  290. Sch.SaveToFile(FN);
  291. end;
  292. end;
  293. procedure TSQLDBRestDispatcherHelper.ReadSchemas(const aIni: TCustomIniFile; ASection: String; aOptions: TDispatcherIniOptions);
  294. Var
  295. S,L,FN : String;
  296. I : Integer;
  297. Sch : TSQLDBRestSchema;
  298. SRef : TSQLDBRestSchemaRef;
  299. begin
  300. // Read Schemas
  301. L:=aIni.ReadString(aSection,KeySchemas,'');
  302. For I:=1 to WordCount(L,[',']) do
  303. begin
  304. S:=ExtractWord(I,L,[',']);
  305. Sch:=TSQLDBRestSchema.Create(Self);
  306. Sch.Name:=S;
  307. SRef:=Schemas.AddSchema(Sch);
  308. SRef.Enabled:=aIni.ReadBool(S,KeyEnabled,True);
  309. if (dioDisableSchemas in aOptions) then
  310. SRef.Enabled:=False;
  311. FN:=aIni.ReadString(S,KeyFileName,'');
  312. if (FN<>'') then
  313. Sch.LoadFromFile(FN);
  314. end;
  315. end;
  316. procedure TSQLDBRestDispatcherHelper.LoadFromIni(const aIni: TCustomIniFile; ASection: String; aOptions: TDispatcherIniOptions);
  317. Var
  318. I : Integer;
  319. FO : TRestFieldOptions;
  320. BAN : String;
  321. BA : TRestBasicAuthenticator;
  322. BAO : TBasicAuthIniOptions;
  323. begin
  324. DispatchOptions:=StrToDispatcherOptions(aIni.ReadString(aSection,keyDispatcherOptions,''));
  325. OutputOptions:=StrToOutputOptions(aIni.ReadString(aSection,keyOutputOptions,''));
  326. BasePath:=aIni.ReadString(aSection,KeyBasePath,'');
  327. DefaultConnection:=aIni.ReadString(aSection,KeyDefaultConnection,'');
  328. EnforceLimit:=aIni.ReadInteger(aSection,KeyEnforceLimit,0);
  329. CORSAllowedOrigins:=aIni.ReadString(aSection,KeyCORSAllowedOrigins,'');
  330. if Not (dioSkipReadConnections in aOptions) then
  331. ReadConnections(aIni,aSection);
  332. if Not (dioSkipReadSchemas in aOptions) then
  333. ReadSchemas(aIni,aSection,aOptions);
  334. // Expose connections
  335. if not (dioSkipExposeConnections in aOptions) then
  336. for I:=0 to Connections.Count-1 do
  337. if Connections[i].Enabled then
  338. begin
  339. FO:=StrToRestFieldOptions(aIni.ReadString(Connections[i].Name,KeyMinFieldOptions,''));
  340. ExposeConnection(Connections[i],Nil,FO);
  341. end;
  342. if not (dioSkipBasicAuth in aOptions) then
  343. begin
  344. BAN:=aIni.ReadString(aSection,KeyBasicAuth,'');
  345. if BAN<>'' then
  346. begin
  347. BAO:=StrToBasicAuthIniOptions(aIni.ReadString(BAN,keyLoadOptions,''));
  348. BA:=TRestBasicAuthenticator.Create(Self);
  349. BA.Name:=BAN;
  350. BA.LoadFromIni(aIni,BAN,BAO);
  351. Self.Authenticator:=BA;
  352. end;
  353. end;
  354. if not (dioSkipStringConfig in aOptions) then
  355. Strings.LoadFromIni(aIni,aSection+'_strings');
  356. end;
  357. procedure TSQLDBRestDispatcherHelper.LoadFromFile(const aFileName: String; aOptions: TDispatcherIniOptions);
  358. begin
  359. Loadfromfile(aFileName,DefaultDispatcherSection,aOptions);
  360. end;
  361. procedure TSQLDBRestDispatcherHelper.LoadFromFile(const aFileName: String; const ASection: String; aOptions: TDispatcherIniOptions);
  362. Var
  363. Ini : TCustomIniFile;
  364. begin
  365. Ini:=TMeminiFile.Create(aFileName);
  366. try
  367. LoadFromIni(Ini,aSection,aOptions);
  368. finally
  369. Ini.Free;
  370. end;
  371. end;
  372. procedure TSQLDBRestDispatcherHelper.SaveToFile(const aFileName: String; aOptions: TDispatcherIniOptions);
  373. begin
  374. SaveTofile(aFileName,DefaultDispatcherSection,aOptions);
  375. end;
  376. procedure TSQLDBRestDispatcherHelper.SaveToFile(const aFileName: String; const ASection: String; aOptions: TDispatcherIniOptions);
  377. Var
  378. Ini : TCustomIniFile;
  379. begin
  380. Ini:=TMeminiFile.Create(aFileName);
  381. try
  382. SaveToIni(Ini,aSection,aOptions);
  383. Ini.UpdateFile;
  384. finally
  385. Ini.Free;
  386. end;
  387. end;
  388. procedure TSQLDBRestDispatcherHelper.SaveToIni(const aIni: TCustomIniFile; aOptions: TDispatcherIniOptions);
  389. begin
  390. SaveToIni(aIni,DefaultDispatcherSection,aOptions);
  391. end;
  392. procedure TSQLDBRestDispatcherHelper.SaveToIni(const aIni: TCustomIniFile; ASection: String; aOptions: TDispatcherIniOptions);
  393. Var
  394. BAN : String;
  395. begin
  396. aIni.WriteString(aSection,keyDispatcherOptions,DispatcherOptionsToStr(DispatchOptions));
  397. aIni.WriteString(aSection,keyOutputOptions,OutputOptionsToStr(OutputOptions));
  398. aIni.WriteString(aSection,KeyBasePath,BasePath);
  399. aIni.WriteString(aSection,KeyDefaultConnection,DefaultConnection);
  400. aIni.WriteInteger(aSection,KeyEnforceLimit,EnforceLimit);
  401. aIni.WriteString(aSection,KeyCORSAllowedOrigins,CORSAllowedOrigins);
  402. if Not (dioSkipWriteConnections in aOptions) then
  403. WriteConnections(aIni,aSection,[]);
  404. if Not (dioSkipWriteSchemas in aOptions) then
  405. WriteSchemas(aIni,aSection,ExtractFilePath(ExpandFileName(aIni.FileName)));
  406. if not (dioSkipBasicAuth in aOptions) then
  407. if Assigned(Authenticator) and (Authenticator is TRestBasicAuthenticator) then
  408. begin
  409. BAN:=Authenticator.Name;
  410. if BAN='' then
  411. BAN:=Self.Name+'_basicauth';
  412. TRestBasicAuthenticator(Authenticator).SaveToIni(aIni,BAN,[]);
  413. aIni.WriteString(aSection,KeyBasicAuth,BAN);
  414. end;
  415. if not (dioSkipStringConfig in aOptions) then
  416. Strings.SaveToIni(aIni,aSection+'_strings');
  417. end;
  418. { TSQLDBRestConnectionHelper }
  419. procedure TSQLDBRestConnectionHelper.ClearValues;
  420. begin
  421. HostName:='';
  422. DatabaseName:='';
  423. UserName:='';
  424. Password:='';
  425. CharSet:='';
  426. Params.Clear;
  427. Port:=0;
  428. end;
  429. Const
  430. ForbiddenParamKeys : Array[1..8] of unicodestring
  431. = (keyHost,KeyDatabaseName,KeyUserName,KeyPassword,KeyPort,keyParams,keyCharSet,keyRole);
  432. ParamSeps = [',',';',' '];
  433. procedure TSQLDBRestConnectionHelper.LoadFromIni(const aIni: TCustomIniFile; ASection: String; aOptions: TConnectionIniOptions);
  434. Var
  435. M,N,P : String;
  436. I : integer;
  437. begin
  438. With aIni do
  439. begin
  440. if (scoClearOnRead in aOptions) then
  441. ClearValues;
  442. ConnectionType:=ReadString(ASection,KeyType,'');
  443. HostName:=ReadString(ASection,KeyHost,HostName);
  444. DatabaseName:=ReadString(ASection,KeyDatabaseName,DatabaseName);
  445. UserName:=ReadString(ASection,KeyUserName,UserName);
  446. CharSet:=ReadString(ASection,KeyCharSet,CharSet);
  447. Role:=ReadString(ASection,KeyRole,Role);
  448. Port:=ReadInteger(ASection,KeyPort,Port);
  449. Enabled:=ReadBool(ASection,KeyEnabled,True);
  450. // optional parts
  451. if not (scoSkipPassword in aOptions) then
  452. begin
  453. if scoSkipMaskPassword in aOptions then
  454. P:=ReadString(ASection,KeyPassword,Password)
  455. else
  456. begin
  457. P:=ReadString(ASection,KeyPassword,'');
  458. if (P<>'') then
  459. begin
  460. if scoUserNameAsMask in aOptions then
  461. M:=UserName
  462. else
  463. M:=TrivialEncryptKey;
  464. P:=XorDecode(M,P);
  465. end;
  466. end;
  467. Password:=P;
  468. end;
  469. if not (scoSkipParams in aOptions) then
  470. begin
  471. M:=ReadString(ASection,keyParams,'');
  472. For I:=1 to WordCount(M,ParamSeps) do
  473. begin
  474. N:=ExtractWord(I,M,ParamSeps);
  475. if IndexStr(Utf8Decode(N),ForbiddenParamKeys)=-1 then
  476. begin
  477. P:=ReadString(ASection,N,'');
  478. Params.Values[N]:=P;
  479. end;
  480. end;
  481. end;
  482. end;
  483. end;
  484. procedure TSQLDBRestConnectionHelper.LoadFromIni(const aIni: TCustomIniFile; aOptions: TConnectionIniOptions);
  485. begin
  486. LoadFromIni(aIni,DefaultConnectionSection,aOptions);
  487. end;
  488. procedure TSQLDBRestConnectionHelper.LoadFromFile(const aFileName: String; aOptions: TConnectionIniOptions);
  489. begin
  490. Loadfromfile(aFileName,DefaultConnectionSection,aOptions);
  491. end;
  492. procedure TSQLDBRestConnectionHelper.LoadFromFile(const aFileName: String; const ASection: String; aOptions: TConnectionIniOptions);
  493. Var
  494. Ini : TCustomIniFile;
  495. begin
  496. Ini:=TMeminiFile.Create(aFileName);
  497. try
  498. LoadFromIni(Ini,aSection,aOptions);
  499. finally
  500. Ini.Free;
  501. end;
  502. end;
  503. procedure TSQLDBRestConnectionHelper.SaveToFile(const aFileName: String; aOptions: TConnectionIniOptions);
  504. begin
  505. SaveToFile(aFileName,DefaultConnectionSection,aOptions);
  506. end;
  507. procedure TSQLDBRestConnectionHelper.SaveToFile(const aFileName: String; const ASection: String; aOptions: TConnectionIniOptions);
  508. Var
  509. Ini : TCustomIniFile;
  510. begin
  511. Ini:=TMeminiFile.Create(aFileName);
  512. try
  513. SaveToini(Ini,aSection,aOptions);
  514. Ini.UpdateFile;
  515. finally
  516. Ini.Free;
  517. end;
  518. end;
  519. procedure TSQLDBRestConnectionHelper.SaveToIni(const aIni: TCustomIniFile; aOptions: TConnectionIniOptions);
  520. begin
  521. SaveToIni(aIni,DefaultConnectionSection,aOptions);
  522. end;
  523. procedure TSQLDBRestConnectionHelper.SaveToIni(const aIni: TCustomIniFile; ASection: String; aOptions: TConnectionIniOptions);
  524. Var
  525. M,N,P : String;
  526. I : integer;
  527. begin
  528. With aIni do
  529. begin
  530. WriteString(ASection,KeyHost,HostName);
  531. WriteString(ASection,KeyDatabaseName,DatabaseName);
  532. WriteString(ASection,KeyUserName,UserName);
  533. WriteString(ASection,KeyCharSet,CharSet);
  534. WriteString(ASection,KeyType,ConnectionType);
  535. WriteString(ASection,KeyRole,Role);
  536. WriteInteger(ASection,KeyPort,Port);
  537. WriteBool(ASection,KeyEnabled,Enabled);
  538. if not (scoSkipPassword in aOptions) then
  539. begin
  540. P:=Password;
  541. if Not (scoSkipMaskPassword in aOptions) then
  542. begin
  543. if scoUserNameAsMask in aOptions then
  544. M:=UserName
  545. else
  546. M:=TrivialEncryptKey;
  547. P:=XorEncode(M,P);
  548. end;
  549. WriteString(ASection,KeyPassword,P);
  550. end;
  551. if not (scoSkipParams in aOptions) then
  552. begin
  553. M:='';
  554. for I:=0 to Params.Count-1 do
  555. begin
  556. Params.GetNameValue(I,N,P);
  557. if (N<>'') and (IndexStr(Utf8Decode(N),ForbiddenParamKeys)=-1) then
  558. begin
  559. WriteString(ASection,N,P);
  560. if (M<>'') then
  561. M:=M+',';
  562. M:=M+N;
  563. end;
  564. end;
  565. WriteString(ASection,KeyParams,M);
  566. end;
  567. end;
  568. end;
  569. end.