| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- //
- // OciStatementHandle.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 <[email protected]>
- //
- // Copyright (C) Tim Coleman, 2003
- //
- using System;
- using System.Collections;
- using System.Runtime.InteropServices;
- namespace System.Data.OracleClient.Oci {
- internal sealed class OciStatementHandle : OciHandle, IOciHandle, IDisposable
- {
- #region Fields
- OciStatementLanguage language;
- OciStatementMode mode;
- OciServiceHandle serviceHandle;
- OciErrorHandle errorHandle;
- ArrayList values;
- ArrayList parameters;
- bool moreResults;
- int columnCount;
-
- #endregion // Fields
- #region Constructors
- public OciStatementHandle (OciEnvironmentHandle environment, IntPtr handle)
- : base (OciHandleType.Statement, environment, handle)
- {
- parameters = new ArrayList ();
- language = OciStatementLanguage.NTV;
- mode = OciStatementMode.Default;
- moreResults = false;
- }
- #endregion // Constructors
- #region Properties
- public int ColumnCount {
- get { return columnCount; }
- }
- public OciErrorHandle ErrorHandle {
- get { return errorHandle; }
- set { errorHandle = value; }
- }
- public OciStatementLanguage Language {
- get { return language; }
- set { language = value; }
- }
- public OciServiceHandle Service {
- get { return serviceHandle; }
- set { serviceHandle = value; }
- }
- public ArrayList Values {
- get { return values; }
- }
- #endregion // Properties
- #region Methods
- [DllImport ("oci")]
- public static extern int OCIDescriptorFree (IntPtr descp,
- [MarshalAs (UnmanagedType.U4)] OciDescriptorType type);
- [DllImport ("oci")]
- public static extern int OCIParamGet (IntPtr hndlp,
- [MarshalAs (UnmanagedType.U4)] OciHandleType htype,
- IntPtr errhp,
- out IntPtr parmdpp,
- [MarshalAs (UnmanagedType.U4)] int pos);
- [DllImport ("oci")]
- public static extern int OCIStmtExecute (IntPtr svchp,
- IntPtr stmthp,
- IntPtr errhp,
- [MarshalAs (UnmanagedType.U4)] bool iters,
- uint rowoff,
- IntPtr snap_in,
- IntPtr snap_out,
- [MarshalAs (UnmanagedType.U4)] OciExecuteMode mode);
- [DllImport ("oci")]
- public static extern int OCIStmtFetch (IntPtr stmtp,
- IntPtr errhp,
- uint nrows,
- ushort orientation,
- uint mode);
-
- [DllImport ("oci")]
- public static extern int OCIStmtPrepare (IntPtr stmthp,
- IntPtr errhp,
- string stmt,
- [MarshalAs (UnmanagedType.U4)] int stmt_length,
- [MarshalAs (UnmanagedType.U4)] OciStatementLanguage language,
- [MarshalAs (UnmanagedType.U4)] OciStatementMode mode);
- public IntPtr CreateParameterHandle (int position)
- {
- IntPtr handle = IntPtr.Zero;
- int status = 0;
- status = OCIParamGet (Handle,
- OciHandleType.Statement,
- ErrorHandle.Handle,
- out handle,
- position);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return handle;
- }
- public void FreeBindHandle (IntPtr handle)
- {
- int status = 0;
- //status = OCIDescriptorFree (handle, OciDescriptorType.Parameter);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- }
- public void FreeParameterHandle (IntPtr handle)
- {
- int status = 0;
- status = OCIDescriptorFree (handle, OciDescriptorType.Parameter);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- }
- void Define ()
- {
- values = new ArrayList ();
- for (int i = 0; i < columnCount; i += 1)
- values.Add (new OciDefineHandle (this, i + 1));
- }
- public void Dispose ()
- {
- Environment.FreeHandle (this);
- }
- public bool ExecuteQuery ()
- {
- return Execute (false);
- }
- public bool ExecuteNonQuery ()
- {
- return Execute (true);
- }
- public bool Execute (bool nonQuery)
- {
- int status = 0;
- columnCount = 0;
- moreResults = false;
- status = OCIStmtExecute (Service.Handle,
- Handle,
- ErrorHandle.Handle,
- nonQuery,
- 0,
- IntPtr.Zero,
- IntPtr.Zero,
- OciExecuteMode.Default);
- switch (status) {
- case OciGlue.OCI_DEFAULT:
- if (!nonQuery) {
- GetColumnCount ();
- Define ();
- moreResults = true;
- }
- break;
- case OciGlue.OCI_NO_DATA:
- break;
- default:
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return true;
- }
- void GetColumnCount ()
- {
- int status = 0;
- status = OciGlue.OCIAttrGetInt32 ( Handle,
- (uint) OciHandleType.Statement,
- out columnCount,
- IntPtr.Zero,
- OciAttributeType.ParameterCount,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- }
- public bool GetAttributeBool (IntPtr handle, OciAttributeType type)
- {
- bool output;
- int status = 0;
- status = OciGlue.OCIAttrGetBool (handle,
- (uint) OciDescriptorType.Parameter,
- out output,
- IntPtr.Zero,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return output;
- }
- public sbyte GetAttributeSByte (IntPtr handle, OciAttributeType type) {
- sbyte output;
- int status = 0;
- status = OciGlue.OCIAttrGetSByte (handle,
- (uint) OciDescriptorType.Parameter,
- out output,
- IntPtr.Zero,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return output;
- }
- public byte GetAttributeByte (IntPtr handle, OciAttributeType type)
- {
- byte output;
- int status = 0;
- status = OciGlue.OCIAttrGetByte (handle,
- (uint) OciDescriptorType.Parameter,
- out output,
- IntPtr.Zero,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return output;
- }
- public ushort GetAttributeUInt16 (IntPtr handle, OciAttributeType type) {
- int status = 0;
- ushort output;
- status = OciGlue.OCIAttrGetUInt16 (handle,
- (uint) OciDescriptorType.Parameter,
- out output,
- IntPtr.Zero,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return output;
- }
- public int GetAttributeInt32 (IntPtr handle, OciAttributeType type)
- {
- int status = 0;
- int output;
- status = OciGlue.OCIAttrGetInt32 (handle,
- (uint) OciDescriptorType.Parameter,
- out output,
- IntPtr.Zero,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return output;
- }
- [MonoTODO]
- public string GetAttributeString (IntPtr handle, OciAttributeType type)
- {
- string output = String.Empty;
- IntPtr outputPtr = IntPtr.Zero;
- int outSize;
- int status = 0;
- status = OciGlue.OCIAttrGet (handle,
- (uint) OciDescriptorType.Parameter,
- out outputPtr,
- out outSize,
- type,
- ErrorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- if (outputPtr != IntPtr.Zero && outSize > 0) {
- object name = Marshal.PtrToStringAnsi (outputPtr, outSize);
- if (name != null) {
- output = String.Copy ((string) name);
- /* TRWC
- * We shouldn't really free this, but how can we make Oracle
- * do it?
- */
- // Marshal.FreeHGlobal (outputPtr); -- this *may* leak memory
- }
- }
- return output;
- }
- [MonoTODO]
- public OciColumnInfo DescribeColumn (int ordinal)
- {
- int status = 0;
- OciColumnInfo columnInfo;
- string schemaName;
- IntPtr parameterHandle = CreateParameterHandle (ordinal + 1);
- columnInfo.ColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
- columnInfo.ColumnOrdinal = ordinal + 1;
- columnInfo.ColumnSize = GetAttributeUInt16 (parameterHandle, OciAttributeType.DataSize);
- columnInfo.Precision = GetAttributeByte (parameterHandle, OciAttributeType.Precision);
- columnInfo.Scale = GetAttributeSByte (parameterHandle, OciAttributeType.Scale);
- columnInfo.DataType = (OciDataType) GetAttributeInt32 (parameterHandle, OciAttributeType.DataType);
- columnInfo.AllowDBNull = GetAttributeBool (parameterHandle, OciAttributeType.IsNull);
- columnInfo.BaseColumnName = GetAttributeString (parameterHandle, OciAttributeType.Name);
- // TRWC not sure what to do with this yet.
- schemaName = GetAttributeString (parameterHandle, OciAttributeType.SchemaName);
- FreeParameterHandle (parameterHandle);
- return columnInfo;
- }
- public OciStatementType GetStatementType ()
- {
- int status = 0;
- int statementType;
- status = OciGlue.OCIAttrGetInt32 (Handle,
- (uint) OciHandleType.Statement,
- out statementType,
- IntPtr.Zero,
- OciAttributeType.StatementType,
- errorHandle.Handle);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return (OciStatementType) statementType;
- }
- public bool Fetch ()
- {
- int status = 0;
- status = OCIStmtFetch (Handle,
- ErrorHandle.Handle,
- 1,
- 2,
- 0);
- switch (status) {
- case OciGlue.OCI_NO_DATA:
- moreResults = false;
- break;
- case OciGlue.OCI_DEFAULT:
- moreResults = true;
- break;
- default:
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return moreResults;
- }
- public void Prepare (string commandText)
- {
- int status = 0;
- status = OCIStmtPrepare (Handle,
- errorHandle.Handle,
- commandText,
- commandText.Length,
- language,
- mode);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- }
- #endregion // Methods
- }
- }
|