OracleConnectionStringBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // System.Data.OracleClient.OracleConnectionStringBuilder.cs
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. // Daniel Morgan <[email protected]>
  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.Data;
  36. using System.Data.Common;
  37. using System.ComponentModel;
  38. using System.Globalization;
  39. namespace System.Data.OracleClient
  40. {
  41. [DefaultPropertyAttribute ("DataSource")]
  42. public sealed class OracleConnectionStringBuilder : DbConnectionStringBuilder
  43. {
  44. private const string DEF_DATASOURCE = "";
  45. private const bool DEF_INTEGRATEDSECURITY = false;
  46. private const int DEF_LOADBALANCETIMEOUT = 0;
  47. private const int DEF_MAXPOOLSIZE = 100;
  48. private const int DEF_MINPOOLSIZE = 0;
  49. private const string DEF_PASSWORD = "";
  50. private const bool DEF_PERSISTSECURITYINFO = false;
  51. private const bool DEF_POOLING = true;
  52. private const string DEF_USERID = "";
  53. private const bool DEF_ENLIST = false;
  54. private const bool DEF_UNICODE = false;
  55. private const bool DEF_OMITORACLECONNECTIONNAME = false;
  56. #region // Fields
  57. private string _dataSource;
  58. private bool _enlist;
  59. private bool _integratedSecurity;
  60. private int _loadBalanceTimeout;
  61. private int _maxPoolSize;
  62. private int _minPoolSize;
  63. private string _password;
  64. private bool _persistSecurityInfo;
  65. private bool _pooling;
  66. private string _userID;
  67. private bool _unicode;
  68. private bool _omitOracleConnectionName;
  69. private static Dictionary <string, string> _keywords; // for mapping duplicate keywords
  70. #endregion // Fields
  71. #region Constructors
  72. public OracleConnectionStringBuilder () : this (String.Empty)
  73. {
  74. Init ();
  75. }
  76. public OracleConnectionStringBuilder (string connectionString)
  77. {
  78. Init ();
  79. base.ConnectionString = connectionString;
  80. }
  81. static OracleConnectionStringBuilder ()
  82. {
  83. _keywords = new Dictionary <string, string> ();
  84. _keywords ["DATA SOURCE"] = "Data Source";
  85. _keywords ["SERVER"] = "Data Source";
  86. _keywords ["ADDRESS"] = "Data Source";
  87. _keywords ["ADDR"] = "Data Source";
  88. _keywords ["NETWORK ADDRESS"] = "Data Source";
  89. _keywords ["ENLIST"] = "Enlist";
  90. _keywords ["INTEGRATED SECURITY"] = "Integrated Security";
  91. _keywords ["TRUSTED_CONNECTION"] = "Integrated Security";
  92. _keywords ["MAX POOL SIZE"] = "Max Pool Size";
  93. _keywords ["MIN POOL SIZE"] = "Min Pool Size";
  94. _keywords ["PASSWORD"] = "Password";
  95. _keywords ["PWD"] = "Password";
  96. _keywords ["PERSISTSECURITYINFO"] = "Persist Security Info";
  97. _keywords ["PERSIST SECURITY INFO"] = "Persist Security Info";
  98. _keywords ["POOLING"] = "Pooling";
  99. _keywords ["UID"] = "User Id";
  100. _keywords ["USER"] = "User Id";
  101. _keywords ["USER ID"] = "User Id";
  102. _keywords ["UNICODE"] = "Unicode";
  103. }
  104. #endregion // Constructors
  105. #region Properties
  106. [DisplayNameAttribute ("Data Source")]
  107. [RefreshPropertiesAttribute (RefreshProperties.All)]
  108. public string DataSource {
  109. get { return _dataSource; }
  110. set {
  111. base ["Data Source"] = value;
  112. _dataSource = value;
  113. }
  114. }
  115. [DisplayNameAttribute ("Enlist")]
  116. [RefreshPropertiesAttribute (RefreshProperties.All)]
  117. public bool Enlist {
  118. get { return _enlist; }
  119. set {
  120. base ["Enlist"] = value;
  121. _enlist = value;
  122. }
  123. }
  124. [DisplayNameAttribute ("Integrated Security")]
  125. [RefreshPropertiesAttribute (RefreshProperties.All)]
  126. public bool IntegratedSecurity {
  127. get { return _integratedSecurity; }
  128. set {
  129. base ["Integrated Security"] = value;
  130. _integratedSecurity = value;
  131. }
  132. }
  133. public override bool IsFixedSize {
  134. get { return true; }
  135. }
  136. public override object this [string keyword] {
  137. get {
  138. string mapped = MapKeyword (keyword);
  139. return base [mapped];
  140. }
  141. set {SetValue (keyword, value);}
  142. }
  143. public override ICollection Keys {
  144. get { return base.Keys; }
  145. }
  146. [DisplayNameAttribute ("Load Balance Timeout")]
  147. [RefreshPropertiesAttribute (RefreshProperties.All)]
  148. public int LoadBalanceTimeout {
  149. get { return _loadBalanceTimeout; }
  150. set {
  151. base ["Load Balance Timeout"] = value;
  152. _loadBalanceTimeout = value;
  153. }
  154. }
  155. [DisplayNameAttribute ("Max Pool Size")]
  156. [RefreshPropertiesAttribute (RefreshProperties.All)]
  157. public int MaxPoolSize {
  158. get { return _maxPoolSize; }
  159. set {
  160. base ["Max Pool Size"] = value;
  161. _maxPoolSize = value;
  162. }
  163. }
  164. [DisplayNameAttribute ("Min Pool Size")]
  165. [RefreshPropertiesAttribute (RefreshProperties.All)]
  166. public int MinPoolSize {
  167. get { return _minPoolSize; }
  168. set {
  169. base ["Min Pool Size"] = value;
  170. _minPoolSize = value;
  171. }
  172. }
  173. [DisplayNameAttribute ("Omit Oracle Connection Name")]
  174. [RefreshPropertiesAttribute (RefreshProperties.All)]
  175. public bool OmitOracleConnectionName {
  176. get { return _omitOracleConnectionName; }
  177. set {
  178. base ["Omit Oracle Connection Name"] = value;
  179. _omitOracleConnectionName = value;
  180. }
  181. }
  182. [DisplayNameAttribute ("Password")]
  183. [PasswordPropertyTextAttribute (true)]
  184. [RefreshPropertiesAttribute (RefreshProperties.All)]
  185. public string Password {
  186. get { return _password; }
  187. set {
  188. base ["Password"] = value;
  189. _password = value;
  190. }
  191. }
  192. [DisplayNameAttribute ("Persist Security Info")]
  193. [RefreshPropertiesAttribute (RefreshProperties.All)]
  194. public bool PersistSecurityInfo {
  195. get { return _persistSecurityInfo; }
  196. set {
  197. base ["Persist Security Info"] = value;
  198. _persistSecurityInfo = value;
  199. }
  200. }
  201. [DisplayNameAttribute ("Pooling")]
  202. [RefreshPropertiesAttribute (RefreshProperties.All)]
  203. public bool Pooling {
  204. get { return _pooling; }
  205. set {
  206. base ["Pooling"] = value;
  207. _pooling = value;
  208. }
  209. }
  210. [DisplayNameAttribute ("User ID")]
  211. [RefreshPropertiesAttribute (RefreshProperties.All)]
  212. public string UserID {
  213. get { return _userID; }
  214. set {
  215. base ["User Id"]= value;
  216. _userID = value;
  217. }
  218. }
  219. [DisplayNameAttribute ("Unicode")]
  220. [RefreshPropertiesAttribute (RefreshProperties.All)]
  221. public bool Unicode {
  222. get { return _unicode; }
  223. set {
  224. base ["Unicode"]= value;
  225. _unicode = value;
  226. }
  227. }
  228. public override ICollection Values {
  229. get { return base.Values; }
  230. }
  231. #endregion // Properties
  232. #region Methods
  233. private void Init ()
  234. {
  235. _dataSource = DEF_DATASOURCE;
  236. _enlist = DEF_ENLIST;
  237. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  238. _loadBalanceTimeout = DEF_LOADBALANCETIMEOUT;
  239. _maxPoolSize = DEF_MAXPOOLSIZE;
  240. _minPoolSize = DEF_MINPOOLSIZE;
  241. _password = DEF_PASSWORD;
  242. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  243. _pooling = DEF_POOLING;
  244. _userID = DEF_USERID;
  245. _unicode = DEF_UNICODE;
  246. _omitOracleConnectionName = DEF_OMITORACLECONNECTIONNAME;
  247. }
  248. public override void Clear ()
  249. {
  250. base.Clear ();
  251. Init ();
  252. }
  253. public override bool ContainsKey (string keyword)
  254. {
  255. keyword = keyword.ToUpper ().Trim ();
  256. if (_keywords.ContainsKey (keyword))
  257. return base.ContainsKey (_keywords [keyword]);
  258. return false;
  259. }
  260. public override bool Remove (string keyword)
  261. {
  262. if (!ContainsKey (keyword))
  263. return false;
  264. this [keyword] = null;
  265. return true;
  266. }
  267. [MonoNotSupported ("")] // Note that base.ShouldSerialize() is called but not implemented
  268. public override bool ShouldSerialize (string keyword)
  269. {
  270. if (!ContainsKey (keyword))
  271. return false;
  272. keyword = keyword.ToUpper ().Trim ();
  273. // Assuming passwords cannot be serialized.
  274. if (_keywords [keyword] == "Password")
  275. return false;
  276. return base.ShouldSerialize (_keywords [keyword]);
  277. }
  278. public override bool TryGetValue (string keyword, out object value)
  279. {
  280. if (! ContainsKey (keyword)) {
  281. value = String.Empty;
  282. return false;
  283. }
  284. return base.TryGetValue (_keywords [keyword.ToUpper ().Trim ()], out value);
  285. }
  286. #endregion // Methods
  287. #region Private Methods
  288. private string MapKeyword (string keyword)
  289. {
  290. keyword = keyword.ToUpper ().Trim ();
  291. if (! _keywords.ContainsKey (keyword))
  292. throw new ArgumentException("Keyword not supported :" + keyword);
  293. return _keywords [keyword];
  294. }
  295. private void SetValue (string key, object value)
  296. {
  297. if (key == null)
  298. throw new ArgumentNullException ("key cannot be null!");
  299. string mappedKey = MapKeyword (key);
  300. switch (mappedKey.ToUpper ().Trim ()) {
  301. case "DATA SOURCE" :
  302. if (value == null) {
  303. _dataSource = DEF_DATASOURCE;
  304. base.Remove (mappedKey);
  305. } else
  306. this.DataSource = value.ToString ();
  307. break;
  308. case "ENLIST" :
  309. if (value == null) {
  310. _enlist = DEF_ENLIST;
  311. base.Remove (mappedKey);
  312. } else if ( ! ConvertToBoolean(value))
  313. throw new NotImplementedException("Disabling the automatic"
  314. + " enlistment of connections in the thread's current"
  315. + " transaction context is not implemented.");
  316. break;
  317. case "INTEGRATED SECURITY" :
  318. if (value == null) {
  319. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  320. base.Remove (mappedKey);
  321. } else
  322. this.IntegratedSecurity = ConvertToBoolean (value);
  323. break;
  324. case "MAX POOL SIZE" :
  325. if (value == null) {
  326. _maxPoolSize = DEF_MAXPOOLSIZE;
  327. base.Remove (mappedKey);
  328. } else
  329. this.MaxPoolSize = ConvertToInt32 (value);
  330. break;
  331. case "MIN POOL SIZE" :
  332. if (value == null) {
  333. _minPoolSize = DEF_MINPOOLSIZE;
  334. base.Remove (mappedKey);
  335. } else
  336. this.MinPoolSize = ConvertToInt32 (value);
  337. break;
  338. case "PASSWORD" :
  339. if (value == null) {
  340. _password = DEF_PASSWORD;
  341. base.Remove (mappedKey);
  342. } else
  343. this.Password = value.ToString ();
  344. break;
  345. case "PERSIST SECURITY INFO" :
  346. if (value == null) {
  347. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  348. base.Remove (mappedKey);
  349. } else if (ConvertToBoolean (value))
  350. throw new NotImplementedException ("Persisting security info" +
  351. " is not yet implemented");
  352. break;
  353. case "POOLING" :
  354. if (value == null) {
  355. _pooling = DEF_POOLING;
  356. base.Remove (mappedKey);
  357. } else
  358. this.Pooling = ConvertToBoolean (value);
  359. break;
  360. case "USER ID" :
  361. if (value == null) {
  362. _userID = DEF_USERID;
  363. base.Remove (mappedKey);
  364. } else
  365. this.UserID = value.ToString ();
  366. break;
  367. case "UNICODE" :
  368. if (value == null) {
  369. _unicode = DEF_UNICODE;
  370. base.Remove (mappedKey);
  371. } else
  372. this.Unicode = ConvertToBoolean (value);
  373. break;
  374. case "OMIT ORACLE CONNECTION NAME" :
  375. if (value == null) {
  376. _pooling = DEF_OMITORACLECONNECTIONNAME;
  377. base.Remove (mappedKey);
  378. } else
  379. this.OmitOracleConnectionName = ConvertToBoolean (value);
  380. break;
  381. default :
  382. throw new ArgumentException("Keyword not supported :" + key);
  383. }
  384. }
  385. private static int ConvertToInt32 (object value)
  386. {
  387. return Int32.Parse (value.ToString (), CultureInfo.InvariantCulture);
  388. }
  389. private static bool ConvertToBoolean (object value)
  390. {
  391. if (value == null)
  392. throw new ArgumentNullException ("null value cannot be converted" +
  393. " to boolean");
  394. string upper = value.ToString ().ToUpper ().Trim ();
  395. if (upper == "YES" || upper == "TRUE")
  396. return true;
  397. if (upper == "NO" || upper == "FALSE")
  398. return false;
  399. throw new ArgumentException (String.Format ("Invalid boolean value: {0}",
  400. value.ToString ()));
  401. }
  402. #endregion // Private Methods
  403. }
  404. }
  405. #endif // NET_2_0