/Gedemin/IBX/IBDatabaseINI.pas

http://gedemin.googlecode.com/ · Pascal · 330 lines · 318 code · 4 blank · 8 comment · 0 complexity · 83c74d5080c606b9d1f4edd484fb528d MD5 · raw file

  1. {*************************************************************}
  2. { }
  3. { Borland Delphi Visual Component Library }
  4. { InterBase Express core components }
  5. { }
  6. { Copyright (c) 2001 Jeff Overcash }
  7. { }
  8. {*************************************************************}
  9. unit IBDatabaseINI;
  10. interface
  11. uses
  12. SysUtils, Windows, Classes, IBDatabase;
  13. const
  14. PathSeparator = '\'; {do not localize}
  15. NL = #13#10; {do not localize}
  16. SWrapLine = '<br>' + NL; {do not localize}
  17. type
  18. TIniFilePathOpt = (ipoPathNone, ipoPathToServer, ipoPathRelative);
  19. TIBDatabaseINI = class(TComponent)
  20. private
  21. FDatabaseName: String;
  22. FDatabase: TIBDatabase;
  23. FPassword: String;
  24. FUsername: String;
  25. FFileName: String;
  26. FSQLRole: String;
  27. FAppPath: TIniFilePathOpt;
  28. FSection: String;
  29. FCharacterSet: String;
  30. procedure SetDatabaseName(const Value: String);
  31. procedure SetDatabase(const Value: TIBDatabase);
  32. procedure SetFileName(const Value: String);
  33. procedure SetPassword(const Value: String);
  34. procedure SetUsername(const Value: String);
  35. { Private declarations }
  36. function GetParam(Name: string): string;
  37. procedure AssignParam(Name, Value: string);
  38. procedure DeleteParam(Name: string);
  39. procedure SetSQLRole(const Value: String);
  40. procedure SetSection(const Value: String);
  41. procedure SetCharacterSet(const Value: String);
  42. protected
  43. { Protected declarations }
  44. procedure Loaded; override;
  45. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  46. public
  47. { Public declarations }
  48. constructor Create(AOwner : Tcomponent); override;
  49. procedure SaveToINI;
  50. procedure ReadFromINI;
  51. procedure WriteToDatabase(ADatabase : TIBDatabase);
  52. procedure ReadFromDatabase;
  53. function IniFileName : string;
  54. published
  55. { Published declarations }
  56. property Database : TIBDatabase read FDatabase write SetDatabase;
  57. property DatabaseName : String read FDatabaseName write SetDatabaseName;
  58. property Username : String read FUsername write SetUsername;
  59. property Password : String read FPassword write SetPassword;
  60. property SQLRole : String read FSQLRole write SetSQLRole;
  61. property CharacterSet : String read FCharacterSet write SetCharacterSet;
  62. property FileName : String read FFileName write SetFileName;
  63. property UseAppPath : TIniFilePathOpt read FAppPath write FAppPath;
  64. property Section : String read FSection write SetSection;
  65. end;
  66. function SlashPath( sPath : string ) : string;
  67. function LocalServerPath( sFile : string = '') : string;
  68. implementation
  69. uses inifiles;
  70. const
  71. SIniDatabase = 'database';
  72. SIniUserName = 'user_name';
  73. SIniPassword = 'password';
  74. SIniSQLRole = 'sql_role';
  75. SIniCharacterSet = 'lc_ctype';
  76. function LocalServerPath(sFile: string): string;
  77. var
  78. FN: array[0..MAX_PATH- 1] of char;
  79. sPath : shortstring;
  80. begin
  81. SetString(sPath, FN, GetModuleFileName(hInstance, FN, SizeOf(FN)));
  82. Result := ExtractFilePath( sPath ) + LowerCase( ExtractFileName( sFile ) );
  83. end;
  84. function SlashPath( sPath : string ) : string;
  85. begin
  86. if ( sPath <> '' ) and ( sPath[ Length( sPath ) ] <> PathSeparator ) then
  87. sPath := sPath + PathSeparator;
  88. Result := sPath;
  89. end;
  90. { TIBDatabaseINI }
  91. procedure TIBDatabaseINI.AssignParam(Name, Value: string);
  92. var
  93. i: Integer;
  94. found: boolean;
  95. begin
  96. found := False;
  97. if FDatabase = nil then
  98. exit;
  99. if Trim(Value) <> '' then
  100. begin
  101. for i := 0 to FDatabase.Params.Count - 1 do
  102. begin
  103. if (Pos(Name, LowerCase(FDatabase.Params.Names[i])) = 1) then {mbcs ok}
  104. begin
  105. FDatabase.Params.Values[FDatabase.Params.Names[i]] := Value;
  106. found := True;
  107. break;
  108. end;
  109. end;
  110. if not found then
  111. FDatabase.Params.Add(Name + '=' + Value);
  112. end
  113. else
  114. DeleteParam(Name);
  115. end;
  116. procedure TIBDatabaseINI.WriteToDatabase(ADatabase: TIBDatabase);
  117. begin
  118. if Assigned(ADatabase) then
  119. begin
  120. if FDatabaseName <> '' then
  121. ADatabase.DatabaseName := FDatabaseName;
  122. if FUserName <> '' then
  123. AssignParam(SIniUserName, FUserName);
  124. if FPassword <> '' then
  125. AssignParam(SIniPassword, FPassword);
  126. if FSQLRole <> '' then
  127. AssignParam(SIniSQLRole, FSQLRole);
  128. if FCharacterSet <> '' then
  129. AssignParam(SIniCharacterSet, FCharacterSet);
  130. end;
  131. end;
  132. procedure TIBDatabaseINI.DeleteParam(Name: string);
  133. var
  134. i: Integer;
  135. begin
  136. if FDatabase = nil then
  137. exit;
  138. for i := 0 to FDatabase.Params.Count - 1 do
  139. begin
  140. if (Pos(Name, LowerCase(FDatabase.Params.Names[i])) = 1) then {mbcs ok}
  141. begin
  142. FDatabase.Params.Delete(i);
  143. break;
  144. end;
  145. end;
  146. end;
  147. function TIBDatabaseINI.GetParam(Name: string): string;
  148. var
  149. i: Integer;
  150. begin
  151. Result := '';
  152. if FDatabase = nil then
  153. exit;
  154. for i := 0 to FDatabase.Params.Count - 1 do
  155. begin
  156. if (Pos(Name, LowerCase(FDatabase.Params.Names[i])) = 1) then {mbcs ok}
  157. begin
  158. Result := FDatabase.Params.Values[FDatabase.Params.Names[i]];
  159. break;
  160. end;
  161. end;
  162. end;
  163. procedure TIBDatabaseINI.Loaded;
  164. begin
  165. inherited;
  166. ReadFromINI;
  167. if Assigned(FDatabase) and ( not ( csDesigning in ComponentState ) ) then
  168. WriteToDatabase(FDatabase);
  169. end;
  170. procedure TIBDatabaseINI.Notification(AComponent: TComponent;
  171. Operation: TOperation);
  172. begin
  173. inherited;
  174. if (AComponent = FDatabase) and (Operation = opRemove) then
  175. FDatabase := nil;
  176. end;
  177. procedure TIBDatabaseINI.ReadFromDatabase;
  178. begin
  179. if Assigned(FDatabase) then
  180. begin
  181. if FDatabase.DatabaseName <> '' then
  182. FDatabaseName := FDatabase.DatabaseName;
  183. if GetParam(SIniUserName) <> '' then
  184. FUserName := GetParam(SIniUserName);
  185. if GetParam(SIniPassword) <> '' then
  186. FPassword := GetParam(SIniPassword);
  187. if GetParam(SIniSQLRole) <> '' then
  188. FSQLRole := GetParam(SIniSQLRole);
  189. if GetParam(SIniCharacterSet) <> '' then
  190. FCharacterSet := GetParam(SIniCharacterSet);
  191. end;
  192. end;
  193. procedure TIBDatabaseINI.ReadFromINI;
  194. var
  195. sININame : String;
  196. begin
  197. sININame := IniFileName;
  198. if sININame = '' then
  199. Exit;
  200. with TIniFile.Create(sININame) do
  201. try
  202. {Do it to the setter so the IBDatabase will be updated if assigned }
  203. if SectionExists(FSection) then
  204. begin
  205. FDatabaseName := ReadString(FSection, SIniDatabase, '' );
  206. FUsername := ReadString(FSection, SIniUserName, '' );
  207. FPassword := ReadString(FSection, SIniPassword, '' );
  208. FSQLRole := ReadString(FSection, SIniSQLRole, '' );
  209. FCharacterSet := ReadString(FSection, SIniCharacterSet, '');
  210. end;
  211. finally
  212. Free;
  213. end;
  214. end;
  215. procedure TIBDatabaseINI.SaveToINI;
  216. begin
  217. if FFileName = '' then
  218. Exit;
  219. with TIniFile.Create(FFileName) do
  220. try
  221. WriteString(FSection, SIniDatabase, FDatabaseName );
  222. WriteString(FSection, SIniUserName, FUsername );
  223. WriteString(FSection, SIniPassword, FPassword );
  224. WriteString(FSection, SIniSQLRole, FSQLRole );
  225. WriteString(FSection, SIniCharacterSet, FCharacterSet);
  226. finally
  227. Free;
  228. end;
  229. end;
  230. procedure TIBDatabaseINI.SetDatabase(const Value: TIBDatabase);
  231. begin
  232. if FDatabase <> Value then
  233. FDatabase := Value;
  234. end;
  235. procedure TIBDatabaseINI.SetDatabaseName(const Value: String);
  236. begin
  237. if FDatabaseName <> Value then
  238. FDatabaseName := Value;
  239. end;
  240. procedure TIBDatabaseINI.SetFileName(const Value: String);
  241. begin
  242. if FFileName <> Value then
  243. begin
  244. FFileName := Value;
  245. ReadFromINI;
  246. end;
  247. end;
  248. procedure TIBDatabaseINI.SetPassword(const Value: String);
  249. begin
  250. if FPassword <> Value then
  251. FPassword := Value;
  252. end;
  253. procedure TIBDatabaseINI.SetSQLRole(const Value: String);
  254. begin
  255. if FSQLRole <> Value then
  256. FSQLRole := Value;
  257. end;
  258. procedure TIBDatabaseINI.SetUsername(const Value: String);
  259. begin
  260. if FUsername <> Value then
  261. FUsername := Value;
  262. end;
  263. constructor TIBDatabaseINI.Create(AOwner: Tcomponent);
  264. begin
  265. inherited;
  266. FSection := 'Database Settings';
  267. FAppPath := ipoPathToServer;
  268. end;
  269. procedure TIBDatabaseINI.SetSection(const Value: String);
  270. begin
  271. if Value = '' then
  272. raise Exception.Create('Section name can not be empty');
  273. FSection := Value;
  274. end;
  275. function TIBDatabaseINI.IniFileName: string;
  276. begin
  277. if FFileName = '' then
  278. Result := ''
  279. else
  280. begin
  281. if FAppPath = ipoPathToServer then
  282. Result := LocalServerPath(FFileName)
  283. else
  284. if FAppPath = ipoPathRelative then
  285. Result := SlashPath(LocalServerPath) + FFileName
  286. else
  287. Result := FFileName;
  288. end;
  289. end;
  290. procedure TIBDatabaseINI.SetCharacterSet(const Value: String);
  291. begin
  292. if FCharacterSet <> Value then
  293. FCharacterSet := Value;
  294. end;
  295. end.