OciGlue.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // ociglue.cs - provides glue between
  3. // managed C#/.NET System.Data.OracleClient.dll and
  4. // unmanaged native c library oci.dll
  5. // to be used in Mono System.Data.OracleClient as
  6. // the Oracle 8i data provider.
  7. //
  8. // Part of managed C#/.NET library System.Data.OracleClient.dll
  9. //
  10. // Part of the Mono class libraries at
  11. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
  12. //
  13. // Assembly: System.Data.OracleClient.dll
  14. // Namespace: System.Data.OracleClient.Oci
  15. //
  16. // Authors:
  17. // Daniel Morgan <[email protected]>
  18. // Tim Coleman <[email protected]>
  19. //
  20. // Copyright (C) Daniel Morgan, 2002
  21. // Copyright (C) Tim Coleman, 2002
  22. //
  23. using System;
  24. using System.Runtime.InteropServices;
  25. using System.Text;
  26. namespace System.Data.OracleClient.Oci {
  27. internal sealed class OciGlue
  28. {
  29. #region Fields
  30. bool connected;
  31. OciEnvironmentHandle environment;
  32. OciErrorHandle error;
  33. OciServerHandle server;
  34. OciServiceHandle service;
  35. OciSessionHandle session;
  36. // other codes
  37. public const int OCI_DEFAULT = 0;
  38. public const int OCI_SUCCESS = 0;
  39. public const int OCI_SUCCESS_WITH_INFO = 1;
  40. public const int OCI_RESERVED_FOR_INT_USE = 200;
  41. public const int OCI_NO_DATA = 100;
  42. public const int OCI_ERROR = -1;
  43. public const int OCI_INVALID_HANDLE = -2;
  44. public const int OCI_NEED_DATA = 99;
  45. public const int OCI_STILL_EXECUTING = -3123;
  46. public const int OCI_CONTINUE = -24200;
  47. #endregion // Fields
  48. #region Properties
  49. public bool Connected {
  50. get { return connected; }
  51. }
  52. public OciEnvironmentHandle Environment {
  53. get { return environment; }
  54. }
  55. public OciErrorHandle ErrorHandle {
  56. get { return error; }
  57. }
  58. public OciServiceHandle ServiceContext {
  59. get { return service; }
  60. }
  61. public OciServerHandle ServerHandle {
  62. get { return server; }
  63. }
  64. public OciSessionHandle SessionHandle {
  65. get { return session; }
  66. }
  67. #endregion // Properties
  68. #region Methods
  69. public void CreateConnection (OracleConnectionInfo conInfo)
  70. {
  71. environment = new OciEnvironmentHandle (OciEnvironmentMode.Threaded | OciEnvironmentMode.NoUserCallback);
  72. if (environment.Handle == IntPtr.Zero)
  73. throw new OracleException (0, "Could not allocate the Oracle environment.");
  74. service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
  75. if (service == null) {
  76. OciErrorInfo info = environment.HandleError ();
  77. Disconnect ();
  78. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  79. }
  80. error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
  81. if (error == null) {
  82. OciErrorInfo info = environment.HandleError ();
  83. Disconnect ();
  84. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  85. }
  86. service.ErrorHandle = error;
  87. server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
  88. if (server == null) {
  89. OciErrorInfo info = environment.HandleError ();
  90. Disconnect ();
  91. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  92. }
  93. session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
  94. if (session == null) {
  95. OciErrorInfo info = environment.HandleError ();
  96. Disconnect ();
  97. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  98. }
  99. session.Username = conInfo.Username;
  100. session.Password = conInfo.Password;
  101. session.Service = service;
  102. if (!server.Attach (conInfo.Database, ErrorHandle)) {
  103. OciErrorInfo info = error.HandleError ();
  104. Disconnect ();
  105. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  106. }
  107. if (!service.SetServer (server)) {
  108. OciErrorInfo info = error.HandleError ();
  109. Disconnect ();
  110. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  111. }
  112. if (!session.BeginSession (OciCredentialType.RDBMS, OciSessionMode.Default, ErrorHandle)) {
  113. OciErrorInfo info = error.HandleError ();
  114. Disconnect ();
  115. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  116. }
  117. if (!service.SetSession (session)) {
  118. OciErrorInfo info = error.HandleError ();
  119. Disconnect ();
  120. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  121. }
  122. connected = true;
  123. }
  124. public OciStatementHandle CreateStatement ()
  125. {
  126. OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
  127. if (statement == null) {
  128. OciErrorInfo info = environment.HandleError ();
  129. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  130. }
  131. statement.ErrorHandle = error;
  132. statement.Service = service;
  133. return statement;
  134. }
  135. public OciTransactionHandle CreateTransaction ()
  136. {
  137. OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
  138. if (transaction == null) {
  139. OciErrorInfo info = environment.HandleError ();
  140. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  141. }
  142. transaction.ErrorHandle = error;
  143. transaction.Service = service;
  144. return transaction;
  145. }
  146. public void Disconnect()
  147. {
  148. if (session != null)
  149. session.Dispose ();
  150. if (server != null)
  151. server.Dispose ();
  152. if (error != null)
  153. error.Dispose ();
  154. if (service != null)
  155. service.Dispose ();
  156. if (environment != null)
  157. environment.Dispose ();
  158. }
  159. #endregion // Methods
  160. }
  161. }