OciStatementHandle.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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);
  102. }
  103. public bool ExecuteNonQuery ()
  104. {
  105. return Execute (true);
  106. }
  107. public bool Execute (bool nonQuery)
  108. {
  109. int status = 0;
  110. columnCount = 0;
  111. moreResults = false;
  112. if (this.disposed)
  113. {
  114. throw new InvalidOperationException ("StatementHandle is already disposed.");
  115. }
  116. status = OciCalls.OCIStmtExecute (Service,
  117. Handle,
  118. ErrorHandle,
  119. nonQuery,
  120. 0,
  121. IntPtr.Zero,
  122. IntPtr.Zero,
  123. OciExecuteMode.Default);
  124. switch (status) {
  125. case OciGlue.OCI_DEFAULT:
  126. if (!nonQuery) {
  127. GetColumnCount ();
  128. Define ();
  129. moreResults = true;
  130. }
  131. break;
  132. case OciGlue.OCI_NO_DATA:
  133. break;
  134. case OciGlue.OCI_INVALID_HANDLE:
  135. throw new OracleException (0, "Invalid handle.");
  136. default:
  137. OciErrorInfo info = ErrorHandle.HandleError ();
  138. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  139. }
  140. return true;
  141. }
  142. void GetColumnCount ()
  143. {
  144. columnCount = GetAttributeInt32 (OciAttributeType.ParameterCount, ErrorHandle);
  145. }
  146. public OciStatementType GetStatementType ()
  147. {
  148. return (OciStatementType) GetAttributeInt32 (OciAttributeType.StatementType, ErrorHandle);
  149. }
  150. public bool Fetch ()
  151. {
  152. int status = 0;
  153. if (this.disposed)
  154. {
  155. throw new InvalidOperationException ("StatementHandle is already disposed.");
  156. }
  157. status = OciCalls.OCIStmtFetch (Handle,
  158. ErrorHandle.Handle,
  159. 1,
  160. 2,
  161. 0);
  162. switch (status) {
  163. case OciGlue.OCI_NO_DATA:
  164. moreResults = false;
  165. break;
  166. case OciGlue.OCI_DEFAULT:
  167. moreResults = true;
  168. break;
  169. default:
  170. OciErrorInfo info = ErrorHandle.HandleError ();
  171. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  172. }
  173. return moreResults;
  174. }
  175. public void Prepare (string commandText)
  176. {
  177. int status = 0;
  178. if (this.disposed) {
  179. throw new InvalidOperationException ("StatementHandle is already disposed.");
  180. }
  181. byte [] buffer = System.Text.Encoding.UTF8.GetBytes (commandText + "\0");
  182. status = OciCalls.OCIStmtPrepare (this,
  183. ErrorHandle,
  184. buffer,
  185. buffer.Length,
  186. OciStatementLanguage.NTV,
  187. OciStatementMode.Default);
  188. if (status != 0) {
  189. OciErrorInfo info = ErrorHandle.HandleError ();
  190. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  191. }
  192. }
  193. #endregion // Methods
  194. }
  195. }