OciHandle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // OciHandle.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.Runtime.InteropServices;
  19. namespace System.Data.OracleClient.Oci {
  20. internal abstract class OciHandle : IDisposable
  21. {
  22. #region Fields
  23. bool disposed = false;
  24. IntPtr handle;
  25. OciHandle parent;
  26. OciHandleType type;
  27. #endregion // Fields
  28. #region Constructors
  29. public OciHandle (OciHandleType type, OciHandle parent, IntPtr newHandle)
  30. {
  31. this.type = type;
  32. this.parent = parent;
  33. this.handle = newHandle;
  34. }
  35. ~OciHandle ()
  36. {
  37. Dispose (false);
  38. }
  39. #endregion // Constructors
  40. #region Properties
  41. public OciHandle Parent {
  42. get { return parent; }
  43. }
  44. public IntPtr Handle {
  45. get { return handle; }
  46. }
  47. public OciHandleType HandleType {
  48. get { return type; }
  49. }
  50. #endregion // Properties
  51. #region Methods
  52. [DllImport ("oci")]
  53. static extern int OCIAttrGet (IntPtr trgthndlp,
  54. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  55. out IntPtr attributep,
  56. out int sizep,
  57. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  58. IntPtr errhp);
  59. [DllImport ("oci", EntryPoint = "OCIAttrGet")]
  60. static extern int OCIAttrGetSByte (IntPtr trgthndlp,
  61. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  62. out sbyte attributep,
  63. IntPtr sizep,
  64. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  65. IntPtr errhp);
  66. [DllImport ("oci", EntryPoint = "OCIAttrGet")]
  67. static extern int OCIAttrGetByte (IntPtr trgthndlp,
  68. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  69. out byte attributep,
  70. IntPtr sizep,
  71. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  72. IntPtr errhp);
  73. [DllImport ("oci", EntryPoint = "OCIAttrGet")]
  74. static extern int OCIAttrGetUInt16 (IntPtr trgthndlp,
  75. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  76. out ushort attributep,
  77. IntPtr sizep,
  78. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  79. IntPtr errhp);
  80. [DllImport ("oci", EntryPoint = "OCIAttrGet")]
  81. static extern int OCIAttrGetInt32 (IntPtr trgthndlp,
  82. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  83. out int attributep,
  84. IntPtr sizep,
  85. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  86. IntPtr errhp);
  87. [DllImport ("oci", EntryPoint = "OCIAttrGet")]
  88. static extern int OCIAttrGetIntPtr (IntPtr trgthndlp,
  89. [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
  90. out IntPtr attributep,
  91. IntPtr sizep,
  92. [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
  93. IntPtr errhp);
  94. [DllImport ("oci")]
  95. static extern int OCIDescriptorAlloc (IntPtr parenth,
  96. out IntPtr hndlpp,
  97. [MarshalAs (UnmanagedType.U4)] OciHandleType type,
  98. int xtramem_sz,
  99. IntPtr usrmempp);
  100. [DllImport ("oci")]
  101. static extern int OCIHandleAlloc (IntPtr parenth,
  102. out IntPtr descpp,
  103. [MarshalAs (UnmanagedType.U4)] OciHandleType type,
  104. int xtramem_sz,
  105. IntPtr usrmempp);
  106. [DllImport ("oci")]
  107. static extern int OCIHandleFree (IntPtr hndlp,
  108. [MarshalAs (UnmanagedType.U4)] OciHandleType type);
  109. public OciHandle Allocate (OciHandleType type)
  110. {
  111. int status = 0;
  112. IntPtr newHandle = IntPtr.Zero;
  113. if (type < OciHandleType.LobLocator)
  114. status = OCIHandleAlloc (this,
  115. out newHandle,
  116. type,
  117. 0,
  118. IntPtr.Zero);
  119. else
  120. status = OCIDescriptorAlloc (this,
  121. out newHandle,
  122. type,
  123. 0,
  124. IntPtr.Zero);
  125. if (status != 0 && status != 1)
  126. throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", type));
  127. switch (type) {
  128. case OciHandleType.Service:
  129. return new OciServiceHandle (this, newHandle);
  130. case OciHandleType.Error:
  131. return new OciErrorHandle (this, newHandle);
  132. case OciHandleType.Server:
  133. return new OciServerHandle (this, newHandle);
  134. case OciHandleType.Session:
  135. return new OciSessionHandle (this, newHandle);
  136. case OciHandleType.Statement:
  137. return new OciStatementHandle (this, newHandle);
  138. case OciHandleType.Transaction:
  139. return new OciTransactionHandle (this, newHandle);
  140. case OciHandleType.LobLocator:
  141. return new OciLobLocator (this, newHandle);
  142. case OciHandleType.RowId:
  143. return new OciRowIdDescriptor (this, newHandle);
  144. }
  145. return null;
  146. }
  147. protected virtual void Dispose (bool disposing)
  148. {
  149. if (!disposed) {
  150. if (disposing) {
  151. parent = null;
  152. }
  153. FreeHandle ();
  154. disposed = true;
  155. }
  156. }
  157. public void Dispose ()
  158. {
  159. Dispose (true);
  160. GC.SuppressFinalize (this);
  161. }
  162. protected virtual void FreeHandle ()
  163. {
  164. OCIHandleFree (Handle, HandleType);
  165. handle = IntPtr.Zero;
  166. }
  167. public bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle)
  168. {
  169. return (GetAttributeInt32 (attrType, errorHandle) != 0);
  170. }
  171. public sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle)
  172. {
  173. int status = 0;
  174. sbyte output;
  175. status = OCIAttrGetSByte (Handle,
  176. HandleType,
  177. out output,
  178. IntPtr.Zero,
  179. attrType,
  180. errorHandle);
  181. if (status != 0) {
  182. OciErrorInfo info = errorHandle.HandleError ();
  183. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  184. }
  185. return output;
  186. }
  187. public byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle)
  188. {
  189. int status = 0;
  190. byte output;
  191. status = OCIAttrGetByte (Handle,
  192. HandleType,
  193. out output,
  194. IntPtr.Zero,
  195. attrType,
  196. errorHandle);
  197. if (status != 0) {
  198. OciErrorInfo info = errorHandle.HandleError ();
  199. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  200. }
  201. return output;
  202. }
  203. public ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle)
  204. {
  205. int status = 0;
  206. ushort output;
  207. status = OCIAttrGetUInt16 (Handle,
  208. HandleType,
  209. out output,
  210. IntPtr.Zero,
  211. attrType,
  212. errorHandle);
  213. if (status != 0) {
  214. OciErrorInfo info = errorHandle.HandleError ();
  215. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  216. }
  217. return output;
  218. }
  219. public int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle)
  220. {
  221. int status = 0;
  222. int output;
  223. status = OCIAttrGetInt32 (Handle,
  224. HandleType,
  225. out output,
  226. IntPtr.Zero,
  227. attrType,
  228. errorHandle);
  229. if (status != 0) {
  230. OciErrorInfo info = errorHandle.HandleError ();
  231. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  232. }
  233. return output;
  234. }
  235. public IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle)
  236. {
  237. int status = 0;
  238. IntPtr output = IntPtr.Zero;
  239. status = OCIAttrGetIntPtr (Handle,
  240. HandleType,
  241. out output,
  242. IntPtr.Zero,
  243. attrType,
  244. errorHandle);
  245. if (status != 0) {
  246. OciErrorInfo info = errorHandle.HandleError ();
  247. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  248. }
  249. return output;
  250. }
  251. public string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle)
  252. {
  253. string output = String.Empty;
  254. IntPtr outputPtr = IntPtr.Zero;
  255. int outSize;
  256. int status = 0;
  257. status = OCIAttrGet (Handle,
  258. HandleType,
  259. out outputPtr,
  260. out outSize,
  261. attrType,
  262. errorHandle);
  263. if (status != 0) {
  264. OciErrorInfo info = errorHandle.HandleError ();
  265. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  266. }
  267. if (outputPtr != IntPtr.Zero && outSize > 0) {
  268. object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
  269. if (str != null)
  270. output = String.Copy ((string) str);
  271. }
  272. return output;
  273. }
  274. public void SetHandle (IntPtr h)
  275. {
  276. handle = h;
  277. }
  278. #endregion // Methods
  279. #region Operators and Type Conversions
  280. public static implicit operator IntPtr (OciHandle h)
  281. {
  282. return h.Handle;
  283. }
  284. #endregion // Operators and Type Conversions
  285. }
  286. }