DbConnectionString.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // System.Data.Common.DbConnectionString
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2003
  8. //
  9. //
  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.Data;
  35. using System.Runtime.Serialization;
  36. using System.Text;
  37. namespace System.Data.Common {
  38. public class DbConnectionString : ISerializable
  39. {
  40. #region Fields
  41. KeyRestrictionBehavior behavior;
  42. string normalizedConnectionString;
  43. internal NameValueCollection options;
  44. #endregion // Fields
  45. #region Constructors
  46. [MonoTODO]
  47. protected internal DbConnectionString (DbConnectionString constr)
  48. {
  49. options = constr.options;
  50. }
  51. [MonoTODO]
  52. public DbConnectionString (string connectionString)
  53. {
  54. options = new NameValueCollection ();
  55. ParseConnectionString (connectionString);
  56. }
  57. [MonoTODO]
  58. protected DbConnectionString (SerializationInfo si, StreamingContext sc)
  59. {
  60. }
  61. [MonoTODO]
  62. public DbConnectionString (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  63. : this (connectionString)
  64. {
  65. this.behavior = behavior;
  66. }
  67. #endregion // Constructors
  68. #region Properties
  69. public KeyRestrictionBehavior Behavior {
  70. get { return behavior; }
  71. }
  72. [MonoTODO]
  73. protected virtual string CacheConnectionString {
  74. get { throw new NotImplementedException (); }
  75. }
  76. [MonoTODO]
  77. public bool IsEmpty {
  78. get { throw new NotImplementedException (); }
  79. }
  80. public string this [string x] {
  81. get { return options [x]; }
  82. }
  83. public ICollection Keys {
  84. get { return options.Keys; }
  85. }
  86. public string NormalizedConnectionString {
  87. get { return normalizedConnectionString; }
  88. }
  89. [MonoTODO]
  90. public string Restrictions {
  91. get { throw new NotImplementedException (); }
  92. }
  93. #endregion // Properties
  94. #region Methods
  95. public static void AppendKeyValuePairBuilder (StringBuilder builder, string keyname, string keyvalue)
  96. {
  97. throw new NotImplementedException ();
  98. }
  99. protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public bool ContainsKey (string keyword)
  104. {
  105. return (options.Get (keyword) != null);
  106. }
  107. public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
  108. {
  109. if (ContainsKey (keyname))
  110. return Boolean.Parse (this [keyname].Trim ());
  111. return defaultvalue;
  112. }
  113. public int ConvertValueToInt32 (string keyname, int defaultvalue)
  114. {
  115. if (ContainsKey (keyname))
  116. return Int32.Parse (this [keyname].Trim ());
  117. return defaultvalue;
  118. }
  119. public bool ConvertValueToIntegratedSecurity ()
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. public string ConvertValueToString (string keyname, string defaultValue)
  124. {
  125. if (ContainsKey (keyname))
  126. return this [keyname];
  127. return defaultValue;
  128. }
  129. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. protected virtual string KeywordLookup (string keyname)
  134. {
  135. return keyname;
  136. }
  137. protected void ParseConnectionString (string connectionString)
  138. {
  139. if (connectionString.Length == 0)
  140. return;
  141. connectionString += ";";
  142. bool inQuote = false;
  143. bool inDQuote = false;
  144. bool inName = true;
  145. string name = String.Empty;
  146. string value = String.Empty;
  147. StringBuilder sb = new StringBuilder ();
  148. for (int i = 0; i < connectionString.Length; i += 1) {
  149. char c = connectionString [i];
  150. char peek;
  151. if (i == connectionString.Length - 1)
  152. peek = '\0';
  153. else
  154. peek = connectionString [i + 1];
  155. switch (c) {
  156. case '\'':
  157. if (inDQuote)
  158. sb.Append (c);
  159. else if (peek.Equals (c)) {
  160. sb.Append (c);
  161. i += 1;
  162. }
  163. else
  164. inQuote = !inQuote;
  165. break;
  166. case '"':
  167. if (inQuote)
  168. sb.Append (c);
  169. else if (peek.Equals (c)) {
  170. sb.Append (c);
  171. i += 1;
  172. }
  173. else
  174. inDQuote = !inDQuote;
  175. break;
  176. case ';':
  177. if (inDQuote || inQuote)
  178. sb.Append (c);
  179. else {
  180. if (name != String.Empty && name != null) {
  181. value = sb.ToString ();
  182. options [KeywordLookup (name.Trim ())] = value;
  183. }
  184. inName = true;
  185. name = String.Empty;
  186. value = String.Empty;
  187. sb = new StringBuilder ();
  188. }
  189. break;
  190. case '=':
  191. if (inDQuote || inQuote || !inName)
  192. sb.Append (c);
  193. else if (peek.Equals (c)) {
  194. sb.Append (c);
  195. i += 1;
  196. }
  197. else {
  198. name = sb.ToString ();
  199. sb = new StringBuilder ();
  200. inName = false;
  201. }
  202. break;
  203. case ' ':
  204. if (inQuote || inDQuote)
  205. sb.Append (c);
  206. else if (sb.Length > 0 && !peek.Equals (';'))
  207. sb.Append (c);
  208. break;
  209. default:
  210. sb.Append (c);
  211. break;
  212. }
  213. }
  214. StringBuilder normalized = new StringBuilder ();
  215. ArrayList keys = new ArrayList ();
  216. keys.AddRange (Keys);
  217. keys.Sort ();
  218. foreach (string key in keys)
  219. {
  220. string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
  221. normalized.Append (entry);
  222. }
  223. normalizedConnectionString = normalized.ToString ();
  224. }
  225. public virtual void PermissionDemand ()
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
  230. {
  231. throw new NotImplementedException ();
  232. }
  233. public string UsersConnectionString (bool hisPasswordPwd)
  234. {
  235. throw new NotImplementedException ();
  236. }
  237. #endregion // Methods
  238. }
  239. }
  240. #endif