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.
8 // Part of managed C#/.NET library System.Data.OracleClient.dll
10 // Part of the Mono class libraries at
11 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
13 // Assembly: System.Data.OracleClient.dll
14 // Namespace: System.Data.OracleClient.Oci
17 // Daniel Morgan <monodanmorg@yahoo.com>
18 // Tim Coleman <tim@timcoleman.com>
20 // Copyright (C) Daniel Morgan, 2002, 2009
21 // Copyright (C) Tim Coleman, 2002
24 //#define ORACLE_DATA_ACCESS
27 using System.Runtime.InteropServices;
30 namespace System.Data.OracleClient.Oci {
31 internal sealed class OciGlue
36 OciEnvironmentHandle environment;
38 OciServerHandle server;
39 OciServiceHandle service;
40 OciSessionHandle session;
43 public const int OCI_DEFAULT = 0;
44 public const int OCI_SUCCESS = 0;
45 public const int OCI_SUCCESS_WITH_INFO = 1; // Diagnostic or Warning message - call OCIErrorGet
46 public const int OCI_RESERVED_FOR_INT_USE = 200;
47 public const int OCI_NO_DATA = 100;
48 public const int OCI_ERROR = -1; // use error handle to get error code and description - call OCIErrorGet
49 public const int OCI_INVALID_HANDLE = -2;
50 public const int OCI_NEED_DATA = 99;
51 public const int OCI_STILL_EXECUTING = -3123;
52 public const int OCI_CONTINUE = -24200;
58 public bool Connected {
59 get { return connected; }
62 public OciEnvironmentHandle Environment {
63 get { return environment; }
66 public OciErrorHandle ErrorHandle {
70 public OciServiceHandle ServiceContext {
71 get { return service; }
74 public OciServerHandle ServerHandle {
75 get { return server; }
78 public OciSessionHandle SessionHandle {
79 get { return session; }
82 #endregion // Properties
86 public void CreateConnection (OracleConnectionInfo conInfo)
88 environment = new OciEnvironmentHandle (OciEnvironmentMode.Threaded | OciEnvironmentMode.NoUserCallback);
90 if (environment.Handle == IntPtr.Zero)
91 throw new OracleException (0, "Could not allocate the Oracle environment.");
93 service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
94 if (service == null) {
95 OciErrorInfo info = environment.HandleError ();
97 throw new OracleException (info.ErrorCode, info.ErrorMessage);
100 error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
102 OciErrorInfo info = environment.HandleError ();
104 throw new OracleException (info.ErrorCode, info.ErrorMessage);
106 service.ErrorHandle = error;
108 server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
109 if (server == null) {
110 OciErrorInfo info = environment.HandleError ();
112 throw new OracleException (info.ErrorCode, info.ErrorMessage);
115 session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
116 if (session == null) {
117 OciErrorInfo info = environment.HandleError ();
119 throw new OracleException (info.ErrorCode, info.ErrorMessage);
121 session.Username = conInfo.Username;
122 session.Password = conInfo.Password;
123 session.Service = service;
125 if (!server.Attach (conInfo.Database, ErrorHandle)) {
126 OciErrorInfo info = error.HandleError ();
128 throw new OracleException (info.ErrorCode, info.ErrorMessage);
131 if (!service.SetServer (server)) {
132 OciErrorInfo info = error.HandleError ();
134 throw new OracleException (info.ErrorCode, info.ErrorMessage);
137 #if ORACLE_DATA_ACCESS
138 if (conInfo.SetNewPassword == true) {
139 // open with new password
140 if (!service.SetSession (session)) {
141 OciErrorInfo info = error.HandleError ();
143 throw new OracleException (info.ErrorCode, info.ErrorMessage);
145 if (!service.ChangePassword (conInfo.NewPassword, error)) {
146 OciErrorInfo info = error.HandleError ();
148 throw new OracleException (info.ErrorCode, info.ErrorMessage);
150 conInfo.Password = conInfo.NewPassword;
151 conInfo.SetNewPassword = false;
152 conInfo.NewPassword = string.Empty;
156 if (!session.BeginSession (conInfo.CredentialType, OciSessionMode.Default, ErrorHandle)) {
157 OciErrorInfo info = error.HandleError ();
159 throw new OracleException (info.ErrorCode, info.ErrorMessage);
162 if (!service.SetSession (session)) {
163 OciErrorInfo info = error.HandleError ();
165 throw new OracleException (info.ErrorCode, info.ErrorMessage);
167 #if ORACLE_DATA_ACCESS
173 public OciStatementHandle CreateStatement ()
175 OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
176 if (statement == null) {
177 OciErrorInfo info = environment.HandleError ();
178 throw new OracleException (info.ErrorCode, info.ErrorMessage);
180 statement.ErrorHandle = error;
181 statement.Service = service;
186 public OciTransactionHandle CreateTransaction ()
188 OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
189 if (transaction == null) {
190 OciErrorInfo info = environment.HandleError ();
191 throw new OracleException (info.ErrorCode, info.ErrorMessage);
193 transaction.ErrorHandle = error;
194 transaction.Service = service;
199 public void Disconnect()
201 if (session != null) {
202 session.EndSession (error);
206 if (server != null) {
207 server.Detach (error);
215 if (service != null) {
219 if (environment != null) {
220 environment.Dispose ();
225 public static string ReturnCodeToString (int status)
229 return "OCI_DEFAULT or OCI_SUCCESS"; // both are zero
230 case OCI_SUCCESS_WITH_INFO:
231 return "OCI_SUCCESS_WITH_INFO";
232 case OCI_RESERVED_FOR_INT_USE:
233 return "OCI_RESERVED_FOR_INT_USE";
235 return "OCI_NO_DATA";
238 case OCI_INVALID_HANDLE:
239 return "OCI_INVALID_HANDLE";
241 return "OCI_NEED_DATA";
242 case OCI_STILL_EXECUTING:
243 return "OCI_STILL_EXECUTING";
245 return "OCI_CONTINUE";
247 return "Unknown Error";
250 #endregion // Methods