KeyMapperBase.cs 7.4 KB

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