DbConnectionOptions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. internal 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. protected internal DbConnectionOptions (DbConnectionOptions connectionOptions)
  47. {
  48. options = connectionOptions.options;
  49. }
  50. public DbConnectionOptions (string connectionString)
  51. {
  52. options = new NameValueCollection ();
  53. ParseConnectionString (connectionString);
  54. }
  55. [MonoTODO]
  56. public DbConnectionOptions (string connectionString, Hashtable synonyms, bool useFirstKeyValuePair)
  57. : this (connectionString)
  58. {
  59. }
  60. #endregion // Constructors
  61. #region Properties
  62. [MonoTODO]
  63. public bool IsEmpty {
  64. get { throw new NotImplementedException (); }
  65. }
  66. public string this [string keyword] {
  67. get { return options [keyword]; }
  68. }
  69. public ICollection Keys {
  70. get { return options.Keys; }
  71. }
  72. #endregion // Properties
  73. #region Methods
  74. [MonoTODO]
  75. protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
  76. {
  77. throw new NotImplementedException ();
  78. }
  79. public bool ContainsKey (string keyword)
  80. {
  81. return (options.Get (keyword) != null);
  82. }
  83. public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
  84. {
  85. if (ContainsKey (keyname))
  86. return Boolean.Parse (this [keyname].Trim ());
  87. return defaultvalue;
  88. }
  89. public int ConvertValueToInt32 (string keyname, int defaultvalue)
  90. {
  91. if (ContainsKey (keyname))
  92. return Int32.Parse (this [keyname].Trim ());
  93. return defaultvalue;
  94. }
  95. [MonoTODO]
  96. public bool ConvertValueToIntegratedSecurity ()
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. public string ConvertValueToString (string keyname, string defaultValue)
  101. {
  102. if (ContainsKey (keyname))
  103. return this [keyname];
  104. return defaultValue;
  105. }
  106. [MonoTODO]
  107. protected internal virtual PermissionSet CreatePermissionSet ()
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. [MonoTODO]
  112. protected internal virtual string Expand ()
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. [MonoTODO]
  117. public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
  118. {
  119. throw new NotImplementedException ();
  120. }
  121. [MonoTODO]
  122. public string UsersConnectionString (bool hisPasswordPwd)
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. internal void ParseConnectionString (string connectionString)
  127. {
  128. if (connectionString.Length == 0)
  129. return;
  130. connectionString += ";";
  131. bool inQuote = false;
  132. bool inDQuote = false;
  133. bool inName = true;
  134. string name = String.Empty;
  135. string value = String.Empty;
  136. StringBuilder sb = new StringBuilder ();
  137. for (int i = 0; i < connectionString.Length; i += 1) {
  138. char c = connectionString [i];
  139. char peek;
  140. if (i == connectionString.Length - 1)
  141. peek = '\0';
  142. else
  143. peek = connectionString [i + 1];
  144. switch (c) {
  145. case '\'':
  146. if (inDQuote)
  147. sb.Append (c);
  148. else if (peek.Equals (c)) {
  149. sb.Append (c);
  150. i += 1;
  151. }
  152. else
  153. inQuote = !inQuote;
  154. break;
  155. case '"':
  156. if (inQuote)
  157. sb.Append (c);
  158. else if (peek.Equals (c)) {
  159. sb.Append (c);
  160. i += 1;
  161. }
  162. else
  163. inDQuote = !inDQuote;
  164. break;
  165. case ';':
  166. if (inDQuote || inQuote)
  167. sb.Append (c);
  168. else {
  169. if (name != String.Empty && name != null) {
  170. value = sb.ToString ();
  171. // FIXME - KeywordLookup is an NOP
  172. // options [KeywordLookup (name.Trim ())] = value;
  173. options [name.Trim ()] = value;
  174. }
  175. inName = true;
  176. name = String.Empty;
  177. value = String.Empty;
  178. sb = new StringBuilder ();
  179. }
  180. break;
  181. case '=':
  182. if (inDQuote || inQuote || !inName)
  183. sb.Append (c);
  184. else if (peek.Equals (c)) {
  185. sb.Append (c);
  186. i += 1;
  187. }
  188. else {
  189. name = sb.ToString ();
  190. sb = new StringBuilder ();
  191. inName = false;
  192. }
  193. break;
  194. case ' ':
  195. if (inQuote || inDQuote)
  196. sb.Append (c);
  197. else if (sb.Length > 0 && !peek.Equals (';'))
  198. sb.Append (c);
  199. break;
  200. default:
  201. sb.Append (c);
  202. break;
  203. }
  204. }
  205. StringBuilder normalized = new StringBuilder ();
  206. ArrayList keys = new ArrayList ();
  207. keys.AddRange (Keys);
  208. keys.Sort ();
  209. foreach (string key in keys)
  210. {
  211. string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
  212. normalized.Append (entry);
  213. }
  214. normalizedConnectionString = normalized.ToString ();
  215. }
  216. #endregion // Methods
  217. }
  218. }
  219. #endif