| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- //
- // 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, IDisposable
- {
- #region Fields
- int columnCount;
- bool disposed = false;
- OciErrorHandle errorHandle;
- bool moreResults;
- OciServiceHandle serviceHandle;
- ArrayList values;
-
- #endregion // Fields
- #region Constructors
- public OciStatementHandle (OciHandle parent, IntPtr handle)
- : base (OciHandleType.Statement, parent, handle)
- {
- moreResults = false;
- }
- #endregion // Constructors
- #region Properties
- public int ColumnCount {
- get { return columnCount; }
- }
- public OciErrorHandle ErrorHandle {
- get { return errorHandle; }
- set { errorHandle = value; }
- }
- public OciServiceHandle Service {
- get { return serviceHandle; }
- set { serviceHandle = value; }
- }
- public ArrayList Values {
- get { return values; }
- }
- #endregion // Properties
- #region Methods
-
- protected override void Dispose (bool disposing)
- {
- if (!disposed) {
- disposed = true;
-
- if (disposing) {
- if (values != null) {
- foreach (OciDefineHandle h in values)
- h.Dispose ();
- values = null;
- }
- }
-
- base.Dispose (disposing);
- }
- }
- public OciParameterDescriptor GetParameter (int position)
- {
- IntPtr handle = IntPtr.Zero;
- int status = 0;
- status = OciCalls.OCIParamGet (this,
- OciHandleType.Statement,
- ErrorHandle,
- out handle,
- position + 1);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- OciParameterDescriptor output = new OciParameterDescriptor (this, handle);
- output.ErrorHandle = ErrorHandle;
- return output;
- }
- public OciDefineHandle GetDefineHandle (int position)
- {
- OciDefineHandle defineHandle = new OciDefineHandle (this, IntPtr.Zero);
- defineHandle.ErrorHandle = ErrorHandle;
- defineHandle.DefineByPosition (position);
- return defineHandle;
- }
- void Define ()
- {
- values = new ArrayList ();
- for (int i = 0; i < columnCount; i += 1)
- values.Add (GetDefineHandle (i));
- }
- public bool ExecuteQuery ()
- {
- return Execute (false);
- }
- public bool ExecuteNonQuery ()
- {
- return Execute (true);
- }
- public bool Execute (bool nonQuery)
- {
- int status = 0;
- columnCount = 0;
- moreResults = false;
- if (this.disposed)
- {
- throw new InvalidOperationException ("StatementHandle is already disposed.");
- }
- status = OciCalls.OCIStmtExecute (Service,
- Handle,
- ErrorHandle,
- 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;
- case OciGlue.OCI_INVALID_HANDLE:
- throw new OracleException (0, "Invalid handle.");
- default:
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- return true;
- }
- void GetColumnCount ()
- {
- columnCount = GetAttributeInt32 (OciAttributeType.ParameterCount, ErrorHandle);
- }
- public OciStatementType GetStatementType ()
- {
- return (OciStatementType) GetAttributeInt32 (OciAttributeType.StatementType, ErrorHandle);
- }
- public bool Fetch ()
- {
- int status = 0;
- if (this.disposed)
- {
- throw new InvalidOperationException ("StatementHandle is already disposed.");
- }
- status = OciCalls.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;
- if (this.disposed) {
- throw new InvalidOperationException ("StatementHandle is already disposed.");
- }
- byte [] buffer = System.Text.Encoding.UTF8.GetBytes (commandText + "\0");
- status = OciCalls.OCIStmtPrepare (this,
- ErrorHandle,
- buffer,
- buffer.Length,
- OciStatementLanguage.NTV,
- OciStatementMode.Default);
- if (status != 0) {
- OciErrorInfo info = ErrorHandle.HandleError ();
- throw new OracleException (info.ErrorCode, info.ErrorMessage);
- }
- }
- #endregion // Methods
- }
- }
|