DbConnectionStringBuilder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
  40. {
  41. #region Fields
  42. Dictionary<string, object> _dictionary = null;
  43. #endregion Fields
  44. #region Constructors
  45. public DbConnectionStringBuilder ()
  46. {
  47. Init ();
  48. }
  49. public DbConnectionStringBuilder (bool useFirstKeyValue)
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. private void Init ()
  54. {
  55. _dictionary = new Dictionary<string, object> ();
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  60. [EditorBrowsable (EditorBrowsableState.Never)]
  61. [Browsable (false)]
  62. [DesignOnly (true)]
  63. public bool BrowsableConnectionString
  64. {
  65. get { throw new NotImplementedException (); }
  66. set { throw new NotImplementedException (); }
  67. }
  68. [RefreshProperties (RefreshProperties.All)]
  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. [Browsable (false)]
  96. public virtual int Count
  97. {
  98. get { return _dictionary.Count; }
  99. }
  100. [Browsable (false)]
  101. public virtual bool IsFixedSize
  102. {
  103. get { return false; }
  104. }
  105. [Browsable (false)]
  106. public bool IsReadOnly
  107. {
  108. get { throw new NotImplementedException (); }
  109. }
  110. [Browsable (false)]
  111. public virtual object this [string keyword]
  112. {
  113. get
  114. {
  115. if (ContainsKey (keyword))
  116. return _dictionary [keyword];
  117. else
  118. throw new ArgumentException ();
  119. }
  120. set { Add (keyword, value); }
  121. }
  122. [Browsable (false)]
  123. public virtual ICollection Keys
  124. {
  125. get { return _dictionary.Keys; }
  126. }
  127. bool ICollection.IsSynchronized
  128. {
  129. get { throw new NotImplementedException (); }
  130. }
  131. object ICollection.SyncRoot
  132. {
  133. get { throw new NotImplementedException (); }
  134. }
  135. object IDictionary.this [object keyword]
  136. {
  137. get { return this [(string) keyword]; }
  138. set { this [(string) keyword] = value; }
  139. }
  140. [Browsable (false)]
  141. public virtual ICollection Values
  142. {
  143. get { return _dictionary.Values; }
  144. }
  145. #endregion // Properties
  146. #region Methods
  147. public void Add (string keyword, object value)
  148. {
  149. if (ContainsKey (keyword)) {
  150. _dictionary [keyword] = value;
  151. } else {
  152. _dictionary.Add (keyword, value);
  153. }
  154. }
  155. [MonoTODO]
  156. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value,
  157. bool useOdbcRules)
  158. {
  159. if (useOdbcRules == false) {
  160. AppendKeyValuePair (builder, keyword, value);
  161. } else {
  162. throw new NotImplementedException ();
  163. }
  164. }
  165. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
  166. {
  167. if (builder.Length > 0) {
  168. char lastChar = builder [builder.Length];
  169. if (lastChar != ';' && lastChar != ' ')
  170. builder.Append (';');
  171. else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
  172. builder.Append (';');
  173. }
  174. builder.AppendFormat ("{0}={1}", keyword, value);
  175. }
  176. public virtual void Clear ()
  177. {
  178. _dictionary.Clear ();
  179. }
  180. public virtual bool ContainsKey (string keyword)
  181. {
  182. return _dictionary.ContainsKey (keyword);
  183. }
  184. public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
  185. {
  186. bool ret = true;
  187. try {
  188. if (Count != connectionStringBuilder.Count)
  189. ret = false;
  190. else {
  191. foreach (string key in Keys) {
  192. if (!this [key].Equals (connectionStringBuilder [key])) {
  193. ret = false;
  194. break;
  195. }
  196. }
  197. }
  198. } catch (ArgumentException e) {
  199. ret = false;
  200. }
  201. return ret;
  202. }
  203. [MonoTODO]
  204. protected virtual void GetProperties (Hashtable propertyDescriptors)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. [MonoTODO]
  209. protected internal void ClearPropertyDescriptors ()
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. public virtual bool Remove (string keyword)
  214. {
  215. return _dictionary.Remove (keyword);
  216. }
  217. public virtual bool ShouldSerialize (string keyword)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. void ICollection.CopyTo (Array array, int index)
  222. {
  223. if (array == null)
  224. throw new ArgumentNullException ("array");
  225. KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
  226. if (arr == null)
  227. throw new ArgumentException ("Target array type is not compatible with the type of items in the collection");
  228. ((ICollection<KeyValuePair<string, object>>) _dictionary).CopyTo (arr, index);
  229. }
  230. void IDictionary.Add (object keyword, object value)
  231. {
  232. this.Add ((string) keyword, value);
  233. }
  234. bool IDictionary.Contains (object keyword)
  235. {
  236. return ContainsKey ((string) keyword);
  237. }
  238. IDictionaryEnumerator IDictionary.GetEnumerator ()
  239. {
  240. return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
  241. }
  242. void IDictionary.Remove (object keyword)
  243. {
  244. Remove ((string) keyword);
  245. }
  246. IEnumerator IEnumerable.GetEnumerator ()
  247. {
  248. return (IEnumerator) _dictionary.GetEnumerator ();
  249. }
  250. private static object _staticAttributeCollection = null;
  251. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  252. {
  253. object value = _staticAttributeCollection;
  254. if (value == null) {
  255. CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
  256. DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
  257. Attribute [] attrs = {clsAttr, defMemAttr};
  258. value = new AttributeCollection (attrs);
  259. }
  260. System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
  261. return _staticAttributeCollection as AttributeCollection;
  262. }
  263. string ICustomTypeDescriptor.GetClassName ()
  264. {
  265. return this.GetType ().ToString ();
  266. }
  267. string ICustomTypeDescriptor.GetComponentName ()
  268. {
  269. return null;
  270. }
  271. TypeConverter ICustomTypeDescriptor.GetConverter ()
  272. {
  273. return new CollectionConverter ();
  274. }
  275. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  276. {
  277. return null;
  278. }
  279. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  280. {
  281. return null;
  282. }
  283. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  284. {
  285. return null;
  286. }
  287. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  288. {
  289. return EventDescriptorCollection.Empty;
  290. }
  291. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  292. {
  293. return EventDescriptorCollection.Empty;
  294. }
  295. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  296. {
  297. return PropertyDescriptorCollection.Empty;
  298. }
  299. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  300. {
  301. return PropertyDescriptorCollection.Empty;
  302. }
  303. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  304. {
  305. throw new NotImplementedException ();
  306. }
  307. public override string ToString ()
  308. {
  309. return ConnectionString;
  310. }
  311. public virtual bool TryGetValue (string keyword, out object value)
  312. {
  313. // FIXME : not sure, difference between this [keyword] and this method
  314. bool found = ContainsKey (keyword);
  315. if (found)
  316. value = this [keyword];
  317. else
  318. value = null;
  319. return found;
  320. }
  321. #endregion // Public Methods
  322. }
  323. }
  324. #endif // NET_2_0 using