DbConnectionStringBuilder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. namespace System.Data.Common
  30. {
  31. using System;
  32. using System.Text;
  33. using System.Reflection;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Collections.Generic;
  37. using System.Data;
  38. using System.Data.Common;
  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 { throw new NotImplementedException (); }
  82. }
  83. public virtual int Count
  84. {
  85. get { return _dictionary.Count; }
  86. }
  87. public virtual bool IsFixedSize
  88. {
  89. get { return false; }
  90. }
  91. public bool IsReadOnly
  92. {
  93. get { throw new NotImplementedException (); }
  94. }
  95. public virtual object this [string keyword]
  96. {
  97. get
  98. {
  99. if (ContainsKey (keyword))
  100. return _dictionary [keyword];
  101. else
  102. throw new ArgumentException ();
  103. }
  104. set { Add (keyword, value); }
  105. }
  106. public virtual ICollection Keys
  107. {
  108. get { return (ICollection) ( (IDictionary <string, object>)_dictionary).Keys; }
  109. }
  110. ICollection<string> IDictionary<string, object>.Keys
  111. {
  112. get { return (ICollection<string>) ( (IDictionary<string, object>) _dictionary).Keys; }
  113. }
  114. ICollection<object> IDictionary<string, object>.Values
  115. {
  116. get { return (ICollection<object>) ( (IDictionary<string, object>)_dictionary).Values; }
  117. }
  118. bool ICollection.IsSynchronized
  119. {
  120. get { throw new NotImplementedException (); }
  121. }
  122. object ICollection.SyncRoot
  123. {
  124. get { throw new NotImplementedException (); }
  125. }
  126. object IDictionary.this [object keyword]
  127. {
  128. get { return this [(string) keyword]; }
  129. set { this [(string) keyword] = value; }
  130. }
  131. public virtual ICollection Values
  132. {
  133. get { return (ICollection) ( (IDictionary<string, object>)_dictionary).Values; }
  134. }
  135. #endregion // Properties
  136. #region Methods
  137. public void Add (string keyword, object value)
  138. {
  139. if (ContainsKey (keyword)) {
  140. _dictionary [keyword] = value;
  141. } else {
  142. _dictionary.Add (keyword, value);
  143. }
  144. }
  145. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
  146. {
  147. if (builder.Length > 0) {
  148. char lastChar = builder [builder.Length];
  149. if (lastChar != ';' && lastChar != ' ')
  150. builder.Append (';');
  151. else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
  152. builder.Append (';');
  153. }
  154. builder.AppendFormat ("{0}={1}", keyword, value);
  155. }
  156. public virtual void Clear ()
  157. {
  158. _dictionary.Clear ();
  159. }
  160. public virtual bool ContainsKey (string keyword)
  161. {
  162. return _dictionary.ContainsKey (keyword);
  163. }
  164. public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
  165. {
  166. bool ret = true;
  167. try {
  168. if (Count != connectionStringBuilder.Count)
  169. ret = false;
  170. else {
  171. foreach (string key in Keys) {
  172. if (!this [key].Equals (connectionStringBuilder [key])) {
  173. ret = false;
  174. break;
  175. }
  176. }
  177. }
  178. } catch (ArgumentException e) {
  179. ret = false;
  180. }
  181. return ret;
  182. }
  183. public virtual bool Remove (string keyword)
  184. {
  185. return _dictionary.Remove (keyword);
  186. }
  187. [Obsolete ("Do not use. Please use the Remove method.")]
  188. public virtual void Reset (string keyword)
  189. {
  190. throw new NotImplementedException ();
  191. }
  192. public virtual bool ShouldSerialize (string keyword)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> keyValuePair)
  197. {
  198. Add (keyValuePair.Key, keyValuePair.Value);
  199. }
  200. bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> keyValuePair)
  201. {
  202. return ContainsKey (keyValuePair.Key);
  203. }
  204. void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int index)
  205. {
  206. if (index + Count > array.Length)
  207. throw new ArgumentException ("The destination does not have enough length!");
  208. foreach (KeyValuePair<string, object> keyValue in this) {
  209. array [index++] = keyValue;
  210. }
  211. }
  212. bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> keyValuePair)
  213. {
  214. return Remove (keyValuePair.Key);
  215. }
  216. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
  217. {
  218. return _dictionary.GetEnumerator ();
  219. }
  220. void ICollection.CopyTo (Array array, int index)
  221. {
  222. KeyValuePair <string, object> [] arr = null;
  223. try {
  224. arr = (KeyValuePair<string, object> []) array;
  225. } catch (InvalidCastException e) {
  226. throw new ArgumentException (
  227. "Target array type is not compatible with the type of items in the collection."
  228. );
  229. }
  230. ICollection<KeyValuePair<string, object>> ptr = (ICollection<KeyValuePair<string, object>>) this;
  231. ptr.CopyTo (arr, index);
  232. }
  233. void IDictionary.Add (object keyword, object value)
  234. {
  235. this.Add ((string) keyword, value);
  236. }
  237. bool IDictionary.Contains (object keyword)
  238. {
  239. return ContainsKey ((string) keyword);
  240. }
  241. IDictionaryEnumerator IDictionary.GetEnumerator ()
  242. {
  243. return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
  244. }
  245. void IDictionary.Remove (object keyword)
  246. {
  247. Remove ((string) keyword);
  248. }
  249. IEnumerator IEnumerable.GetEnumerator ()
  250. {
  251. return (IEnumerator) _dictionary.GetEnumerator ();
  252. }
  253. private static object _staticAttributeCollection = null;
  254. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  255. {
  256. object value = _staticAttributeCollection;
  257. if (value == null) {
  258. CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
  259. DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
  260. Attribute [] attrs = {clsAttr, defMemAttr};
  261. value = new AttributeCollection (attrs);
  262. }
  263. System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
  264. return _staticAttributeCollection as AttributeCollection;
  265. }
  266. string ICustomTypeDescriptor.GetClassName ()
  267. {
  268. return this.GetType ().ToString ();
  269. }
  270. string ICustomTypeDescriptor.GetComponentName ()
  271. {
  272. return null;
  273. }
  274. TypeConverter ICustomTypeDescriptor.GetConverter ()
  275. {
  276. return new CollectionConverter ();
  277. }
  278. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  279. {
  280. return null;
  281. }
  282. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  283. {
  284. return null;
  285. }
  286. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  287. {
  288. return null;
  289. }
  290. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  291. {
  292. return EventDescriptorCollection.Empty;
  293. }
  294. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  295. {
  296. return EventDescriptorCollection.Empty;
  297. }
  298. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  299. {
  300. return PropertyDescriptorCollection.Empty;
  301. }
  302. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  303. {
  304. return PropertyDescriptorCollection.Empty;
  305. }
  306. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  307. {
  308. throw new NotImplementedException ();
  309. }
  310. public override string ToString ()
  311. {
  312. return ConnectionString;
  313. }
  314. public virtual bool TryGetValue (string keyword, out object value)
  315. {
  316. // FIXME : not sure, difference between this [keyword] and this method
  317. bool found = ContainsKey (keyword);
  318. if (found)
  319. value = this [keyword];
  320. else
  321. value = null;
  322. return found;
  323. }
  324. #endregion // Public Methods
  325. }
  326. }
  327. #endif // NET_2_0 using