OracleCommand.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // System.Data.OracleClient.OracleCommand
  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 System.Globalization;
  38. using java.sql;
  39. // Cannot use this because it makes ArrayList ambiguous reference
  40. //using java.util;
  41. #if !USE_DOTNET_REGEXP
  42. using java.util.regex;
  43. #endif
  44. namespace System.Data.OracleClient {
  45. public sealed class OracleCommand : AbstractDbCommand {
  46. #region Fields
  47. #if USE_DOTNET_REGEXP
  48. internal static readonly Regex NamedParameterStoredProcedureRegExp = new Regex(@"^\s*{?\s*((?<RETVAL>\:\w+)\s*=\s*)?call\s+(?<PROCNAME>(((\[[^\]]*\])|([^\.\(])*)\s*\.\s*){0,2}(\[[^\]]*\]|((\s*[^\.\(\)\{\}\s])+)))\s*(\(\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)?\s*(,\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)\s*)*\))?\s*}?\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  49. #else
  50. internal static readonly Pattern NamedParameterStoredProcedureRegExp = Pattern.compile(@"^\s*\{?\s*(?:(\:\w+)\s*=\s*)?call\s+((?:(?:(?:\[[^\]]*\])|(?:[^\.\(\)\{\}\[\]])*)\s*\.\s*){0,2}(?:\[[^\]]*\]|(?:(?:\s*[^\.\(\)\{\}\[\]])+)))\s*(?:\((.*)\))?\s*\}?\s*$", Pattern.CASE_INSENSITIVE);
  51. #endif
  52. internal static readonly SimpleRegex NamedParameterRegExp = new OracleParamsRegex();
  53. // internal static readonly int oracleTypeRefCursor = java.sql.Types.OTHER;
  54. private int _currentParameterIndex = 0;
  55. private ResultSet _currentRefCursor;
  56. #endregion // Fields
  57. #region Constructors
  58. /**
  59. * Initializes a new instance of the OracleCommand class.
  60. * The base constructor initializes all fields to their default values.
  61. * The following table shows initial property values for an instance of SqlCommand.
  62. */
  63. public OracleCommand() : this(null, null, null) {
  64. }
  65. public OracleCommand(OracleConnection connection) : this(null, connection, null) {
  66. }
  67. /**
  68. * Initializes a new instance of the OracleCommand class with the text of the query.
  69. * @param cmdText The text of the query.
  70. */
  71. public OracleCommand(String cmdText) : this(cmdText, null, null) {
  72. }
  73. /**
  74. * Initializes a new instance of the OracleCommand class with the text of the query and a SqlConnection.
  75. * @param cmdText The text of the query.
  76. * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
  77. */
  78. public OracleCommand(String cmdText, OracleConnection connection) : this(cmdText, connection, null) {
  79. }
  80. /**
  81. * Initializes a new instance of the OracleCommand class with the text of the query, a SqlConnection, and the Transaction.
  82. * @param cmdText The text of the query.
  83. * @param connection A SqlConnection that represents the connection to an instance of SQL Server.
  84. * @param transaction The SqlTransaction in which the OracleCommand executes.
  85. */
  86. public OracleCommand(
  87. String cmdText,
  88. OracleConnection connection,
  89. OracleTransaction transaction)
  90. : base(cmdText, connection, transaction) {
  91. }
  92. #endregion // Constructors
  93. #region Properties
  94. public new OracleConnection Connection {
  95. get { return (OracleConnection)base.Connection; }
  96. set { base.Connection = (AbstractDBConnection)value; }
  97. }
  98. public new OracleParameterCollection Parameters {
  99. get {
  100. return (OracleParameterCollection)base.Parameters;
  101. }
  102. }
  103. public new OracleTransaction Transaction {
  104. get { return (OracleTransaction)base.Transaction; }
  105. set { base.Transaction = (DbTransaction)value; }
  106. }
  107. protected override bool SkipParameter(DbParameter parameter) {
  108. return ((OracleParameter)parameter).OracleType == OracleType.Cursor;
  109. }
  110. protected sealed override ResultSet CurrentResultSet {
  111. get {
  112. try {
  113. ResultSet resultSet = base.CurrentResultSet;
  114. if (resultSet != null) {
  115. return resultSet;
  116. }
  117. return CurrentRefCursor;
  118. }
  119. catch(SQLException e) {
  120. throw CreateException(e);
  121. }
  122. }
  123. }
  124. private ResultSet CurrentRefCursor {
  125. get {
  126. if (_currentParameterIndex < 0) {
  127. NextRefCursor();
  128. }
  129. if (_currentRefCursor == null && _currentParameterIndex < InternalParameters.Count) {
  130. _currentRefCursor = (ResultSet)((CallableStatement)Statement).getObject(_currentParameterIndex + 1);
  131. }
  132. return _currentRefCursor;
  133. }
  134. }
  135. #if USE_DOTNET_REGEX
  136. protected override Regex StoredProcedureRegExp
  137. #else
  138. protected override java.util.regex.Pattern StoredProcedureRegExp {
  139. #endif
  140. get { return NamedParameterStoredProcedureRegExp; }
  141. }
  142. protected override SimpleRegex ParameterRegExp {
  143. get { return NamedParameterRegExp; }
  144. }
  145. #endregion // Properties
  146. #region Methods
  147. protected override bool NextResultSet() {
  148. try {
  149. bool hasMoreResults = base.NextResultSet();
  150. if (hasMoreResults) {
  151. return true;
  152. }
  153. else {
  154. return NextRefCursor();
  155. }
  156. }
  157. catch (SQLException e) {
  158. throw CreateException(e);
  159. }
  160. }
  161. private bool NextRefCursor() {
  162. _currentRefCursor = null;
  163. for (_currentParameterIndex++;InternalParameters.Count > _currentParameterIndex;_currentParameterIndex++) {
  164. OracleParameter param = (OracleParameter)InternalParameters[_currentParameterIndex];
  165. if (param.OracleType == OracleType.Cursor && ((param.Direction & ParameterDirection.Output) == ParameterDirection.Output))
  166. return true;
  167. }
  168. return false;
  169. }
  170. public new OracleDataReader ExecuteReader() {
  171. return (OracleDataReader)ExecuteReader(CommandBehavior.Default);
  172. }
  173. public new OracleDataReader ExecuteReader(CommandBehavior behavior) {
  174. return (OracleDataReader)base.ExecuteReader(behavior);
  175. }
  176. public new OracleParameter CreateParameter() {
  177. return (OracleParameter)CreateParameterInternal();
  178. }
  179. protected sealed override void CheckParameters() {
  180. //TBD
  181. }
  182. protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition) {
  183. for(int i=0; i < userParametersList.Count; i++) {
  184. OracleParameter userParameter = (OracleParameter)userParametersList[i];
  185. if (String.Compare(parameterName, userParameter.InternalPlaceholder.Trim(), true, CultureInfo.InvariantCulture) == 0) {
  186. return userParameter;
  187. }
  188. }
  189. return null;
  190. }
  191. protected override AbstractDbParameter GetReturnParameter (IList userParametersList) {
  192. for(int i=0; i < userParametersList.Count; i++) {
  193. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  194. if (userParameter.Direction == ParameterDirection.ReturnValue) {
  195. return userParameter;
  196. }
  197. }
  198. return null;
  199. }
  200. protected sealed override DbParameter CreateParameterInternal() {
  201. return new OracleParameter();
  202. }
  203. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent) {
  204. return new OracleParameterCollection((OracleCommand)parent);
  205. }
  206. public override object Clone() {
  207. OracleCommand clone = (OracleCommand)base.Clone();
  208. clone._currentParameterIndex = 0;
  209. clone._currentRefCursor = null;
  210. return clone;
  211. }
  212. protected override void PrepareInternalParameters() {
  213. InternalParameters.Clear();
  214. _currentParameterIndex = -1;
  215. }
  216. protected sealed override DbDataReader CreateReader() {
  217. return new OracleDataReader(this);
  218. }
  219. protected sealed override SystemException CreateException(SQLException e) {
  220. return new OracleException(e,Connection);
  221. }
  222. public object ExecuteOracleScalar() {
  223. throw new NotImplementedException();
  224. }
  225. #if SUPPORT_ORACLE_TYPES
  226. public int ExecuteOracleNonQuery(
  227. out OracleString rowid
  228. ) {
  229. throw new NotImplementedException();
  230. }
  231. #endif
  232. #endregion // Methods
  233. }
  234. }