ConnectionString.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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
  17. using System;
  18. using System.Collections.Specialized;
  19. namespace ByteFX.Data.Common
  20. {
  21. /// <summary>
  22. /// Summary description for Utility.
  23. /// </summary>
  24. internal class ConnectionString
  25. {
  26. private StringDictionary elements;
  27. private string connString;
  28. public ConnectionString(string connString)
  29. {
  30. this.connString = connString;
  31. elements = new StringDictionary();
  32. Parse( connString );
  33. }
  34. public string Value
  35. {
  36. get { return connString; }
  37. }
  38. public string this[string key]
  39. {
  40. get
  41. {
  42. string val = elements[key];
  43. return val;
  44. }
  45. }
  46. public int GetIntOption( string key, int defaultvalue )
  47. {
  48. string val = this[ key ];
  49. if (null == val) return defaultvalue;
  50. return Convert.ToInt32( val );
  51. }
  52. public bool GetBoolOption( string key, bool defaultvalue )
  53. {
  54. string val = this[ key ];
  55. if (null == val) return defaultvalue;
  56. val = val.ToLower();
  57. if (val == "true" || val == "yes") return true;
  58. return false;
  59. }
  60. public bool Contains( string key )
  61. {
  62. return elements.ContainsKey(key);
  63. }
  64. public bool Equals( ConnectionString obj )
  65. {
  66. foreach (string key in elements.Keys)
  67. {
  68. if (! obj.Contains(key)) return false;
  69. if ( ! this[key].Equals( obj[key] )) return false;
  70. }
  71. return true;
  72. }
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. /// <param name="obj"></param>
  77. /// <returns></returns>
  78. public bool PoolingEquals( ConnectionString obj )
  79. {
  80. foreach (string key in elements.Keys)
  81. {
  82. // these connection string elements only affect pooling
  83. // so we don't check them when making sure connection strings
  84. // are alike
  85. if (key.Equals("connection lifetime")) continue;
  86. if (key.Equals("connection reset")) continue;
  87. if (key.Equals("enlist")) continue;
  88. if (key.Equals("max pool size")) continue;
  89. if (key.Equals("min pool size")) continue;
  90. if (key.Equals("pooling")) continue;
  91. if (! obj.Contains(key)) return false;
  92. if ( ! this[key].Equals( obj[key] )) return false;
  93. }
  94. return true;
  95. }
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. /// <param name="s"></param>
  100. public void Parse( String s )
  101. {
  102. String[] keyvalues = s.Split( ';' );
  103. String[] newkeyvalues = new String[keyvalues.Length];
  104. int x = 0;
  105. elements.Clear();
  106. // first run through the array and check for any keys that
  107. // have ; in their value
  108. foreach (String keyvalue in keyvalues)
  109. {
  110. // check for trailing ; at the end of the connection string
  111. if (keyvalue.Length == 0) continue;
  112. // this value has an '=' sign so we are ok
  113. if (keyvalue.IndexOf('=') >= 0)
  114. {
  115. newkeyvalues[x++] = keyvalue;
  116. }
  117. else
  118. {
  119. newkeyvalues[x-1] += ";";
  120. newkeyvalues[x-1] += keyvalue;
  121. }
  122. }
  123. // now we run through our normalized key-values, splitting on equals
  124. for (int y=0; y < x; y++)
  125. {
  126. String[] parts = newkeyvalues[y].Split( '=' );
  127. // first trim off any space and lowercase the key
  128. parts[0] = parts[0].Trim().ToLower();
  129. parts[1] = parts[1].Trim();
  130. // normalize the keys going in. We want to support the same synonyms that
  131. // SqlClient supports
  132. switch (parts[0])
  133. {
  134. case "uid": parts[0] = "user id"; break;
  135. case "pwd": parts[0] = "password"; break;
  136. case "user": parts[0] = "user id"; break;
  137. case "initial catalog": parts[0] = "database"; break;
  138. case "server": parts[0] = "data source"; break;
  139. }
  140. // we also want to clear off any quotes
  141. String newvalue = parts[1].Trim( '\'' );
  142. if (newvalue.Length == parts[1].Length)
  143. {
  144. newvalue = parts[1].Trim('"');
  145. }
  146. parts[1] = newvalue;
  147. // make sure we don't get dupliate keys
  148. if (elements.ContainsKey(parts[0]))
  149. {
  150. throw new ArgumentException("Duplicate key in connection string", parts[0]);
  151. }
  152. elements.Add( parts[0], parts[1] );
  153. // now put the correct parsed string into the connection string !! (AG 4/8/2003)
  154. connString="";
  155. foreach(string key in elements.Keys)
  156. {
  157. connString=connString+key+"="+elements[key]+"; ";
  158. }
  159. connString=connString.Substring(0,connString.Length-2);
  160. }
  161. }
  162. }
  163. }