SqlCommand.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // System.Data.SqlClient.SqlCommand
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 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.Text;
  33. using System.Text.RegularExpressions;
  34. using System.Data;
  35. using System.Data.Common;
  36. using System.Data.ProviderBase;
  37. using System.Xml;
  38. using java.sql;
  39. namespace System.Data.SqlClient
  40. {
  41. public class SqlCommand : AbstractDbCommand
  42. {
  43. #region Fields
  44. #endregion // Fields
  45. #region Constructors
  46. // Initializes a new instance of the SqlCommand class.
  47. // The base constructor initializes all fields to their default values.
  48. // The following table shows initial property values for an instance of SqlCommand.
  49. public SqlCommand() : this(null, null, null)
  50. {
  51. }
  52. public SqlCommand(SqlConnection connection) : this(null, connection, null)
  53. {
  54. }
  55. // Initializes a new instance of the SqlCommand class with the text of the query.
  56. public SqlCommand(String cmdText) : this(cmdText, null, null)
  57. {
  58. }
  59. // Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.
  60. public SqlCommand(String cmdText, SqlConnection connection) : this(cmdText, connection, null)
  61. {
  62. }
  63. // Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the Transaction.
  64. public SqlCommand(
  65. String cmdText,
  66. SqlConnection connection,
  67. SqlTransaction transaction)
  68. : base(cmdText, connection, transaction)
  69. {
  70. }
  71. #endregion // Constructors
  72. #region Properties
  73. public override string CommandText {
  74. get {
  75. string commandText = base.CommandText;
  76. if (CommandType != CommandType.StoredProcedure ||
  77. string.IsNullOrEmpty (commandText) ||
  78. commandText [0] == '[' ||
  79. commandText.IndexOf ('.') >= 0)
  80. return commandText;
  81. string trimmedCommandText = commandText.TrimEnd ();
  82. if (trimmedCommandText.Length > 0 && trimmedCommandText [trimmedCommandText.Length - 1] != ')')
  83. commandText = String.Concat ("[", commandText, "]");
  84. return commandText;
  85. }
  86. set {
  87. base.CommandText = value;
  88. }
  89. }
  90. public new SqlConnection Connection
  91. {
  92. get { return (SqlConnection)base.Connection; }
  93. set { base.Connection = value; }
  94. }
  95. public new SqlParameterCollection Parameters
  96. {
  97. get {
  98. return (SqlParameterCollection)base.Parameters;
  99. }
  100. }
  101. public new SqlTransaction Transaction
  102. {
  103. get { return (SqlTransaction)base.Transaction; }
  104. set { base.Transaction = value; }
  105. }
  106. #if USE_DOTNET_REGEX
  107. protected override Regex StoredProcedureRegExp
  108. #else
  109. protected override java.util.regex.Pattern StoredProcedureRegExp {
  110. #endif
  111. get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }
  112. }
  113. protected override SimpleRegex ParameterRegExp
  114. {
  115. get { return SqlStatementsHelper.NamedParameterRegExp; }
  116. }
  117. #endregion // Properties
  118. #region Methods
  119. public XmlReader ExecuteXmlReader() {
  120. return SqlXmlTextReader.Create(ExecuteReader(CommandBehavior.SequentialAccess));
  121. }
  122. public new SqlDataReader ExecuteReader()
  123. {
  124. return (SqlDataReader)ExecuteReader(CommandBehavior.Default);
  125. }
  126. public new SqlDataReader ExecuteReader(CommandBehavior behavior)
  127. {
  128. return (SqlDataReader)base.ExecuteReader(behavior);
  129. }
  130. public new SqlParameter CreateParameter()
  131. {
  132. return (SqlParameter)CreateParameterInternal();
  133. }
  134. protected sealed override void CheckParameters()
  135. {
  136. // do nothing
  137. }
  138. protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)
  139. {
  140. // Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
  141. // parameterName = match.Result("${USERPARAM}");
  142. // if (parameterName.Length == 0)
  143. // return null;
  144. for(int i=0; i < userParametersList.Count; i++) {
  145. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  146. if (String.Compare(parameterName, userParameter.Placeholder.Trim(), true, System.Globalization.CultureInfo.InvariantCulture) == 0) {
  147. return userParameter;
  148. }
  149. }
  150. return null;
  151. }
  152. protected override AbstractDbParameter GetReturnParameter (IList userParametersList)
  153. {
  154. for(int i=0; i < userParametersList.Count; i++) {
  155. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  156. if (userParameter.Direction == ParameterDirection.ReturnValue) {
  157. return userParameter;
  158. }
  159. }
  160. return null;
  161. }
  162. protected sealed override DbParameter CreateParameterInternal()
  163. {
  164. return new SqlParameter();
  165. }
  166. protected sealed override DbDataReader CreateReader()
  167. {
  168. return new SqlDataReader(this);
  169. }
  170. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  171. {
  172. return new SqlParameterCollection((SqlCommand)parent);
  173. }
  174. protected internal sealed override SystemException CreateException(SQLException e)
  175. {
  176. return new SqlException(e, Connection);
  177. }
  178. #endregion // Methods
  179. }
  180. }