OciHandle.cs 6.7 KB

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