KeyMapperBase.cs 6.6 KB

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