New test.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient.Oci / OciHandle.cs
1 // 
2 // OciHandle.cs 
3 //  
4 // Part of managed C#/.NET library System.Data.OracleClient.dll
5 //
6 // Part of the Mono class libraries at
7 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
8 //
9 // Assembly: System.Data.OracleClient.dll
10 // Namespace: System.Data.OracleClient.Oci
11 // 
12 // Author: 
13 //     Tim Coleman <tim@timcoleman.com>
14 //     Daniel Morgan <danielmorgan@verizon.net>
15 //         
16 // Copyright (C) Tim Coleman, 2003
17 // Copyright (C) Daniel Morgan, 2005
18 // 
19
20 using System;
21 using System.Runtime.InteropServices;
22
23 namespace System.Data.OracleClient.Oci {
24         internal abstract class OciHandle : IDisposable {
25                 #region Fields
26
27                 bool disposed = false;
28                 IntPtr handle = IntPtr.Zero;
29                 OciHandle parent = null;
30                 OciHandleType type;
31
32                 #endregion // Fields
33
34                 #region Constructors
35
36                 public OciHandle (OciHandleType type, OciHandle parent, IntPtr newHandle) {
37                         this.type = type;
38                         this.parent = parent;
39                         this.handle = newHandle;
40                 }
41
42                 ~OciHandle () {
43                         Dispose (false);
44                 }
45
46                 #endregion // Constructors
47
48                 #region Properties
49
50                 public OciHandle Parent {
51                         get { return parent; }
52                 }
53
54                 public IntPtr Handle { 
55                         get { return handle; }
56                 }
57
58                 public OciHandleType HandleType { 
59                         get { return type; }
60                 }
61
62                 #endregion // Properties
63
64                 #region Methods
65
66                 public OciHandle Allocate (OciHandleType type) {
67                         int status = 0;
68                         IntPtr newHandle = IntPtr.Zero;
69
70                         if (type < OciHandleType.LobLocator)
71                                 status = OciCalls.OCIHandleAlloc (this,
72                                         out newHandle,
73                                         type,
74                                         0,
75                                         IntPtr.Zero);
76                         else
77                                 status = OciCalls.OCIDescriptorAlloc (this,
78                                         out newHandle,
79                                         type,
80                                         0,
81                                         IntPtr.Zero);
82                 
83                         if (status != 0 && status != 1)
84                                 throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", type));
85
86                         switch (type) {
87                         case OciHandleType.Service:
88                                 return new OciServiceHandle (this, newHandle);
89                         case OciHandleType.Error:
90                                 return new OciErrorHandle (this, newHandle);
91                         case OciHandleType.Server:
92                                 return new OciServerHandle (this, newHandle);
93                         case OciHandleType.Session:
94                                 return new OciSessionHandle (this, newHandle);
95                         case OciHandleType.Statement:
96                                 return new OciStatementHandle (this, newHandle);
97                         case OciHandleType.Transaction:
98                                 return new OciTransactionHandle (this, newHandle);
99                         case OciHandleType.LobLocator:
100                                 return new OciLobLocator (this, newHandle);
101                         case OciHandleType.RowId:
102                                 return new OciRowIdDescriptor (this, newHandle);
103                         case OciHandleType.TimeStamp:
104                                 return new OciDateTimeDescriptor (this, newHandle);
105                         }
106                         return null;
107                 }
108
109                 protected virtual void Dispose (bool disposing) {
110                         if (!disposed) {
111                                 FreeHandle ();
112                                 if (disposing) {
113                                         parent = null;
114                                 }
115                                 disposed = true;
116                         }
117                 }
118
119                 public void Dispose () {
120                         Dispose (true);
121                         GC.SuppressFinalize (this);
122                 }
123
124                 protected virtual void FreeHandle () 
125                 {
126                         if (type < OciHandleType.LobLocator) {
127                                 switch (type) {
128                                 case OciHandleType.Bind:
129                                 case OciHandleType.Define:
130                                         // Bind and Define handles are freed when Statement handle is disposed
131                                         break;
132                                 case OciHandleType.Environment:
133                                         if (handle != IntPtr.Zero) {
134                                                 OciCalls.OCIHandleFree (handle, type);
135                                         }
136                                         break;
137                                 default:
138                                         if ( handle != IntPtr.Zero &&
139                                                 parent != null && 
140                                                 parent.Handle != IntPtr.Zero )  {
141
142                                                 OciCalls.OCIHandleFree (handle, type);
143                                         }
144                                         break;
145                                 }
146                                 handle = IntPtr.Zero;
147                         }
148                 }
149
150                 public bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle) {
151                         return (GetAttributeInt32 (attrType, errorHandle) != 0);
152                 }
153
154                 public sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
155                         int status = 0;
156                         sbyte output;
157
158                         status = OciCalls.OCIAttrGetSByte (Handle,
159                                 HandleType,
160                                 out output,
161                                 IntPtr.Zero,
162                                 attrType,
163                                 errorHandle);
164
165                         if (status != 0) {
166                                 OciErrorInfo info = errorHandle.HandleError ();
167                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
168                         }
169
170                         return output;
171                 }
172
173                 public byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
174                         int status = 0;
175                         byte output;
176
177                         status = OciCalls.OCIAttrGetByte (Handle,
178                                 HandleType,
179                                 out output,
180                                 IntPtr.Zero,
181                                 attrType,
182                                 errorHandle);
183
184                         if (status != 0) {
185                                 OciErrorInfo info = errorHandle.HandleError ();
186                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
187                         }
188
189                         return output;
190                 }
191
192                 public ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle) {
193                         int status = 0;
194                         ushort output;
195
196                         status = OciCalls.OCIAttrGetUInt16 (Handle,
197                                 HandleType,
198                                 out output,
199                                 IntPtr.Zero,
200                                 attrType,
201                                 errorHandle);
202
203                         if (status != 0) {
204                                 OciErrorInfo info = errorHandle.HandleError ();
205                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
206                         }
207
208                         return output;
209                 }
210
211                 public int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle) {
212                         int status = 0;
213                         int output;
214
215                         status = OciCalls.OCIAttrGetInt32 (Handle,
216                                 HandleType,
217                                 out output,
218                                 IntPtr.Zero,
219                                 attrType,
220                                 errorHandle);
221
222                         if (status != 0) {
223                                 OciErrorInfo info = errorHandle.HandleError ();
224                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
225                         }
226
227                         return output;
228                 }
229
230                 public IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle) {
231                         int status = 0;
232                         IntPtr output = IntPtr.Zero;
233                         status = OciCalls.OCIAttrGetIntPtr (Handle,
234                                 HandleType,
235                                 out output,
236                                 IntPtr.Zero,
237                                 attrType,
238                                 errorHandle);
239
240                         if (status != 0) {
241                                 OciErrorInfo info = errorHandle.HandleError ();
242                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
243                         }
244
245                         return output;
246                 }
247
248                 public string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle) {
249                         string output = String.Empty;
250                         IntPtr outputPtr = IntPtr.Zero;
251                         int outSize;
252                         int status = 0;
253
254                         status = OciCalls.OCIAttrGet (Handle,
255                                 HandleType,
256                                 out outputPtr,
257                                 out outSize,
258                                 attrType,
259                                 errorHandle);
260
261                         if (status != 0) {
262                                 OciErrorInfo info = errorHandle.HandleError ();
263                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
264                         }
265
266                         if (outputPtr != IntPtr.Zero && outSize > 0) {
267                                 object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
268                                 if (str != null) 
269                                         output = String.Copy ((string) str);
270                         }
271
272                         return output;
273                 }
274
275                 public void SetAttributeString (string attribute, OciAttributeType attrType, OciErrorHandle errorHandle) 
276                 {
277                         int status = 0;
278                         
279                         status = OciCalls.OCIAttrSetString (Handle,
280                                 HandleType,
281                                 attribute,
282                                 (uint) attribute.Length,
283                                 attrType,
284                                 errorHandle);
285
286                         if (status != 0) {
287                                 OciErrorInfo info = errorHandle.HandleError ();
288                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
289                         }
290                 }
291
292                 public void SetHandle (IntPtr h)
293                 {
294                         handle = h;
295                 }
296
297                 #endregion // Methods
298
299                 #region Operators and Type Conversions
300
301                 public static implicit operator IntPtr (OciHandle h)
302                 {
303                         return h.Handle;
304                 }
305
306                 #endregion // Operators and Type Conversions
307         }
308 }