2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Data / System.Data.OleDb.jvm / OleDbCommand.cs
1 //\r
2 // System.Data.OleDb.OleDbCommand\r
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 //
10
11 //
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.
30 //\r
31 \r
32 \r
33 using System;\r
34 using System.Collections;\r
35 using System.Text;\r
36 using System.Text.RegularExpressions;\r
37 using System.Data;\r
38 using System.Data.Common;\r
39 using System.Data.ProviderBase;\r
40 \r
41 using java.sql;\r
42 // Cannot use this because it makes ArrayList ambiguous reference\r
43 //using java.util;\r
44 \r
45 namespace System.Data.OleDb\r
46 {\r
47         public sealed class OleDbCommand : AbstractDbCommand\r
48         {\r
49 \r
50                 #region Fields\r
51 \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
56 \r
57                 #endregion // Fields\r
58 \r
59                 #region Constructors\r
60 \r
61                 static OleDbCommand()\r
62                 {\r
63                         try {\r
64                                 java.lang.Class OracleTypesClass = java.lang.Class.forName("oracle.jdbc.OracleTypes");\r
65                                 _oracleRefCursor = OracleTypesClass.getField("CURSOR").getInt(null);\r
66                         }\r
67                         catch(java.lang.ClassNotFoundException e) {\r
68                                 // oracle driver is not in classpath - just continue\r
69                         }\r
70                 }\r
71 \r
72                 /**\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
76                  */\r
77                 public OleDbCommand() : this(null, null, null)\r
78                 {\r
79                 }\r
80 \r
81                 public OleDbCommand(OleDbConnection connection) : this(null, connection, null)\r
82                 {\r
83                 }\r
84 \r
85                 /**\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
88                  */\r
89                 public OleDbCommand(String cmdText) : this(cmdText, null, null)\r
90                 {\r
91                 }\r
92 \r
93                 /**\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
97                  */\r
98                 public OleDbCommand(String cmdText, OleDbConnection connection) : this(cmdText, connection, null)\r
99                 {\r
100                 }\r
101 \r
102                 /**\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
107                  */\r
108                 public OleDbCommand(\r
109                         String cmdText,\r
110                         OleDbConnection connection,\r
111                         OleDbTransaction transaction)\r
112                         : base(cmdText, connection, transaction)\r
113                 {\r
114                 }\r
115 \r
116                 #endregion // Constructors\r
117 \r
118                 #region Properties\r
119 \r
120                 public new OleDbConnection Connection\r
121                 {\r
122                         get { return (OleDbConnection)base.Connection; }\r
123                         set { base.Connection = (AbstractDBConnection)value; }\r
124                 }\r
125 \r
126                 public new OleDbParameterCollection Parameters\r
127                 {\r
128                         get { \r
129                                 return (OleDbParameterCollection)base.Parameters; \r
130                         }\r
131                 }\r
132 \r
133                 public new OleDbTransaction Transaction\r
134                 {\r
135                         get { return (OleDbTransaction)base.Transaction; }\r
136                         set { base.Transaction = (DbTransaction)value; }\r
137                 }\r
138 \r
139                 protected internal sealed override ResultSet CurrentResultSet\r
140                 {\r
141                         get { \r
142                                 try {\r
143                                         ResultSet resultSet = base.CurrentResultSet;\r
144  \r
145                                         if (resultSet != null) {\r
146                                                 return resultSet;                                               \r
147                                         }\r
148                                         return CurrentRefCursor;\r
149                                 }\r
150                                 catch(SQLException e) {\r
151                                         throw CreateException(e);\r
152                                 }\r
153                         }\r
154                 }\r
155 \r
156                 private ResultSet CurrentRefCursor\r
157                 {\r
158                         get {\r
159                                 if (_currentParameterIndex < 0) {\r
160                                         NextRefCursor();\r
161                                 }\r
162                                 if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {\r
163                                         _currentRefCursor = (ResultSet)((CallableStatement)Statement).getObject(_currentParameterIndex + 1);\r
164                                 }\r
165                                 return _currentRefCursor;\r
166                         }\r
167                 }\r
168 \r
169                 #endregion // Properties\r
170 \r
171                 #region Methods\r
172 \r
173                 public new OleDbDataReader ExecuteReader()\r
174                 {\r
175                         return (OleDbDataReader)ExecuteReader(CommandBehavior.Default);\r
176                 }\r
177 \r
178                 public new OleDbDataReader ExecuteReader(CommandBehavior behavior)\r
179                 {\r
180                         return (OleDbDataReader)base.ExecuteReader(behavior);\r
181                 }\r
182 \r
183                 public new OleDbParameter CreateParameter()\r
184                 {\r
185                         return (OleDbParameter)CreateParameterInternal();\r
186                 } \r
187 \r
188                 protected sealed override void CheckParameters()\r
189                 {\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
194                                 }\r
195 \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
200                                 }\r
201                         }\r
202                 }\r
203 \r
204                 protected sealed override DbParameter CreateParameterInternal()\r
205                 {\r
206                         return new OleDbParameter();\r
207                 }\r
208 \r
209                 protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)\r
210                 {\r
211                         return new OleDbParameterCollection((OleDbCommand)parent);\r
212                 }\r
213 \r
214                 public override object Clone() {\r
215                         OleDbCommand clone = (OleDbCommand)base.Clone();\r
216                         clone._currentParameterIndex = 0;\r
217                         clone._currentRefCursor = null;\r
218                         return clone;\r
219                 }\r
220 \r
221                 protected override void PrepareInternalParameters()\r
222                 {\r
223                         InternalParameters.Clear();\r
224                         _currentParameterIndex = -1;\r
225                 }\r
226 \r
227                 protected override void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)\r
228                 {\r
229                         CallableStatement callableStatement = ((CallableStatement)Statement);\r
230                         if (((OleDbParameter)parameter).IsOracleRefCursor) {\r
231                                 callableStatement.registerOutParameter(++parameterIndex, _oracleRefCursor);\r
232                         }\r
233                         else {\r
234                                 base.BindOutputParameter(parameter, parameterIndex);\r
235                         }\r
236                 }\r
237 \r
238                 protected override bool SkipParameter(DbParameter parameter)\r
239                 {\r
240                         return ((OleDbParameter)parameter).IsOracleRefCursor;\r
241                 }\r
242 \r
243                 protected internal override bool NextResultSet()\r
244                 {\r
245                         try { \r
246                                 bool hasMoreResults = base.NextResultSet();\r
247 \r
248                                 if (hasMoreResults) {\r
249                                         return true;\r
250                                 }\r
251                                 else {\r
252                                         return NextRefCursor();\r
253                                 }\r
254                         }\r
255                         catch (SQLException e) {\r
256                                 throw CreateException(e);\r
257                         }\r
258                 }\r
259 \r
260                 private bool NextRefCursor()\r
261                 {\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
266                                         return true;                                            \r
267                                 }\r
268                         }\r
269                         return false;\r
270                 }\r
271 \r
272                 protected sealed override DbDataReader CreateReader()\r
273                 {\r
274                         return new OleDbDataReader(this);\r
275                 }\r
276 \r
277                 protected internal sealed override SystemException CreateException(SQLException e)\r
278                 {\r
279                         return new OleDbException(e,Connection);                \r
280                 }\r
281 \r
282                 #endregion // Methods\r
283       \r
284         }\r
285 }