DbConnectionString.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // System.Data.Common.DbConnectionString
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2003
  8. //
  9. #if NET_1_2
  10. using System.Collections;
  11. using System.Collections.Specialized;
  12. using System.Data;
  13. using System.Runtime.Serialization;
  14. using System.Text;
  15. namespace System.Data.Common {
  16. public class DbConnectionString : ISerializable
  17. {
  18. #region Fields
  19. KeyRestrictionBehavior behavior;
  20. string normalizedConnectionString;
  21. internal NameValueCollection options;
  22. #endregion // Fields
  23. #region Constructors
  24. [MonoTODO]
  25. protected internal DbConnectionString (DbConnectionString constr)
  26. {
  27. options = constr.options;
  28. }
  29. [MonoTODO]
  30. public DbConnectionString (string connectionString)
  31. {
  32. options = new NameValueCollection ();
  33. ParseConnectionString (connectionString);
  34. }
  35. [MonoTODO]
  36. protected DbConnectionString (SerializationInfo si, StreamingContext sc)
  37. {
  38. }
  39. [MonoTODO]
  40. public DbConnectionString (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  41. : this (connectionString)
  42. {
  43. this.behavior = behavior;
  44. }
  45. #endregion // Constructors
  46. #region Properties
  47. public KeyRestrictionBehavior Behavior {
  48. get { return behavior; }
  49. }
  50. [MonoTODO]
  51. protected virtual string CacheConnectionString {
  52. get { throw new NotImplementedException (); }
  53. }
  54. [MonoTODO]
  55. public bool IsEmpty {
  56. get { throw new NotImplementedException (); }
  57. }
  58. public string this [string x] {
  59. get { return options [x]; }
  60. }
  61. public ICollection Keys {
  62. get { return options.Keys; }
  63. }
  64. public string NormalizedConnectionString {
  65. get { return normalizedConnectionString; }
  66. }
  67. [MonoTODO]
  68. public string Restrictions {
  69. get { throw new NotImplementedException (); }
  70. }
  71. #endregion // Properties
  72. #region Methods
  73. public static void AppendKeyValuePairBuilder (StringBuilder builder, string keyname, string keyvalue)
  74. {
  75. throw new NotImplementedException ();
  76. }
  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. public bool ConvertValueToIntegratedSecurity ()
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. public string ConvertValueToString (string keyname, string defaultValue)
  102. {
  103. if (ContainsKey (keyname))
  104. return this [keyname];
  105. return defaultValue;
  106. }
  107. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. protected virtual string KeywordLookup (string keyname)
  112. {
  113. return keyname;
  114. }
  115. protected void ParseConnectionString (string connectionString)
  116. {
  117. if (connectionString.Length == 0)
  118. return;
  119. connectionString += ";";
  120. bool inQuote = false;
  121. bool inDQuote = false;
  122. bool inName = true;
  123. string name = String.Empty;
  124. string value = String.Empty;
  125. StringBuilder sb = new StringBuilder ();
  126. for (int i = 0; i < connectionString.Length; i += 1) {
  127. char c = connectionString [i];
  128. char peek;
  129. if (i == connectionString.Length - 1)
  130. peek = '\0';
  131. else
  132. peek = connectionString [i + 1];
  133. switch (c) {
  134. case '\'':
  135. if (inDQuote)
  136. sb.Append (c);
  137. else if (peek.Equals (c)) {
  138. sb.Append (c);
  139. i += 1;
  140. }
  141. else
  142. inQuote = !inQuote;
  143. break;
  144. case '"':
  145. if (inQuote)
  146. sb.Append (c);
  147. else if (peek.Equals (c)) {
  148. sb.Append (c);
  149. i += 1;
  150. }
  151. else
  152. inDQuote = !inDQuote;
  153. break;
  154. case ';':
  155. if (inDQuote || inQuote)
  156. sb.Append (c);
  157. else {
  158. if (name != String.Empty && name != null) {
  159. value = sb.ToString ();
  160. options [KeywordLookup (name.Trim ())] = value;
  161. }
  162. inName = true;
  163. name = String.Empty;
  164. value = String.Empty;
  165. sb = new StringBuilder ();
  166. }
  167. break;
  168. case '=':
  169. if (inDQuote || inQuote || !inName)
  170. sb.Append (c);
  171. else if (peek.Equals (c)) {
  172. sb.Append (c);
  173. i += 1;
  174. }
  175. else {
  176. name = sb.ToString ();
  177. sb = new StringBuilder ();
  178. inName = false;
  179. }
  180. break;
  181. case ' ':
  182. if (inQuote || inDQuote)
  183. sb.Append (c);
  184. else if (sb.Length > 0 && !peek.Equals (';'))
  185. sb.Append (c);
  186. break;
  187. default:
  188. sb.Append (c);
  189. break;
  190. }
  191. }
  192. StringBuilder normalized = new StringBuilder ();
  193. ArrayList keys = new ArrayList ();
  194. keys.AddRange (Keys);
  195. keys.Sort ();
  196. foreach (string key in keys)
  197. {
  198. string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
  199. normalized.Append (entry);
  200. }
  201. normalizedConnectionString = normalized.ToString ();
  202. }
  203. public virtual void PermissionDemand ()
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. public string UsersConnectionString (bool hisPasswordPwd)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. #endregion // Methods
  216. }
  217. }
  218. #endif