2 // OciStatementHandle.cs
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>
15 // Copyright (C) Tim Coleman, 2003
19 using System.Collections;
20 using System.Runtime.InteropServices;
22 namespace System.Data.OracleClient.Oci {
23 internal sealed class OciStatementHandle : OciHandle, IOciHandle, IDisposable
27 OciStatementLanguage language;
28 OciStatementMode mode;
29 OciServiceHandle serviceHandle;
30 OciErrorHandle errorHandle;
40 public OciStatementHandle (OciEnvironmentHandle environment, IntPtr handle)
41 : base (OciHandleType.Statement, environment, handle)
43 language = OciStatementLanguage.NTV;
44 mode = OciStatementMode.Default;
48 #endregion // Constructors
52 public int ColumnCount {
53 get { return columnCount; }
56 public OciErrorHandle ErrorHandle {
57 get { return errorHandle; }
58 set { errorHandle = value; }
61 public OciStatementLanguage Language {
62 get { return language; }
63 set { language = value; }
66 public OciServiceHandle Service {
67 get { return serviceHandle; }
68 set { serviceHandle = value; }
71 public ArrayList Values {
72 get { return values; }
75 #endregion // Properties
80 public static extern int OCIDescriptorFree (IntPtr descp,
81 [MarshalAs (UnmanagedType.U4)] OciDescriptorType type);
84 public static extern int OCIParamGet (IntPtr hndlp,
85 [MarshalAs (UnmanagedType.U4)] OciHandleType htype,
88 [MarshalAs (UnmanagedType.U4)] int pos);
91 public static extern int OCIStmtExecute (IntPtr svchp,
94 [MarshalAs (UnmanagedType.U4)] bool iters,
98 [MarshalAs (UnmanagedType.U4)] OciExecuteMode mode);
101 public static extern int OCIStmtFetch (IntPtr stmtp,
109 public static extern int OCIStmtPrepare (IntPtr stmthp,
112 [MarshalAs (UnmanagedType.U4)] int stmt_length,
113 [MarshalAs (UnmanagedType.U4)] OciStatementLanguage language,
114 [MarshalAs (UnmanagedType.U4)] OciStatementMode mode);
116 public IntPtr CreateParameterHandle (int position)
118 IntPtr handle = IntPtr.Zero;
121 status = OCIParamGet (Handle,
122 OciHandleType.Statement,
127 OciErrorInfo info = ErrorHandle.HandleError ();
128 throw new OracleException (info.ErrorCode, info.ErrorMessage);
134 public void FreeParameterHandle (IntPtr handle)
138 status = OCIDescriptorFree (handle, OciDescriptorType.Parameter);
140 OciErrorInfo info = ErrorHandle.HandleError ();
141 throw new OracleException (info.ErrorCode, info.ErrorMessage);
147 values = new ArrayList ();
148 for (int i = 0; i < columnCount; i += 1)
149 values.Add (new OciDefineHandle (this, i + 1));
152 public void Dispose ()
154 Environment.FreeHandle (this);
157 public bool ExecuteQuery ()
159 return Execute (false);
162 public bool ExecuteNonQuery ()
164 return Execute (true);
167 public bool Execute (bool nonQuery)
173 status = OCIStmtExecute (Service.Handle,
180 OciExecuteMode.Default);
183 case OciGlue.OCI_DEFAULT:
190 case OciGlue.OCI_NO_DATA:
193 OciErrorInfo info = ErrorHandle.HandleError ();
194 throw new OracleException (info.ErrorCode, info.ErrorMessage);
200 void GetColumnCount ()
203 status = OciGlue.OCIAttrGetInt32 ( Handle,
204 (uint) OciHandleType.Statement,
207 OciAttributeType.ParameterCount,
210 OciErrorInfo info = ErrorHandle.HandleError ();
211 throw new OracleException (info.ErrorCode, info.ErrorMessage);
215 public bool GetAttributeBool (IntPtr handle, OciAttributeType type)
220 status = OciGlue.OCIAttrGetBool (handle,
221 (uint) OciDescriptorType.Parameter,
227 OciErrorInfo info = ErrorHandle.HandleError ();
228 throw new OracleException (info.ErrorCode, info.ErrorMessage);
234 public sbyte GetAttributeSByte (IntPtr handle, OciAttributeType type) {
238 status = OciGlue.OCIAttrGetSByte (handle,
239 (uint) OciDescriptorType.Parameter,
245 OciErrorInfo info = ErrorHandle.HandleError ();
246 throw new OracleException (info.ErrorCode, info.ErrorMessage);
252 public byte GetAttributeByte (IntPtr handle, OciAttributeType type)
257 status = OciGlue.OCIAttrGetByte (handle,
258 (uint) OciDescriptorType.Parameter,
264 OciErrorInfo info = ErrorHandle.HandleError ();
265 throw new OracleException (info.ErrorCode, info.ErrorMessage);
271 public ushort GetAttributeUInt16 (IntPtr handle, OciAttributeType type) {
275 status = OciGlue.OCIAttrGetUInt16 (handle,
276 (uint) OciDescriptorType.Parameter,
283 OciErrorInfo info = ErrorHandle.HandleError ();
284 throw new OracleException (info.ErrorCode, info.ErrorMessage);
290 public int GetAttributeInt32 (IntPtr handle, OciAttributeType type)
295 status = OciGlue.OCIAttrGetInt32 (handle,
296 (uint) OciDescriptorType.Parameter,
303 OciErrorInfo info = ErrorHandle.HandleError ();
304 throw new OracleException (info.ErrorCode, info.ErrorMessage);
311 public string GetAttributeString (IntPtr handle, OciAttributeType type)
313 string output = String.Empty;
314 IntPtr outputPtr = IntPtr.Zero;
318 status = OciGlue.OCIAttrGet (handle,
319 (uint) OciDescriptorType.Parameter,
325 OciErrorInfo info = ErrorHandle.HandleError ();
326 throw new OracleException (info.ErrorCode, info.ErrorMessage);
329 if (outputPtr != IntPtr.Zero && outSize > 0) {
330 object name = Marshal.PtrToStringAnsi (outputPtr, outSize);
332 output = String.Copy ((string) name);
334 * We shouldn't really free this, but how can we make Oracle
337 // Marshal.FreeHGlobal (outputPtr); -- this *may* leak memory
345 public OciColumnInfo DescribeColumn (int ordinal)
348 OciColumnInfo columnInfo;
352 IntPtr parameterHandle = CreateParameterHandle (ordinal + 1);
354 columnInfo.ColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
355 columnInfo.ColumnOrdinal = ordinal + 1;
356 columnInfo.ColumnSize = GetAttributeUInt16 (parameterHandle, OciAttributeType.DataSize);
357 columnInfo.Precision = GetAttributeByte (parameterHandle, OciAttributeType.Precision);
358 columnInfo.Scale = GetAttributeSByte (parameterHandle, OciAttributeType.Scale);
359 columnInfo.DataType = (OciDataType) GetAttributeInt32 (parameterHandle, OciAttributeType.DataType);
360 columnInfo.AllowDBNull = GetAttributeBool (parameterHandle, OciAttributeType.IsNull);
361 columnInfo.BaseColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
363 // TRWC not sure what to do with this yet.
364 schemaName = GetAttributeString (parameterHandle, OciAttributeType.SchemaName);
366 FreeParameterHandle (parameterHandle);
371 public OciStatementType GetStatementType ()
376 status = OciGlue.OCIAttrGetInt32 (Handle,
377 (uint) OciHandleType.Statement,
380 OciAttributeType.StatementType,
383 OciErrorInfo info = ErrorHandle.HandleError ();
384 throw new OracleException (info.ErrorCode, info.ErrorMessage);
387 return (OciStatementType) statementType;
393 status = OCIStmtFetch (Handle,
400 case OciGlue.OCI_NO_DATA:
403 case OciGlue.OCI_DEFAULT:
407 OciErrorInfo info = ErrorHandle.HandleError ();
408 throw new OracleException (info.ErrorCode, info.ErrorMessage);
414 public void Prepare (string commandText)
417 status = OCIStmtPrepare (Handle,
424 OciErrorInfo info = ErrorHandle.HandleError ();
425 throw new OracleException (info.ErrorCode, info.ErrorMessage);
429 #endregion // Methods