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 byte GetAttributeByte (IntPtr handle, OciAttributeType type)
239 status = OciGlue.OCIAttrGetByte (handle,
240 (uint) OciDescriptorType.Parameter,
246 OciErrorInfo info = ErrorHandle.HandleError ();
247 throw new OracleException (info.ErrorCode, info.ErrorMessage);
253 public int GetAttributeInt32 (IntPtr handle, OciAttributeType type)
258 status = OciGlue.OCIAttrGetInt32 (handle,
259 (uint) OciDescriptorType.Parameter,
266 OciErrorInfo info = ErrorHandle.HandleError ();
267 throw new OracleException (info.ErrorCode, info.ErrorMessage);
274 public string GetAttributeString (IntPtr handle, OciAttributeType type)
276 string output = String.Empty;
277 IntPtr outputPtr = IntPtr.Zero;
281 status = OciGlue.OCIAttrGet (handle,
282 (uint) OciDescriptorType.Parameter,
288 OciErrorInfo info = ErrorHandle.HandleError ();
289 throw new OracleException (info.ErrorCode, info.ErrorMessage);
292 if (outputPtr != IntPtr.Zero && outSize > 0) {
293 object name = Marshal.PtrToStringAnsi (outputPtr, outSize);
295 output = String.Copy ((string) name);
297 * We shouldn't really free this, but how can we make Oracle
300 // Marshal.FreeHGlobal (outputPtr); -- this *may* leak memory
308 public OciColumnInfo DescribeColumn (int ordinal)
311 OciColumnInfo columnInfo;
315 IntPtr parameterHandle = CreateParameterHandle (ordinal + 1);
317 columnInfo.ColumnName = ""; // GetAttributeString (parameterHandle, OciAttributeType.DisplayName);
318 columnInfo.ColumnOrdinal = ordinal + 1;
319 columnInfo.ColumnSize = GetAttributeInt32 (parameterHandle, OciAttributeType.DataSize);
320 columnInfo.Precision = GetAttributeByte (parameterHandle, OciAttributeType.Precision);
321 columnInfo.Scale = GetAttributeByte (parameterHandle, OciAttributeType.Scale);
322 columnInfo.DataType = (OciDataType) GetAttributeInt32 (parameterHandle, OciAttributeType.DataType);
323 columnInfo.AllowDBNull = GetAttributeBool (parameterHandle, OciAttributeType.IsNull);
324 columnInfo.BaseColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
326 // TRWC not sure what to do with this yet.
327 schemaName = GetAttributeString (parameterHandle, OciAttributeType.SchemaName);
329 FreeParameterHandle (parameterHandle);
334 public OciStatementType GetStatementType ()
339 status = OciGlue.OCIAttrGetInt32 (Handle,
340 (uint) OciHandleType.Statement,
343 OciAttributeType.StatementType,
346 OciErrorInfo info = ErrorHandle.HandleError ();
347 throw new OracleException (info.ErrorCode, info.ErrorMessage);
350 return (OciStatementType) statementType;
356 status = OCIStmtFetch (Handle,
363 case OciGlue.OCI_NO_DATA:
366 case OciGlue.OCI_DEFAULT:
370 OciErrorInfo info = ErrorHandle.HandleError ();
371 throw new OracleException (info.ErrorCode, info.ErrorMessage);
377 public void Prepare (string commandText)
380 status = OCIStmtPrepare (Handle,
387 OciErrorInfo info = ErrorHandle.HandleError ();
388 throw new OracleException (info.ErrorCode, info.ErrorMessage);
392 #endregion // Methods