OciHandle.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. public OciHandle Allocate (OciHandleType type)
  53. {
  54. int status = 0;
  55. IntPtr newHandle = IntPtr.Zero;
  56. if (type < OciHandleType.LobLocator)
  57. status = OciCalls.OCIHandleAlloc (this,
  58. out newHandle,
  59. type,
  60. 0,
  61. IntPtr.Zero);
  62. else
  63. status = OciCalls.OCIDescriptorAlloc (this,
  64. out newHandle,
  65. type,
  66. 0,
  67. IntPtr.Zero);
  68. if (status != 0 && status != 1)
  69. throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", type));
  70. switch (type) {
  71. case OciHandleType.Service:
  72. return new OciServiceHandle (this, newHandle);
  73. case OciHandleType.Error:
  74. return new OciErrorHandle (this, newHandle);
  75. case OciHandleType.Server:
  76. return new OciServerHandle (this, newHandle);
  77. case OciHandleType.Session:
  78. return new OciSessionHandle (this, newHandle);
  79. case OciHandleType.Statement:
  80. return new OciStatementHandle (this, newHandle);
  81. case OciHandleType.Transaction:
  82. return new OciTransactionHandle (this, newHandle);
  83. case OciHandleType.LobLocator:
  84. return new OciLobLocator (this, newHandle);
  85. case OciHandleType.RowId:
  86. return new OciRowIdDescriptor (this, newHandle);
  87. }
  88. return null;
  89. }
  90. protected virtual void Dispose (bool disposing)
  91. {
  92. if (!disposed) {
  93. FreeHandle ();
  94. if (disposing) {
  95. parent = null;
  96. }
  97. disposed = true;
  98. }
  99. }
  100. public void Dispose ()
  101. {
  102. Dispose (true);
  103. GC.SuppressFinalize (this);
  104. }
  105. protected virtual void FreeHandle ()
  106. {
  107. switch (type) {
  108. case OciHandleType.Bind:
  109. case OciHandleType.Define:
  110. // Bind and Define handles are freed when Statement handle is disposed
  111. break;
  112. case OciHandleType.Environment:
  113. if (handle != IntPtr.Zero) {
  114. OciCalls.OCIHandleFree (handle, type);
  115. }
  116. break;
  117. default:
  118. if ( handle != IntPtr.Zero &&
  119. parent != null &&
  120. parent.Handle != IntPtr.Zero ) {
  121. OciCalls.OCIHandleFree (handle, type);
  122. }
  123. break;
  124. }
  125. handle = IntPtr.Zero;
  126. }
  127. public bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle)
  128. {
  129. return (GetAttributeInt32 (attrType, errorHandle) != 0);
  130. }
  131. public sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle)
  132. {
  133. int status = 0;
  134. sbyte output;
  135. status = OciCalls.OCIAttrGetSByte (Handle,
  136. HandleType,
  137. out output,
  138. IntPtr.Zero,
  139. attrType,
  140. errorHandle);
  141. if (status != 0) {
  142. OciErrorInfo info = errorHandle.HandleError ();
  143. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  144. }
  145. return output;
  146. }
  147. public byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle)
  148. {
  149. int status = 0;
  150. byte output;
  151. status = OciCalls.OCIAttrGetByte (Handle,
  152. HandleType,
  153. out output,
  154. IntPtr.Zero,
  155. attrType,
  156. errorHandle);
  157. if (status != 0) {
  158. OciErrorInfo info = errorHandle.HandleError ();
  159. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  160. }
  161. return output;
  162. }
  163. public ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle)
  164. {
  165. int status = 0;
  166. ushort output;
  167. status = OciCalls.OCIAttrGetUInt16 (Handle,
  168. HandleType,
  169. out output,
  170. IntPtr.Zero,
  171. attrType,
  172. errorHandle);
  173. if (status != 0) {
  174. OciErrorInfo info = errorHandle.HandleError ();
  175. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  176. }
  177. return output;
  178. }
  179. public int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle)
  180. {
  181. int status = 0;
  182. int output;
  183. status = OciCalls.OCIAttrGetInt32 (Handle,
  184. HandleType,
  185. out output,
  186. IntPtr.Zero,
  187. attrType,
  188. errorHandle);
  189. if (status != 0) {
  190. OciErrorInfo info = errorHandle.HandleError ();
  191. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  192. }
  193. return output;
  194. }
  195. public IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle)
  196. {
  197. int status = 0;
  198. IntPtr output = IntPtr.Zero;
  199. status = OciCalls.OCIAttrGetIntPtr (Handle,
  200. HandleType,
  201. out output,
  202. IntPtr.Zero,
  203. attrType,
  204. errorHandle);
  205. if (status != 0) {
  206. OciErrorInfo info = errorHandle.HandleError ();
  207. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  208. }
  209. return output;
  210. }
  211. public string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle)
  212. {
  213. string output = String.Empty;
  214. IntPtr outputPtr = IntPtr.Zero;
  215. int outSize;
  216. int status = 0;
  217. status = OciCalls.OCIAttrGet (Handle,
  218. HandleType,
  219. out outputPtr,
  220. out outSize,
  221. attrType,
  222. errorHandle);
  223. if (status != 0) {
  224. OciErrorInfo info = errorHandle.HandleError ();
  225. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  226. }
  227. if (outputPtr != IntPtr.Zero && outSize > 0) {
  228. object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
  229. if (str != null)
  230. output = String.Copy ((string) str);
  231. }
  232. return output;
  233. }
  234. public void SetHandle (IntPtr h)
  235. {
  236. handle = h;
  237. }
  238. #endregion // Methods
  239. #region Operators and Type Conversions
  240. public static implicit operator IntPtr (OciHandle h)
  241. {
  242. return h.Handle;
  243. }
  244. #endregion // Operators and Type Conversions
  245. }
  246. }