2
0

DbConnectionStringBuilder.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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> (StringComparer.InvariantCultureIgnoreCase);
  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 ("Keyword does not exist");
  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 (keyword == null || keyword.Trim () == "")
  150. throw new ArgumentException ("Keyword should not be emtpy");
  151. if (value == null)
  152. throw new ArgumentException ("Keyword not supported", keyword);
  153. if (ContainsKey (keyword)) {
  154. _dictionary [keyword] = value;
  155. } else {
  156. _dictionary.Add (keyword, value);
  157. }
  158. }
  159. [MonoLimitation("useOdbcRules set to true is not supported")]
  160. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value,
  161. bool useOdbcRules)
  162. {
  163. if (useOdbcRules == false) {
  164. AppendKeyValuePair (builder, keyword, value);
  165. } else {
  166. throw new NotImplementedException ();
  167. }
  168. }
  169. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
  170. {
  171. if (builder.Length > 0) {
  172. char lastChar = builder [builder.Length];
  173. if (lastChar != ';' && lastChar != ' ')
  174. builder.Append (';');
  175. else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
  176. builder.Append (';');
  177. }
  178. builder.AppendFormat ("{0}={1}", keyword, value);
  179. }
  180. public virtual void Clear ()
  181. {
  182. _dictionary.Clear ();
  183. }
  184. public virtual bool ContainsKey (string keyword)
  185. {
  186. if (keyword == null)
  187. throw new ArgumentNullException ("Invalid argument", keyword);
  188. return _dictionary.ContainsKey (keyword);
  189. }
  190. public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
  191. {
  192. bool ret = true;
  193. try {
  194. if (Count != connectionStringBuilder.Count)
  195. ret = false;
  196. else {
  197. foreach (string key in Keys) {
  198. if (!this [key].Equals (connectionStringBuilder [key])) {
  199. ret = false;
  200. break;
  201. }
  202. }
  203. }
  204. } catch (ArgumentException e) {
  205. ret = false;
  206. }
  207. return ret;
  208. }
  209. [MonoTODO]
  210. protected virtual void GetProperties (Hashtable propertyDescriptors)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. protected internal void ClearPropertyDescriptors ()
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. public virtual bool Remove (string keyword)
  220. {
  221. return _dictionary.Remove (keyword);
  222. }
  223. public virtual bool ShouldSerialize (string keyword)
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. void ICollection.CopyTo (Array array, int index)
  228. {
  229. if (array == null)
  230. throw new ArgumentNullException ("array");
  231. KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
  232. if (arr == null)
  233. throw new ArgumentException ("Target array type is not compatible with the type of items in the collection");
  234. ((ICollection<KeyValuePair<string, object>>) _dictionary).CopyTo (arr, index);
  235. }
  236. void IDictionary.Add (object keyword, object value)
  237. {
  238. this.Add ((string) keyword, value);
  239. }
  240. bool IDictionary.Contains (object keyword)
  241. {
  242. return ContainsKey ((string) keyword);
  243. }
  244. IDictionaryEnumerator IDictionary.GetEnumerator ()
  245. {
  246. return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
  247. }
  248. void IDictionary.Remove (object keyword)
  249. {
  250. Remove ((string) keyword);
  251. }
  252. IEnumerator IEnumerable.GetEnumerator ()
  253. {
  254. return (IEnumerator) _dictionary.GetEnumerator ();
  255. }
  256. private static object _staticAttributeCollection = null;
  257. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  258. {
  259. object value = _staticAttributeCollection;
  260. if (value == null) {
  261. CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
  262. DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
  263. Attribute [] attrs = {clsAttr, defMemAttr};
  264. value = new AttributeCollection (attrs);
  265. }
  266. System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
  267. return _staticAttributeCollection as AttributeCollection;
  268. }
  269. string ICustomTypeDescriptor.GetClassName ()
  270. {
  271. return this.GetType ().ToString ();
  272. }
  273. string ICustomTypeDescriptor.GetComponentName ()
  274. {
  275. return null;
  276. }
  277. TypeConverter ICustomTypeDescriptor.GetConverter ()
  278. {
  279. return new CollectionConverter ();
  280. }
  281. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  282. {
  283. return null;
  284. }
  285. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  286. {
  287. return null;
  288. }
  289. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  290. {
  291. return null;
  292. }
  293. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  294. {
  295. return EventDescriptorCollection.Empty;
  296. }
  297. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  298. {
  299. return EventDescriptorCollection.Empty;
  300. }
  301. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  302. {
  303. return PropertyDescriptorCollection.Empty;
  304. }
  305. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  306. {
  307. return PropertyDescriptorCollection.Empty;
  308. }
  309. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  310. {
  311. throw new NotImplementedException ();
  312. }
  313. public override string ToString ()
  314. {
  315. return ConnectionString;
  316. }
  317. public virtual bool TryGetValue (string keyword, out object value)
  318. {
  319. // FIXME : not sure, difference between this [keyword] and this method
  320. bool found = ContainsKey (keyword);
  321. if (found)
  322. value = this [keyword];
  323. else
  324. value = null;
  325. return found;
  326. }
  327. #endregion // Public Methods
  328. }
  329. }
  330. #endif // NET_2_0 using