PageRenderTime 97ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities.SQL/SQL/MicroORM/Command.cs

#
C# | 256 lines | 119 code | 36 blank | 101 comment | 20 complexity | 232f357573cfcb4c8394ef0bc13101a7 MD5 | raw file
  1. /*
  2. Copyright (c) 2012 <a href="http://www.gutgames.com">James Craig</a>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.*/
  18. #region Usings
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Data;
  22. using System.Globalization;
  23. using System.Linq;
  24. using Utilities.DataTypes.ExtensionMethods;
  25. using Utilities.SQL.Interfaces;
  26. using Utilities.SQL.MicroORM.Interfaces;
  27. #endregion
  28. namespace Utilities.SQL.MicroORM
  29. {
  30. /// <summary>
  31. /// Holds information for an individual SQL command
  32. /// </summary>
  33. public class Command : ICommand
  34. {
  35. #region Constructor
  36. /// <summary>
  37. /// Constructor
  38. /// </summary>
  39. /// <param name="SQLCommand">Actual SQL command</param>
  40. /// <param name="CommandType">Command type</param>
  41. /// <param name="Parameters">Parameters</param>
  42. /// <param name="ParameterStarter">Parameter prefix</param>
  43. public Command(string SQLCommand, CommandType CommandType, string ParameterStarter = "@", params object[] Parameters)
  44. {
  45. this.CommandType = CommandType;
  46. this.SQLCommand = SQLCommand;
  47. this.Parameters = new List<IParameter>();
  48. AddParameter(ParameterStarter, Parameters);
  49. }
  50. /// <summary>
  51. /// Constructor
  52. /// </summary>
  53. /// <param name="SQLCommand">Actual SQL command</param>
  54. /// <param name="CommandType">Command type</param>
  55. /// <param name="Parameters">Parameters</param>
  56. public Command(string SQLCommand, CommandType CommandType, params IParameter[] Parameters)
  57. {
  58. this.CommandType = CommandType;
  59. this.SQLCommand = SQLCommand;
  60. this.Parameters = new List<IParameter>();
  61. AddParameter(Parameters);
  62. }
  63. #endregion
  64. #region Properties
  65. /// <summary>
  66. /// Actual SQL command
  67. /// </summary>
  68. public string SQLCommand { get; protected set; }
  69. /// <summary>
  70. /// Command type
  71. /// </summary>
  72. public CommandType CommandType { get; protected set; }
  73. /// <summary>
  74. /// Parameters associated with the command
  75. /// </summary>
  76. public ICollection<IParameter> Parameters { get; private set; }
  77. #endregion
  78. #region Functions
  79. #region AddParameter
  80. /// <summary>
  81. /// Adds a parameter to the command
  82. /// </summary>
  83. /// <param name="ID">Parameter ID</param>
  84. /// <param name="Parameter">Parameter value</param>
  85. /// <typeparam name="DataType">Data type of the parameter</typeparam>
  86. /// <param name="Direction">Direction of the parameter</param>
  87. /// <param name="ParameterStarter">Parameter prefix</param>
  88. public virtual void AddParameter<DataType>(string ID, DataType Parameter, ParameterDirection Direction = ParameterDirection.Input, string ParameterStarter = "@")
  89. {
  90. this.Parameters.Add(new Parameter<DataType>(ID, Parameter, Direction, ParameterStarter));
  91. }
  92. /// <summary>
  93. /// Adds a parameter to the command
  94. /// </summary>
  95. /// <param name="ID">Parameter ID</param>
  96. /// <param name="Parameter">Parameter value</param>
  97. /// <param name="Direction">Direction of the parameter</param>
  98. /// <param name="ParameterStarter">Parameter starter</param>
  99. public virtual void AddParameter(string ID, string Parameter, ParameterDirection Direction = ParameterDirection.Input, string ParameterStarter = "@")
  100. {
  101. this.Parameters.Add(new StringParameter(ID, Parameter, Direction, ParameterStarter));
  102. }
  103. /// <summary>
  104. /// Adds a parameter to the command
  105. /// </summary>
  106. /// <param name="Parameters">Parameter values</param>
  107. /// <param name="ParameterStarter">Parameter starter</param>
  108. public virtual void AddParameter(string ParameterStarter = "@", params object[] Parameters)
  109. {
  110. if (Parameters != null)
  111. {
  112. foreach (object Parameter in Parameters)
  113. {
  114. string TempParameter = Parameter as string;
  115. if (Parameter == null)
  116. this.Parameters.Add(new Parameter<object>(this.Parameters.Count.ToString(CultureInfo.InvariantCulture), default(DbType), null, ParameterDirection.Input, ParameterStarter));
  117. else if (TempParameter != null)
  118. this.Parameters.Add(new StringParameter(this.Parameters.Count.ToString(CultureInfo.InvariantCulture), TempParameter, ParameterDirection.Input, ParameterStarter));
  119. else
  120. this.Parameters.Add(new Parameter<object>(this.Parameters.Count.ToString(CultureInfo.InvariantCulture), Parameter, ParameterDirection.Input, ParameterStarter));
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Adds a parameter to the call (for all types other than strings)
  126. /// </summary>
  127. /// <param name="ID">Name of the parameter</param>
  128. /// <param name="Value">Value to add</param>
  129. /// <param name="Type">SQL type of the parameter</param>
  130. /// <param name="Direction">Parameter direction (defaults to input)</param>
  131. /// <param name="ParameterStarter">Parameter starter</param>
  132. public virtual void AddParameter(string ID, SqlDbType Type, object Value = null, ParameterDirection Direction = ParameterDirection.Input, string ParameterStarter = "@")
  133. {
  134. AddParameter(ID, Type.To(DbType.Int32), Value, Direction, ParameterStarter);
  135. }
  136. /// <summary>
  137. /// Adds a parameter to the call (for all types other than strings)
  138. /// </summary>
  139. /// <param name="ID">Name of the parameter</param>
  140. /// <param name="Value">Value to add</param>
  141. /// <param name="Type">SQL type of the parameter</param>
  142. /// <param name="Direction">Parameter direction (defaults to input)</param>
  143. /// <param name="ParameterStarter">Parameter starter</param>
  144. public virtual void AddParameter(string ID, DbType Type, object Value = null, ParameterDirection Direction = ParameterDirection.Input, string ParameterStarter = "@")
  145. {
  146. Parameters.Add(new Parameter<object>(ID, Type, Value, Direction, ParameterStarter));
  147. }
  148. /// <summary>
  149. /// Adds a parameter to the command
  150. /// </summary>
  151. /// <param name="Parameters">Parameter values</param>
  152. public virtual void AddParameter(params IParameter[] Parameters)
  153. {
  154. if (Parameters != null)
  155. foreach (IParameter Parameter in Parameters)
  156. this.Parameters.Add(Parameter);
  157. }
  158. #endregion
  159. #region ClearParameters
  160. /// <summary>
  161. /// Clears the parameters for the command
  162. /// </summary>
  163. public virtual void ClearParameters()
  164. {
  165. this.Parameters.Clear();
  166. }
  167. #endregion
  168. #region Equals
  169. /// <summary>
  170. /// Determines if the objects are equal
  171. /// </summary>
  172. /// <param name="obj">Object to compare to</param>
  173. /// <returns>Determines if the commands are equal</returns>
  174. public override bool Equals(object obj)
  175. {
  176. Command OtherCommand = obj as Command;
  177. if (OtherCommand == null)
  178. return false;
  179. if (OtherCommand.SQLCommand != SQLCommand
  180. || OtherCommand.CommandType != CommandType
  181. || Parameters.Count != OtherCommand.Parameters.Count)
  182. return false;
  183. foreach (IParameter Parameter in Parameters)
  184. if (!OtherCommand.Parameters.Contains(Parameter))
  185. return false;
  186. foreach (IParameter Parameter in OtherCommand.Parameters)
  187. if (!Parameters.Contains(Parameter))
  188. return false;
  189. return true;
  190. }
  191. #endregion
  192. #region GetHashCode
  193. /// <summary>
  194. /// Returns the hash code for the command
  195. /// </summary>
  196. /// <returns>The hash code for the object</returns>
  197. public override int GetHashCode()
  198. {
  199. int ParameterTotal = Parameters.Sum(x => x.GetHashCode());
  200. if (ParameterTotal > 0)
  201. return (SQLCommand.GetHashCode() * 23 + CommandType.GetHashCode()) * 23 + ParameterTotal;
  202. return SQLCommand.GetHashCode() * 23 + CommandType.GetHashCode();
  203. }
  204. #endregion
  205. #region ToString
  206. /// <summary>
  207. /// Returns the string representation of the command
  208. /// </summary>
  209. /// <returns>The string representation of the command</returns>
  210. public override string ToString()
  211. {
  212. return SQLCommand;
  213. }
  214. #endregion
  215. #endregion
  216. }
  217. }