OciStatementHandle.cs 5.8 KB

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