OciEnvironmentHandle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // OciEnvironmentHandle.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 class OciEnvironmentHandle : IOciHandle, IDisposable
  21. {
  22. #region Fields
  23. IntPtr handle;
  24. #endregion // Fields
  25. #region Constructors
  26. public OciEnvironmentHandle ()
  27. : this (OciEnvironmentMode.Default)
  28. {
  29. }
  30. public OciEnvironmentHandle (OciEnvironmentMode mode)
  31. {
  32. int status = 0;
  33. this.handle = IntPtr.Zero;
  34. status = OCIEnvCreate (out handle, mode,
  35. IntPtr.Zero,
  36. IntPtr.Zero,
  37. IntPtr.Zero,
  38. IntPtr.Zero,
  39. 0,
  40. IntPtr.Zero);
  41. }
  42. #endregion // Constructors
  43. #region Properties
  44. public IntPtr Handle {
  45. get { return handle; }
  46. set { handle = value; }
  47. }
  48. public OciHandleType HandleType {
  49. get { return OciHandleType.Environment; }
  50. }
  51. #endregion // Properties
  52. #region Methods
  53. [DllImport ("oci")]
  54. public static extern int OCIEnvCreate (out IntPtr envhpp,
  55. [MarshalAs (UnmanagedType.U4)] OciEnvironmentMode mode,
  56. IntPtr ctxp,
  57. IntPtr malocfp,
  58. IntPtr ralocfp,
  59. IntPtr mfreep,
  60. int xtramem_sz,
  61. IntPtr usrmempp);
  62. [DllImport ("oci")]
  63. public static extern int OCIHandleAlloc (IntPtr parenth,
  64. out IntPtr hndlpp,
  65. [MarshalAs (UnmanagedType.U4)] OciHandleType type,
  66. int xtramem_sz,
  67. IntPtr usrmempp);
  68. [DllImport ("oci")]
  69. public static extern int OCIDescriptorAlloc (IntPtr parenth,
  70. out IntPtr descpp,
  71. [MarshalAs (UnmanagedType.U4)] OciDescriptorType type,
  72. int xtramem_sz,
  73. IntPtr usrmempp);
  74. [DllImport ("oci")]
  75. public static extern int OCIHandleFree (IntPtr hndlp,
  76. [MarshalAs (UnmanagedType.U4)] OciHandleType type);
  77. [DllImport ("oci")]
  78. public static extern int OCIDescriptorFree (IntPtr hndlp,
  79. [MarshalAs (UnmanagedType.U4)] OciDescriptorType type);
  80. public IOciHandle Allocate (OciHandleType type)
  81. {
  82. IntPtr newHandle = IntPtr.Zero;
  83. int status = 0;
  84. status = OCIHandleAlloc (Handle,
  85. out newHandle,
  86. type,
  87. 0,
  88. IntPtr.Zero);
  89. if (status != 0 && status != 1)
  90. return null;
  91. switch (type) {
  92. case OciHandleType.Service:
  93. return new OciServiceHandle (this, newHandle);
  94. case OciHandleType.Error:
  95. return new OciErrorHandle (this, newHandle);
  96. case OciHandleType.Server:
  97. return new OciServerHandle (this, newHandle);
  98. case OciHandleType.Session:
  99. return new OciSessionHandle (this, newHandle);
  100. case OciHandleType.Statement:
  101. return new OciStatementHandle (this, newHandle);
  102. case OciHandleType.Transaction:
  103. return new OciTransactionHandle (this, newHandle);
  104. default:
  105. OCIHandleFree (newHandle, type);
  106. newHandle = IntPtr.Zero;
  107. throw new ArgumentException ("Invalid handle type.");
  108. }
  109. }
  110. public IOciDescriptorHandle AllocateDescriptor (OciDescriptorType type)
  111. {
  112. IntPtr newHandle = IntPtr.Zero;
  113. int status = 0;
  114. status = OCIDescriptorAlloc (Handle,
  115. out newHandle,
  116. type,
  117. 0,
  118. IntPtr.Zero);
  119. if (status != 0 && status != 1)
  120. return null;
  121. switch (type) {
  122. case OciDescriptorType.LobLocator:
  123. return new OciLobLocator (this, newHandle);
  124. default:
  125. OCIDescriptorFree (newHandle, type);
  126. newHandle = IntPtr.Zero;
  127. throw new ArgumentException ("Invalid handle type.");
  128. }
  129. }
  130. public void Dispose ()
  131. {
  132. FreeHandle (this);
  133. }
  134. public void FreeHandle (IOciHandle toBeDisposed)
  135. {
  136. OCIHandleFree (toBeDisposed.Handle, toBeDisposed.HandleType);
  137. toBeDisposed.Handle = IntPtr.Zero;
  138. }
  139. public void FreeDescriptor (IOciDescriptorHandle toBeDisposed)
  140. {
  141. OCIDescriptorFree (toBeDisposed.Handle, toBeDisposed.HandleType);
  142. toBeDisposed.Handle = IntPtr.Zero;
  143. }
  144. public OciErrorInfo HandleError ()
  145. {
  146. int errbufSize = 512;
  147. IntPtr errbuf = Marshal.AllocHGlobal (errbufSize);
  148. OciErrorInfo info;
  149. info.ErrorCode = 0;
  150. info.ErrorMessage = String.Empty;
  151. OciGlue.OCIErrorGet (handle,
  152. 1,
  153. IntPtr.Zero,
  154. out info.ErrorCode,
  155. errbuf,
  156. (uint) errbufSize,
  157. OciHandleType.Environment);
  158. object err = Marshal.PtrToStringAnsi (errbuf);
  159. if (err != null) {
  160. string errmsg = (string) err;
  161. info.ErrorMessage = String.Copy (errmsg);
  162. Marshal.FreeHGlobal (errbuf);
  163. }
  164. return info;
  165. }
  166. #endregion // Methods
  167. }
  168. }