SqlCommand.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 sealed 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. protected override string InternalCommandText {
  74. get {
  75. string commandText = 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. }
  87. public new SqlConnection Connection
  88. {
  89. get { return (SqlConnection)base.Connection; }
  90. set { base.Connection = value; }
  91. }
  92. public new SqlParameterCollection Parameters
  93. {
  94. get {
  95. return (SqlParameterCollection)base.Parameters;
  96. }
  97. }
  98. public new SqlTransaction Transaction
  99. {
  100. get { return (SqlTransaction)base.Transaction; }
  101. set { base.Transaction = value; }
  102. }
  103. #if USE_DOTNET_REGEX
  104. protected override Regex StoredProcedureRegExp
  105. #else
  106. protected override java.util.regex.Pattern StoredProcedureRegExp {
  107. #endif
  108. get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }
  109. }
  110. protected override SimpleRegex ParameterRegExp
  111. {
  112. get { return SqlStatementsHelper.NamedParameterRegExp; }
  113. }
  114. #endregion // Properties
  115. #region Methods
  116. public XmlReader ExecuteXmlReader() {
  117. return SqlXmlTextReader.Create(ExecuteReader(CommandBehavior.SequentialAccess));
  118. }
  119. public new SqlDataReader ExecuteReader()
  120. {
  121. return (SqlDataReader)ExecuteReader(CommandBehavior.Default);
  122. }
  123. public new SqlDataReader ExecuteReader(CommandBehavior behavior)
  124. {
  125. return (SqlDataReader)base.ExecuteReader(behavior);
  126. }
  127. public new SqlParameter CreateParameter()
  128. {
  129. return (SqlParameter)CreateParameterInternal();
  130. }
  131. protected sealed override void CheckParameters()
  132. {
  133. // do nothing
  134. }
  135. protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)
  136. {
  137. // Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
  138. // parameterName = match.Result("${USERPARAM}");
  139. // if (parameterName.Length == 0)
  140. // return null;
  141. for(int i=0; i < userParametersList.Count; i++) {
  142. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  143. if (String.Compare(parameterName, userParameter.Placeholder.Trim(), true, System.Globalization.CultureInfo.InvariantCulture) == 0) {
  144. return userParameter;
  145. }
  146. }
  147. return null;
  148. }
  149. protected override AbstractDbParameter GetReturnParameter (IList userParametersList)
  150. {
  151. for(int i=0; i < userParametersList.Count; i++) {
  152. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  153. if (userParameter.Direction == ParameterDirection.ReturnValue) {
  154. return userParameter;
  155. }
  156. }
  157. return null;
  158. }
  159. protected sealed override DbParameter CreateParameterInternal()
  160. {
  161. return new SqlParameter();
  162. }
  163. protected sealed override DbDataReader CreateReader()
  164. {
  165. return new SqlDataReader(this);
  166. }
  167. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  168. {
  169. return new SqlParameterCollection((SqlCommand)parent);
  170. }
  171. protected internal sealed override SystemException CreateException(SQLException e)
  172. {
  173. return new SqlException(e, Connection);
  174. }
  175. #region Asynchronous behavior
  176. #if NET_2_0
  177. [MonoNotSupported ("Asynchronous behavior not implemented")]
  178. public IAsyncResult BeginExecuteReader ()
  179. {
  180. throw new NotImplementedException ();
  181. }
  182. [MonoNotSupported ("Asynchronous behavior not implemented")]
  183. public IAsyncResult BeginExecuteReader (CommandBehavior behavior)
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. [MonoNotSupported ("Asynchronous behavior not implemented")]
  188. public IAsyncResult BeginExecuteReader (AsyncCallback callback, Object stateObject)
  189. {
  190. throw new NotImplementedException ();
  191. }
  192. [MonoNotSupported ("Asynchronous behavior not implemented")]
  193. public IAsyncResult BeginExecuteReader (AsyncCallback callback, Object stateObject, CommandBehavior behavior)
  194. {
  195. throw new NotImplementedException ();
  196. }
  197. [MonoNotSupported ("Asynchronous behavior not implemented")]
  198. public SqlDataReader EndExecuteReader (IAsyncResult asyncResult)
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. [MonoNotSupported ("Asynchronous behavior not implemented")]
  203. public IAsyncResult BeginExecuteXmlReader ()
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. [MonoNotSupported ("Asynchronous behavior not implemented")]
  208. public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, Object stateObject)
  209. {
  210. throw new NotImplementedException ();
  211. }
  212. [MonoNotSupported ("Asynchronous behavior not implemented")]
  213. public XmlReader EndExecuteXmlReader (IAsyncResult asyncResult)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. [MonoNotSupported ("Asynchronous behavior not implemented")]
  218. public IAsyncResult BeginExecuteNonQuery ()
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. [MonoNotSupported ("Asynchronous behavior not implemented")]
  223. public IAsyncResult BeginExecuteNonQuery (AsyncCallback callback, Object stateObject)
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. [MonoNotSupported ("Asynchronous behavior not implemented")]
  228. public int EndExecuteNonQuery (IAsyncResult asyncResult)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. #endif
  233. #endregion
  234. #endregion // Methods
  235. }
  236. }