KeyMapperBase.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // System.Data.OleDb.OleDbConnection
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  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. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Text;
  34. using System.Globalization;
  35. namespace Mainsoft.Data.Configuration
  36. {
  37. public class ConnectionStringDictionary: IConnectionStringDictionary
  38. {
  39. #region Fields
  40. private readonly IDictionary _dictionary;
  41. private readonly NameValueCollection _mapping;
  42. private readonly NameValueCollection _actualKeys;
  43. #endregion // Fields
  44. #region Constructors
  45. public ConnectionStringDictionary(string connectionString, NameValueCollection defaultMapping)
  46. {
  47. _actualKeys = new NameValueCollection();
  48. _dictionary = Parse (connectionString);
  49. _mapping = defaultMapping;
  50. }
  51. #endregion // Constructors
  52. #region Methods
  53. public string GetConnectionStringKey (string key)
  54. {
  55. string cached = _actualKeys [key];
  56. if (cached != null)
  57. return cached;
  58. if (_mapping != null)
  59. for(int i = 0, c = _mapping.Keys.Count; i < c; i++) {
  60. if (string.Compare(key, _mapping.Keys[i], true,
  61. CultureInfo.InvariantCulture) == 0) {
  62. string[] values = _mapping.GetValues(i);
  63. for(int j = 0; j < values.Length; j++) {
  64. string actualKey = values[j];
  65. if (_dictionary.Contains (actualKey)) {
  66. _actualKeys.Add (key, actualKey);
  67. return actualKey;
  68. }
  69. }
  70. }
  71. }
  72. if (_dictionary.Contains(key))
  73. return key;
  74. return null;
  75. }
  76. public static IDictionary Parse (string connectionString)
  77. {
  78. IDictionary userParameters = CollectionsUtil.CreateCaseInsensitiveHashtable();
  79. if (connectionString == null || connectionString.Length == 0) {
  80. return userParameters;
  81. }
  82. //connectionString += ";";
  83. bool inQuote = false;
  84. bool inDQuote = false;
  85. bool inName = true;
  86. string name = String.Empty;
  87. string value = String.Empty;
  88. StringBuilder sb = new StringBuilder (connectionString.Length);
  89. for (int i = 0; i < connectionString.Length; i ++) {
  90. char c = connectionString [i];
  91. char peek;
  92. if (i == connectionString.Length - 1)
  93. peek = '\0';
  94. else
  95. peek = connectionString [i + 1];
  96. switch (c) {
  97. case '\'':
  98. if (inDQuote)
  99. sb.Append (c);
  100. else if (peek == c) {
  101. sb.Append(c);
  102. i ++;
  103. }
  104. else
  105. inQuote = !inQuote;
  106. break;
  107. case '"':
  108. if (inQuote)
  109. sb.Append(c);
  110. else if (peek == c) {
  111. sb.Append(c);
  112. i ++;
  113. }
  114. else
  115. inDQuote = !inDQuote;
  116. break;
  117. case ';':
  118. if (inDQuote || inQuote)
  119. sb.Append(c);
  120. else {
  121. if (name != null && name.Length > 0) {
  122. value = sb.ToString();
  123. userParameters [name.Trim()] = value.Trim();
  124. }
  125. inName = true;
  126. name = String.Empty;
  127. value = String.Empty;
  128. sb.Length = 0;
  129. }
  130. break;
  131. case '=':
  132. if (inDQuote || inQuote || !inName)
  133. sb.Append (c);
  134. else if (peek == c) {
  135. sb.Append (c);
  136. i += 1;
  137. }
  138. else {
  139. name = sb.ToString();
  140. sb.Length = 0;
  141. inName = false;
  142. }
  143. break;
  144. case ' ':
  145. if (inQuote || inDQuote)
  146. sb.Append(c);
  147. else if (sb.Length > 0 && peek != ';')
  148. sb.Append(c);
  149. break;
  150. default:
  151. sb.Append(c);
  152. break;
  153. }
  154. }
  155. if (inDQuote || inQuote)
  156. throw new ArgumentException("connectionString");
  157. if (name != null && name.Length > 0) {
  158. value = sb.ToString();
  159. userParameters [name.Trim()] = value.Trim();
  160. }
  161. return userParameters;
  162. }
  163. #endregion // Methods
  164. #region IDictionary Members
  165. public virtual bool IsFixedSize
  166. {
  167. get { return _dictionary.IsFixedSize; }
  168. }
  169. public virtual bool IsReadOnly
  170. {
  171. get { return _dictionary.IsReadOnly; }
  172. }
  173. public virtual ICollection Keys
  174. {
  175. get {
  176. return _dictionary.Keys;
  177. }
  178. }
  179. public virtual object this [object key] {
  180. get {
  181. if (!(key is String))
  182. throw new ArgumentException("key is not string");
  183. string skey = (string)key;
  184. skey = GetConnectionStringKey(skey);
  185. if (skey == null)
  186. return null;
  187. return _dictionary[skey];
  188. }
  189. set {
  190. if (!(key is String))
  191. throw new ArgumentException("key is not string");
  192. string skey = (string)key;
  193. skey = GetConnectionStringKey(skey);
  194. if (skey == null)
  195. skey = (string)key;
  196. _dictionary[skey] = value;
  197. }
  198. }
  199. public virtual ICollection Values
  200. {
  201. get { return _dictionary.Values; }
  202. }
  203. public virtual void Add (object key, object value)
  204. {
  205. _dictionary.Add ((string)key, (string)value);
  206. }
  207. public virtual void Clear ()
  208. {
  209. _dictionary.Clear ();
  210. }
  211. public virtual bool Contains (object key)
  212. {
  213. return _dictionary.Contains (key);
  214. }
  215. public virtual IDictionaryEnumerator GetEnumerator ()
  216. {
  217. return _dictionary.GetEnumerator ();
  218. }
  219. public virtual void Remove (object key)
  220. {
  221. _dictionary.Remove ((string)key);
  222. }
  223. #endregion // IDictionary Members
  224. #region IEnumerable Members
  225. IEnumerator IEnumerable.GetEnumerator ()
  226. {
  227. return this.GetEnumerator();
  228. }
  229. #endregion // IEnumerable Members
  230. #region ICollection Members
  231. public virtual bool IsSynchronized
  232. {
  233. get { return ((ICollection)_dictionary).IsSynchronized; }
  234. }
  235. public virtual int Count
  236. {
  237. get { return _dictionary.Count; }
  238. }
  239. public virtual void CopyTo (Array array, int index)
  240. {
  241. _dictionary.CopyTo (array, index);
  242. }
  243. public virtual object SyncRoot
  244. {
  245. get {return ((ICollection)_dictionary).SyncRoot; }
  246. }
  247. #endregion // ICollection Members
  248. }
  249. }