OciStatementHandle.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // OciStatementHandle.cs
  3. //
  4. // Part of managed C#/.NET library System.Data.OracleClient.dll
  5. //
  6. // Part of the Mono class libraries at
  7. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
  8. //
  9. // Assembly: System.Data.OracleClient.dll
  10. // Namespace: System.Data.OracleClient.Oci
  11. //
  12. // Author:
  13. // Tim Coleman <[email protected]>
  14. //
  15. // Copyright (C) Tim Coleman, 2003
  16. //
  17. using System;
  18. using System.Collections;
  19. using System.Runtime.InteropServices;
  20. namespace System.Data.OracleClient.Oci {
  21. internal sealed class OciStatementHandle : OciHandle, IDisposable
  22. {
  23. #region Fields
  24. int columnCount;
  25. bool disposed = false;
  26. OciErrorHandle errorHandle;
  27. bool moreResults;
  28. OciServiceHandle serviceHandle;
  29. ArrayList values;
  30. #endregion // Fields
  31. #region Constructors
  32. public OciStatementHandle (OciHandle parent, IntPtr handle)
  33. : base (OciHandleType.Statement, parent, handle)
  34. {
  35. moreResults = false;
  36. }
  37. #endregion // Constructors
  38. #region Properties
  39. public int ColumnCount {
  40. get { return columnCount; }
  41. }
  42. public OciErrorHandle ErrorHandle {
  43. get { return errorHandle; }
  44. set { errorHandle = value; }
  45. }
  46. public OciServiceHandle Service {
  47. get { return serviceHandle; }
  48. set { serviceHandle = value; }
  49. }
  50. public ArrayList Values {
  51. get { return values; }
  52. }
  53. #endregion // Properties
  54. #region Methods
  55. protected override void Dispose (bool disposing)
  56. {
  57. if (!disposed) {
  58. disposed = true;
  59. if (disposing) {
  60. if (values != null) {
  61. foreach (OciDefineHandle h in values)
  62. h.Dispose ();
  63. values = null;
  64. }
  65. }
  66. base.Dispose (disposing);
  67. }
  68. }
  69. public OciParameterDescriptor GetParameter (int position)
  70. {
  71. IntPtr handle = IntPtr.Zero;
  72. int status = 0;
  73. status = OciCalls.OCIParamGet (this,
  74. OciHandleType.Statement,
  75. ErrorHandle,
  76. out handle,
  77. position + 1);
  78. if (status != 0) {
  79. OciErrorInfo info = ErrorHandle.HandleError ();
  80. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  81. }
  82. OciParameterDescriptor output = new OciParameterDescriptor (this, handle);
  83. output.ErrorHandle = ErrorHandle;
  84. return output;
  85. }
  86. public OciDefineHandle GetDefineHandle (int position)
  87. {
  88. OciDefineHandle defineHandle = new OciDefineHandle (this, IntPtr.Zero);
  89. defineHandle.ErrorHandle = ErrorHandle;
  90. defineHandle.DefineByPosition (position);
  91. return defineHandle;
  92. }
  93. void Define ()
  94. {
  95. values = new ArrayList ();
  96. for (int i = 0; i < columnCount; i += 1)
  97. values.Add (GetDefineHandle (i));
  98. }
  99. public bool ExecuteQuery ()
  100. {
  101. return Execute (false,false);
  102. }
  103. public bool ExecuteNonQuery (bool useAutoCommit)
  104. {
  105. return Execute (true, useAutoCommit);
  106. }
  107. public bool Execute (bool nonQuery, bool useAutoCommit)
  108. {
  109. int status = 0;
  110. columnCount = 0;
  111. moreResults = false;
  112. int executeMode;
  113. if( useAutoCommit)
  114. executeMode = (int)OciExecuteMode.CommitOnSuccess;
  115. else
  116. executeMode = (int)OciExecuteMode.Default;
  117. if (this.disposed)
  118. {
  119. throw new InvalidOperationException ("StatementHandle is already disposed.");
  120. }
  121. status = OciCalls.OCIStmtExecute (Service,
  122. Handle,
  123. ErrorHandle,
  124. nonQuery,
  125. 0,
  126. IntPtr.Zero,
  127. IntPtr.Zero,
  128. (OciExecuteMode)executeMode);
  129. switch (status) {
  130. case OciGlue.OCI_DEFAULT:
  131. if (!nonQuery) {
  132. GetColumnCount ();
  133. Define ();
  134. moreResults = true;
  135. }
  136. break;
  137. case OciGlue.OCI_NO_DATA:
  138. break;
  139. case OciGlue.OCI_INVALID_HANDLE:
  140. throw new OracleException (0, "Invalid handle.");
  141. default:
  142. OciErrorInfo info = ErrorHandle.HandleError ();
  143. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  144. }
  145. return true;
  146. }
  147. void GetColumnCount ()
  148. {
  149. columnCount = GetAttributeInt32 (OciAttributeType.ParameterCount, ErrorHandle);
  150. }
  151. public OciStatementType GetStatementType ()
  152. {
  153. return (OciStatementType) GetAttributeInt32 (OciAttributeType.StatementType, ErrorHandle);
  154. }
  155. public bool Fetch ()
  156. {
  157. int status = 0;
  158. if (this.disposed)
  159. {
  160. throw new InvalidOperationException ("StatementHandle is already disposed.");
  161. }
  162. status = OciCalls.OCIStmtFetch (Handle,
  163. ErrorHandle.Handle,
  164. 1,
  165. 2,
  166. 0);
  167. switch (status) {
  168. case OciGlue.OCI_NO_DATA:
  169. moreResults = false;
  170. break;
  171. case OciGlue.OCI_DEFAULT:
  172. moreResults = true;
  173. break;
  174. default:
  175. OciErrorInfo info = ErrorHandle.HandleError ();
  176. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  177. }
  178. return moreResults;
  179. }
  180. public void Prepare (string commandText)
  181. {
  182. int status = 0;
  183. if (this.disposed) {
  184. throw new InvalidOperationException ("StatementHandle is already disposed.");
  185. }
  186. int rsize = 0;
  187. byte [] buffer;
  188. // Get size of buffer
  189. OciCalls.OCIUnicodeToCharSet (Parent, null, commandText, out rsize);
  190. // Fill buffer
  191. buffer = new byte[rsize];
  192. OciCalls.OCIUnicodeToCharSet (Parent, buffer, commandText, out rsize);
  193. // Execute statement
  194. status = OciCalls.OCIStmtPrepare (this,
  195. ErrorHandle,
  196. buffer,
  197. buffer.Length,
  198. OciStatementLanguage.NTV,
  199. OciStatementMode.Default);
  200. if (status != 0) {
  201. OciErrorInfo info = ErrorHandle.HandleError ();
  202. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  203. }
  204. }
  205. #endregion // Methods
  206. }
  207. }