//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Microsoft
// Microsoft
//------------------------------------------------------------------------------
namespace System.Data.Common {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
public class DbConnectionStringBuilder : System.Collections.IDictionary, ICustomTypeDescriptor {
// keyword->value currently listed in the connection string
private Dictionary _currentValues;
// cached connectionstring to avoid constant rebuilding
// and to return a user's connectionstring as is until editing occurs
private string _connectionString = "";
private PropertyDescriptorCollection _propertyDescriptors;
private bool _browsableConnectionString = true;
private readonly bool UseOdbcRules;
private static int _objectTypeCount; // Bid counter
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
public DbConnectionStringBuilder() {
}
public DbConnectionStringBuilder(bool useOdbcRules) {
UseOdbcRules = useOdbcRules;
}
private ICollection Collection {
get { return (ICollection)CurrentValues; }
}
private IDictionary Dictionary {
get { return (IDictionary)CurrentValues; }
}
private Dictionary CurrentValues {
get {
Dictionary values = _currentValues;
if (null == values) {
values = new Dictionary(StringComparer.OrdinalIgnoreCase);
_currentValues = values;
}
return values;
}
}
object System.Collections.IDictionary.this[object keyword] {
// delegate to this[string keyword]
get { return this[ObjectToString(keyword)]; }
set { this[ObjectToString(keyword)] = value; }
}
[Browsable(false)]
public virtual object this[string keyword] {
get {
Bid.Trace(" %d#, keyword='%ls'\n", ObjectID, keyword);
ADP.CheckArgumentNull(keyword, "keyword");
object value;
if (CurrentValues.TryGetValue(keyword, out value)) {
return value;
}
throw ADP.KeywordNotSupported(keyword);
}
set {
ADP.CheckArgumentNull(keyword, "keyword");
bool flag = false;
if (null != value) {
string keyvalue = DbConnectionStringBuilderUtil.ConvertToString(value);
DbConnectionOptions.ValidateKeyValuePair(keyword, keyvalue);
flag = CurrentValues.ContainsKey(keyword);
// store keyword/value pair
CurrentValues[keyword] = keyvalue;
}
else {
flag = Remove(keyword);
}
_connectionString = null;
if (flag) {
_propertyDescriptors = null;
}
}
}
[Browsable(false)]
[DesignOnly(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
public bool BrowsableConnectionString {
get {
return _browsableConnectionString;
}
set {
_browsableConnectionString = value;
_propertyDescriptors = null;
}
}
[RefreshPropertiesAttribute(RefreshProperties.All)]
[ResCategoryAttribute(Res.DataCategory_Data)]
[ResDescriptionAttribute(Res.DbConnectionString_ConnectionString)]
public string ConnectionString {
get {
Bid.Trace(" %d#\n", ObjectID);
string connectionString = _connectionString;
if (null == connectionString) {
StringBuilder builder = new StringBuilder();
foreach(string keyword in Keys) {
object value;
if (ShouldSerialize(keyword) && TryGetValue(keyword, out value)) {
string keyvalue = ConvertValueToString(value);
AppendKeyValuePair(builder, keyword, keyvalue, UseOdbcRules);
}
}
connectionString = builder.ToString();
_connectionString = connectionString;
}
return connectionString;
}
set {
Bid.Trace(" %d#\n", ObjectID);
DbConnectionOptions constr = new DbConnectionOptions(value, null, UseOdbcRules);
string originalValue = ConnectionString;
Clear();
try {
for(NameValuePair pair = constr.KeyChain; null != pair; pair = pair.Next) {
if (null != pair.Value) {
this[pair.Name] = pair.Value;
}
else {
Remove(pair.Name);
}
}
_connectionString = null;
}
catch(ArgumentException) { // restore original string
ConnectionString = originalValue;
_connectionString = originalValue;
throw;
}
}
}
[Browsable(false)]
public virtual int Count {
get { return CurrentValues.Count; }
}
[Browsable(false)]
public bool IsReadOnly {
get { return false; }
}
[Browsable(false)]
public virtual bool IsFixedSize {
get { return false; }
}
bool System.Collections.ICollection.IsSynchronized {
get { return Collection.IsSynchronized; }
}
[Browsable(false)]
public virtual ICollection Keys {
get {
Bid.Trace(" %d#\n", ObjectID);
return Dictionary.Keys;
}
}
internal int ObjectID {
get {
return _objectID;
}
}
object System.Collections.ICollection.SyncRoot {
get { return Collection.SyncRoot; }
}
[Browsable(false)]
public virtual ICollection Values {
get {
Bid.Trace(" %d#\n", ObjectID);
System.Collections.Generic.ICollection keys = (System.Collections.Generic.ICollection)Keys;
System.Collections.Generic.IEnumerator keylist = keys.GetEnumerator();
object[] values = new object[keys.Count];
for(int i = 0; i < values.Length; ++i) {
keylist.MoveNext();
values[i] = this[keylist.Current];
Debug.Assert(null != values[i], "null value " + keylist.Current);
}
return new System.Data.Common.ReadOnlyCollection