DbConnectionStringBuilder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //
  2. // System.Data.Common.DbConnectionStringBuilder.cs
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Text;
  31. using System.Reflection;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Collections.Generic;
  35. using System.Data;
  36. using System.Data.Common;
  37. namespace System.Data.Common
  38. {
  39. [CLSCompliant (true)]
  40. public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable,
  41. IDictionary<string, object>,
  42. ICollection<KeyValuePair<string, object>>,
  43. IEnumerable<KeyValuePair<string, object>>,
  44. ICustomTypeDescriptor
  45. {
  46. #region Fields
  47. Dictionary<string, object> _dictionary = null;
  48. #endregion Fields
  49. #region Constructors
  50. public DbConnectionStringBuilder ()
  51. {
  52. Init ();
  53. }
  54. public DbConnectionStringBuilder (bool useFirstKeyValue)
  55. {
  56. throw new NotImplementedException ();
  57. }
  58. private void Init ()
  59. {
  60. _dictionary = new Dictionary<string, object> ();
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. public bool BrowsableConnectionString
  65. {
  66. get { throw new NotImplementedException (); }
  67. set { throw new NotImplementedException (); }
  68. }
  69. public string ConnectionString
  70. {
  71. get
  72. {
  73. IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
  74. string conn = "";
  75. foreach (string key in dictionary.Keys) {
  76. conn += key + "=" + dictionary [key].ToString () + ";";
  77. }
  78. conn = conn.TrimEnd (';');
  79. return conn;
  80. }
  81. set {
  82. if (value == null)
  83. throw new ArgumentNullException ("ConnectionString cannot be null");
  84. string [] parameters = value.Split (new char [] {';'});
  85. foreach (string args in parameters) {
  86. string [] arg = args.Split (new char [] {'='}, 2);
  87. if (arg.Length == 2) {
  88. string key = arg [0].Trim ().ToUpper ();
  89. string val = arg [1].Trim ();
  90. this [key] = val;
  91. }
  92. }
  93. }
  94. }
  95. public virtual int Count
  96. {
  97. get { return _dictionary.Count; }
  98. }
  99. public virtual bool IsFixedSize
  100. {
  101. get { return false; }
  102. }
  103. public bool IsReadOnly
  104. {
  105. get { throw new NotImplementedException (); }
  106. }
  107. public virtual object this [string keyword]
  108. {
  109. get
  110. {
  111. if (ContainsKey (keyword))
  112. return _dictionary [keyword];
  113. else
  114. throw new ArgumentException ();
  115. }
  116. set { Add (keyword, value); }
  117. }
  118. public virtual ICollection Keys
  119. {
  120. get { return (ICollection) ( (IDictionary <string, object>)_dictionary).Keys; }
  121. }
  122. ICollection<string> IDictionary<string, object>.Keys
  123. {
  124. get { return (ICollection<string>) ( (IDictionary<string, object>) _dictionary).Keys; }
  125. }
  126. ICollection<object> IDictionary<string, object>.Values
  127. {
  128. get { return (ICollection<object>) ( (IDictionary<string, object>)_dictionary).Values; }
  129. }
  130. bool ICollection.IsSynchronized
  131. {
  132. get { throw new NotImplementedException (); }
  133. }
  134. object ICollection.SyncRoot
  135. {
  136. get { throw new NotImplementedException (); }
  137. }
  138. object IDictionary.this [object keyword]
  139. {
  140. get { return this [(string) keyword]; }
  141. set { this [(string) keyword] = value; }
  142. }
  143. public virtual ICollection Values
  144. {
  145. get { return (ICollection) ( (IDictionary<string, object>)_dictionary).Values; }
  146. }
  147. #endregion // Properties
  148. #region Methods
  149. public void Add (string keyword, object value)
  150. {
  151. if (ContainsKey (keyword)) {
  152. _dictionary [keyword] = value;
  153. } else {
  154. _dictionary.Add (keyword, value);
  155. }
  156. }
  157. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
  158. {
  159. if (builder.Length > 0) {
  160. char lastChar = builder [builder.Length];
  161. if (lastChar != ';' && lastChar != ' ')
  162. builder.Append (';');
  163. else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
  164. builder.Append (';');
  165. }
  166. builder.AppendFormat ("{0}={1}", keyword, value);
  167. }
  168. public virtual void Clear ()
  169. {
  170. _dictionary.Clear ();
  171. }
  172. public virtual bool ContainsKey (string keyword)
  173. {
  174. return _dictionary.ContainsKey (keyword);
  175. }
  176. public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
  177. {
  178. bool ret = true;
  179. try {
  180. if (Count != connectionStringBuilder.Count)
  181. ret = false;
  182. else {
  183. foreach (string key in Keys) {
  184. if (!this [key].Equals (connectionStringBuilder [key])) {
  185. ret = false;
  186. break;
  187. }
  188. }
  189. }
  190. } catch (ArgumentException e) {
  191. ret = false;
  192. }
  193. return ret;
  194. }
  195. public virtual bool Remove (string keyword)
  196. {
  197. return _dictionary.Remove (keyword);
  198. }
  199. [Obsolete ("Do not use. Please use the Remove method.")]
  200. public virtual void Reset (string keyword)
  201. {
  202. throw new NotImplementedException ();
  203. }
  204. public virtual bool ShouldSerialize (string keyword)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> keyValuePair)
  209. {
  210. Add (keyValuePair.Key, keyValuePair.Value);
  211. }
  212. bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> keyValuePair)
  213. {
  214. return ContainsKey (keyValuePair.Key);
  215. }
  216. void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int index)
  217. {
  218. if (index + Count > array.Length)
  219. throw new ArgumentException ("The destination does not have enough length!");
  220. foreach (KeyValuePair<string, object> keyValue in this) {
  221. array [index++] = keyValue;
  222. }
  223. }
  224. bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> keyValuePair)
  225. {
  226. return Remove (keyValuePair.Key);
  227. }
  228. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
  229. {
  230. return _dictionary.GetEnumerator ();
  231. }
  232. void ICollection.CopyTo (Array array, int index)
  233. {
  234. KeyValuePair <string, object> [] arr = null;
  235. try {
  236. arr = (KeyValuePair<string, object> []) array;
  237. } catch (InvalidCastException e) {
  238. throw new ArgumentException (
  239. "Target array type is not compatible with the type of items in the collection."
  240. );
  241. }
  242. ICollection<KeyValuePair<string, object>> ptr = (ICollection<KeyValuePair<string, object>>) this;
  243. ptr.CopyTo (arr, index);
  244. }
  245. void IDictionary.Add (object keyword, object value)
  246. {
  247. this.Add ((string) keyword, value);
  248. }
  249. bool IDictionary.Contains (object keyword)
  250. {
  251. return ContainsKey ((string) keyword);
  252. }
  253. IDictionaryEnumerator IDictionary.GetEnumerator ()
  254. {
  255. return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
  256. }
  257. void IDictionary.Remove (object keyword)
  258. {
  259. Remove ((string) keyword);
  260. }
  261. IEnumerator IEnumerable.GetEnumerator ()
  262. {
  263. return (IEnumerator) _dictionary.GetEnumerator ();
  264. }
  265. private static object _staticAttributeCollection = null;
  266. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  267. {
  268. object value = _staticAttributeCollection;
  269. if (value == null) {
  270. CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
  271. DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
  272. Attribute [] attrs = {clsAttr, defMemAttr};
  273. value = new AttributeCollection (attrs);
  274. }
  275. System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
  276. return _staticAttributeCollection as AttributeCollection;
  277. }
  278. string ICustomTypeDescriptor.GetClassName ()
  279. {
  280. return this.GetType ().ToString ();
  281. }
  282. string ICustomTypeDescriptor.GetComponentName ()
  283. {
  284. return null;
  285. }
  286. TypeConverter ICustomTypeDescriptor.GetConverter ()
  287. {
  288. return new CollectionConverter ();
  289. }
  290. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  291. {
  292. return null;
  293. }
  294. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  295. {
  296. return null;
  297. }
  298. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  299. {
  300. return null;
  301. }
  302. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  303. {
  304. return EventDescriptorCollection.Empty;
  305. }
  306. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  307. {
  308. return EventDescriptorCollection.Empty;
  309. }
  310. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  311. {
  312. return PropertyDescriptorCollection.Empty;
  313. }
  314. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  315. {
  316. return PropertyDescriptorCollection.Empty;
  317. }
  318. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  319. {
  320. throw new NotImplementedException ();
  321. }
  322. public override string ToString ()
  323. {
  324. return ConnectionString;
  325. }
  326. public virtual bool TryGetValue (string keyword, out object value)
  327. {
  328. // FIXME : not sure, difference between this [keyword] and this method
  329. bool found = ContainsKey (keyword);
  330. if (found)
  331. value = this [keyword];
  332. else
  333. value = null;
  334. return found;
  335. }
  336. #endregion // Public Methods
  337. }
  338. }
  339. #endif // NET_2_0 using