PageRenderTime 59ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/EndTask/2008.04.16 ?????/Oracle Data Access Components(ODAC) v6/Source/OdacGui.inc

http://xinhaining-dianjianyiqi-tongxunchengxu.googlecode.com/
Pascal | 358 lines | 279 code | 42 blank | 37 comment | 36 complexity | fe36c2d77fcfa5c88eeaf13d28a8d1cf MD5 | raw file
Possible License(s): GPL-3.0
  1. type
  2. { TConnectDialog }
  3. TConnectDialog = class(TCustomConnectDialog)
  4. private
  5. FReadAliases: boolean;
  6. function GetSession: TOraSession;
  7. protected
  8. function GetKeyPath: string; override;
  9. function GetServerStoreName: string; override;
  10. function DefDialogClass: TClass; override;
  11. public
  12. constructor Create(Owner: TComponent); override;
  13. procedure GetServerList(List: TStrings); override;
  14. property Session: TOraSession read GetSession;
  15. published
  16. property ReadAliases: boolean read FReadAliases write FReadAliases default{$IFDEF MSWINDOWS} False {$ENDIF}{$IFDEF LINUX} True {$ENDIF};
  17. property Retries;
  18. property SavePassword;
  19. property StoreLogInfo;
  20. property DialogClass;
  21. property Caption;
  22. property UsernameLabel;
  23. property PasswordLabel;
  24. property ServerLabel;
  25. property ConnectButton;
  26. property CancelButton;
  27. property LabelSet;
  28. end;
  29. function DefConnectDialogClass: TClass;
  30. procedure GetOraServerList(List: TStrings; HomePath: string;
  31. ReadAliases, Direct: boolean; RaiseErrors: boolean = True);
  32. implementation
  33. {$IFDEF MSWINDOWS}
  34. uses
  35. Forms, Messages;
  36. {$ENDIF}
  37. function DefConnectDialogClass: TClass;
  38. begin
  39. Result := TConnectDialog;
  40. end;
  41. type
  42. _TParser = class (TParser)
  43. protected
  44. function IsInlineComment(Pos: integer): boolean; override;
  45. end;
  46. const
  47. KeyPath = '\SOFTWARE\CoreLab\ODAC\';
  48. procedure GetOraServerList(List: TStrings; HomePath: string;
  49. ReadAliases, Direct: boolean; RaiseErrors: boolean = True);
  50. const
  51. sCannotFind = 'Cannot find ';
  52. sError = 'sError';
  53. sTnsAdmin = 'TNS_ADMIN';
  54. {$IFDEF MSWINDOWS}
  55. sOraHomeKey = 'SOFTWARE\ORACLE\';
  56. sNetwork = '\network\admin\';
  57. sNet80 = '\net80\admin\';
  58. sTnsNames = 'tnsnames.ora';
  59. {$ENDIF}
  60. {$IFDEF LINUX}
  61. sTnsNames = '/network/admin/tnsnames.ora';
  62. {$ENDIF}
  63. var
  64. {$IFDEF MSWINDOWS}
  65. Registry: TRegistry;
  66. RegIniFile: TRegIniFile;
  67. ServerLabel: string;
  68. {$ENDIF}
  69. i, j: integer;
  70. IFile, F: TStrings;
  71. Parser: _TParser;
  72. Code: integer;
  73. Text,St,OldSt,Alias: string;
  74. Bracket: integer;
  75. CurrentDir, FileName: string;
  76. function AddPath(Path, FileName: string): string;
  77. begin
  78. if (Path <> '') and (Path[Length(Path)] <> '\') then
  79. Result := Path + '\' + FileName
  80. else
  81. Result := Path + FileName;
  82. end;
  83. begin
  84. if ReadAliases and not Direct then begin
  85. try
  86. if not OCIInited then
  87. OCIInit;
  88. except
  89. if RaiseErrors then
  90. raise;
  91. end;
  92. if HomePath = '' then
  93. HomePath := OracleHomePath;
  94. if HomePath <> '' then
  95. try
  96. F := TStringList.Create;
  97. Parser := _TParser.Create('');
  98. Parser.OmitBlank := True;
  99. Parser.OmitComment := True;
  100. try
  101. {$IFDEF MSWINDOWS}
  102. /// check if TNS_ADMIN environment variable is set
  103. {$IFNDEF CLR}
  104. i := GetEnvironmentVariable(sTnsAdmin, nil, 0);
  105. if i > 0 then begin
  106. SetLength(FileName, i - 1);
  107. GetEnvironmentVariable(sTnsAdmin, PChar(FileName), Length(FileName) + 1);
  108. FileName := AddPath(FileName, sTnsNames);
  109. end
  110. else
  111. {$ENDIF}
  112. FileName := '';
  113. if FileExists(FileName) then
  114. F.LoadFromFile(FileName)
  115. else begin
  116. /// look for TNS_ADMIN variable in registry
  117. RegIniFile := TRegIniFile.Create('');
  118. try
  119. RegIniFile.RootKey := HKEY_LOCAL_MACHINE;
  120. if OracleHomeName = '' then
  121. FileName := AddPath(RegIniFile.ReadString(sOraHomeKey +
  122. OracleHomeKeys[DefaultOracleHome], sTnsAdmin, ''), sTnsNames)
  123. else
  124. for i := 0 to OracleHomeCount - 1 do
  125. if AnsiCompareText(OracleHomeNames[i] , OracleHomeName) = 0 then begin
  126. FileName := AddPath(RegIniFile.ReadString(sOraHomeKey +
  127. OracleHomeKeys[i], sTnsAdmin, ''), sTnsNames);
  128. break;
  129. end;
  130. finally
  131. RegIniFile.Free;
  132. end;
  133. if FileExists(FileName) then
  134. F.LoadFromFile(FileName)
  135. else begin
  136. /// look for tnsnames.ora in Oracle Home
  137. FileName := HomePath + sNetwork + sTnsNames;
  138. if FileExists(FileName) then
  139. F.LoadFromFile(FileName)
  140. else begin
  141. FileName := HomePath + sNet80 + sTnsNames;
  142. if FileExists(FileName) then
  143. F.LoadFromFile(FileName)
  144. end;
  145. end;
  146. end;
  147. {$ENDIF}
  148. {$IFDEF LINUX}
  149. FileName := HomePath + sTnsNames;
  150. if FileExists(FileName) then
  151. F.LoadFromFile(FileName);
  152. {$ENDIF}
  153. IFile := TStringList.Create;
  154. i := 0;
  155. CurrentDir := GetCurrentDir;
  156. SetCurrentDir(ExtractFileDir(FileName));
  157. try
  158. while i < F.Count do begin
  159. if Pos('IFILE', AnsiUpperCase(F[i])) > 0 then begin
  160. FileName := Trim(Copy(F[i], Pos('=',F[i]) + 1, MaxInt));
  161. if FileExists(FileName) then begin
  162. IFile.LoadFromFile(FileName);
  163. F.Delete(i);
  164. for j := IFile.Count - 1 downto 0 do
  165. F.Insert(i, IFile[j]);
  166. Continue;
  167. end;
  168. end;
  169. Inc(i);
  170. end;
  171. finally
  172. SetCurrentDir(CurrentDir);
  173. IFile.Free;
  174. end;
  175. Text := F.Text;
  176. Parser.SetText(PChar(Text));
  177. Code := 0;
  178. St := '';
  179. Alias := '';
  180. Bracket := 0;
  181. List.Clear;
  182. repeat
  183. if (Bracket = 0) and ((Code = lcIdent) or (Code = lcNumber) or (St = '-')) then
  184. if OldSt = '.' then begin
  185. if (AnsiUpperCase(St) <> 'WORLD') or (OCIVersion > 8100) then
  186. Alias := Alias + '.' + St
  187. end
  188. else
  189. Alias := Alias + St;
  190. OldSt := St;
  191. Code := Parser.GetNext(St);
  192. if St = '(' then
  193. Inc(Bracket)
  194. else
  195. if St = ')' then
  196. Dec(Bracket)
  197. else
  198. if (St = '=') and (Bracket = 0) then begin
  199. List.Add(Alias);
  200. Alias := '';
  201. end;
  202. until Code = lcEnd;
  203. finally
  204. Parser.Free;
  205. F.Free;
  206. end;
  207. except
  208. // do not raise parser errors
  209. end;
  210. end
  211. {$IFDEF MSWINDOWS}
  212. else begin
  213. {$IFDEF NET}
  214. if Direct then
  215. ServerLabel := 'NServer'
  216. else
  217. {$ENDIF}
  218. ServerLabel := 'Server';
  219. Registry := TRegistry.Create;
  220. try
  221. if Registry.OpenKey(KeyPath + 'Connect', False) then begin
  222. List.Clear;
  223. i := 1;
  224. while Registry.ValueExists(ServerLabel + IntToStr(i)) do begin
  225. List.Add(Registry.ReadString(ServerLabel + IntToStr(i)));
  226. Inc(i);
  227. end;
  228. Registry.CloseKey;
  229. end;
  230. finally
  231. Registry.Free;
  232. end;
  233. end;
  234. {$ENDIF}
  235. end;
  236. { TConnectDialog }
  237. constructor TConnectDialog.Create(Owner: TComponent);
  238. begin
  239. inherited Create(Owner);
  240. {$IFDEF LINUX}
  241. ReadAliases := True;
  242. {$ENDIF}
  243. end;
  244. function TConnectDialog.DefDialogClass: TClass;
  245. begin
  246. Result := TConnectForm;
  247. end;
  248. function TConnectDialog.GetKeyPath: string;
  249. begin
  250. Result := KeyPath;
  251. end;
  252. function TConnectDialog.GetServerStoreName: string;
  253. begin
  254. {$IFDEF NET}
  255. if Session.Options.Direct then
  256. Result := 'NServer'
  257. else
  258. {$ENDIF}
  259. Result := 'Server';
  260. end;
  261. procedure TConnectDialog.GetServerList(List: TStrings);
  262. var
  263. Direct: boolean;
  264. begin
  265. Direct := False;
  266. if Session <> nil then
  267. Direct := Session.Options.Direct;
  268. GetOraServerList(List, OracleHomePath, ReadAliases, Direct);
  269. end;
  270. function _TParser.IsInlineComment(Pos: integer): boolean;
  271. begin
  272. Result := (TextLength >= Pos + 1) and (Text[Pos] = '#');
  273. end;
  274. function TConnectDialog.GetSession: TOraSession;
  275. begin
  276. Result := TOraSession(Connection);
  277. end;
  278. {$IFDEF MSWINDOWS}
  279. var
  280. CheckIniChange: TWindowHook;
  281. {$IFDEF CLR}
  282. function DoCheckIniChange(var Message: TMessage): Boolean;
  283. var
  284. AApplication: TApplication;
  285. begin
  286. AApplication := Application;
  287. {$ELSE}
  288. function DoCheckIniChange(AApplication: TApplication; var Message: TMessage): Boolean;
  289. begin
  290. {$ENDIF}
  291. Result := False;
  292. if (Message.Msg = WM_WININICHANGE) then
  293. begin
  294. if AApplication.UpdateFormatSettings then
  295. begin
  296. SetThreadLocale(LOCALE_USER_DEFAULT);
  297. OraClasses.GetTimeFormat;
  298. end;
  299. end;
  300. end;
  301. {$ENDIF}
  302. initialization
  303. DefConnectDialogClassProc := DefConnectDialogClass;
  304. {$IFDEF MSWINDOWS}
  305. {$IFDEF CLR}
  306. CheckIniChange := DoCheckIniChange;
  307. {$ELSE}
  308. TMethod(CheckIniChange).Code := @DoCheckIniChange;
  309. TMethod(CheckIniChange).Data := Application;
  310. {$ENDIF}
  311. Application.HookMainWindow(CheckIniChange);
  312. finalization
  313. Application.UnhookMainWindow(CheckIniChange);
  314. {$ENDIF}
  315. end.