PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Library/WP7/SQLiteDriver/SQLClient/SqliteParameterCollection.cs

https://bitbucket.org/digitalizarte/coolstorage
C# | 313 lines | 228 code | 45 blank | 40 comment | 33 complexity | 9755e6d70b2504661bfc4e89f44f082f MD5 | raw file
  1. //
  2. // Community.CsharpSqlite.SQLiteClient.SqliteParameterCollection.cs
  3. //
  4. // Represents a collection of parameters relevant to a SqliteCommand as well as
  5. // their respective mappings to columns in a DataSet.
  6. //
  7. //Author(s): Vladimir Vukicevic <vladimir@pobox.com>
  8. // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
  9. // Chris Turchin <chris@turchin.net>
  10. // Jeroen Zwartepoorte <jeroen@xs4all.nl>
  11. // Thomas Zoechling <thomas.zoechling@gmx.at>
  12. //
  13. // Copyright (C) 2002 Vladimir Vukicevic
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Collections;
  37. namespace Community.CsharpSqlite.SQLiteClient
  38. {
  39. public class SqliteParameterCollection : IList
  40. {
  41. List<object> numeric_param_list = new List<object>();
  42. Hashtable named_param_hash = new Hashtable();
  43. private void CheckSqliteParam (object value)
  44. {
  45. if (!(value is SqliteParameter))
  46. throw new InvalidCastException ("Can only use SqliteParameter objects");
  47. SqliteParameter sqlp = value as SqliteParameter;
  48. if (sqlp.ParameterName == null || sqlp.ParameterName.Length == 0)
  49. sqlp.ParameterName = this.GenerateParameterName();
  50. }
  51. private void RecreateNamedHash ()
  52. {
  53. for (int i = 0; i < numeric_param_list.Count; i++)
  54. {
  55. named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
  56. }
  57. }
  58. //FIXME: if the user is calling Insert at various locations with unnamed parameters, this is not going to work....
  59. private string GenerateParameterName()
  60. {
  61. int index = this.Count + 1;
  62. string name = String.Empty;
  63. while (index > 0)
  64. {
  65. name = ":" + index.ToString();
  66. if (this.IndexOf(name) == -1)
  67. index = -1;
  68. else
  69. index++;
  70. }
  71. return name;
  72. }
  73. object IList.this[int index] {
  74. get
  75. {
  76. return this[index];
  77. }
  78. set
  79. {
  80. CheckSqliteParam (value);
  81. this[index] = (SqliteParameter) value;
  82. }
  83. }
  84. private bool isPrefixed (string parameterName)
  85. {
  86. return parameterName.Length > 1 && (parameterName[0] == ':' || parameterName[0] == '$');
  87. }
  88. SqliteParameter GetParameter (int parameterIndex)
  89. {
  90. if (this.Count >= parameterIndex+1)
  91. return (SqliteParameter) numeric_param_list[parameterIndex];
  92. else
  93. throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
  94. }
  95. SqliteParameter GetParameter (string parameterName)
  96. {
  97. if (this.Contains(parameterName))
  98. return this[(int) named_param_hash[parameterName]];
  99. else if (isPrefixed(parameterName) && this.Contains(parameterName.Substring(1)))
  100. return this[(int) named_param_hash[parameterName.Substring(1)]];
  101. else
  102. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  103. }
  104. void SetParameter (int parameterIndex, SqliteParameter parameter)
  105. {
  106. if (this.Count >= parameterIndex+1)
  107. numeric_param_list[parameterIndex] = parameter;
  108. else
  109. throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
  110. }
  111. void SetParameter (string parameterName, SqliteParameter parameter)
  112. {
  113. if (this.Contains(parameterName))
  114. numeric_param_list[(int) named_param_hash[parameterName]] = parameter;
  115. else if (parameterName.Length > 1 && this.Contains(parameterName.Substring(1)))
  116. numeric_param_list[(int) named_param_hash[parameterName.Substring(1)]] = parameter;
  117. else
  118. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  119. }
  120. public SqliteParameter this[string parameterName]
  121. {
  122. get { return GetParameter (parameterName); }
  123. set { SetParameter (parameterName, value); }
  124. }
  125. public SqliteParameter this[int parameterIndex]
  126. {
  127. get { return GetParameter (parameterIndex); }
  128. set { SetParameter (parameterIndex, value); }
  129. }
  130. public int Count
  131. {
  132. get
  133. {
  134. return this.numeric_param_list.Count;
  135. }
  136. }
  137. bool IList.IsFixedSize
  138. {
  139. get
  140. {
  141. return ((IList)numeric_param_list).IsFixedSize;
  142. }
  143. }
  144. bool IList.IsReadOnly
  145. {
  146. get
  147. {
  148. return ((IList)numeric_param_list).IsReadOnly;
  149. }
  150. }
  151. bool ICollection.IsSynchronized
  152. {
  153. get
  154. {
  155. return ((IList)numeric_param_list).IsSynchronized;
  156. }
  157. }
  158. object ICollection.SyncRoot
  159. {
  160. get
  161. {
  162. return ((IList)numeric_param_list).SyncRoot;
  163. }
  164. }
  165. public int Add (object value)
  166. {
  167. CheckSqliteParam (value);
  168. SqliteParameter sqlp = value as SqliteParameter;
  169. if (named_param_hash.Contains (sqlp.ParameterName))
  170. throw new ApplicationException("Parameter collection already contains the a SqliteParameter with the given ParameterName.");
  171. named_param_hash[sqlp.ParameterName] = numeric_param_list.Count;
  172. numeric_param_list.Add(value);
  173. return (int) named_param_hash[sqlp.ParameterName];
  174. }
  175. public SqliteParameter Add (SqliteParameter param)
  176. {
  177. Add ((object)param);
  178. return param;
  179. }
  180. public SqliteParameter Add (string name, object value)
  181. {
  182. return Add (new SqliteParameter (name, value));
  183. }
  184. /*
  185. public SqliteParameter Add (string name, DbType type)
  186. {
  187. return Add (new SqliteParameter (name, type));
  188. }
  189. * */
  190. public void Clear ()
  191. {
  192. numeric_param_list.Clear ();
  193. named_param_hash.Clear ();
  194. }
  195. public void CopyTo (Array array, int index)
  196. {
  197. this.numeric_param_list.CopyTo((object[])array, index);
  198. }
  199. bool IList.Contains (object value)
  200. {
  201. return Contains ((SqliteParameter) value);
  202. }
  203. public bool Contains (string parameterName)
  204. {
  205. return named_param_hash.Contains (parameterName);
  206. }
  207. public bool Contains (SqliteParameter param)
  208. {
  209. return Contains (param.ParameterName);
  210. }
  211. public IEnumerator GetEnumerator ()
  212. {
  213. return this.numeric_param_list.GetEnumerator();
  214. }
  215. int IList.IndexOf (object param)
  216. {
  217. return IndexOf ((SqliteParameter) param);
  218. }
  219. public int IndexOf (string parameterName)
  220. {
  221. if (isPrefixed (parameterName)){
  222. string sub = parameterName.Substring (1);
  223. if (named_param_hash.Contains(sub))
  224. return (int) named_param_hash [sub];
  225. }
  226. if (named_param_hash.Contains(parameterName))
  227. return (int) named_param_hash[parameterName];
  228. else
  229. return -1;
  230. }
  231. public int IndexOf (SqliteParameter param)
  232. {
  233. return IndexOf (param.ParameterName);
  234. }
  235. public void Insert (int index, object value)
  236. {
  237. CheckSqliteParam (value);
  238. if (numeric_param_list.Count == index)
  239. {
  240. Add (value);
  241. return;
  242. }
  243. numeric_param_list.Insert (index, value);
  244. RecreateNamedHash ();
  245. }
  246. public void Remove (object value)
  247. {
  248. CheckSqliteParam (value);
  249. RemoveAt ((SqliteParameter) value);
  250. }
  251. public void RemoveAt (int index)
  252. {
  253. RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
  254. }
  255. public void RemoveAt (string parameterName)
  256. {
  257. if (!named_param_hash.Contains (parameterName))
  258. throw new ApplicationException ("Parameter " + parameterName + " not found");
  259. numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
  260. named_param_hash.Remove (parameterName);
  261. RecreateNamedHash ();
  262. }
  263. public void RemoveAt (SqliteParameter param)
  264. {
  265. RemoveAt (param.ParameterName);
  266. }
  267. }
  268. }