New test.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OciGlue.cs
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 <danmorg@sc.rr.com>
18 //     Tim Coleman <tim@timcoleman.com>
19 //         
20 // Copyright (C) Daniel Morgan, 2002
21 // Copyright (C) Tim Coleman, 2002
22 // 
23
24 using System;
25 using System.Runtime.InteropServices;
26 using System.Text;
27
28 namespace System.Data.OracleClient.Oci {
29         internal sealed class OciGlue 
30         {
31                 #region Fields
32
33                 bool connected;
34                 OciEnvironmentHandle environment;
35                 OciErrorHandle error;
36                 OciServerHandle server;
37                 OciServiceHandle service;
38                 OciSessionHandle session;
39
40                 // other codes
41                 public const int OCI_DEFAULT = 0;
42                 public const int OCI_SUCCESS = 0;
43                 public const int OCI_SUCCESS_WITH_INFO = 1;
44                 public const int OCI_RESERVED_FOR_INT_USE = 200;
45                 public const int OCI_NO_DATA = 100;
46                 public const int OCI_ERROR = -1;
47                 public const int OCI_INVALID_HANDLE = -2;
48                 public const int OCI_NEED_DATA = 99;
49                 public const int OCI_STILL_EXECUTING = -3123;
50                 public const int OCI_CONTINUE = -24200;
51
52                 #endregion // Fields
53
54                 #region Properties
55
56                 public bool Connected {
57                         get { return connected; }
58                 }
59
60                 public OciEnvironmentHandle Environment {
61                         get { return environment; }
62                 }
63
64                 public OciErrorHandle ErrorHandle {
65                         get { return error; }
66                 }
67
68                 public OciServiceHandle ServiceContext {
69                         get { return service; }
70                 }
71
72                 public OciServerHandle ServerHandle {
73                         get { return server; }
74                 }
75
76                 public OciSessionHandle SessionHandle {
77                         get { return session; }
78                 }
79
80                 #endregion // Properties
81
82                 #region Methods
83
84                 public void CreateConnection (OracleConnectionInfo conInfo) 
85                 {
86                         environment = new OciEnvironmentHandle (OciEnvironmentMode.Threaded | OciEnvironmentMode.NoUserCallback);
87
88                         if (environment.Handle == IntPtr.Zero)
89                                 throw new OracleException (0, "Could not allocate the Oracle environment.");
90
91                         service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
92                         if (service == null) {
93                                 OciErrorInfo info = environment.HandleError ();
94                                 Disconnect ();
95                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
96                         }
97
98                         error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
99                         if (error == null) {
100                                 OciErrorInfo info = environment.HandleError ();
101                                 Disconnect ();
102                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
103                         }
104                         service.ErrorHandle = error;
105
106                         server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
107                         if (server == null) {
108                                 OciErrorInfo info = environment.HandleError ();
109                                 Disconnect ();
110                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
111                         }
112
113                         session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
114                         if (session == null) {
115                                 OciErrorInfo info = environment.HandleError ();
116                                 Disconnect ();
117                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
118                         }
119                         session.Username = conInfo.Username;
120                         session.Password = conInfo.Password;
121                         session.Service = service;
122                                 
123                         if (!server.Attach (conInfo.Database, ErrorHandle)) {
124                                 OciErrorInfo info = error.HandleError ();
125                                 Disconnect ();
126                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
127                         }
128
129                         if (!service.SetServer (server)) {
130                                 OciErrorInfo info = error.HandleError ();
131                                 Disconnect ();
132                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
133                         }
134
135                         if (!session.BeginSession (conInfo.CredentialType, OciSessionMode.Default, ErrorHandle)) {
136                                 OciErrorInfo info = error.HandleError ();
137                                 Disconnect ();
138                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
139                         }
140
141                         if (!service.SetSession (session)) {
142                                 OciErrorInfo info = error.HandleError ();
143                                 Disconnect ();
144                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
145                         }
146
147                         connected = true;
148                 }
149
150                 public OciStatementHandle CreateStatement ()
151                 {
152                         OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
153                         if (statement == null) {
154                                 OciErrorInfo info = environment.HandleError ();
155                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
156                         }
157                         statement.ErrorHandle = error;
158                         statement.Service = service;
159
160                         return statement;       
161                 }
162
163                 public OciTransactionHandle CreateTransaction ()
164                 {
165                         OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
166                         if (transaction == null) {
167                                 OciErrorInfo info = environment.HandleError ();
168                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
169                         }
170                         transaction.ErrorHandle = error;
171                         transaction.Service = service;
172
173                         return transaction;
174                 }
175
176                 public void Disconnect() 
177                 {
178                         if (session != null) {
179                                 session.EndSession (error);
180                                 session.Dispose ();
181                                 session = null;
182                         }
183                         if (server != null) {
184                                 server.Detach (error);
185                                 server.Dispose ();
186                                 server = null;
187                         }
188                         if (error != null) {
189                                 error.Dispose ();
190                                 error = null;
191                         }
192                         if (service != null) {
193                                 service.Dispose ();
194                                 service = null;
195                         }
196                         if (environment != null) {
197                                 environment.Dispose ();
198                                 environment = null;
199                         }
200                 }
201
202                 #endregion // Methods
203         }
204 }