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 <danmorg@sc.rr.com>
18 // Tim Coleman <tim@timcoleman.com>
20 // Copyright (C) Daniel Morgan, 2002
21 // Copyright (C) Tim Coleman, 2002
25 using System.Runtime.InteropServices;
28 namespace System.Data.OracleClient.Oci {
29 internal sealed class OciGlue
32 OciEnvironmentHandle environment;
34 OciServerHandle server;
35 OciServiceHandle service;
36 OciSessionHandle session;
38 public bool Connected {
39 get { return connected; }
43 public const int OCI_DEFAULT = 0;
44 public const int OCI_SUCCESS = 0;
45 public const int OCI_SUCCESS_WITH_INFO = 1;
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;
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;
54 [DllImport ("oci", EntryPoint = "OCIAttrSet")]
55 public static extern int OCIAttrSet (IntPtr trgthndlp,
56 [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
59 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
62 [DllImport ("oci", EntryPoint = "OCIAttrSet")]
63 public static extern int OCIAttrSetString (IntPtr trgthndlp,
64 [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
67 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
71 public static extern int OCIAttrGet (IntPtr trgthndlp,
73 out IntPtr attributep,
75 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
78 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
79 public static extern int OCIAttrGetBool (IntPtr trgthndlp,
83 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
86 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
87 public static extern int OCIAttrGetSByte (IntPtr trgthndlp,
91 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
94 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
95 public static extern int OCIAttrGetByte (IntPtr trgthndlp,
99 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
102 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
103 public static extern int OCIAttrGetUInt16 (IntPtr trgthndlp,
105 out ushort attributep,
107 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
110 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
111 public static extern int OCIAttrGetInt32 (IntPtr trgthndlp,
115 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
119 public static extern int OCIErrorGet (IntPtr hndlp,
125 [MarshalAs (UnmanagedType.U4)] OciHandleType type);
127 public void CreateConnection (OracleConnectionInfo conInfo)
129 environment = new OciEnvironmentHandle (OciEnvironmentMode.NoUserCallback);
130 if (environment.Handle == IntPtr.Zero)
131 throw new OracleException (0, "Could not allocate the Oracle environment.");
133 service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
134 if (service == null) {
135 OciErrorInfo info = environment.HandleError ();
137 throw new OracleException (info.ErrorCode, info.ErrorMessage);
140 error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
142 OciErrorInfo info = environment.HandleError ();
144 throw new OracleException (info.ErrorCode, info.ErrorMessage);
146 service.ErrorHandle = error;
148 server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
149 if (server == null) {
150 OciErrorInfo info = environment.HandleError ();
152 throw new OracleException (info.ErrorCode, info.ErrorMessage);
154 server.ErrorHandle = error;
155 server.TNSName = conInfo.Database;
157 session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
158 if (session == null) {
159 OciErrorInfo info = environment.HandleError ();
161 throw new OracleException (info.ErrorCode, info.ErrorMessage);
163 session.ErrorHandle = error;
164 session.Username = conInfo.Username;
165 session.Password = conInfo.Password;
166 session.Service = service;
168 if (!server.Attach ()) {
169 OciErrorInfo info = error.HandleError ();
171 throw new OracleException (info.ErrorCode, info.ErrorMessage);
174 if (!service.SetServer (server)) {
175 OciErrorInfo info = error.HandleError ();
177 throw new OracleException (info.ErrorCode, info.ErrorMessage);
180 if (!session.Begin (OciCredentialType.RDBMS, OciSessionMode.Default)) {
181 OciErrorInfo info = error.HandleError ();
183 throw new OracleException (info.ErrorCode, info.ErrorMessage);
187 if (!service.SetSession (session)) {
188 OciErrorInfo info = error.HandleError ();
190 throw new OracleException (info.ErrorCode, info.ErrorMessage);
196 public OciStatementHandle CreateStatement ()
198 OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
199 if (statement == null) {
200 OciErrorInfo info = environment.HandleError ();
201 throw new OracleException (info.ErrorCode, info.ErrorMessage);
203 statement.ErrorHandle = error;
204 statement.Service = service;
209 public OciTransactionHandle CreateTransaction ()
211 OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
212 if (transaction == null) {
213 OciErrorInfo info = environment.HandleError ();
214 throw new OracleException (info.ErrorCode, info.ErrorMessage);
216 transaction.ErrorHandle = error;
217 transaction.Service = service;
222 public void Disconnect()
232 if (environment != null)
233 environment.Dispose ();