PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionStringBuilder.cs

https://bitbucket.org/danipen/mono
C# | 502 lines | 427 code | 45 blank | 30 comment | 56 complexity | 68f007d9bd8954ed32330f62059587ae MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Data.OracleClient.OracleConnectionStringBuilder.cs
  3. //
  4. // Author:
  5. // Sureshkumar T (tsureshkumar@novell.com)
  6. // Daniel Morgan <monodanmorg@yahoo.com>
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. // Copyright (C) 2008 Daniel Morgan
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Text;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Collections.ObjectModel;
  36. using System.Data;
  37. using System.Data.Common;
  38. using System.ComponentModel;
  39. using System.Globalization;
  40. namespace System.Data.OracleClient
  41. {
  42. [DefaultPropertyAttribute ("DataSource")]
  43. public sealed class OracleConnectionStringBuilder : DbConnectionStringBuilder
  44. {
  45. private const string DEF_DATASOURCE = "";
  46. private const bool DEF_INTEGRATEDSECURITY = false;
  47. private const int DEF_LOADBALANCETIMEOUT = 0;
  48. private const int DEF_MAXPOOLSIZE = 100;
  49. private const int DEF_MINPOOLSIZE = 0;
  50. private const string DEF_PASSWORD = "";
  51. private const bool DEF_PERSISTSECURITYINFO = false;
  52. private const bool DEF_POOLING = true;
  53. private const string DEF_USERID = "";
  54. private const bool DEF_ENLIST = false;
  55. private const bool DEF_UNICODE = false;
  56. private const bool DEF_OMITORACLECONNECTIONNAME = false;
  57. #region // Fields
  58. private string _dataSource;
  59. private bool _enlist;
  60. private bool _integratedSecurity;
  61. private int _loadBalanceTimeout;
  62. private int _maxPoolSize;
  63. private int _minPoolSize;
  64. private string _password;
  65. private bool _persistSecurityInfo;
  66. private bool _pooling;
  67. private string _userID;
  68. private bool _unicode;
  69. private bool _omitOracleConnectionName;
  70. private static Dictionary <string, string> _keywords; // for mapping duplicate keywords
  71. private static Dictionary <string, object> _defaults;
  72. #endregion // Fields
  73. #region Constructors
  74. public OracleConnectionStringBuilder () : this (String.Empty)
  75. {
  76. Init ();
  77. }
  78. public OracleConnectionStringBuilder (string connectionString)
  79. {
  80. Init ();
  81. base.ConnectionString = connectionString;
  82. }
  83. static OracleConnectionStringBuilder ()
  84. {
  85. _keywords = new Dictionary <string, string> ();
  86. _keywords ["DATA SOURCE"] = "Data Source";
  87. _keywords ["SERVER"] = "Data Source";
  88. _keywords ["ADDRESS"] = "Data Source";
  89. _keywords ["ADDR"] = "Data Source";
  90. _keywords ["NETWORK ADDRESS"] = "Data Source";
  91. _keywords ["ENLIST"] = "Enlist";
  92. _keywords ["INTEGRATED SECURITY"] = "Integrated Security";
  93. _keywords ["TRUSTED_CONNECTION"] = "Integrated Security";
  94. _keywords ["MAX POOL SIZE"] = "Max Pool Size";
  95. _keywords ["MIN POOL SIZE"] = "Min Pool Size";
  96. _keywords ["PASSWORD"] = "Password";
  97. _keywords ["PWD"] = "Password";
  98. _keywords ["PERSISTSECURITYINFO"] = "Persist Security Info";
  99. _keywords ["PERSIST SECURITY INFO"] = "Persist Security Info";
  100. _keywords ["POOLING"] = "Pooling";
  101. _keywords ["UID"] = "User ID";
  102. _keywords ["USER"] = "User ID";
  103. _keywords ["USER ID"] = "User ID";
  104. _keywords ["UNICODE"] = "Unicode";
  105. _keywords ["LOAD BALANCE TIMEOUT"] = "Load Balance Timeout";
  106. _keywords ["OMIT ORACLE CONNECTION NAME"] = "Omit Oracle Connection Name";
  107. _defaults = new Dictionary <string, object> ();
  108. _defaults.Add("Data Source", DEF_DATASOURCE);
  109. _defaults.Add("Persist Security Info", DEF_PERSISTSECURITYINFO);
  110. _defaults.Add("Integrated Security", DEF_INTEGRATEDSECURITY);
  111. _defaults.Add("User ID", DEF_USERID);
  112. _defaults.Add("Password", DEF_PASSWORD);
  113. _defaults.Add("Enlist", DEF_ENLIST);
  114. _defaults.Add("Pooling", DEF_POOLING);
  115. _defaults.Add("Min Pool Size", DEF_MINPOOLSIZE);
  116. _defaults.Add("Max Pool Size", DEF_MAXPOOLSIZE);
  117. _defaults.Add("Unicode", DEF_UNICODE);
  118. _defaults.Add("Load Balance Timeout", DEF_LOADBALANCETIMEOUT);
  119. _defaults.Add("Omit Oracle Connection Name", DEF_OMITORACLECONNECTIONNAME);
  120. }
  121. #endregion // Constructors
  122. #region Properties
  123. [DisplayNameAttribute ("Data Source")]
  124. [RefreshPropertiesAttribute (RefreshProperties.All)]
  125. public string DataSource {
  126. get { return _dataSource; }
  127. set {
  128. base ["Data Source"] = value;
  129. _dataSource = value;
  130. }
  131. }
  132. [DisplayNameAttribute ("Enlist")]
  133. [RefreshPropertiesAttribute (RefreshProperties.All)]
  134. public bool Enlist {
  135. get { return _enlist; }
  136. set {
  137. base ["Enlist"] = value;
  138. _enlist = value;
  139. }
  140. }
  141. [DisplayNameAttribute ("Integrated Security")]
  142. [RefreshPropertiesAttribute (RefreshProperties.All)]
  143. public bool IntegratedSecurity {
  144. get { return _integratedSecurity; }
  145. set {
  146. base ["Integrated Security"] = value;
  147. _integratedSecurity = value;
  148. }
  149. }
  150. public override bool IsFixedSize {
  151. get { return true; }
  152. }
  153. public override object this [string keyword] {
  154. get {
  155. string mapped = MapKeyword (keyword);
  156. if (base.ContainsKey (mapped))
  157. return base [mapped];
  158. else
  159. return _defaults [mapped];
  160. }
  161. set {SetValue (keyword, value);}
  162. }
  163. public override ICollection Keys {
  164. get {
  165. List<string> keys = new List<string>();
  166. keys.Add("Data Source");
  167. keys.Add("Persist Security Info");
  168. keys.Add("Integrated Security");
  169. keys.Add("User ID");
  170. keys.Add("Password");
  171. keys.Add("Enlist");
  172. keys.Add("Pooling");
  173. keys.Add("Min Pool Size");
  174. keys.Add("Max Pool Size");
  175. keys.Add("Unicode");
  176. keys.Add("Load Balance Timeout");
  177. keys.Add("Omit Oracle Connection Name");
  178. ReadOnlyCollection<string> coll = new ReadOnlyCollection<string>(keys);
  179. return coll;
  180. }
  181. }
  182. [DisplayNameAttribute ("Load Balance Timeout")]
  183. [RefreshPropertiesAttribute (RefreshProperties.All)]
  184. public int LoadBalanceTimeout {
  185. get { return _loadBalanceTimeout; }
  186. set {
  187. base ["Load Balance Timeout"] = value;
  188. _loadBalanceTimeout = value;
  189. }
  190. }
  191. [DisplayNameAttribute ("Max Pool Size")]
  192. [RefreshPropertiesAttribute (RefreshProperties.All)]
  193. public int MaxPoolSize {
  194. get { return _maxPoolSize; }
  195. set {
  196. base ["Max Pool Size"] = value;
  197. _maxPoolSize = value;
  198. }
  199. }
  200. [DisplayNameAttribute ("Min Pool Size")]
  201. [RefreshPropertiesAttribute (RefreshProperties.All)]
  202. public int MinPoolSize {
  203. get { return _minPoolSize; }
  204. set {
  205. base ["Min Pool Size"] = value;
  206. _minPoolSize = value;
  207. }
  208. }
  209. [DisplayNameAttribute ("Omit Oracle Connection Name")]
  210. [RefreshPropertiesAttribute (RefreshProperties.All)]
  211. public bool OmitOracleConnectionName {
  212. get { return _omitOracleConnectionName; }
  213. set {
  214. base ["Omit Oracle Connection Name"] = value;
  215. _omitOracleConnectionName = value;
  216. }
  217. }
  218. [DisplayNameAttribute ("Password")]
  219. [PasswordPropertyTextAttribute (true)]
  220. [RefreshPropertiesAttribute (RefreshProperties.All)]
  221. public string Password {
  222. get { return _password; }
  223. set {
  224. base ["Password"] = value;
  225. _password = value;
  226. }
  227. }
  228. [DisplayNameAttribute ("Persist Security Info")]
  229. [RefreshPropertiesAttribute (RefreshProperties.All)]
  230. public bool PersistSecurityInfo {
  231. get { return _persistSecurityInfo; }
  232. set {
  233. base ["Persist Security Info"] = value;
  234. _persistSecurityInfo = value;
  235. }
  236. }
  237. [DisplayNameAttribute ("Pooling")]
  238. [RefreshPropertiesAttribute (RefreshProperties.All)]
  239. public bool Pooling {
  240. get { return _pooling; }
  241. set {
  242. base ["Pooling"] = value;
  243. _pooling = value;
  244. }
  245. }
  246. [DisplayNameAttribute ("User ID")]
  247. [RefreshPropertiesAttribute (RefreshProperties.All)]
  248. public string UserID {
  249. get { return _userID; }
  250. set {
  251. base ["User Id"]= value;
  252. _userID = value;
  253. }
  254. }
  255. [DisplayNameAttribute ("Unicode")]
  256. [RefreshPropertiesAttribute (RefreshProperties.All)]
  257. public bool Unicode {
  258. get { return _unicode; }
  259. set {
  260. base ["Unicode"]= value;
  261. _unicode = value;
  262. }
  263. }
  264. public override ICollection Values {
  265. get {
  266. List<object> values = new List<object>();
  267. values.Add(_dataSource);
  268. values.Add(_persistSecurityInfo);
  269. values.Add(_integratedSecurity);
  270. values.Add(_userID);
  271. values.Add(_password);
  272. values.Add(_enlist);
  273. values.Add(_pooling);
  274. values.Add(_minPoolSize);
  275. values.Add(_maxPoolSize);
  276. values.Add(_unicode);
  277. values.Add(_loadBalanceTimeout);
  278. values.Add(_omitOracleConnectionName);
  279. ReadOnlyCollection<object> coll = new ReadOnlyCollection<object>(values);
  280. return coll;
  281. }
  282. }
  283. #endregion // Properties
  284. #region Methods
  285. private void Init ()
  286. {
  287. _dataSource = DEF_DATASOURCE;
  288. _enlist = DEF_ENLIST;
  289. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  290. _loadBalanceTimeout = DEF_LOADBALANCETIMEOUT;
  291. _maxPoolSize = DEF_MAXPOOLSIZE;
  292. _minPoolSize = DEF_MINPOOLSIZE;
  293. _password = DEF_PASSWORD;
  294. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  295. _pooling = DEF_POOLING;
  296. _userID = DEF_USERID;
  297. _unicode = DEF_UNICODE;
  298. _omitOracleConnectionName = DEF_OMITORACLECONNECTIONNAME;
  299. }
  300. public override void Clear ()
  301. {
  302. base.Clear ();
  303. Init ();
  304. }
  305. public override bool ContainsKey (string keyword)
  306. {
  307. keyword = keyword.ToUpper ().Trim ();
  308. if (_keywords.ContainsKey (keyword))
  309. return base.ContainsKey (_keywords [keyword]);
  310. return false;
  311. }
  312. public override bool Remove (string keyword)
  313. {
  314. if (!ContainsKey (keyword))
  315. return false;
  316. this [keyword] = null;
  317. return true;
  318. }
  319. [MonoNotSupported ("")] // Note that base.ShouldSerialize() is called but not implemented
  320. public override bool ShouldSerialize (string keyword)
  321. {
  322. if (!ContainsKey (keyword))
  323. return false;
  324. keyword = keyword.ToUpper ().Trim ();
  325. // Assuming passwords cannot be serialized.
  326. if (_keywords [keyword] == "Password")
  327. return false;
  328. return base.ShouldSerialize (_keywords [keyword]);
  329. }
  330. public override bool TryGetValue (string keyword, out object value)
  331. {
  332. if (! ContainsKey (keyword)) {
  333. value = String.Empty;
  334. return false;
  335. }
  336. return base.TryGetValue (_keywords [keyword.ToUpper ().Trim ()], out value);
  337. }
  338. #endregion // Methods
  339. #region Private Methods
  340. private string MapKeyword (string keyword)
  341. {
  342. keyword = keyword.ToUpper ().Trim ();
  343. if (! _keywords.ContainsKey (keyword))
  344. throw new ArgumentException("Keyword not supported :" + keyword);
  345. return _keywords [keyword];
  346. }
  347. private void SetValue (string key, object value)
  348. {
  349. if (key == null)
  350. throw new ArgumentNullException ("key cannot be null!");
  351. string mappedKey = MapKeyword (key);
  352. switch (mappedKey.ToUpper ().Trim ()) {
  353. case "DATA SOURCE" :
  354. if (value == null) {
  355. _dataSource = DEF_DATASOURCE;
  356. base.Remove (mappedKey);
  357. } else
  358. this.DataSource = value.ToString ();
  359. break;
  360. case "ENLIST" :
  361. if (value == null) {
  362. _enlist = DEF_ENLIST;
  363. base.Remove (mappedKey);
  364. } else if ( ! ConvertToBoolean(value))
  365. throw new NotImplementedException("Disabling the automatic"
  366. + " enlistment of connections in the thread's current"
  367. + " transaction context is not implemented.");
  368. break;
  369. case "INTEGRATED SECURITY" :
  370. if (value == null) {
  371. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  372. base.Remove (mappedKey);
  373. } else
  374. this.IntegratedSecurity = ConvertToBoolean (value);
  375. break;
  376. case "MAX POOL SIZE" :
  377. if (value == null) {
  378. _maxPoolSize = DEF_MAXPOOLSIZE;
  379. base.Remove (mappedKey);
  380. } else
  381. this.MaxPoolSize = ConvertToInt32 (value);
  382. break;
  383. case "MIN POOL SIZE" :
  384. if (value == null) {
  385. _minPoolSize = DEF_MINPOOLSIZE;
  386. base.Remove (mappedKey);
  387. } else
  388. this.MinPoolSize = ConvertToInt32 (value);
  389. break;
  390. case "PASSWORD" :
  391. if (value == null) {
  392. _password = DEF_PASSWORD;
  393. base.Remove (mappedKey);
  394. } else
  395. this.Password = value.ToString ();
  396. break;
  397. case "PERSIST SECURITY INFO" :
  398. if (value == null) {
  399. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  400. base.Remove (mappedKey);
  401. } else if (ConvertToBoolean (value))
  402. throw new NotImplementedException ("Persisting security info" +
  403. " is not yet implemented");
  404. break;
  405. case "POOLING" :
  406. if (value == null) {
  407. _pooling = DEF_POOLING;
  408. base.Remove (mappedKey);
  409. } else
  410. this.Pooling = ConvertToBoolean (value);
  411. break;
  412. case "USER ID" :
  413. if (value == null) {
  414. _userID = DEF_USERID;
  415. base.Remove (mappedKey);
  416. } else
  417. this.UserID = value.ToString ();
  418. break;
  419. case "UNICODE" :
  420. if (value == null) {
  421. _unicode = DEF_UNICODE;
  422. base.Remove (mappedKey);
  423. } else
  424. this.Unicode = ConvertToBoolean (value);
  425. break;
  426. case "OMIT ORACLE CONNECTION NAME" :
  427. if (value == null) {
  428. _pooling = DEF_OMITORACLECONNECTIONNAME;
  429. base.Remove (mappedKey);
  430. } else
  431. this.OmitOracleConnectionName = ConvertToBoolean (value);
  432. break;
  433. default :
  434. throw new ArgumentException("Keyword not supported :" + key);
  435. }
  436. }
  437. private static int ConvertToInt32 (object value)
  438. {
  439. return Int32.Parse (value.ToString (), CultureInfo.InvariantCulture);
  440. }
  441. private static bool ConvertToBoolean (object value)
  442. {
  443. if (value == null)
  444. throw new ArgumentNullException ("null value cannot be converted" +
  445. " to boolean");
  446. string upper = value.ToString ().ToUpper ().Trim ();
  447. if (upper == "YES" || upper == "TRUE")
  448. return true;
  449. if (upper == "NO" || upper == "FALSE")
  450. return false;
  451. throw new ArgumentException (String.Format ("Invalid boolean value: {0}",
  452. value.ToString ()));
  453. }
  454. #endregion // Private Methods
  455. }
  456. }
  457. #endif // NET_2_0