OciStatementHandle.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. [DllImport ("oci")]
  56. static extern int OCIDescriptorFree (IntPtr descp,
  57. [MarshalAs (UnmanagedType.U4)] OciHandleType type);
  58. [DllImport ("oci")]
  59. static extern int OCIParamGet (IntPtr hndlp,
  60. [MarshalAs (UnmanagedType.U4)] OciHandleType htype,
  61. IntPtr errhp,
  62. out IntPtr parmdpp,
  63. [MarshalAs (UnmanagedType.U4)] int pos);
  64. [DllImport ("oci")]
  65. static extern int OCIStmtExecute (IntPtr svchp,
  66. IntPtr stmthp,
  67. IntPtr errhp,
  68. [MarshalAs (UnmanagedType.U4)] bool iters,
  69. uint rowoff,
  70. IntPtr snap_in,
  71. IntPtr snap_out,
  72. [MarshalAs (UnmanagedType.U4)] OciExecuteMode mode);
  73. [DllImport ("oci")]
  74. public static extern int OCIStmtFetch (IntPtr stmtp,
  75. IntPtr errhp,
  76. uint nrows,
  77. ushort orientation,
  78. uint mode);
  79. [DllImport ("oci")]
  80. public static extern int OCIStmtPrepare (IntPtr stmthp,
  81. IntPtr errhp,
  82. string stmt,
  83. [MarshalAs (UnmanagedType.U4)] int stmt_length,
  84. [MarshalAs (UnmanagedType.U4)] OciStatementLanguage language,
  85. [MarshalAs (UnmanagedType.U4)] OciStatementMode mode);
  86. protected override void Dispose (bool disposing)
  87. {
  88. if (!disposed) {
  89. disposed = true;
  90. base.Dispose (disposing);
  91. }
  92. }
  93. public OciParameterDescriptor GetParameter (int position)
  94. {
  95. IntPtr handle = IntPtr.Zero;
  96. int status = 0;
  97. status = OCIParamGet (this,
  98. OciHandleType.Statement,
  99. ErrorHandle,
  100. out handle,
  101. position + 1);
  102. if (status != 0) {
  103. OciErrorInfo info = ErrorHandle.HandleError ();
  104. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  105. }
  106. OciParameterDescriptor output = new OciParameterDescriptor (this, handle);
  107. output.ErrorHandle = ErrorHandle;
  108. return output;
  109. }
  110. public OciDefineHandle GetDefineHandle (int position)
  111. {
  112. OciDefineHandle defineHandle = new OciDefineHandle (this, IntPtr.Zero);
  113. defineHandle.ErrorHandle = ErrorHandle;
  114. defineHandle.DefineByPosition (position);
  115. return defineHandle;
  116. }
  117. void Define ()
  118. {
  119. values = new ArrayList ();
  120. for (int i = 0; i < columnCount; i += 1)
  121. values.Add (GetDefineHandle (i));
  122. }
  123. public bool ExecuteQuery ()
  124. {
  125. return Execute (false);
  126. }
  127. public bool ExecuteNonQuery ()
  128. {
  129. return Execute (true);
  130. }
  131. public bool Execute (bool nonQuery)
  132. {
  133. int status = 0;
  134. columnCount = 0;
  135. moreResults = false;
  136. status = OCIStmtExecute (Service,
  137. Handle,
  138. ErrorHandle,
  139. nonQuery,
  140. 0,
  141. IntPtr.Zero,
  142. IntPtr.Zero,
  143. OciExecuteMode.Default);
  144. switch (status) {
  145. case OciGlue.OCI_DEFAULT:
  146. if (!nonQuery) {
  147. GetColumnCount ();
  148. Define ();
  149. moreResults = true;
  150. }
  151. break;
  152. case OciGlue.OCI_NO_DATA:
  153. break;
  154. default:
  155. OciErrorInfo info = ErrorHandle.HandleError ();
  156. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  157. }
  158. return true;
  159. }
  160. void GetColumnCount ()
  161. {
  162. columnCount = GetAttributeInt32 (OciAttributeType.ParameterCount, ErrorHandle);
  163. }
  164. public OciStatementType GetStatementType ()
  165. {
  166. return (OciStatementType) GetAttributeInt32 (OciAttributeType.StatementType, ErrorHandle);
  167. }
  168. public bool Fetch ()
  169. {
  170. int status = 0;
  171. status = OCIStmtFetch (Handle,
  172. ErrorHandle.Handle,
  173. 1,
  174. 2,
  175. 0);
  176. switch (status) {
  177. case OciGlue.OCI_NO_DATA:
  178. moreResults = false;
  179. break;
  180. case OciGlue.OCI_DEFAULT:
  181. moreResults = true;
  182. break;
  183. default:
  184. OciErrorInfo info = ErrorHandle.HandleError ();
  185. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  186. }
  187. return moreResults;
  188. }
  189. public void Prepare (string commandText)
  190. {
  191. int status = 0;
  192. status = OCIStmtPrepare (this,
  193. ErrorHandle,
  194. commandText,
  195. commandText.Length,
  196. OciStatementLanguage.NTV,
  197. OciStatementMode.Default);
  198. if (status != 0) {
  199. OciErrorInfo info = ErrorHandle.HandleError ();
  200. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  201. }
  202. }
  203. #endregion // Methods
  204. }
  205. }