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 namespace System.Data.OracleClient.Oci {
24 internal abstract class OciHandle : IDisposable {
27 bool disposed = false;
28 IntPtr handle = IntPtr.Zero;
29 OciHandle parent = null;
36 public OciHandle (OciHandleType type, OciHandle parent, IntPtr newHandle) {
39 this.handle = newHandle;
46 #endregion // Constructors
50 public OciHandle Parent {
51 get { return parent; }
54 public IntPtr Handle {
55 get { return handle; }
58 public OciHandleType HandleType {
62 #endregion // Properties
66 public OciHandle Allocate (OciHandleType type) {
68 IntPtr newHandle = IntPtr.Zero;
70 if (type < OciHandleType.LobLocator)
71 status = OciCalls.OCIHandleAlloc (this,
77 status = OciCalls.OCIDescriptorAlloc (this,
83 if (status != 0 && status != 1)
84 throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", 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);
109 protected virtual void Dispose (bool disposing) {
119 public void Dispose () {
121 GC.SuppressFinalize (this);
124 protected virtual void FreeHandle ()
126 if (type < OciHandleType.LobLocator) {
128 case OciHandleType.Bind:
129 case OciHandleType.Define:
130 // Bind and Define handles are freed when Statement handle is disposed
132 case OciHandleType.Environment:
133 if (handle != IntPtr.Zero) {
134 OciCalls.OCIHandleFree (handle, type);
138 if ( handle != IntPtr.Zero &&
140 parent.Handle != IntPtr.Zero ) {
142 OciCalls.OCIHandleFree (handle, type);
146 handle = IntPtr.Zero;
150 public bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle) {
151 return (GetAttributeInt32 (attrType, errorHandle) != 0);
154 public sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
158 status = OciCalls.OCIAttrGetSByte (Handle,
166 OciErrorInfo info = errorHandle.HandleError ();
167 throw new OracleException (info.ErrorCode, info.ErrorMessage);
173 public byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
177 status = OciCalls.OCIAttrGetByte (Handle,
185 OciErrorInfo info = errorHandle.HandleError ();
186 throw new OracleException (info.ErrorCode, info.ErrorMessage);
192 public ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle) {
196 status = OciCalls.OCIAttrGetUInt16 (Handle,
204 OciErrorInfo info = errorHandle.HandleError ();
205 throw new OracleException (info.ErrorCode, info.ErrorMessage);
211 public int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle) {
215 status = OciCalls.OCIAttrGetInt32 (Handle,
223 OciErrorInfo info = errorHandle.HandleError ();
224 throw new OracleException (info.ErrorCode, info.ErrorMessage);
230 public IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle) {
232 IntPtr output = IntPtr.Zero;
233 status = OciCalls.OCIAttrGetIntPtr (Handle,
241 OciErrorInfo info = errorHandle.HandleError ();
242 throw new OracleException (info.ErrorCode, info.ErrorMessage);
248 public string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle) {
249 string output = String.Empty;
250 IntPtr outputPtr = IntPtr.Zero;
254 status = OciCalls.OCIAttrGet (Handle,
262 OciErrorInfo info = errorHandle.HandleError ();
263 throw new OracleException (info.ErrorCode, info.ErrorMessage);
266 if (outputPtr != IntPtr.Zero && outSize > 0) {
267 object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
269 output = String.Copy ((string) str);
275 public void SetAttributeString (string attribute, OciAttributeType attrType, OciErrorHandle errorHandle)
279 status = OciCalls.OCIAttrSetString (Handle,
282 (uint) attribute.Length,
287 OciErrorInfo info = errorHandle.HandleError ();
288 throw new OracleException (info.ErrorCode, info.ErrorMessage);
292 public void SetHandle (IntPtr h)
297 #endregion // Methods
299 #region Operators and Type Conversions
301 public static implicit operator IntPtr (OciHandle h)
306 #endregion // Operators and Type Conversions