2 // System.Data.OleDb.OleDbCommand
\r
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Boris Kirzner <borisk@mainsoft.com>
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
\r
36 using System.Text.RegularExpressions;
\r
38 using System.Data.Common;
\r
39 using System.Data.ProviderBase;
\r
42 // Cannot use this because it makes ArrayList ambiguous reference
\r
45 namespace System.Data.OleDb
\r
47 public sealed class OleDbCommand : AbstractDbCommand
\r
52 internal static readonly int oracleTypeRefCursor = java.sql.Types.OTHER;
\r
53 private static readonly int _oracleRefCursor = -10; // oracle.jdbc.OracleTypes.CURSOR
\r
54 private int _currentParameterIndex = 0;
\r
55 private ResultSet _currentRefCursor;
\r
57 #endregion // Fields
\r
59 #region Constructors
\r
61 static OleDbCommand()
\r
64 java.lang.Class OracleTypesClass = java.lang.Class.forName("oracle.jdbc.OracleTypes");
\r
65 _oracleRefCursor = OracleTypesClass.getField("CURSOR").getInt(null);
\r
67 catch(java.lang.ClassNotFoundException e) {
\r
68 // oracle driver is not in classpath - just continue
\r
73 * Initializes a new instance of the OleDbCommand class.
\r
74 * The base constructor initializes all fields to their default values.
\r
75 * The following table shows initial property values for an instance of SqlCommand.
\r
77 public OleDbCommand() : this(null, null, null)
\r
81 public OleDbCommand(OleDbConnection connection) : this(null, connection, null)
\r
86 * Initializes a new instance of the OleDbCommand class with the text of the query.
\r
87 * @param cmdText The text of the query.
\r
89 public OleDbCommand(String cmdText) : this(cmdText, null, null)
\r
94 * Initializes a new instance of the OleDbCommand class with the text of the query and a SqlConnection.
\r
95 * @param cmdText The text of the query.
\r
96 * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
\r
98 public OleDbCommand(String cmdText, OleDbConnection connection) : this(cmdText, connection, null)
\r
103 * Initializes a new instance of the OleDbCommand class with the text of the query, a SqlConnection, and the Transaction.
\r
104 * @param cmdText The text of the query.
\r
105 * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
\r
106 * @param transaction The SqlTransaction in which the OleDbCommand executes.
\r
108 public OleDbCommand(
\r
110 OleDbConnection connection,
\r
111 OleDbTransaction transaction)
\r
112 : base(cmdText, connection, transaction)
\r
116 #endregion // Constructors
\r
120 public new OleDbConnection Connection
\r
122 get { return (OleDbConnection)base.Connection; }
\r
123 set { base.Connection = (AbstractDBConnection)value; }
\r
126 public new OleDbParameterCollection Parameters
\r
129 return (OleDbParameterCollection)base.Parameters;
\r
133 public new OleDbTransaction Transaction
\r
135 get { return (OleDbTransaction)base.Transaction; }
\r
136 set { base.Transaction = (DbTransaction)value; }
\r
139 protected internal sealed override ResultSet CurrentResultSet
\r
143 ResultSet resultSet = base.CurrentResultSet;
\r
145 if (resultSet != null) {
\r
148 return CurrentRefCursor;
\r
150 catch(SQLException e) {
\r
151 throw CreateException(e);
\r
156 private ResultSet CurrentRefCursor
\r
159 if (_currentParameterIndex < 0) {
\r
162 if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {
\r
163 _currentRefCursor = (ResultSet)((CallableStatement)Statement).getObject(_currentParameterIndex + 1);
\r
165 return _currentRefCursor;
\r
169 #endregion // Properties
\r
173 public new OleDbDataReader ExecuteReader()
\r
175 return (OleDbDataReader)ExecuteReader(CommandBehavior.Default);
\r
178 public new OleDbDataReader ExecuteReader(CommandBehavior behavior)
\r
180 return (OleDbDataReader)base.ExecuteReader(behavior);
\r
183 public new OleDbParameter CreateParameter()
\r
185 return (OleDbParameter)CreateParameterInternal();
\r
188 protected sealed override void CheckParameters()
\r
190 for(int i = 0; i < Parameters.Count; i++) {
\r
191 OleDbParameter parameter = (OleDbParameter)Parameters[i];
\r
192 if ((parameter.OleDbType == OleDbType.Empty) || (parameter.OleDbType == OleDbType.Error)) {
\r
193 throw ExceptionHelper.ParametersNotInitialized(i,parameter.ParameterName,parameter.OleDbType.ToString());
\r
196 if (((parameter.OleDbType == OleDbType.Char) || (parameter.OleDbType == OleDbType.Binary) ||
\r
197 (parameter.OleDbType == OleDbType.VarWChar) || (parameter.OleDbType == OleDbType.VarBinary) ||
\r
198 (parameter.OleDbType == OleDbType.VarNumeric)) && (parameter.Size == 0)) {
\r
199 throw ExceptionHelper.WrongParameterSize("OleDb");
\r
204 protected sealed override DbParameter CreateParameterInternal()
\r
206 return new OleDbParameter();
\r
209 protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
\r
211 return new OleDbParameterCollection((OleDbCommand)parent);
\r
214 public override object Clone() {
\r
215 OleDbCommand clone = (OleDbCommand)base.Clone();
\r
216 clone._currentParameterIndex = 0;
\r
217 clone._currentRefCursor = null;
\r
221 protected override void PrepareInternalParameters()
\r
223 InternalParameters.Clear();
\r
224 _currentParameterIndex = -1;
\r
227 protected override void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)
\r
229 CallableStatement callableStatement = ((CallableStatement)Statement);
\r
230 if (((OleDbParameter)parameter).IsOracleRefCursor) {
\r
231 callableStatement.registerOutParameter(++parameterIndex, _oracleRefCursor);
\r
234 base.BindOutputParameter(parameter, parameterIndex);
\r
238 protected override bool SkipParameter(DbParameter parameter)
\r
240 return ((OleDbParameter)parameter).IsOracleRefCursor;
\r
243 protected internal override bool NextResultSet()
\r
246 bool hasMoreResults = base.NextResultSet();
\r
248 if (hasMoreResults) {
\r
252 return NextRefCursor();
\r
255 catch (SQLException e) {
\r
256 throw CreateException(e);
\r
260 private bool NextRefCursor()
\r
262 _currentRefCursor = null;
\r
263 // FIXME : should we count all parameters or only out ones?
\r
264 for (_currentParameterIndex++;InternalParameters.Count > _currentParameterIndex;_currentParameterIndex++) {
\r
265 if (((OleDbParameter)InternalParameters[_currentParameterIndex]).IsOracleRefCursor) {
\r
272 protected sealed override DbDataReader CreateReader()
\r
274 return new OleDbDataReader(this);
\r
277 protected internal sealed override SystemException CreateException(SQLException e)
\r
279 return new OleDbException(e,Connection);
\r
282 #endregion // Methods
\r