KeyMapperBase.cs 7.3 KB

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