PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SubSonic/Sql Tools/QueryCommand.cs

http://github.com/subsonic/SubSonic-2.0
C# | 466 lines | 202 code | 48 blank | 216 comment | 13 complexity | 92dc7fbc7966398b2306c97cda31aeb1 MD5 | raw file
  1. /*
  2. * SubSonic - http://subsonicproject.com
  3. *
  4. * The contents of this file are subject to the Mozilla Public
  5. * License Version 1.1 (the "License"); you may not use this file
  6. * except in compliance with the License. You may obtain a copy of
  7. * the License at http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an
  10. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. * implied. See the License for the specific language governing
  12. * rights and limitations under the License.
  13. */
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Data;
  17. using System.Data.Common;
  18. using SubSonic.Utilities;
  19. namespace SubSonic
  20. {
  21. /// <summary>
  22. /// This set of classes abstracts out commands and their parameters so that
  23. /// the DataProviders can work their magic regardless of the client type. The
  24. /// System.Data.Common class was supposed to do this, but sort of fell flat
  25. /// when it came to MySQL and other DB Providers that don't implement the Data
  26. /// Factory pattern. Abstracts out the assignment of parameters, etc
  27. /// </summary>
  28. public class QueryParameter
  29. {
  30. internal const ParameterDirection DefaultParameterDirection = ParameterDirection.Input;
  31. internal const int DefaultSize = 50;
  32. private ParameterDirection _mode = DefaultParameterDirection;
  33. private int _size = DefaultSize;
  34. /// <summary>
  35. /// Gets or sets the size.
  36. /// </summary>
  37. /// <value>The size.</value>
  38. public int Size
  39. {
  40. get { return _size; }
  41. set { _size = value; }
  42. }
  43. /// <summary>
  44. /// Gets or sets the mode.
  45. /// </summary>
  46. /// <value>The mode.</value>
  47. public ParameterDirection Mode
  48. {
  49. get { return _mode; }
  50. set { _mode = value; }
  51. }
  52. /// <summary>
  53. /// Gets or sets the name of the parameter.
  54. /// </summary>
  55. /// <value>The name of the parameter.</value>
  56. public string ParameterName { get; set; }
  57. /// <summary>
  58. /// Gets or sets the parameter value.
  59. /// </summary>
  60. /// <value>The parameter value.</value>
  61. public object ParameterValue { get; set; }
  62. /// <summary>
  63. /// Gets or sets the type of the data.
  64. /// </summary>
  65. /// <value>The type of the data.</value>
  66. public DbType DataType { get; set; }
  67. /// <summary>
  68. /// Gets or sets the scale.
  69. /// </summary>
  70. /// <value>The scale.</value>
  71. public int? Scale { get; set; }
  72. /// <summary>
  73. /// Gets or sets the precision.
  74. /// </summary>
  75. /// <value>The precision.</value>
  76. public int? Precision { get; set; }
  77. }
  78. /// <summary>
  79. /// Summary for the QueryParameterCollection class
  80. /// </summary>
  81. public class QueryParameterCollection : List<QueryParameter>
  82. {
  83. /// <summary>
  84. /// Checks to see if specified parameter exists in the current collection
  85. /// </summary>
  86. /// <param name="parameterName"></param>
  87. /// <returns></returns>
  88. public bool Contains(string parameterName)
  89. {
  90. foreach(QueryParameter p in this)
  91. {
  92. if(Utility.IsMatch(p.ParameterName, parameterName, true))
  93. return true;
  94. }
  95. return false;
  96. }
  97. /// <summary>
  98. /// returns the specified QueryParameter, if it exists in this collection
  99. /// </summary>
  100. /// <param name="parameterName"></param>
  101. /// <returns></returns>
  102. public QueryParameter GetParameter(string parameterName)
  103. {
  104. foreach(QueryParameter p in this)
  105. {
  106. if(Utility.IsMatch(p.ParameterName, parameterName, true))
  107. return p;
  108. }
  109. return null;
  110. }
  111. /// <summary>
  112. /// Adds the specified parameter name.
  113. /// </summary>
  114. /// <param name="parameterName">Name of the parameter.</param>
  115. /// <param name="value">The value.</param>
  116. public void Add(string parameterName, object value)
  117. {
  118. Add(parameterName, value, DbType.AnsiString, ParameterDirection.Input);
  119. }
  120. /// <summary>
  121. /// Adds the specified parameter name.
  122. /// </summary>
  123. /// <param name="parameterName">Name of the parameter.</param>
  124. /// <param name="value">The value.</param>
  125. /// <param name="dataType">Type of the data.</param>
  126. public void Add(string parameterName, object value, DbType dataType)
  127. {
  128. Add(parameterName, value, dataType, ParameterDirection.Input);
  129. }
  130. /// <summary>
  131. /// Adds the specified parameter name.
  132. /// </summary>
  133. /// <param name="parameterName">Name of the parameter.</param>
  134. /// <param name="value">The value.</param>
  135. /// <param name="dataType">Type of the data.</param>
  136. /// <param name="mode">The mode.</param>
  137. public void Add(string parameterName, object value, DbType dataType, ParameterDirection mode)
  138. {
  139. //remove if already added, and replace with last in
  140. if(Contains(parameterName))
  141. Remove(GetParameter(parameterName));
  142. QueryParameter param = new QueryParameter
  143. {
  144. ParameterName = parameterName,
  145. ParameterValue = value,
  146. DataType = dataType,
  147. Mode = mode
  148. };
  149. Add(param);
  150. }
  151. }
  152. /// <summary>
  153. /// Summary for the QueryCommandCollection class
  154. /// </summary>
  155. public class QueryCommandCollection : List<QueryCommand> {}
  156. /// <summary>
  157. /// Summary for the QueryCommand class
  158. /// </summary>
  159. public class QueryCommand
  160. {
  161. private string _providerName = String.Empty;
  162. private int commandTimeout = 60;
  163. /// <summary>
  164. ///
  165. /// </summary>
  166. public List<object> OutputValues;
  167. private QueryParameterCollection parameters;
  168. /// <summary>
  169. /// Initializes a new instance of the <see cref="QueryCommand"/> class.
  170. /// </summary>
  171. /// <param name="sql">The SQL.</param>
  172. /// <param name="providerName">Name of the provider.</param>
  173. public QueryCommand(string sql, string providerName)
  174. {
  175. ProviderName = providerName;
  176. Provider = DataService.GetInstance(providerName);
  177. CommandSql = sql;
  178. CommandType = CommandType.Text;
  179. parameters = new QueryParameterCollection();
  180. OutputValues = new List<object>();
  181. }
  182. /// <summary>
  183. /// Initializes a new instance of the <see cref="QueryCommand"/> class.
  184. /// </summary>
  185. /// <param name="sql">The SQL.</param>
  186. [Obsolete("Deprecated: Use QueryCommand(string sql, string providerName) instead.")]
  187. public QueryCommand(string sql)
  188. {
  189. //use the default
  190. ProviderName = DataService.Provider.Name;
  191. Provider = DataService.Provider;
  192. CommandSql = sql;
  193. CommandType = CommandType.Text;
  194. parameters = new QueryParameterCollection();
  195. OutputValues = new List<object>();
  196. }
  197. /// <summary>
  198. /// Gets or sets the command timeout (in seconds).
  199. /// </summary>
  200. /// <value>The command timeout.</value>
  201. public int CommandTimeout
  202. {
  203. get { return commandTimeout; }
  204. set { commandTimeout = value; }
  205. }
  206. /// <summary>
  207. /// Gets or sets the name of the provider.
  208. /// </summary>
  209. /// <value>The name of the provider.</value>
  210. public string ProviderName
  211. {
  212. get { return _providerName; }
  213. set { _providerName = value; }
  214. }
  215. /// <summary>
  216. /// Gets or sets the provider.
  217. /// </summary>
  218. /// <value>The provider.</value>
  219. public DataProvider Provider { get; set; }
  220. /// <summary>
  221. /// Gets or sets the type of the command.
  222. /// </summary>
  223. /// <value>The type of the command.</value>
  224. public CommandType CommandType { get; set; }
  225. /// <summary>
  226. /// Gets or sets the command SQL.
  227. /// </summary>
  228. /// <value>The command SQL.</value>
  229. public string CommandSql { get; set; }
  230. /// <summary>
  231. /// Gets or sets the parameters.
  232. /// </summary>
  233. /// <value>The parameters.</value>
  234. public QueryParameterCollection Parameters
  235. {
  236. get { return parameters; }
  237. set { parameters = value; }
  238. }
  239. /// <summary>
  240. /// Determines whether [has output params].
  241. /// </summary>
  242. /// <returns>
  243. /// <c>true</c> if [has output params]; otherwise, <c>false</c>.
  244. /// </returns>
  245. public bool HasOutputParams()
  246. {
  247. bool bOut = false;
  248. //loop the params and see if one is in/out
  249. foreach(QueryParameter param in Parameters)
  250. {
  251. if(param.Mode != ParameterDirection.Input)
  252. {
  253. bOut = true;
  254. break;
  255. }
  256. }
  257. return bOut;
  258. }
  259. /// <summary>
  260. /// Adds the parameter. The public AddParameter methods should call this one.
  261. /// </summary>
  262. /// <param name="parameterName">Name of the parameter.</param>
  263. /// <param name="parameterValue">The parameter value.</param>
  264. /// <param name="maxSize">Size of the max.</param>
  265. /// <param name="dbType">Type of the db.</param>
  266. /// <param name="direction">The direction.</param>
  267. /// <param name="scale">The scale.</param>
  268. /// <param name="precision">The precision.</param>
  269. private void AddParameter(string parameterName, object parameterValue, int maxSize, DbType dbType, ParameterDirection direction, int? scale, int? precision)
  270. {
  271. if(parameters == null)
  272. parameters = new QueryParameterCollection();
  273. QueryParameter param = new QueryParameter
  274. {
  275. ParameterName = CommandType == CommandType.StoredProcedure ? parameterName : Provider.FormatParameterNameForSQL(parameterName),
  276. ParameterValue = parameterValue ?? DBNull.Value,
  277. Mode = direction,
  278. DataType = dbType,
  279. Scale = scale,
  280. Precision = precision
  281. };
  282. if(maxSize > -1 && direction != ParameterDirection.Output)
  283. param.Size = maxSize;
  284. parameters.Add(param);
  285. }
  286. /// <summary>
  287. /// Adds the parameter.
  288. /// </summary>
  289. /// <param name="parameterName">Name of the parameter.</param>
  290. /// <param name="parameterValue">The parameter value.</param>
  291. /// <param name="dataType">Type of the data.</param>
  292. /// <param name="parameterDirection">The parameter direction.</param>
  293. public void AddParameter(string parameterName, object parameterValue, DbType dataType, ParameterDirection parameterDirection)
  294. {
  295. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, parameterDirection, null, null);
  296. }
  297. /// <summary>
  298. /// Adds the parameter.
  299. /// </summary>
  300. /// <param name="parameterName">Name of the parameter.</param>
  301. /// <param name="parameterValue">The parameter value.</param>
  302. /// <param name="dataType">Type of the data.</param>
  303. /// <param name="parameterDirection">The parameter direction.</param>
  304. /// <param name="scale">The scale.</param>
  305. /// <param name="precision">The precision.</param>
  306. public void AddParameter(string parameterName, object parameterValue, DbType dataType, ParameterDirection parameterDirection, int? scale, int? precision)
  307. {
  308. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, parameterDirection, scale, precision);
  309. }
  310. /// <summary>
  311. /// Adds the parameter.
  312. /// </summary>
  313. /// <param name="parameterName">Name of the parameter.</param>
  314. /// <param name="parameterValue">The parameter value.</param>
  315. /// <param name="dataType">Type of the data.</param>
  316. public void AddParameter(string parameterName, object parameterValue, DbType dataType)
  317. {
  318. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, QueryParameter.DefaultParameterDirection, null, null);
  319. }
  320. /// <summary>
  321. /// Adds the parameter.
  322. /// </summary>
  323. /// <param name="parameterName">Name of the parameter.</param>
  324. /// <param name="parameterValue">The parameter value.</param>
  325. /// <param name="dataType">Type of the data.</param>
  326. /// <param name="scale">The scale.</param>
  327. /// <param name="precision">The precision.</param>
  328. public void AddParameter(string parameterName, object parameterValue, DbType dataType, int? scale, int? precision)
  329. {
  330. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, QueryParameter.DefaultParameterDirection, scale, precision);
  331. }
  332. /// <summary>
  333. /// Adds the output parameter.
  334. /// </summary>
  335. /// <param name="parameterName">Name of the parameter.</param>
  336. /// <param name="maxSize">Size of the max.</param>
  337. /// <param name="dbType">Type of the db.</param>
  338. public void AddOutputParameter(string parameterName, int maxSize, DbType dbType)
  339. {
  340. AddOutputParameter(parameterName, maxSize, dbType, null, null);
  341. }
  342. /// <summary>
  343. /// Adds the output parameter.
  344. /// </summary>
  345. /// <param name="parameterName">Name of the parameter.</param>
  346. /// <param name="maxSize">Size of the max.</param>
  347. /// <param name="dbType">Type of the db.</param>
  348. /// <param name="scale">The scale.</param>
  349. /// <param name="precision">The precision.</param>
  350. public void AddOutputParameter(string parameterName, int maxSize, DbType dbType, int? scale, int? precision)
  351. {
  352. AddParameter(parameterName, DBNull.Value, maxSize, dbType, ParameterDirection.Output, scale, precision);
  353. }
  354. /// <summary>
  355. /// Adds the output parameter.
  356. /// </summary>
  357. /// <param name="parameterName">Name of the parameter.</param>
  358. /// <param name="maxSize">Size of the max.</param>
  359. public void AddOutputParameter(string parameterName, int maxSize)
  360. {
  361. AddOutputParameter(parameterName, maxSize, DbType.AnsiString, null, null);
  362. }
  363. /// <summary>
  364. /// Adds the output parameter.
  365. /// </summary>
  366. /// <param name="parameterName">Name of the parameter.</param>
  367. public void AddOutputParameter(string parameterName)
  368. {
  369. AddOutputParameter(parameterName, -1, DbType.AnsiString, null, null);
  370. }
  371. /// <summary>
  372. /// Adds the output parameter.
  373. /// </summary>
  374. /// <param name="parameterName">Name of the parameter.</param>
  375. /// <param name="dbType">Type of the db.</param>
  376. public void AddOutputParameter(string parameterName, DbType dbType)
  377. {
  378. AddOutputParameter(parameterName, -1, dbType, null, null);
  379. }
  380. /// <summary>
  381. /// Adds the output parameter.
  382. /// </summary>
  383. /// <param name="parameterName">Name of the parameter.</param>
  384. /// <param name="dbType">Type of the db.</param>
  385. /// <param name="scale">The scale.</param>
  386. /// <param name="precision">The precision.</param>
  387. public void AddOutputParameter(string parameterName, DbType dbType, int? scale, int? precision)
  388. {
  389. AddOutputParameter(parameterName, -1, dbType, scale, precision);
  390. }
  391. /// <summary>
  392. /// Adds a return parameter (RETURN_VALUE) to the command.
  393. /// </summary>
  394. public void AddReturnParameter()
  395. {
  396. if(Provider != null)
  397. AddParameter(String.Concat(Provider.GetParameterPrefix(), "RETURN_VALUE"), null, DbType.Int32, ParameterDirection.ReturnValue);
  398. else
  399. AddParameter("@RETURN_VALUE", null, DbType.Int32, ParameterDirection.ReturnValue);
  400. }
  401. /// <summary>
  402. /// Converts the QueryCommand to an IDbCommand.
  403. /// </summary>
  404. /// <returns></returns>
  405. public IDbCommand ToIDbCommand()
  406. {
  407. return DataService.GetIDbCommand(this);
  408. }
  409. /// <summary>
  410. /// Converts the QueryCommand to a DbCommand.
  411. /// </summary>
  412. /// <returns></returns>
  413. public DbCommand ToDbCommand()
  414. {
  415. return DataService.GetDbCommand(this);
  416. }
  417. }
  418. }