(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / ByteFX.Data / Common / ConnectionString.cs
blob263923effed2c64de508215c4d464904702e217a
1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 using System;
19 using System.Collections.Specialized;
21 namespace ByteFX.Data.Common
23 /// <summary>
24 /// Summary description for Utility.
25 /// </summary>
26 internal class ConnectionString
28 private StringDictionary elements;
29 private string connString;
31 public ConnectionString(string connString)
33 this.connString = connString;
34 elements = new StringDictionary();
35 Parse( connString );
38 public string Value
40 get { return connString; }
43 public string this[string key]
45 get
47 string val = elements[key];
48 return val;
52 public int GetIntOption( string key, int defaultvalue )
54 string val = this[ key ];
55 if (null == val) return defaultvalue;
56 return Convert.ToInt32( val );
59 public bool GetBoolOption( string key, bool defaultvalue )
61 string val = this[ key ];
62 if (null == val) return defaultvalue;
63 val = val.ToLower();
64 if (val == "true" || val == "yes") return true;
65 return false;
68 public bool Contains( string key )
70 return elements.ContainsKey(key);
73 public bool Equals( ConnectionString obj )
75 foreach (string key in elements.Keys)
77 if (! obj.Contains(key)) return false;
78 if ( ! this[key].Equals( obj[key] )) return false;
80 return true;
83 /// <summary>
84 ///
85 /// </summary>
86 /// <param name="obj"></param>
87 /// <returns></returns>
88 public bool PoolingEquals( ConnectionString obj )
90 foreach (string key in elements.Keys)
92 // these connection string elements only affect pooling
93 // so we don't check them when making sure connection strings
94 // are alike
95 if (key.Equals("connection lifetime")) continue;
96 if (key.Equals("connection reset")) continue;
97 if (key.Equals("enlist")) continue;
98 if (key.Equals("max pool size")) continue;
99 if (key.Equals("min pool size")) continue;
100 if (key.Equals("pooling")) continue;
102 if (! obj.Contains(key)) return false;
103 if ( ! this[key].Equals( obj[key] )) return false;
105 return true;
108 /// <summary>
109 ///
110 /// </summary>
111 /// <param name="s"></param>
112 public void Parse( String s )
114 String[] keyvalues = s.Split( ';' );
115 String[] newkeyvalues = new String[keyvalues.Length];
116 int x = 0;
118 elements.Clear();
120 // first run through the array and check for any keys that
121 // have ; in their value
122 foreach (String keyvalue in keyvalues)
124 // check for trailing ; at the end of the connection string
125 if (keyvalue.Length == 0) continue;
127 // this value has an '=' sign so we are ok
128 if (keyvalue.IndexOf('=') >= 0)
130 newkeyvalues[x++] = keyvalue;
132 else
134 newkeyvalues[x-1] += ";";
135 newkeyvalues[x-1] += keyvalue;
139 // now we run through our normalized key-values, splitting on equals
140 for (int y=0; y < x; y++)
142 String[] parts = newkeyvalues[y].Split( '=' );
144 // first trim off any space and lowercase the key
145 parts[0] = parts[0].Trim().ToLower();
146 parts[1] = parts[1].Trim();
148 // normalize the keys going in. We want to support the same synonyms that
149 // SqlClient supports
150 switch (parts[0])
152 case "uid": parts[0] = "user id"; break;
153 case "pwd": parts[0] = "password"; break;
154 case "user": parts[0] = "user id"; break;
155 case "initial catalog": parts[0] = "database"; break;
156 case "server": parts[0] = "data source"; break;
159 // we also want to clear off any quotes
160 String newvalue = parts[1].Trim( '\'' );
161 if (newvalue.Length == parts[1].Length)
163 newvalue = parts[1].Trim('"');
165 parts[1] = newvalue;
167 // make sure we don't get dupliate keys
168 if (elements.ContainsKey(parts[0]))
170 throw new ArgumentException("Duplicate key in connection string", parts[0]);
173 elements.Add( parts[0], parts[1] );
175 // now put the correct parsed string into the connection string !! (AG 4/8/2003)
176 connString="";
177 foreach(string key in elements.Keys)
179 connString=connString+key+"="+elements[key]+"; ";
181 connString=connString.Substring(0,connString.Length-2);