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 OCIAttrGetByte (IntPtr trgthndlp,
91 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
94 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
95 public static extern int OCIAttrGetInt32 (IntPtr trgthndlp,
99 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
103 public static extern int OCIErrorGet (IntPtr hndlp,
109 [MarshalAs (UnmanagedType.U4)] OciHandleType type);
111 public void CreateConnection (OracleConnectionInfo conInfo)
113 environment = new OciEnvironmentHandle (OciEnvironmentMode.NoUserCallback);
114 if (environment.Handle == IntPtr.Zero)
115 throw new OracleException (0, "Could not allocate the Oracle environment.");
117 service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
118 if (service == null) {
119 OciErrorInfo info = environment.HandleError ();
121 throw new OracleException (info.ErrorCode, info.ErrorMessage);
124 error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
126 OciErrorInfo info = environment.HandleError ();
128 throw new OracleException (info.ErrorCode, info.ErrorMessage);
130 service.ErrorHandle = error;
132 server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
133 if (server == null) {
134 OciErrorInfo info = environment.HandleError ();
136 throw new OracleException (info.ErrorCode, info.ErrorMessage);
138 server.ErrorHandle = error;
139 server.TNSName = conInfo.Database;
141 session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
142 if (session == null) {
143 OciErrorInfo info = environment.HandleError ();
145 throw new OracleException (info.ErrorCode, info.ErrorMessage);
147 session.ErrorHandle = error;
148 session.Username = conInfo.Username;
149 session.Password = conInfo.Password;
150 session.Service = service;
152 if (!server.Attach ()) {
153 OciErrorInfo info = error.HandleError ();
155 throw new OracleException (info.ErrorCode, info.ErrorMessage);
158 if (!service.SetServer (server)) {
159 OciErrorInfo info = error.HandleError ();
161 throw new OracleException (info.ErrorCode, info.ErrorMessage);
164 if (!session.Begin (OciCredentialType.RDBMS, OciSessionMode.Default)) {
165 OciErrorInfo info = error.HandleError ();
167 throw new OracleException (info.ErrorCode, info.ErrorMessage);
171 if (!service.SetSession (session)) {
172 OciErrorInfo info = error.HandleError ();
174 throw new OracleException (info.ErrorCode, info.ErrorMessage);
180 public OciStatementHandle CreateStatement ()
182 OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
183 if (statement == null) {
184 OciErrorInfo info = environment.HandleError ();
185 throw new OracleException (info.ErrorCode, info.ErrorMessage);
187 statement.ErrorHandle = error;
188 statement.Service = service;
193 public OciTransactionHandle CreateTransaction ()
195 OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
196 if (transaction == null) {
197 OciErrorInfo info = environment.HandleError ();
198 throw new OracleException (info.ErrorCode, info.ErrorMessage);
200 transaction.ErrorHandle = error;
201 transaction.Service = service;
206 public void Disconnect()
216 if (environment != null)
217 environment.Dispose ();