OciStatementHandle.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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, IOciHandle, IDisposable
  22. {
  23. #region Fields
  24. OciStatementLanguage language;
  25. OciStatementMode mode;
  26. OciServiceHandle serviceHandle;
  27. OciErrorHandle errorHandle;
  28. ArrayList values;
  29. bool moreResults;
  30. int columnCount;
  31. #endregion // Fields
  32. #region Constructors
  33. public OciStatementHandle (OciEnvironmentHandle environment, IntPtr handle)
  34. : base (OciHandleType.Statement, environment, handle)
  35. {
  36. language = OciStatementLanguage.NTV;
  37. mode = OciStatementMode.Default;
  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 OciStatementLanguage Language {
  50. get { return language; }
  51. set { language = value; }
  52. }
  53. public OciServiceHandle Service {
  54. get { return serviceHandle; }
  55. set { serviceHandle = value; }
  56. }
  57. public ArrayList Values {
  58. get { return values; }
  59. }
  60. #endregion // Properties
  61. #region Methods
  62. [DllImport ("oci")]
  63. public static extern int OCIDescriptorFree (IntPtr descp,
  64. [MarshalAs (UnmanagedType.U4)] OciDescriptorType type);
  65. [DllImport ("oci")]
  66. public static extern int OCIParamGet (IntPtr hndlp,
  67. [MarshalAs (UnmanagedType.U4)] OciHandleType htype,
  68. IntPtr errhp,
  69. out IntPtr parmdpp,
  70. [MarshalAs (UnmanagedType.U4)] int pos);
  71. [DllImport ("oci")]
  72. public static extern int OCIStmtExecute (IntPtr svchp,
  73. IntPtr stmthp,
  74. IntPtr errhp,
  75. [MarshalAs (UnmanagedType.U4)] bool iters,
  76. uint rowoff,
  77. IntPtr snap_in,
  78. IntPtr snap_out,
  79. [MarshalAs (UnmanagedType.U4)] OciExecuteMode mode);
  80. [DllImport ("oci")]
  81. public static extern int OCIStmtFetch (IntPtr stmtp,
  82. IntPtr errhp,
  83. uint nrows,
  84. ushort orientation,
  85. uint mode);
  86. [DllImport ("oci")]
  87. public static extern int OCIStmtPrepare (IntPtr stmthp,
  88. IntPtr errhp,
  89. string stmt,
  90. [MarshalAs (UnmanagedType.U4)] int stmt_length,
  91. [MarshalAs (UnmanagedType.U4)] OciStatementLanguage language,
  92. [MarshalAs (UnmanagedType.U4)] OciStatementMode mode);
  93. public IntPtr CreateParameterHandle (int position)
  94. {
  95. IntPtr handle = IntPtr.Zero;
  96. int status = 0;
  97. status = OCIParamGet (Handle,
  98. OciHandleType.Statement,
  99. ErrorHandle.Handle,
  100. out handle,
  101. position);
  102. if (status != 0) {
  103. OciErrorInfo info = ErrorHandle.HandleError ();
  104. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  105. }
  106. return handle;
  107. }
  108. public void FreeParameterHandle (IntPtr handle)
  109. {
  110. int status = 0;
  111. status = OCIDescriptorFree (handle, OciDescriptorType.Parameter);
  112. if (status != 0) {
  113. OciErrorInfo info = ErrorHandle.HandleError ();
  114. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  115. }
  116. }
  117. void Define ()
  118. {
  119. values = new ArrayList ();
  120. for (int i = 0; i < columnCount; i += 1)
  121. values.Add (new OciDefineHandle (this, i + 1));
  122. }
  123. public void Dispose ()
  124. {
  125. Environment.FreeHandle (this);
  126. }
  127. public bool ExecuteQuery ()
  128. {
  129. return Execute (false);
  130. }
  131. public bool ExecuteNonQuery ()
  132. {
  133. return Execute (true);
  134. }
  135. public bool Execute (bool nonQuery)
  136. {
  137. int status = 0;
  138. columnCount = 0;
  139. moreResults = false;
  140. status = OCIStmtExecute (Service.Handle,
  141. Handle,
  142. ErrorHandle.Handle,
  143. nonQuery,
  144. 0,
  145. IntPtr.Zero,
  146. IntPtr.Zero,
  147. OciExecuteMode.Default);
  148. switch (status) {
  149. case OciGlue.OCI_DEFAULT:
  150. if (!nonQuery) {
  151. GetColumnCount ();
  152. Define ();
  153. moreResults = true;
  154. }
  155. break;
  156. case OciGlue.OCI_NO_DATA:
  157. break;
  158. default:
  159. OciErrorInfo info = ErrorHandle.HandleError ();
  160. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  161. }
  162. return true;
  163. }
  164. void GetColumnCount ()
  165. {
  166. int status = 0;
  167. status = OciGlue.OCIAttrGetInt32 ( Handle,
  168. (uint) OciHandleType.Statement,
  169. out columnCount,
  170. IntPtr.Zero,
  171. OciAttributeType.ParameterCount,
  172. ErrorHandle.Handle);
  173. if (status != 0) {
  174. OciErrorInfo info = ErrorHandle.HandleError ();
  175. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  176. }
  177. }
  178. public bool GetAttributeBool (IntPtr handle, OciAttributeType type)
  179. {
  180. bool output;
  181. int status = 0;
  182. status = OciGlue.OCIAttrGetBool (handle,
  183. (uint) OciDescriptorType.Parameter,
  184. out output,
  185. IntPtr.Zero,
  186. type,
  187. ErrorHandle.Handle);
  188. if (status != 0) {
  189. OciErrorInfo info = ErrorHandle.HandleError ();
  190. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  191. }
  192. return output;
  193. }
  194. public sbyte GetAttributeSByte (IntPtr handle, OciAttributeType type) {
  195. sbyte output;
  196. int status = 0;
  197. status = OciGlue.OCIAttrGetSByte (handle,
  198. (uint) OciDescriptorType.Parameter,
  199. out output,
  200. IntPtr.Zero,
  201. type,
  202. ErrorHandle.Handle);
  203. if (status != 0) {
  204. OciErrorInfo info = ErrorHandle.HandleError ();
  205. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  206. }
  207. return output;
  208. }
  209. public byte GetAttributeByte (IntPtr handle, OciAttributeType type)
  210. {
  211. byte output;
  212. int status = 0;
  213. status = OciGlue.OCIAttrGetByte (handle,
  214. (uint) OciDescriptorType.Parameter,
  215. out output,
  216. IntPtr.Zero,
  217. type,
  218. ErrorHandle.Handle);
  219. if (status != 0) {
  220. OciErrorInfo info = ErrorHandle.HandleError ();
  221. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  222. }
  223. return output;
  224. }
  225. public ushort GetAttributeUInt16 (IntPtr handle, OciAttributeType type) {
  226. int status = 0;
  227. ushort output;
  228. status = OciGlue.OCIAttrGetUInt16 (handle,
  229. (uint) OciDescriptorType.Parameter,
  230. out output,
  231. IntPtr.Zero,
  232. type,
  233. ErrorHandle.Handle);
  234. if (status != 0) {
  235. OciErrorInfo info = ErrorHandle.HandleError ();
  236. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  237. }
  238. return output;
  239. }
  240. public int GetAttributeInt32 (IntPtr handle, OciAttributeType type)
  241. {
  242. int status = 0;
  243. int output;
  244. status = OciGlue.OCIAttrGetInt32 (handle,
  245. (uint) OciDescriptorType.Parameter,
  246. out output,
  247. IntPtr.Zero,
  248. type,
  249. ErrorHandle.Handle);
  250. if (status != 0) {
  251. OciErrorInfo info = ErrorHandle.HandleError ();
  252. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  253. }
  254. return output;
  255. }
  256. [MonoTODO]
  257. public string GetAttributeString (IntPtr handle, OciAttributeType type)
  258. {
  259. string output = String.Empty;
  260. IntPtr outputPtr = IntPtr.Zero;
  261. int outSize;
  262. int status = 0;
  263. status = OciGlue.OCIAttrGet (handle,
  264. (uint) OciDescriptorType.Parameter,
  265. out outputPtr,
  266. out outSize,
  267. type,
  268. ErrorHandle.Handle);
  269. if (status != 0) {
  270. OciErrorInfo info = ErrorHandle.HandleError ();
  271. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  272. }
  273. if (outputPtr != IntPtr.Zero && outSize > 0) {
  274. object name = Marshal.PtrToStringAnsi (outputPtr, outSize);
  275. if (name != null) {
  276. output = String.Copy ((string) name);
  277. /* TRWC
  278. * We shouldn't really free this, but how can we make Oracle
  279. * do it?
  280. */
  281. // Marshal.FreeHGlobal (outputPtr); -- this *may* leak memory
  282. }
  283. }
  284. return output;
  285. }
  286. [MonoTODO]
  287. public OciColumnInfo DescribeColumn (int ordinal)
  288. {
  289. int status = 0;
  290. OciColumnInfo columnInfo;
  291. string schemaName;
  292. IntPtr parameterHandle = CreateParameterHandle (ordinal + 1);
  293. columnInfo.ColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
  294. columnInfo.ColumnOrdinal = ordinal + 1;
  295. columnInfo.ColumnSize = GetAttributeUInt16 (parameterHandle, OciAttributeType.DataSize);
  296. columnInfo.Precision = GetAttributeByte (parameterHandle, OciAttributeType.Precision);
  297. columnInfo.Scale = GetAttributeSByte (parameterHandle, OciAttributeType.Scale);
  298. columnInfo.DataType = (OciDataType) GetAttributeInt32 (parameterHandle, OciAttributeType.DataType);
  299. columnInfo.AllowDBNull = GetAttributeBool (parameterHandle, OciAttributeType.IsNull);
  300. columnInfo.BaseColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
  301. // TRWC not sure what to do with this yet.
  302. schemaName = GetAttributeString (parameterHandle, OciAttributeType.SchemaName);
  303. FreeParameterHandle (parameterHandle);
  304. return columnInfo;
  305. }
  306. public OciStatementType GetStatementType ()
  307. {
  308. int status = 0;
  309. int statementType;
  310. status = OciGlue.OCIAttrGetInt32 (Handle,
  311. (uint) OciHandleType.Statement,
  312. out statementType,
  313. IntPtr.Zero,
  314. OciAttributeType.StatementType,
  315. errorHandle.Handle);
  316. if (status != 0) {
  317. OciErrorInfo info = ErrorHandle.HandleError ();
  318. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  319. }
  320. return (OciStatementType) statementType;
  321. }
  322. public bool Fetch ()
  323. {
  324. int status = 0;
  325. status = OCIStmtFetch (Handle,
  326. ErrorHandle.Handle,
  327. 1,
  328. 2,
  329. 0);
  330. switch (status) {
  331. case OciGlue.OCI_NO_DATA:
  332. moreResults = false;
  333. break;
  334. case OciGlue.OCI_DEFAULT:
  335. moreResults = true;
  336. break;
  337. default:
  338. OciErrorInfo info = ErrorHandle.HandleError ();
  339. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  340. }
  341. return moreResults;
  342. }
  343. public void Prepare (string commandText)
  344. {
  345. int status = 0;
  346. status = OCIStmtPrepare (Handle,
  347. errorHandle.Handle,
  348. commandText,
  349. commandText.Length,
  350. language,
  351. mode);
  352. if (status != 0) {
  353. OciErrorInfo info = ErrorHandle.HandleError ();
  354. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  355. }
  356. }
  357. #endregion // Methods
  358. }
  359. }