OleDbCommand.cs 7.0 KB

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