[Backport to 2.4.3]
[mcs.git] / class / System.Data / System.Data.OleDb.jvm / OleDbCommand.cs
blobe7f163593ba47d78ab046923ca70c94174604496
1 //
2 // System.Data.OleDb.OleDbCommand
3 //
4 // Authors:
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Boris Kirzner <borisk@mainsoft.com>
7 //
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections;
35 using System.Text;
36 using System.Text.RegularExpressions;
37 using System.Data;
38 using System.Data.Common;
39 using System.Data.ProviderBase;
41 using java.sql;
42 // Cannot use this because it makes ArrayList ambiguous reference
43 //using java.util;
45 namespace System.Data.OleDb
47 public sealed class OleDbCommand : AbstractDbCommand
50 #region Fields
52 internal static readonly int oracleTypeRefCursor = java.sql.Types.OTHER;
53 private static readonly int _oracleRefCursor = -10; // oracle.jdbc.OracleTypes.CURSOR
54 private int _currentParameterIndex = 0;
55 private ResultSet _currentRefCursor;
57 #endregion // Fields
59 #region Constructors
61 static OleDbCommand()
63 try {
64 java.lang.Class OracleTypesClass = java.lang.Class.forName("oracle.jdbc.OracleTypes");
65 _oracleRefCursor = OracleTypesClass.getField("CURSOR").getInt(null);
67 catch(java.lang.ClassNotFoundException e) {
68 // oracle driver is not in classpath - just continue
72 /**
73 * Initializes a new instance of the OleDbCommand class.
74 * The base constructor initializes all fields to their default values.
75 * The following table shows initial property values for an instance of SqlCommand.
77 public OleDbCommand() : this(null, null, null)
81 public OleDbCommand(OleDbConnection connection) : this(null, connection, null)
85 /**
86 * Initializes a new instance of the OleDbCommand class with the text of the query.
87 * @param cmdText The text of the query.
89 public OleDbCommand(String cmdText) : this(cmdText, null, null)
93 /**
94 * Initializes a new instance of the OleDbCommand class with the text of the query and a SqlConnection.
95 * @param cmdText The text of the query.
96 * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
98 public OleDbCommand(String cmdText, OleDbConnection connection) : this(cmdText, connection, null)
103 * Initializes a new instance of the OleDbCommand class with the text of the query, a SqlConnection, and the Transaction.
104 * @param cmdText The text of the query.
105 * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
106 * @param transaction The SqlTransaction in which the OleDbCommand executes.
108 public OleDbCommand(
109 String cmdText,
110 OleDbConnection connection,
111 OleDbTransaction transaction)
112 : base(cmdText, connection, transaction)
116 #endregion // Constructors
118 #region Properties
120 public new OleDbConnection Connection
122 get { return (OleDbConnection)base.Connection; }
123 set { base.Connection = (AbstractDBConnection)value; }
126 public new OleDbParameterCollection Parameters
128 get {
129 return (OleDbParameterCollection)base.Parameters;
133 public new OleDbTransaction Transaction
135 get { return (OleDbTransaction)base.Transaction; }
136 set { base.Transaction = (DbTransaction)value; }
139 protected internal sealed override ResultSet CurrentResultSet
141 get {
142 try {
143 ResultSet resultSet = base.CurrentResultSet;
145 if (resultSet != null) {
146 return resultSet;
148 return CurrentRefCursor;
150 catch(SQLException e) {
151 throw CreateException(e);
156 private ResultSet CurrentRefCursor
158 get {
159 if (_currentParameterIndex < 0) {
160 NextRefCursor();
162 if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {
163 _currentRefCursor = (ResultSet)((CallableStatement)Statement).getObject(_currentParameterIndex + 1);
165 return _currentRefCursor;
169 #endregion // Properties
171 #region Methods
173 public new OleDbDataReader ExecuteReader()
175 return (OleDbDataReader)ExecuteReader(CommandBehavior.Default);
178 public new OleDbDataReader ExecuteReader(CommandBehavior behavior)
180 return (OleDbDataReader)base.ExecuteReader(behavior);
183 public new OleDbParameter CreateParameter()
185 return (OleDbParameter)CreateParameterInternal();
188 protected sealed override void CheckParameters()
190 for(int i = 0; i < Parameters.Count; i++) {
191 OleDbParameter parameter = (OleDbParameter)Parameters[i];
192 if ((parameter.OleDbType == OleDbType.Empty) || (parameter.OleDbType == OleDbType.Error)) {
193 throw ExceptionHelper.ParametersNotInitialized(i,parameter.ParameterName,parameter.OleDbType.ToString());
196 if (((parameter.OleDbType == OleDbType.Char) || (parameter.OleDbType == OleDbType.Binary) ||
197 (parameter.OleDbType == OleDbType.VarWChar) || (parameter.OleDbType == OleDbType.VarBinary) ||
198 (parameter.OleDbType == OleDbType.VarNumeric)) && (parameter.Size == 0)) {
199 throw ExceptionHelper.WrongParameterSize("OleDb");
204 protected sealed override DbParameter CreateParameterInternal()
206 return new OleDbParameter();
209 protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
211 return new OleDbParameterCollection((OleDbCommand)parent);
214 public override object Clone() {
215 OleDbCommand clone = (OleDbCommand)base.Clone();
216 clone._currentParameterIndex = 0;
217 clone._currentRefCursor = null;
218 return clone;
221 protected override void PrepareInternalParameters()
223 InternalParameters.Clear();
224 _currentParameterIndex = -1;
227 protected override void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)
229 CallableStatement callableStatement = ((CallableStatement)Statement);
230 if (((OleDbParameter)parameter).IsOracleRefCursor) {
231 callableStatement.registerOutParameter(++parameterIndex, _oracleRefCursor);
233 else {
234 base.BindOutputParameter(parameter, parameterIndex);
238 protected override bool SkipParameter(DbParameter parameter)
240 return ((OleDbParameter)parameter).IsOracleRefCursor;
243 protected internal override bool NextResultSet()
245 try {
246 bool hasMoreResults = base.NextResultSet();
248 if (hasMoreResults) {
249 return true;
251 else {
252 return NextRefCursor();
255 catch (SQLException e) {
256 throw CreateException(e);
260 private bool NextRefCursor()
262 _currentRefCursor = null;
263 // FIXME : should we count all parameters or only out ones?
264 for (_currentParameterIndex++;InternalParameters.Count > _currentParameterIndex;_currentParameterIndex++) {
265 if (((OleDbParameter)InternalParameters[_currentParameterIndex]).IsOracleRefCursor) {
266 return true;
269 return false;
272 protected sealed override DbDataReader CreateReader()
274 return new OleDbDataReader(this);
277 protected internal sealed override SystemException CreateException(SQLException e)
279 return new OleDbException(e,Connection);
282 #endregion // Methods