PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Backup/SubSonic.Core/Query/QueryCommand.cs

https://bitbucket.org/moisesmiranda/subsonic-4
C# | 408 lines | 200 code | 44 blank | 164 comment | 19 complexity | 0b6102a9bd4e34ca319192fce8fbffcc MD5 | raw file
  1. //
  2. // SubSonic - http://subsonicproject.com
  3. //
  4. // The contents of this file are subject to the New BSD
  5. // License (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.opensource.org/licenses/bsd-license.php
  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.Extensions;
  19. using SubSonic.DataProviders;
  20. namespace SubSonic.Query
  21. {
  22. /// <summary>
  23. /// This set of classes abstracts out commands and their parameters so that
  24. /// the DataProviders can work their magic regardless of the client type. The
  25. /// System.Data.Common class was supposed to do this, but sort of fell flat
  26. /// when it came to MySQL and other DB Providers that don't implement the Data
  27. /// Factory pattern. Abstracts out the assignment of parameters, etc
  28. /// </summary>
  29. public class QueryParameter
  30. {
  31. internal const ParameterDirection DefaultParameterDirection = ParameterDirection.Input;
  32. internal const int DefaultSize = 50;
  33. private ParameterDirection _mode = DefaultParameterDirection;
  34. private int _size = DefaultSize;
  35. public int Scale { get; set; }
  36. public int Precision { get; set; }
  37. /// <summary>
  38. /// Gets or sets the size.
  39. /// </summary>
  40. /// <value>The size.</value>
  41. public int Size
  42. {
  43. get { return _size; }
  44. set { _size = value; }
  45. }
  46. /// <summary>
  47. /// Gets or sets the mode.
  48. /// </summary>
  49. /// <value>The mode.</value>
  50. public ParameterDirection Mode
  51. {
  52. get { return _mode; }
  53. set { _mode = value; }
  54. }
  55. /// <summary>
  56. /// Gets or sets the name of the parameter.
  57. /// </summary>
  58. /// <value>The name of the parameter.</value>
  59. public string ParameterName { get; set; }
  60. /// <summary>
  61. /// Gets or sets the parameter value.
  62. /// </summary>
  63. /// <value>The parameter value.</value>
  64. public object ParameterValue { get; set; }
  65. /// <summary>
  66. /// Gets or sets the type of the data.
  67. /// </summary>
  68. /// <value>The type of the data.</value>
  69. public DbType DataType { get; set; }
  70. }
  71. /// <summary>
  72. /// Summary for the QueryParameterCollection class
  73. /// </summary>
  74. public class QueryParameterCollection : List<QueryParameter>
  75. {
  76. /// <summary>
  77. /// Checks to see if specified parameter exists in the current collection
  78. /// </summary>
  79. /// <param name="parameterName"></param>
  80. /// <returns></returns>
  81. public bool Contains(string parameterName)
  82. {
  83. foreach(QueryParameter p in this)
  84. {
  85. if(p.ParameterName.MatchesTrimmed(parameterName))
  86. return true;
  87. }
  88. return false;
  89. }
  90. /// <summary>
  91. /// returns the specified QueryParameter, if it exists in this collection
  92. /// </summary>
  93. /// <param name="parameterName"></param>
  94. /// <returns></returns>
  95. public QueryParameter GetParameter(string parameterName)
  96. {
  97. foreach(QueryParameter p in this)
  98. {
  99. if(p.ParameterName.MatchesTrimmed(parameterName))
  100. return p;
  101. }
  102. return null;
  103. }
  104. /// <summary>
  105. /// Adds the specified parameter name.
  106. /// </summary>
  107. /// <param name="parameterName">Name of the parameter.</param>
  108. /// <param name="value">The value.</param>
  109. public void Add(string parameterName, object value)
  110. {
  111. Add(parameterName, value, DbType.AnsiString, ParameterDirection.Input);
  112. }
  113. /// <summary>
  114. /// Adds the specified parameter name.
  115. /// </summary>
  116. /// <param name="parameterName">Name of the parameter.</param>
  117. /// <param name="value">The value.</param>
  118. /// <param name="dataType">Type of the data.</param>
  119. public void Add(string parameterName, object value, DbType dataType)
  120. {
  121. Add(parameterName, value, dataType, ParameterDirection.Input);
  122. }
  123. /// <summary>
  124. /// Adds the specified parameter name.
  125. /// </summary>
  126. /// <param name="parameterName">Name of the parameter.</param>
  127. /// <param name="value">The value.</param>
  128. /// <param name="dataType">Type of the data.</param>
  129. /// <param name="mode">The mode.</param>
  130. public void Add(string parameterName, object value, DbType dataType, ParameterDirection mode)
  131. {
  132. //remove if already added, and replace with last in
  133. if(Contains(parameterName))
  134. Remove(GetParameter(parameterName));
  135. QueryParameter param = new QueryParameter
  136. {
  137. ParameterName = parameterName,
  138. ParameterValue = value,
  139. DataType = dataType,
  140. Mode = mode
  141. };
  142. Add(param);
  143. }
  144. }
  145. /// <summary>
  146. /// Summary for the QueryCommandCollection class
  147. /// </summary>
  148. public class QueryCommandCollection : List<QueryCommand> {}
  149. /// <summary>
  150. /// Summary for the QueryCommand class
  151. /// </summary>
  152. public class QueryCommand
  153. {
  154. private int commandTimeout = 60;
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. public List<object> OutputValues;
  159. private QueryParameterCollection parameters;
  160. /// <summary>
  161. /// Initializes a new instance of the <see cref="QueryCommand"/> class.
  162. /// </summary>
  163. /// <param name="sql">The SQL.</param>
  164. /// <param name="provider">The provider.</param>
  165. public QueryCommand(string sql, IDataProvider provider)
  166. {
  167. CommandSql = sql;
  168. CommandType = CommandType.Text;
  169. parameters = new QueryParameterCollection();
  170. OutputValues = new List<object>();
  171. Provider = provider;
  172. }
  173. public IDataProvider Provider { get; set; }
  174. public string ProviderName { get; set; }
  175. /// <summary>
  176. /// Gets or sets the command timeout (in seconds).
  177. /// </summary>
  178. /// <value>The command timeout.</value>
  179. public int CommandTimeout
  180. {
  181. get { return commandTimeout; }
  182. set { commandTimeout = value; }
  183. }
  184. /// <summary>
  185. /// Gets or sets the type of the command.
  186. /// </summary>
  187. /// <value>The type of the command.</value>
  188. public CommandType CommandType { get; set; }
  189. /// <summary>
  190. /// Gets or sets the command SQL.
  191. /// </summary>
  192. /// <value>The command SQL.</value>
  193. public string CommandSql { get; set; }
  194. /// <summary>
  195. /// Gets or sets the parameters.
  196. /// </summary>
  197. /// <value>The parameters.</value>
  198. public QueryParameterCollection Parameters
  199. {
  200. get { return parameters; }
  201. set { parameters = value; }
  202. }
  203. /// <summary>
  204. /// Determines whether [has output params].
  205. /// </summary>
  206. /// <returns>
  207. /// <c>true</c> if [has output params]; otherwise, <c>false</c>.
  208. /// </returns>
  209. public bool HasOutputParams()
  210. {
  211. bool bOut = false;
  212. //loop the params and see if one is in/out
  213. foreach(QueryParameter param in Parameters)
  214. {
  215. if(param.Mode != ParameterDirection.Input)
  216. {
  217. bOut = true;
  218. break;
  219. }
  220. }
  221. return bOut;
  222. }
  223. /// <summary>
  224. /// Adds the parameter. The public AddParameter methods should call this one.
  225. /// </summary>
  226. /// <param name="parameterName">Name of the parameter.</param>
  227. /// <param name="parameterValue">The parameter value.</param>
  228. /// <param name="maxSize">Size of the max.</param>
  229. /// <param name="dbType">Type of the db.</param>
  230. /// <param name="direction">The direction.</param>
  231. private void AddParameter(string parameterName, object parameterValue, int maxSize, DbType dbType, ParameterDirection direction)
  232. {
  233. if(parameters == null)
  234. parameters = new QueryParameterCollection();
  235. QueryParameter param = new QueryParameter
  236. {
  237. ParameterName = parameterName,
  238. ParameterValue = (parameterValue ?? DBNull.Value),
  239. Mode = direction,
  240. DataType = dbType
  241. };
  242. if(maxSize > -1 && direction != ParameterDirection.Output)
  243. param.Size = maxSize;
  244. parameters.Add(param);
  245. }
  246. /// <summary>
  247. /// Adds the parameter.
  248. /// </summary>
  249. /// <param name="parameterName">Name of the parameter.</param>
  250. /// <param name="parameterValue">The parameter value.</param>
  251. /// <param name="dataType">Type of the data.</param>
  252. /// <param name="parameterDirection">The parameter direction.</param>
  253. public void AddParameter(string parameterName, object parameterValue, DbType dataType, ParameterDirection parameterDirection)
  254. {
  255. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, parameterDirection);
  256. }
  257. /// <summary>
  258. /// Adds the parameter.
  259. /// </summary>
  260. /// <param name="parameterName">Name of the parameter.</param>
  261. /// <param name="parameterValue">The parameter value.</param>
  262. /// <param name="dataType">Type of the data.</param>
  263. public void AddParameter(string parameterName, object parameterValue, DbType dataType)
  264. {
  265. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, QueryParameter.DefaultParameterDirection);
  266. }
  267. /// <summary>
  268. /// Adds the parameter.
  269. /// </summary>
  270. /// <param name="parameterName">Name of the parameter.</param>
  271. /// <param name="parameterValue">The parameter value.</param>
  272. public void AddParameter(string parameterName, object parameterValue)
  273. {
  274. AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, DbType.Object, QueryParameter.DefaultParameterDirection);
  275. }
  276. /// <summary>
  277. /// Adds the output parameter.
  278. /// </summary>
  279. /// <param name="parameterName">Name of the parameter.</param>
  280. /// <param name="maxSize">Size of the max.</param>
  281. /// <param name="dbType">Type of the db.</param>
  282. public void AddOutputParameter(string parameterName, int maxSize, DbType dbType)
  283. {
  284. AddParameter(parameterName, DBNull.Value, maxSize, dbType, ParameterDirection.Output);
  285. }
  286. /// <summary>
  287. /// Adds the output parameter.
  288. /// </summary>
  289. /// <param name="parameterName">Name of the parameter.</param>
  290. /// <param name="maxSize">Size of the max.</param>
  291. public void AddOutputParameter(string parameterName, int maxSize)
  292. {
  293. AddOutputParameter(parameterName, maxSize, DbType.AnsiString);
  294. }
  295. /// <summary>
  296. /// Adds the output parameter.
  297. /// </summary>
  298. /// <param name="parameterName">Name of the parameter.</param>
  299. public void AddOutputParameter(string parameterName)
  300. {
  301. AddOutputParameter(parameterName, -1, DbType.AnsiString);
  302. }
  303. /// <summary>
  304. /// Adds the output parameter.
  305. /// </summary>
  306. /// <param name="parameterName">Name of the parameter.</param>
  307. /// <param name="dbType">Type of the db.</param>
  308. public void AddOutputParameter(string parameterName, DbType dbType)
  309. {
  310. AddOutputParameter(parameterName, -1, dbType);
  311. }
  312. /// <summary>
  313. /// Adds a return parameter (RETURN_VALUE) to the command.
  314. ///
  315. /// </summary>
  316. public void AddReturnParameter()
  317. {
  318. AddParameter("@RETURN_VALUE", null, DbType.Int32, ParameterDirection.ReturnValue);
  319. }
  320. public DbCommand ToDbCommand()
  321. {
  322. return ToDbCommand(null);
  323. }
  324. public DbCommand ToDbCommand(DbTransaction trannie)
  325. {
  326. var connection = Provider.Factory.CreateConnection();
  327. connection.ConnectionString = Provider.ConnectionString;
  328. var cmd = connection.CreateCommand();
  329. cmd.CommandText = CommandSql;
  330. cmd.CommandType = CommandType.Text;
  331. if(trannie != null)
  332. cmd.Transaction = trannie;
  333. foreach(var param in parameters)
  334. {
  335. DbParameter p = cmd.CreateParameter();
  336. p.ParameterName = param.ParameterName;
  337. p.Value = param.ParameterValue ?? DBNull.Value;
  338. p.DbType = param.DataType;
  339. cmd.Parameters.Add(p);
  340. }
  341. return cmd;
  342. }
  343. /// <summary>
  344. /// Suggested by feroalien@hotmail.com
  345. /// Issue 11 fix
  346. /// </summary>
  347. /// <param name="command"></param>
  348. public void GetOutputParameters(DbCommand command)
  349. {
  350. if(HasOutputParams())
  351. {
  352. foreach(QueryParameter p in Parameters)
  353. {
  354. if(p.Mode == ParameterDirection.InputOutput || p.Mode == ParameterDirection.Output || p.Mode == ParameterDirection.ReturnValue)
  355. {
  356. object oVal = command.Parameters[p.ParameterName].Value;
  357. p.ParameterValue = oVal;
  358. OutputValues.Add(oVal);
  359. }
  360. }
  361. }
  362. }
  363. }
  364. }