DbConnectionStringBuilder.cs 14 KB

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