DbConnectionString.cs 5.0 KB

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