OleDbCommand.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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
  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. return (OleDbParameterCollection)base.Parameters;
  111. }
  112. }
  113. public new OleDbTransaction Transaction
  114. {
  115. get { return (OleDbTransaction)base.Transaction; }
  116. set { base.Transaction = (DbTransaction)value; }
  117. }
  118. protected internal sealed override ResultSet CurrentResultSet
  119. {
  120. get {
  121. try {
  122. ResultSet resultSet = base.CurrentResultSet;
  123. if (resultSet != null) {
  124. return resultSet;
  125. }
  126. return CurrentRefCursor;
  127. }
  128. catch(SQLException e) {
  129. throw CreateException(e);
  130. }
  131. }
  132. }
  133. private ResultSet CurrentRefCursor
  134. {
  135. get {
  136. if (_currentParameterIndex < 0) {
  137. NextRefCursor();
  138. }
  139. if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {
  140. _currentRefCursor = (ResultSet)((CallableStatement)Statement).getObject(_currentParameterIndex + 1);
  141. }
  142. return _currentRefCursor;
  143. }
  144. }
  145. #endregion // Properties
  146. #region Methods
  147. public new OleDbDataReader ExecuteReader()
  148. {
  149. return (OleDbDataReader)ExecuteReader(CommandBehavior.Default);
  150. }
  151. public new OleDbDataReader ExecuteReader(CommandBehavior behavior)
  152. {
  153. return (OleDbDataReader)base.ExecuteReader(behavior);
  154. }
  155. public new OleDbParameter CreateParameter()
  156. {
  157. return (OleDbParameter)CreateParameterInternal();
  158. }
  159. protected sealed override void CheckParameters()
  160. {
  161. for(int i = 0; i < Parameters.Count; i++) {
  162. OleDbParameter parameter = (OleDbParameter)Parameters[i];
  163. if ((parameter.OleDbType == OleDbType.Empty) || (parameter.OleDbType == OleDbType.Error)) {
  164. throw ExceptionHelper.ParametersNotInitialized(i,parameter.ParameterName,parameter.OleDbType.ToString());
  165. }
  166. if (((parameter.OleDbType == OleDbType.Char) || (parameter.OleDbType == OleDbType.Binary) ||
  167. (parameter.OleDbType == OleDbType.VarWChar) || (parameter.OleDbType == OleDbType.VarBinary) ||
  168. (parameter.OleDbType == OleDbType.VarNumeric)) && (parameter.Size == 0)) {
  169. throw ExceptionHelper.WrongParameterSize("OleDb");
  170. }
  171. }
  172. }
  173. protected sealed override DbParameter CreateParameterInternal()
  174. {
  175. return new OleDbParameter();
  176. }
  177. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  178. {
  179. return new OleDbParameterCollection((OleDbCommand)parent);
  180. }
  181. public override object Clone() {
  182. OleDbCommand clone = (OleDbCommand)base.Clone();
  183. clone._currentParameterIndex = 0;
  184. clone._currentRefCursor = null;
  185. return clone;
  186. }
  187. protected override void PrepareInternalParameters()
  188. {
  189. InternalParameters.Clear();
  190. _currentParameterIndex = -1;
  191. }
  192. protected override void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)
  193. {
  194. CallableStatement callableStatement = ((CallableStatement)Statement);
  195. if (((OleDbParameter)parameter).IsOracleRefCursor) {
  196. callableStatement.registerOutParameter(++parameterIndex, _oracleRefCursor);
  197. }
  198. else {
  199. base.BindOutputParameter(parameter, parameterIndex);
  200. }
  201. }
  202. protected override bool SkipParameter(DbParameter parameter)
  203. {
  204. return ((OleDbParameter)parameter).IsOracleRefCursor;
  205. }
  206. protected internal override bool NextResultSet()
  207. {
  208. try {
  209. bool hasMoreResults = base.NextResultSet();
  210. if (hasMoreResults) {
  211. return true;
  212. }
  213. else {
  214. return NextRefCursor();
  215. }
  216. }
  217. catch (SQLException e) {
  218. throw CreateException(e);
  219. }
  220. }
  221. private bool NextRefCursor()
  222. {
  223. _currentRefCursor = null;
  224. // FIXME : should we count all parameters or only out ones?
  225. for (_currentParameterIndex++;InternalParameters.Count > _currentParameterIndex;_currentParameterIndex++) {
  226. if (((OleDbParameter)InternalParameters[_currentParameterIndex]).IsOracleRefCursor) {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. protected sealed override DbDataReader CreateReader()
  233. {
  234. return new OleDbDataReader(this);
  235. }
  236. protected internal sealed override SystemException CreateException(SQLException e)
  237. {
  238. return new OleDbException(e,Connection);
  239. }
  240. #endregion // Methods
  241. }
  242. }