| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- //
- // System.Data.Common.DbConnectionStringBuilder.cs
- //
- // Author:
- // Sureshkumar T ([email protected])
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Text;
- using System.Reflection;
- using System.Collections;
- using System.ComponentModel;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- namespace System.Data.Common
- {
- [CLSCompliant (true)]
- public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable,
- IDictionary<string, object>,
- ICollection<KeyValuePair<string, object>>,
- IEnumerable<KeyValuePair<string, object>>,
- ICustomTypeDescriptor
- {
- #region Fields
- Dictionary<string, object> _dictionary = null;
- #endregion Fields
- #region Constructors
- public DbConnectionStringBuilder ()
- {
- Init ();
- }
- public DbConnectionStringBuilder (bool useFirstKeyValue)
- {
- throw new NotImplementedException ();
- }
- private void Init ()
- {
- _dictionary = new Dictionary<string, object> ();
- }
- #endregion // Constructors
- #region Properties
- public bool BrowsableConnectionString
- {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- public string ConnectionString
- {
- get
- {
- IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
- string conn = "";
- foreach (string key in dictionary.Keys) {
- conn += key + "=" + dictionary [key].ToString () + ";";
- }
- conn = conn.TrimEnd (';');
- return conn;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ("ConnectionString cannot be null");
-
- string [] parameters = value.Split (new char [] {';'});
- foreach (string args in parameters) {
- string [] arg = args.Split (new char [] {'='}, 2);
- if (arg.Length == 2) {
- string key = arg [0].Trim ().ToUpper ();
- string val = arg [1].Trim ();
- this [key] = val;
- }
- }
- }
- }
- public virtual int Count
- {
- get { return _dictionary.Count; }
- }
- public virtual bool IsFixedSize
- {
- get { return false; }
- }
- public bool IsReadOnly
- {
- get { throw new NotImplementedException (); }
- }
- public virtual object this [string keyword]
- {
- get
- {
- if (ContainsKey (keyword))
- return _dictionary [keyword];
- else
- throw new ArgumentException ();
- }
- set { Add (keyword, value); }
- }
- public virtual ICollection Keys
- {
- get { return (ICollection) ( (IDictionary <string, object>)_dictionary).Keys; }
- }
- ICollection<string> IDictionary<string, object>.Keys
- {
- get { return (ICollection<string>) ( (IDictionary<string, object>) _dictionary).Keys; }
- }
- ICollection<object> IDictionary<string, object>.Values
- {
- get { return (ICollection<object>) ( (IDictionary<string, object>)_dictionary).Values; }
- }
- bool ICollection.IsSynchronized
- {
- get { throw new NotImplementedException (); }
- }
- object ICollection.SyncRoot
- {
- get { throw new NotImplementedException (); }
- }
- object IDictionary.this [object keyword]
- {
- get { return this [(string) keyword]; }
- set { this [(string) keyword] = value; }
- }
- public virtual ICollection Values
- {
- get { return (ICollection) ( (IDictionary<string, object>)_dictionary).Values; }
- }
- #endregion // Properties
- #region Methods
- public void Add (string keyword, object value)
- {
- if (ContainsKey (keyword)) {
- _dictionary [keyword] = value;
- } else {
- _dictionary.Add (keyword, value);
- }
- }
- public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
- {
- if (builder.Length > 0) {
- char lastChar = builder [builder.Length];
- if (lastChar != ';' && lastChar != ' ')
- builder.Append (';');
- else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
- builder.Append (';');
- }
- builder.AppendFormat ("{0}={1}", keyword, value);
- }
- public virtual void Clear ()
- {
- _dictionary.Clear ();
- }
- public virtual bool ContainsKey (string keyword)
- {
- return _dictionary.ContainsKey (keyword);
- }
- public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
- {
- bool ret = true;
- try {
- if (Count != connectionStringBuilder.Count)
- ret = false;
- else {
- foreach (string key in Keys) {
- if (!this [key].Equals (connectionStringBuilder [key])) {
- ret = false;
- break;
- }
- }
- }
- } catch (ArgumentException e) {
- ret = false;
- }
- return ret;
- }
- public virtual bool Remove (string keyword)
- {
- return _dictionary.Remove (keyword);
- }
- [Obsolete ("Do not use. Please use the Remove method.")]
- public virtual void Reset (string keyword)
- {
- throw new NotImplementedException ();
- }
- public virtual bool ShouldSerialize (string keyword)
- {
- throw new NotImplementedException ();
- }
- void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> keyValuePair)
- {
- Add (keyValuePair.Key, keyValuePair.Value);
- }
- bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> keyValuePair)
- {
- return ContainsKey (keyValuePair.Key);
- }
- void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int index)
- {
- if (index + Count > array.Length)
- throw new ArgumentException ("The destination does not have enough length!");
- foreach (KeyValuePair<string, object> keyValue in this) {
- array [index++] = keyValue;
- }
- }
- bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> keyValuePair)
- {
- return Remove (keyValuePair.Key);
- }
- IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
- {
- return _dictionary.GetEnumerator ();
- }
- void ICollection.CopyTo (Array array, int index)
- {
- KeyValuePair <string, object> [] arr = null;
- try {
- arr = (KeyValuePair<string, object> []) array;
- } catch (InvalidCastException e) {
- throw new ArgumentException (
- "Target array type is not compatible with the type of items in the collection."
- );
- }
- ICollection<KeyValuePair<string, object>> ptr = (ICollection<KeyValuePair<string, object>>) this;
- ptr.CopyTo (arr, index);
- }
- void IDictionary.Add (object keyword, object value)
- {
- this.Add ((string) keyword, value);
- }
- bool IDictionary.Contains (object keyword)
- {
- return ContainsKey ((string) keyword);
- }
- IDictionaryEnumerator IDictionary.GetEnumerator ()
- {
- return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
- }
- void IDictionary.Remove (object keyword)
- {
- Remove ((string) keyword);
- }
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return (IEnumerator) _dictionary.GetEnumerator ();
- }
- private static object _staticAttributeCollection = null;
- AttributeCollection ICustomTypeDescriptor.GetAttributes ()
- {
- object value = _staticAttributeCollection;
- if (value == null) {
- CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
- DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
- Attribute [] attrs = {clsAttr, defMemAttr};
- value = new AttributeCollection (attrs);
- }
- System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
- return _staticAttributeCollection as AttributeCollection;
- }
- string ICustomTypeDescriptor.GetClassName ()
- {
- return this.GetType ().ToString ();
- }
- string ICustomTypeDescriptor.GetComponentName ()
- {
- return null;
- }
- TypeConverter ICustomTypeDescriptor.GetConverter ()
- {
- return new CollectionConverter ();
- }
- EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
- {
- return null;
- }
- PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
- {
- return null;
- }
- object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
- {
- return null;
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
- {
- return EventDescriptorCollection.Empty;
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
- {
- return EventDescriptorCollection.Empty;
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
- {
- return PropertyDescriptorCollection.Empty;
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
- {
- return PropertyDescriptorCollection.Empty;
- }
- object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
- {
- throw new NotImplementedException ();
- }
- public override string ToString ()
- {
- return ConnectionString;
- }
- public virtual bool TryGetValue (string keyword, out object value)
- {
- // FIXME : not sure, difference between this [keyword] and this method
- bool found = ContainsKey (keyword);
- if (found)
- value = this [keyword];
- else
- value = null;
- return found;
- }
- #endregion // Public Methods
- }
- }
- #endif // NET_2_0 using
|