OleDbCommand.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // System.Data.OleDb.OleDbCommand
  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 java.sql;
  38. // Cannot use this because it makes ArrayList ambiguous reference
  39. //using java.util;
  40. namespace System.Data.OleDb
  41. {
  42. public sealed class OleDbCommand : AbstractDbCommand, IDbCommand, ICloneable
  43. {
  44. #region Fields
  45. internal static readonly int oracleTypeRefCursor = java.sql.Types.OTHER;
  46. private static readonly int _oracleRefCursor = -10; // oracle.jdbc.OracleTypes.CURSOR
  47. private int _currentParameterIndex = 0;
  48. private ResultSet _currentRefCursor;
  49. #endregion // Fields
  50. #region Constructors
  51. static OleDbCommand()
  52. {
  53. try {
  54. java.lang.Class OracleTypesClass = java.lang.Class.forName("oracle.jdbc.OracleTypes");
  55. _oracleRefCursor = OracleTypesClass.getField("CURSOR").getInt(null);
  56. }
  57. catch(java.lang.ClassNotFoundException e) {
  58. // oracle driver is not in classpath - just continue
  59. }
  60. }
  61. /**
  62. * Initializes a new instance of the OleDbCommand class.
  63. * The base constructor initializes all fields to their default values.
  64. * The following table shows initial property values for an instance of SqlCommand.
  65. */
  66. public OleDbCommand() : this(null, null, null)
  67. {
  68. }
  69. public OleDbCommand(OleDbConnection connection) : this(null, connection, null)
  70. {
  71. }
  72. /**
  73. * Initializes a new instance of the OleDbCommand class with the text of the query.
  74. * @param cmdText The text of the query.
  75. */
  76. public OleDbCommand(String cmdText) : this(cmdText, null, null)
  77. {
  78. }
  79. /**
  80. * Initializes a new instance of the OleDbCommand class with the text of the query and a SqlConnection.
  81. * @param cmdText The text of the query.
  82. * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
  83. */
  84. public OleDbCommand(String cmdText, OleDbConnection connection) : this(cmdText, connection, null)
  85. {
  86. }
  87. /**
  88. * Initializes a new instance of the OleDbCommand class with the text of the query, a SqlConnection, and the Transaction.
  89. * @param cmdText The text of the query.
  90. * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
  91. * @param transaction The SqlTransaction in which the OleDbCommand executes.
  92. */
  93. public OleDbCommand(
  94. String cmdText,
  95. OleDbConnection connection,
  96. OleDbTransaction transaction)
  97. : base(cmdText, connection, transaction)
  98. {
  99. }
  100. #endregion // Constructors
  101. #region Properties
  102. public new OleDbConnection Connection
  103. {
  104. get { return (OleDbConnection)base.Connection; }
  105. set { base.Connection = (AbstractDBConnection)value; }
  106. }
  107. public new OleDbParameterCollection Parameters
  108. {
  109. get {
  110. if (_parameters == null) {
  111. _parameters = CreateParameterCollection(this);
  112. }
  113. return (OleDbParameterCollection)_parameters;
  114. }
  115. }
  116. public new OleDbTransaction Transaction
  117. {
  118. get { return (OleDbTransaction)base.Transaction; }
  119. set { base.Transaction = (DbTransaction)value; }
  120. }
  121. protected internal sealed override ResultSet CurrentResultSet
  122. {
  123. get {
  124. try {
  125. ResultSet resultSet = base.CurrentResultSet;
  126. if (resultSet != null) {
  127. return resultSet;
  128. }
  129. return CurrentRefCursor;
  130. }
  131. catch(SQLException e) {
  132. throw new Exception(e.Message, e);
  133. }
  134. }
  135. }
  136. private ResultSet CurrentRefCursor
  137. {
  138. get {
  139. if (_currentParameterIndex < 0) {
  140. NextRefCursor();
  141. }
  142. if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {
  143. _currentRefCursor = (ResultSet)((CallableStatement)_statement).getObject(_currentParameterIndex + 1);
  144. }
  145. return _currentRefCursor;
  146. }
  147. }
  148. #endregion // Properties
  149. #region Methods
  150. public new OleDbDataReader ExecuteReader()
  151. {
  152. return (OleDbDataReader)ExecuteReader(CommandBehavior.Default);
  153. }
  154. public new OleDbDataReader ExecuteReader(CommandBehavior behavior)
  155. {
  156. return (OleDbDataReader)base.ExecuteReader(behavior);
  157. }
  158. public new OleDbParameter CreateParameter()
  159. {
  160. return (OleDbParameter)CreateParameterInternal();
  161. }
  162. protected sealed override void CheckParameters()
  163. {
  164. for(int i = 0; i < Parameters.Count; i++) {
  165. OleDbParameter parameter = (OleDbParameter)Parameters[i];
  166. if ((parameter.OleDbType == OleDbType.Empty) || (parameter.OleDbType == OleDbType.Error)) {
  167. throw ExceptionHelper.ParametersNotInitialized(i,parameter.ParameterName,parameter.OleDbType.ToString());
  168. }
  169. if (((parameter.OleDbType == OleDbType.Char) || (parameter.OleDbType == OleDbType.Binary) ||
  170. (parameter.OleDbType == OleDbType.VarWChar) || (parameter.OleDbType == OleDbType.VarBinary) ||
  171. (parameter.OleDbType == OleDbType.VarNumeric)) && (parameter.Size == 0)) {
  172. throw ExceptionHelper.WrongParameterSize("OleDb");
  173. }
  174. }
  175. }
  176. protected sealed override DbParameter CreateParameterInternal()
  177. {
  178. return new OleDbParameter();
  179. }
  180. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  181. {
  182. return new OleDbParameterCollection((OleDbCommand)parent);
  183. }
  184. public object Clone()
  185. {
  186. OleDbCommand clone = new OleDbCommand();
  187. CopyTo(clone);
  188. return clone;
  189. }
  190. protected override void PrepareInternalParameters()
  191. {
  192. InternalParameters.Clear();
  193. _currentParameterIndex = -1;
  194. }
  195. protected override void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)
  196. {
  197. CallableStatement callableStatement = ((CallableStatement)_statement);
  198. if (((OleDbParameter)parameter).IsOracleRefCursor) {
  199. callableStatement.registerOutParameter(++parameterIndex, _oracleRefCursor);
  200. }
  201. else {
  202. base.BindOutputParameter(parameter, parameterIndex);
  203. }
  204. }
  205. protected override bool SkipParameter(DbParameter parameter)
  206. {
  207. return ((OleDbParameter)parameter).IsOracleRefCursor;
  208. }
  209. protected internal override bool NextResultSet()
  210. {
  211. try {
  212. bool hasMoreResults = base.NextResultSet();
  213. if (hasMoreResults) {
  214. return true;
  215. }
  216. else {
  217. return NextRefCursor();
  218. }
  219. }
  220. catch (SQLException e) {
  221. throw CreateException(e);
  222. }
  223. }
  224. private bool NextRefCursor()
  225. {
  226. _currentRefCursor = null;
  227. // FIXME : should we count all parameters or only out ones?
  228. for (_currentParameterIndex++;InternalParameters.Count > _currentParameterIndex;_currentParameterIndex++) {
  229. if (((OleDbParameter)InternalParameters[_currentParameterIndex]).IsOracleRefCursor) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. protected sealed override DbDataReader CreateReader()
  236. {
  237. return new OleDbDataReader(this);
  238. }
  239. protected internal sealed override SystemException CreateException(SQLException e)
  240. {
  241. return new OleDbException(e,Connection);
  242. }
  243. #endregion // Methods
  244. }
  245. }