File: OciHandle.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (308 lines) | stat: -rw-r--r-- 7,091 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// 
// OciHandle.cs 
//  
// Part of managed C#/.NET library System.Data.OracleClient.dll
//
// Part of the Mono class libraries at
// mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
//
// Assembly: System.Data.OracleClient.dll
// Namespace: System.Data.OracleClient.Oci
// 
// Author: 
//     Tim Coleman <tim@timcoleman.com>
//     Daniel Morgan <danielmorgan@verizon.net>
//         
// Copyright (C) Tim Coleman, 2003
// Copyright (C) Daniel Morgan, 2005
// 

using System;
using System.Runtime.InteropServices;

namespace System.Data.OracleClient.Oci {
	internal abstract class OciHandle : IDisposable {
		#region Fields

		bool disposed = false;
		IntPtr handle = IntPtr.Zero;
		OciHandle parent = null;
		OciHandleType type;

		#endregion // Fields

		#region Constructors

		public OciHandle (OciHandleType type, OciHandle parent, IntPtr newHandle) {
			this.type = type;
			this.parent = parent;
			this.handle = newHandle;
		}

		~OciHandle () {
			Dispose (false);
		}

		#endregion // Constructors

		#region Properties

		public OciHandle Parent {
			get { return parent; }
		}

		public IntPtr Handle { 
			get { return handle; }
		}

		public OciHandleType HandleType { 
			get { return type; }
		}

		#endregion // Properties

		#region Methods

		public OciHandle Allocate (OciHandleType type) {
			int status = 0;
			IntPtr newHandle = IntPtr.Zero;

			if (type < OciHandleType.LobLocator)
				status = OciCalls.OCIHandleAlloc (this,
					out newHandle,
					type,
					0,
					IntPtr.Zero);
			else
				status = OciCalls.OCIDescriptorAlloc (this,
					out newHandle,
					type,
					0,
					IntPtr.Zero);
		
			if (status != 0 && status != 1)
				throw new Exception (String.Format ("Could not allocate new OCI Handle of type {0}", type));

			switch (type) {
			case OciHandleType.Service:
				return new OciServiceHandle (this, newHandle);
			case OciHandleType.Error:
				return new OciErrorHandle (this, newHandle);
			case OciHandleType.Server:
				return new OciServerHandle (this, newHandle);
			case OciHandleType.Session:
				return new OciSessionHandle (this, newHandle);
			case OciHandleType.Statement:
				return new OciStatementHandle (this, newHandle);
			case OciHandleType.Transaction:
				return new OciTransactionHandle (this, newHandle);
			case OciHandleType.LobLocator:
				return new OciLobLocator (this, newHandle);
			case OciHandleType.RowId:
				return new OciRowIdDescriptor (this, newHandle);
			case OciHandleType.TimeStamp:
				return new OciDateTimeDescriptor (this, newHandle);
			}
			return null;
		}

		protected virtual void Dispose (bool disposing) {
			if (!disposed) {
				FreeHandle ();
				if (disposing) {
					parent = null;
				}
				disposed = true;
			}
		}

		public void Dispose () {
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void FreeHandle () 
		{
			if (type < OciHandleType.LobLocator) {
				switch (type) {
				case OciHandleType.Bind:
				case OciHandleType.Define:
					// Bind and Define handles are freed when Statement handle is disposed
					break;
				case OciHandleType.Environment:
					if (handle != IntPtr.Zero) {
						OciCalls.OCIHandleFree (handle, type);
					}
					break;
				default:
					if ( handle != IntPtr.Zero &&
						parent != null && 
						parent.Handle != IntPtr.Zero )	{

						OciCalls.OCIHandleFree (handle, type);
					}
					break;
				}
				handle = IntPtr.Zero;
			}
		}

		public bool GetAttributeBool (OciAttributeType attrType, OciErrorHandle errorHandle) {
			return (GetAttributeInt32 (attrType, errorHandle) != 0);
		}

		public sbyte GetAttributeSByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
			int status = 0;
			sbyte output;

			status = OciCalls.OCIAttrGetSByte (Handle,
				HandleType,
				out output,
				IntPtr.Zero,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			return output;
		}

		public byte GetAttributeByte (OciAttributeType attrType, OciErrorHandle errorHandle) {
			int status = 0;
			byte output;

			status = OciCalls.OCIAttrGetByte (Handle,
				HandleType,
				out output,
				IntPtr.Zero,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			return output;
		}

		public ushort GetAttributeUInt16 (OciAttributeType attrType, OciErrorHandle errorHandle) {
			int status = 0;
			ushort output;

			status = OciCalls.OCIAttrGetUInt16 (Handle,
				HandleType,
				out output,
				IntPtr.Zero,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			return output;
		}

		public int GetAttributeInt32 (OciAttributeType attrType, OciErrorHandle errorHandle) {
			int status = 0;
			int output;

			status = OciCalls.OCIAttrGetInt32 (Handle,
				HandleType,
				out output,
				IntPtr.Zero,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			return output;
		}

		public IntPtr GetAttributeIntPtr (OciAttributeType attrType, OciErrorHandle errorHandle) {
			int status = 0;
			IntPtr output = IntPtr.Zero;
			status = OciCalls.OCIAttrGetIntPtr (Handle,
				HandleType,
				out output,
				IntPtr.Zero,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			return output;
		}

		public string GetAttributeString (OciAttributeType attrType, OciErrorHandle errorHandle) {
			string output = String.Empty;
			IntPtr outputPtr = IntPtr.Zero;
			int outSize;
			int status = 0;

			status = OciCalls.OCIAttrGet (Handle,
				HandleType,
				out outputPtr,
				out outSize,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}

			if (outputPtr != IntPtr.Zero && outSize > 0) {
				object str = Marshal.PtrToStringAnsi (outputPtr, outSize);
				if (str != null) 
					output = String.Copy ((string) str);
			}

			return output;
		}

		public void SetAttributeString (string attribute, OciAttributeType attrType, OciErrorHandle errorHandle) 
		{
			int status = 0;
			
			status = OciCalls.OCIAttrSetString (Handle,
				HandleType,
				attribute,
				(uint) attribute.Length,
				attrType,
				errorHandle);

			if (status != 0) {
				OciErrorInfo info = errorHandle.HandleError ();
				throw new OracleException (info.ErrorCode, info.ErrorMessage);
			}
		}

		public void SetHandle (IntPtr h)
		{
			handle = h;
		}

		#endregion // Methods

		#region Operators and Type Conversions

		public static implicit operator IntPtr (OciHandle h)
		{
			return h.Handle;
		}

		#endregion // Operators and Type Conversions
	}
}