4 // Part of managed C#/.NET library System.Data.OracleClient.dll
6 // Part of the Mono class libraries at
7 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
9 // Assembly: System.Data.OracleClient.dll
10 // Namespace: System.Data.OracleClient.Oci
13 // Tim Coleman <tim@timcoleman.com>
14 // Daniel Morgan <danielmorgan@verizon.net>
16 // Copyright (C) Tim Coleman, 2003
17 // Copyright (C) Daniel Morgan, 2005
21 using System.Runtime.InteropServices;
23 using System.Security.Cryptography;
24 using System.Globalization;
26 namespace System.Data.OracleClient.Oci {
27 internal abstract class OciHandle : IDisposable {
30 bool disposed = false;
32 protected internal IntPtr handle = IntPtr.Zero;
33 OciHandle parent = null;
40 internal OciHandle (OciHandleType type, OciHandle parent, IntPtr newHandle) {
43 this.handle = newHandle;
50 #endregion // Constructors
54 internal OciHandle Parent {
55 get { return parent; }
58 internal IntPtr Handle {
59 get { return handle; }
62 internal OciHandleType HandleType {
66 #endregion // Properties
70 internal OciHandle Allocate (OciHandleType type) {
72 IntPtr newHandle = IntPtr.Zero;
74 if (type < OciHandleType.LobLocator)
75 status = OciCalls.OCIHandleAlloc (this,
81 status = OciCalls.OCIDescriptorAlloc (this,
87 if (status != 0 && status != 1)
88 throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", type));
91 case OciHandleType.Service:
92 return new OciServiceHandle (this, newHandle);
93 case OciHandleType.Error:
94 return new OciErrorHandle (this, newHandle);
95 case OciHandleType.Server:
96 return new OciServerHandle (this, newHandle);
97 case OciHandleType.Session:
98 return new OciSessionHandle (this, newHandle);
99 case OciHandleType.Statement:
100 return new OciStatementHandle (this, newHandle);
101 case OciHandleType.Transaction:
102 return new OciTransactionHandle (this, newHandle);
103 case OciHandleType.LobLocator:
104 return new OciLobLocator (this, newHandle);
105 case OciHandleType.RowId:
106 return new OciRowIdDescriptor (this, newHandle);
107 case OciHandleType.TimeStamp:
108 return new OciDateTimeDescriptor (this, newHandle);
109 case OciHandleType.IntervalDayToSecond:
110 case OciHandleType.IntervalYearToMonth:
111 return new OciIntervalDescriptor (this, type, newHandle);
116 protected virtual void Dispose (bool disposing) {
126 public void Dispose () {
128 GC.SuppressFinalize (this);
131 protected virtual void FreeHandle ()
133 if (type < OciHandleType.LobLocator) {
135 case OciHandleType.Bind:
136 case OciHandleType.Define:
137 // Bind and Define handles are freed when Statement handle is disposed
139 case OciHandleType.Environment:
140 if (handle != IntPtr.Zero) {
141 OciCalls.OCIHandleFree (handle, type);
145 if ( handle != IntPtr.Zero &&
147 parent.Handle != IntPtr.Zero ) {
149 OciCalls.OCIHandleFree (handle, type);
153 handle = IntPtr.Zero;
157 internal bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle) {
158 return (GetAttributeInt32 (attrType, errorHandle) != 0);
161 internal sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
165 status = OciCalls.OCIAttrGetSByte (Handle,
173 OciErrorInfo info = errorHandle.HandleError ();
174 throw new OracleException (info.ErrorCode, info.ErrorMessage);
180 internal byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
184 status = OciCalls.OCIAttrGetByte (Handle,
192 OciErrorInfo info = errorHandle.HandleError ();
193 throw new OracleException (info.ErrorCode, info.ErrorMessage);
199 internal ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle) {
203 status = OciCalls.OCIAttrGetUInt16 (Handle,
211 OciErrorInfo info = errorHandle.HandleError ();
212 throw new OracleException (info.ErrorCode, info.ErrorMessage);
218 internal int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle) {
222 status = OciCalls.OCIAttrGetInt32 (Handle,
230 OciErrorInfo info = OciErrorHandle.HandleError (errorHandle, status);
231 throw new OracleException (info.ErrorCode, info.ErrorMessage);
237 [DllImport ("oci", EntryPoint = "OCIAttrGet")]
238 internal static extern int OCIAttrGetRowIdDesc (IntPtr trgthndlp,
239 [MarshalAs (UnmanagedType.U4)] OciHandleType trghndltyp,
242 [MarshalAs (UnmanagedType.U4)] OciAttributeType attrtype,
244 internal OciRowIdDescriptor GetAttributeRowIdDescriptor (OciErrorHandle errorHandle, OciHandle env)
246 OciRowIdDescriptor descriptor = null;
247 IntPtr outputPtr = IntPtr.Zero;
250 OciAttributeType attrType = OciAttributeType.RowId;
252 outputPtr = OciCalls.AllocateClear (outSize);
254 uint siz = (uint) outSize;
255 status = OCIAttrGetRowIdDesc (Handle,
263 OciErrorInfo info = errorHandle.HandleError ();
264 throw new OracleException (info.ErrorCode, info.ErrorMessage);
267 if (outputPtr != IntPtr.Zero && siz > 0) {
268 descriptor = (OciRowIdDescriptor) env.Allocate(OciHandleType.RowId);
269 descriptor.SetHandle (outputPtr);
275 internal IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle) {
277 IntPtr output = IntPtr.Zero;
278 status = OciCalls.OCIAttrGetIntPtr (Handle,
286 OciErrorInfo info = errorHandle.HandleError ();
287 throw new OracleException (info.ErrorCode, info.ErrorMessage);
293 internal string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle) {
294 string output = String.Empty;
295 IntPtr outputPtr = IntPtr.Zero;
299 status = OciCalls.OCIAttrGet (Handle,
307 OciErrorInfo info = errorHandle.HandleError ();
308 throw new OracleException (info.ErrorCode, info.ErrorMessage);
311 if (outputPtr != IntPtr.Zero && outSize > 0) {
312 object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
314 output = String.Copy ((string) str);
320 internal void SetAttributeString (string attribute, OciAttributeType attrType, OciErrorHandle errorHandle)
324 status = OciCalls.OCIAttrSetString (Handle,
327 (uint) attribute.Length,
332 OciErrorInfo info = errorHandle.HandleError ();
333 throw new OracleException (info.ErrorCode, info.ErrorMessage);
337 internal void SetHandle (IntPtr h)
342 #endregion // Methods
344 #region Operators and Type Conversions
346 public static implicit operator IntPtr (OciHandle h)
351 #endregion // Operators and Type Conversions