DbConnectionOptions.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // System.Data.Common.DbConnectionOptions
  3. // adapted from older (pre beta1) DbConnectionString
  4. //
  5. // Authors:
  6. // Tim Coleman ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // Copyright (C) Tim Coleman, 2003
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Security;
  35. using System.Text;
  36. namespace System.Data.Common {
  37. public class DbConnectionOptions {
  38. #region Fields
  39. internal NameValueCollection options;
  40. internal string normalizedConnectionString;
  41. #endregion // Fields
  42. #region Constructors
  43. internal DbConnectionOptions ()
  44. {
  45. }
  46. [MonoTODO]
  47. protected internal DbConnectionOptions (DbConnectionOptions connectionOptions)
  48. {
  49. options = connectionOptions.options;
  50. }
  51. [MonoTODO]
  52. public DbConnectionOptions (string connectionString)
  53. {
  54. options = new NameValueCollection ();
  55. ParseConnectionString (connectionString);
  56. }
  57. [MonoTODO]
  58. public DbConnectionOptions (string connectionString, Hashtable synonyms, bool useFirstKeyValuePair)
  59. : this (connectionString)
  60. {
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. [MonoTODO]
  65. public bool IsEmpty {
  66. get { throw new NotImplementedException (); }
  67. }
  68. public string this [string keyword] {
  69. get { return options [keyword]; }
  70. }
  71. public ICollection Keys {
  72. get { return options.Keys; }
  73. }
  74. #endregion // Properties
  75. #region Methods
  76. [MonoTODO]
  77. protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. public bool ContainsKey (string keyword)
  82. {
  83. return (options.Get (keyword) != null);
  84. }
  85. public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
  86. {
  87. if (ContainsKey (keyname))
  88. return Boolean.Parse (this [keyname].Trim ());
  89. return defaultvalue;
  90. }
  91. public int ConvertValueToInt32 (string keyname, int defaultvalue)
  92. {
  93. if (ContainsKey (keyname))
  94. return Int32.Parse (this [keyname].Trim ());
  95. return defaultvalue;
  96. }
  97. [MonoTODO]
  98. public bool ConvertValueToIntegratedSecurity ()
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. public string ConvertValueToString (string keyname, string defaultValue)
  103. {
  104. if (ContainsKey (keyname))
  105. return this [keyname];
  106. return defaultValue;
  107. }
  108. [MonoTODO]
  109. protected internal virtual PermissionSet CreatePermissionSet ()
  110. {
  111. throw new NotImplementedException ();
  112. }
  113. [MonoTODO]
  114. protected internal virtual string Expand ()
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. [MonoTODO]
  119. public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. [MonoTODO]
  124. public string UsersConnectionString (bool hisPasswordPwd)
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. internal void ParseConnectionString (string connectionString)
  129. {
  130. if (connectionString.Length == 0)
  131. return;
  132. connectionString += ";";
  133. bool inQuote = false;
  134. bool inDQuote = false;
  135. bool inName = true;
  136. string name = String.Empty;
  137. string value = String.Empty;
  138. StringBuilder sb = new StringBuilder ();
  139. for (int i = 0; i < connectionString.Length; i += 1) {
  140. char c = connectionString [i];
  141. char peek;
  142. if (i == connectionString.Length - 1)
  143. peek = '\0';
  144. else
  145. peek = connectionString [i + 1];
  146. switch (c) {
  147. case '\'':
  148. if (inDQuote)
  149. sb.Append (c);
  150. else if (peek.Equals (c)) {
  151. sb.Append (c);
  152. i += 1;
  153. }
  154. else
  155. inQuote = !inQuote;
  156. break;
  157. case '"':
  158. if (inQuote)
  159. sb.Append (c);
  160. else if (peek.Equals (c)) {
  161. sb.Append (c);
  162. i += 1;
  163. }
  164. else
  165. inDQuote = !inDQuote;
  166. break;
  167. case ';':
  168. if (inDQuote || inQuote)
  169. sb.Append (c);
  170. else {
  171. if (name != String.Empty && name != null) {
  172. value = sb.ToString ();
  173. // FIXME - KeywordLookup is an NOP
  174. // options [KeywordLookup (name.Trim ())] = value;
  175. options [name.Trim ()] = value;
  176. }
  177. inName = true;
  178. name = String.Empty;
  179. value = String.Empty;
  180. sb = new StringBuilder ();
  181. }
  182. break;
  183. case '=':
  184. if (inDQuote || inQuote || !inName)
  185. sb.Append (c);
  186. else if (peek.Equals (c)) {
  187. sb.Append (c);
  188. i += 1;
  189. }
  190. else {
  191. name = sb.ToString ();
  192. sb = new StringBuilder ();
  193. inName = false;
  194. }
  195. break;
  196. case ' ':
  197. if (inQuote || inDQuote)
  198. sb.Append (c);
  199. else if (sb.Length > 0 && !peek.Equals (';'))
  200. sb.Append (c);
  201. break;
  202. default:
  203. sb.Append (c);
  204. break;
  205. }
  206. }
  207. StringBuilder normalized = new StringBuilder ();
  208. ArrayList keys = new ArrayList ();
  209. keys.AddRange (Keys);
  210. keys.Sort ();
  211. foreach (string key in keys)
  212. {
  213. string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
  214. normalized.Append (entry);
  215. }
  216. normalizedConnectionString = normalized.ToString ();
  217. }
  218. #endregion // Methods
  219. }
  220. }
  221. #endif