2 // System.Data.OleDb.OleDbCommand
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
;
36 using System
.Text
.RegularExpressions
;
38 using System
.Data
.Common
;
39 using System
.Data
.ProviderBase
;
42 // Cannot use this because it makes ArrayList ambiguous reference
45 namespace System
.Data
.OleDb
47 public sealed class OleDbCommand
: AbstractDbCommand
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
;
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
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)
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)
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.
110 OleDbConnection connection
,
111 OleDbTransaction transaction
)
112 : base(cmdText
, connection
, transaction
)
116 #endregion // Constructors
120 public new OleDbConnection Connection
122 get { return (OleDbConnection)base.Connection; }
123 set { base.Connection = (AbstractDBConnection)value; }
126 public new OleDbParameterCollection Parameters
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
143 ResultSet resultSet
= base.CurrentResultSet
;
145 if (resultSet
!= null) {
148 return CurrentRefCursor
;
150 catch(SQLException e
) {
151 throw CreateException(e
);
156 private ResultSet CurrentRefCursor
159 if (_currentParameterIndex
< 0) {
162 if (_currentRefCursor
== null && _currentParameterIndex
< InternalParameters
.Count
) {
163 _currentRefCursor
= (ResultSet
)((CallableStatement
)Statement
).getObject(_currentParameterIndex
+ 1);
165 return _currentRefCursor
;
169 #endregion // Properties
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;
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
);
234 base.BindOutputParameter(parameter
, parameterIndex
);
238 protected override bool SkipParameter(DbParameter parameter
)
240 return ((OleDbParameter
)parameter
).IsOracleRefCursor
;
243 protected internal override bool NextResultSet()
246 bool hasMoreResults
= base.NextResultSet();
248 if (hasMoreResults
) {
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
) {
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