internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
 {
     if (retcode != RetCode.SUCCESS)
     {
         short record = 0;
         short cchActual = 0;
         StringBuilder message = new StringBuilder(0x400);
         bool flag = true;
         while (flag)
         {
             int num3;
             string str;
             record = (short) (record + 1);
             retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             if ((RetCode.SUCCESS_WITH_INFO == retcode) && ((message.Capacity - 1) < cchActual))
             {
                 message.Capacity = cchActual + 1;
                 retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             }
             if ((retcode == RetCode.SUCCESS) || (retcode == RetCode.SUCCESS_WITH_INFO))
             {
                 errors.Add(new OdbcError(source, message.ToString(), str, num3));
             }
         }
     }
 }
        protected override bool ReleaseHandle()
        {
            IntPtr statementHandle = base.handle;

            base.handle = IntPtr.Zero;
            if (IntPtr.Zero != statementHandle)
            {
                ODBC32.SQL_HANDLE handleType = this.HandleType;
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                {
                    ODBC32.RetCode code = UnsafeNativeMethods.SQLFreeHandle(handleType, statementHandle);
                    Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", code);
                    break;
                }
                }
            }
            OdbcHandle handle = this._parentHandle;

            this._parentHandle = null;
            if (handle != null)
            {
                handle.DangerousRelease();
                handle = null;
            }
            return(true);
        }
        internal OdbcHandle(OdbcStatementHandle parentHandle, ODBC32.SQL_ATTR attribute) : base(IntPtr.Zero, true)
        {
            ODBC32.RetCode code;
            this._handleType = ODBC32.SQL_HANDLE.DESC;
            bool success = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                int num;
                parentHandle.DangerousAddRef(ref success);
                code = parentHandle.GetStatementAttribute(attribute, out this.handle, out num);
            }
            finally
            {
                if (success)
                {
                    if (IntPtr.Zero != base.handle)
                    {
                        this._parentHandle = parentHandle;
                    }
                    else
                    {
                        parentHandle.DangerousRelease();
                    }
                }
            }
            if (ADP.PtrZero == base.handle)
            {
                throw ODBC.FailedToGetDescriptorHandle(code);
            }
        }
 internal OdbcHandle(OdbcStatementHandle parentHandle, ODBC32.SQL_ATTR attribute) : base(IntPtr.Zero, true)
 {
     ODBC32.RetCode code;
     this._handleType = ODBC32.SQL_HANDLE.DESC;
     bool success = false;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         int num;
         parentHandle.DangerousAddRef(ref success);
         code = parentHandle.GetStatementAttribute(attribute, out this.handle, out num);
     }
     finally
     {
         if (success)
         {
             if (IntPtr.Zero != base.handle)
             {
                 this._parentHandle = parentHandle;
             }
             else
             {
                 parentHandle.DangerousRelease();
             }
         }
     }
     if (ADP.PtrZero == base.handle)
     {
         throw ODBC.FailedToGetDescriptorHandle(code);
     }
 }
Esempio n. 5
0
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
                break;

            case ODBC32.RetCode.SUCCESS_WITH_INFO:
                if (this.infoMessageEventHandler != null)
                {
                    OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                    errors.SetSource(this.Driver);
                    this.OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                }
                break;

            default:
            {
                OdbcException innerException = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                if (innerException != null)
                {
                    innerException.Errors.SetSource(this.Driver);
                }
                this.ConnectionIsAlive(innerException);
                return(innerException);
            }
            }
            return(null);
        }
Esempio n. 6
0
        // Helpers
        internal static OdbcErrorCollection GetDiagErrors(string source, OdbcHandle hrHandle, RetCode retcode)
        {
            OdbcErrorCollection errors = new OdbcErrorCollection();

            GetDiagErrors(errors, source, hrHandle, retcode);
            return(errors);
        }
Esempio n. 7
0
        // non-throwing HandleError
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            Debug.Assert(retcode != ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");

            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
                break;

            case ODBC32.RetCode.SUCCESS_WITH_INFO:
            {
                //Optimize to only create the event objects and obtain error info if
                //the user is really interested in retriveing the events...
                if (_infoMessageEventHandler != null)
                {
                    OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                    errors.SetSource(this.Driver);
                    OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                }
                break;
            }

            default:
                OdbcException e = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                if (e != null)
                {
                    e.Errors.SetSource(this.Driver);
                }
                ConnectionIsAlive(e);            // this will close and throw if the connection is dead
                return((Exception)e);
            }
            return(null);
        }
Esempio n. 8
0
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true) {

            _handleType   = handleType;

            bool mustRelease = false;
            ODBC32.RetCode retcode = ODBC32.RetCode.SUCCESS;

            // using ConstrainedRegions to make the native ODBC call and AddRef the parent
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                // validate handleType
                switch(handleType) {
                case ODBC32.SQL_HANDLE.ENV:
                    Debug.Assert(null == parentHandle, "did not expect a parent handle");
                    retcode = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out base.handle);
                    break;
                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    // must addref before calling native so it won't be released just after
                    Debug.Assert(null != parentHandle, "expected a parent handle"); // safehandle can't be null
                    parentHandle.DangerousAddRef(ref mustRelease);

                    retcode = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out base.handle);
                    break;
//              case ODBC32.SQL_HANDLE.DESC:
                default:
                    Debug.Assert(false, "unexpected handleType");
                    break;
                }
            }
            finally {
                if (mustRelease) {
                    switch(handleType) {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (IntPtr.Zero != base.handle) {
                            // must assign _parentHandle after a handle is actually created
                            // since ReleaseHandle will only call DangerousRelease if a handle exists
                            _parentHandle = parentHandle;
                        }
                        else {
                            // without a handle, ReleaseHandle may not be called
                            parentHandle.DangerousRelease();
                        }
                        break;
                    }
                }
            }
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", retcode);

            if((ADP.PtrZero == base.handle) || (ODBC32.RetCode.SUCCESS != retcode)) {
                // 
                throw ODBC.CantAllocateEnvironmentHandle(retcode);
            }
        }
Esempio n. 9
0
        internal void HandleError(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            Exception exception = this.HandleErrorNoThrow(hrHandle, retcode);

            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
            case ODBC32.RetCode.SUCCESS_WITH_INFO:
                return;
            }
            throw exception;
        }
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            this._handleType = handleType;
            bool success = false;

            ODBC32.RetCode sUCCESS = ODBC32.RetCode.SUCCESS;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out this.handle);
                    goto Label_0099;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    parentHandle.DangerousAddRef(ref success);
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out this.handle);
                    goto Label_0099;
                }
            }
            finally
            {
                if (success)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (!(IntPtr.Zero != base.handle))
                        {
                            goto Label_0092;
                        }
                        this._parentHandle = parentHandle;
                        break;
                    }
                }
                goto Label_0098;
Label_0092:
                parentHandle.DangerousRelease();
                Label_0098 :;
            }
            Label_0099 :
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", sUCCESS);
            if ((ADP.PtrZero == base.handle) || (sUCCESS != ODBC32.RetCode.SUCCESS))
            {
                throw ODBC.CantAllocateEnvironmentHandle(sUCCESS);
            }
        }
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            this._handleType = handleType;
            bool success = false;
            ODBC32.RetCode sUCCESS = ODBC32.RetCode.SUCCESS;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switch (handleType)
                {
                    case ODBC32.SQL_HANDLE.ENV:
                        sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out this.handle);
                        goto Label_0099;

                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        parentHandle.DangerousAddRef(ref success);
                        sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out this.handle);
                        goto Label_0099;
                }
            }
            finally
            {
                if (success)
                {
                    switch (handleType)
                    {
                        case ODBC32.SQL_HANDLE.DBC:
                        case ODBC32.SQL_HANDLE.STMT:
                            if (!(IntPtr.Zero != base.handle))
                            {
                                goto Label_0092;
                            }
                            this._parentHandle = parentHandle;
                            break;
                    }
                }
                goto Label_0098;
            Label_0092:
                parentHandle.DangerousRelease();
            Label_0098:;
            }
        Label_0099:
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", sUCCESS);
            if ((ADP.PtrZero == base.handle) || (sUCCESS != ODBC32.RetCode.SUCCESS))
            {
                throw ODBC.CantAllocateEnvironmentHandle(sUCCESS);
            }
        }
Esempio n. 12
0
        internal void HandleError(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            Exception e = HandleErrorNoThrow(hrHandle, retcode);

            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
            case ODBC32.RetCode.SUCCESS_WITH_INFO:
                Debug.Assert(null == e, "success exception");
                break;

            default:
                Debug.Assert(null != e, "failure without exception");
                throw e;
            }
        }
Esempio n. 13
0
        override protected bool ReleaseHandle()
        {
            // NOTE: The SafeHandle class guarantees this will be called exactly once and is non-interrutible.
            IntPtr handle = base.handle;

            base.handle = IntPtr.Zero;

            if (IntPtr.Zero != handle)
            {
                ODBC32.SQL_HANDLE handleType = HandleType;

                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.DBC:
                // Disconnect happens in OdbcConnectionHandle.ReleaseHandle
                case ODBC32.SQL_HANDLE.ENV:
                case ODBC32.SQL_HANDLE.STMT:
                    ODBC32.RetCode retcode = UnsafeNativeMethods.SQLFreeHandle(handleType, handle);
                    Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", retcode);
                    break;

                case ODBC32.SQL_HANDLE.DESC:
                    // nothing to free on the handle
                    break;

                // case 0: ThreadAbortException setting handle before HandleType
                default:
                    Debug.Assert(ADP.PtrZero == handle, "unknown handle type");
                    break;
                }
            }

            // If we ended up getting released, then we have to release
            // our reference on our parent.
            OdbcHandle parentHandle = _parentHandle;

            _parentHandle = null;
            if (null != parentHandle)
            {
                parentHandle.DangerousRelease();
                parentHandle = null;
            }
            return(true);
        }
Esempio n. 14
0
        internal OdbcHandle(OdbcStatementHandle parentHandle, ODBC32.SQL_ATTR attribute) : base(IntPtr.Zero, true)
        {
            Debug.Assert((ODBC32.SQL_ATTR.APP_PARAM_DESC == attribute) || (ODBC32.SQL_ATTR.APP_ROW_DESC == attribute), "invalid attribute");
            _handleType = ODBC32.SQL_HANDLE.DESC;

            int cbActual;

            ODBC32.RetCode retcode;
            bool           mustRelease = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // must addref before calling native so it won't be released just after
                parentHandle.DangerousAddRef(ref mustRelease);

                retcode = parentHandle.GetStatementAttribute(attribute, out base.handle, out cbActual);
            }
            finally
            {
                if (mustRelease)
                {
                    if (IntPtr.Zero != base.handle)
                    {
                        // must call DangerousAddRef after a handle is actually created
                        // since ReleaseHandle will only call DangerousRelease if a handle exists
                        _parentHandle = parentHandle;
                    }
                    else
                    {
                        // without a handle, ReleaseHandle may not be called
                        parentHandle.DangerousRelease();
                    }
                }
            }
            if (ADP.PtrZero == base.handle)
            {
                throw ODBC.FailedToGetDescriptorHandle(retcode);
            }
            // no info-message handle on getting a descriptor handle
        }
Esempio n. 15
0
        static internal void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode) {
            Debug.Assert(retcode!=ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");
            if (RetCode.SUCCESS != retcode) {
                Int32       NativeError;
                Int16       iRec            = 0;
                Int16       cchActual       = 0;

                StringBuilder message = new StringBuilder(1024);
                string sqlState;
                bool moreerrors = true;
                while(moreerrors) {

                    ++iRec;

                    retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    if ((RetCode.SUCCESS_WITH_INFO == retcode) && (message.Capacity-1 < cchActual)) {
                        message.Capacity = cchActual+1;
                        retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    }

                    //Note: SUCCESS_WITH_INFO from SQLGetDiagRec would be because
                    //the buffer is not large enough for the error string.
                    moreerrors = (retcode == RetCode.SUCCESS || retcode == RetCode.SUCCESS_WITH_INFO);
                    if(moreerrors) {
                        //Sets up the InnerException as well...
                        errors.Add(new OdbcError(
                            source,
                            message.ToString(),
                            sqlState,
                            NativeError
                            )
                        );
                    }
                }
            }
        }
Esempio n. 16
0
        internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
        {
            Debug.Assert(retcode != ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");
            if (RetCode.SUCCESS != retcode)
            {
                int   NativeError;
                short iRec      = 0;
                short cchActual = 0;

                StringBuilder message = new StringBuilder(1024);
                string        sqlState;
                bool          moreerrors = true;
                while (moreerrors)
                {
                    ++iRec;

                    retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    if ((RetCode.SUCCESS_WITH_INFO == retcode) && (message.Capacity - 1 < cchActual))
                    {
                        message.Capacity = cchActual + 1;
                        retcode          = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    }

                    //Note: SUCCESS_WITH_INFO from SQLGetDiagRec would be because
                    //the buffer is not large enough for the error string.
                    moreerrors = (retcode == RetCode.SUCCESS || retcode == RetCode.SUCCESS_WITH_INFO);
                    if (moreerrors)
                    {
                        //Sets up the InnerException as well...
                        errors.Add(new OdbcError(
                                       source,
                                       message.ToString(),
                                       sqlState,
                                       NativeError
                                       )
                                   );
                    }
                }
            }
        }
Esempio n. 17
0
 // Helpers
 static internal OdbcErrorCollection GetDiagErrors(string source, OdbcHandle hrHandle, RetCode retcode) {
     OdbcErrorCollection errors = new OdbcErrorCollection();
     GetDiagErrors(errors, source, hrHandle, retcode);
     return errors;
 }
 protected override bool ReleaseHandle()
 {
     IntPtr statementHandle = base.handle;
     base.handle = IntPtr.Zero;
     if (IntPtr.Zero != statementHandle)
     {
         ODBC32.SQL_HANDLE handleType = this.HandleType;
         switch (handleType)
         {
             case ODBC32.SQL_HANDLE.ENV:
             case ODBC32.SQL_HANDLE.DBC:
             case ODBC32.SQL_HANDLE.STMT:
             {
                 ODBC32.RetCode code = UnsafeNativeMethods.SQLFreeHandle(handleType, statementHandle);
                 Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", code);
                 break;
             }
         }
     }
     OdbcHandle handle = this._parentHandle;
     this._parentHandle = null;
     if (handle != null)
     {
         handle.DangerousRelease();
         handle = null;
     }
     return true;
 }
 internal static extern ODBC32.RetCode SQLGetDiagRecW(ODBC32.SQL_HANDLE HandleType, OdbcHandle Handle, short RecNumber, StringBuilder rchState, out int NativeError, StringBuilder MessageText, short BufferLength, out short TextLength);
Esempio n. 20
0
 static internal extern /*SQLRETURN*/ODBC32.RetCode SQLAllocHandle(
     /*SQLSMALLINT*/ODBC32.SQL_HANDLE HandleType,
     /*SQLHANDLE*/OdbcHandle InputHandle,
     /*SQLHANDLE* */out IntPtr OutputHandle);
 internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
 {
     if (retcode != RetCode.SUCCESS)
     {
         short         record    = 0;
         short         cchActual = 0;
         StringBuilder message   = new StringBuilder(0x400);
         bool          flag      = true;
         while (flag)
         {
             int    num3;
             string str;
             record  = (short)(record + 1);
             retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             if ((RetCode.SUCCESS_WITH_INFO == retcode) && ((message.Capacity - 1) < cchActual))
             {
                 message.Capacity = cchActual + 1;
                 retcode          = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             }
             if ((retcode == RetCode.SUCCESS) || (retcode == RetCode.SUCCESS_WITH_INFO))
             {
                 errors.Add(new OdbcError(source, message.ToString(), str, num3));
             }
         }
     }
 }
 internal void HandleError(OdbcHandle hrHandle, ODBC32.RetCode retcode)
 {
     Exception exception = this.HandleErrorNoThrow(hrHandle, retcode);
     switch (retcode)
     {
         case ODBC32.RetCode.SUCCESS:
         case ODBC32.RetCode.SUCCESS_WITH_INFO:
             return;
     }
     throw exception;
 }
Esempio n. 23
0
        internal OdbcHandle(OdbcStatementHandle parentHandle, ODBC32.SQL_ATTR attribute) : base(IntPtr.Zero, true) {
            Debug.Assert((ODBC32.SQL_ATTR.APP_PARAM_DESC == attribute) || (ODBC32.SQL_ATTR.APP_ROW_DESC == attribute), "invalid attribute");
            _handleType   = ODBC32.SQL_HANDLE.DESC;

            int cbActual;
            ODBC32.RetCode retcode;
            bool mustRelease = false;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                // must addref before calling native so it won't be released just after
                parentHandle.DangerousAddRef(ref mustRelease);

                retcode = parentHandle.GetStatementAttribute(attribute, out base.handle, out cbActual);
            }
            finally {
                if (mustRelease) {
                    if (IntPtr.Zero != base.handle) {
                        // must call DangerousAddRef after a handle is actually created
                        // since ReleaseHandle will only call DangerousRelease if a handle exists
                        _parentHandle = parentHandle;
                    }
                    else {
                        // without a handle, ReleaseHandle may not be called
                        parentHandle.DangerousRelease();
                    }
                }
            }
            if (ADP.PtrZero == base.handle) {
                throw ODBC.FailedToGetDescriptorHandle(retcode);
            }
            // no info-message handle on getting a descriptor handle
        }
Esempio n. 24
0
 protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
 {
     throw new NotImplementedException();
 }
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            switch (retcode)
            {
                case ODBC32.RetCode.SUCCESS:
                    break;

                case ODBC32.RetCode.SUCCESS_WITH_INFO:
                    if (this.infoMessageEventHandler != null)
                    {
                        OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                        errors.SetSource(this.Driver);
                        this.OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                    }
                    break;

                default:
                {
                    OdbcException innerException = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                    if (innerException != null)
                    {
                        innerException.Errors.SetSource(this.Driver);
                    }
                    this.ConnectionIsAlive(innerException);
                    return innerException;
                }
            }
            return null;
        }
 internal void HandleError(OdbcHandle hrHandle, ODBC32.RetCode retcode) {
     Exception e = HandleErrorNoThrow(hrHandle, retcode);
     switch(retcode) {
     case ODBC32.RetCode.SUCCESS:
     case ODBC32.RetCode.SUCCESS_WITH_INFO:
         Debug.Assert(null == e, "success exception");
         break;
     default:
         Debug.Assert(null != e, "failure without exception");
         throw e;
     }
 }
 internal static extern ODBC32.RetCode SQLGetDiagFieldW(ODBC32.SQL_HANDLE HandleType, OdbcHandle Handle, short RecNumber, short DiagIdentifier, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder rchState, short BufferLength, out short StringLength);
Esempio n. 28
0
 static internal extern /*SQLRETURN*/ODBC32.RetCode SQLGetDiagRecW(
     /*SQLSMALLINT*/ODBC32.SQL_HANDLE HandleType,
     /*SQLHANDLE*/OdbcHandle Handle,
     /*SQLSMALLINT*/Int16 RecNumber,
     /*SQLCHAR* */  StringBuilder rchState,
     /*SQLINTEGER* */out Int32 NativeError,
     /*SQLCHAR* */StringBuilder MessageText,
     /*SQLSMALLINT*/Int16 BufferLength,
     /*SQLSMALLINT* */out Int16 TextLength);
Esempio n. 29
0
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            _handleType = handleType;

            bool mustRelease = false;

            ODBC32.RetCode retcode = ODBC32.RetCode.SUCCESS;

            // using ConstrainedRegions to make the native ODBC call and AddRef the parent
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // validate handleType
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    Debug.Assert(null == parentHandle, "did not expect a parent handle");
                    retcode = Interop.Odbc.SQLAllocHandle(handleType, IntPtr.Zero, out base.handle);
                    break;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    // must addref before calling native so it won't be released just after
                    Debug.Assert(null != parentHandle, "expected a parent handle");     // safehandle can't be null
                    parentHandle.DangerousAddRef(ref mustRelease);

                    retcode = Interop.Odbc.SQLAllocHandle(handleType, parentHandle, out base.handle);
                    break;

                //              case ODBC32.SQL_HANDLE.DESC:
                default:
                    Debug.Fail("unexpected handleType");
                    break;
                }
            }
            finally
            {
                if (mustRelease)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (IntPtr.Zero != base.handle)
                        {
                            // must assign _parentHandle after a handle is actually created
                            // since ReleaseHandle will only call DangerousRelease if a handle exists
                            _parentHandle = parentHandle;
                        }
                        else
                        {
                            // without a handle, ReleaseHandle may not be called
                            parentHandle.DangerousRelease();
                        }
                        break;
                    }
                }
            }

            if ((ADP.PtrZero == base.handle) || (ODBC32.RetCode.SUCCESS != retcode))
            {
                //
                throw ODBC.CantAllocateEnvironmentHandle(retcode);
            }
        }
Esempio n. 30
0
        override protected bool ReleaseHandle() {
            // NOTE: The SafeHandle class guarantees this will be called exactly once and is non-interrutible.
            IntPtr handle = base.handle;
            base.handle = IntPtr.Zero;

            if (IntPtr.Zero != handle) {
                ODBC32.SQL_HANDLE handleType   = HandleType;

                switch (handleType) {
                case ODBC32.SQL_HANDLE.DBC:
                    // Disconnect happens in OdbcConnectionHandle.ReleaseHandle
                case ODBC32.SQL_HANDLE.ENV:
                case ODBC32.SQL_HANDLE.STMT:
                    ODBC32.RetCode retcode = UnsafeNativeMethods.SQLFreeHandle(handleType, handle);
                    Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", retcode);
                    break;

                case ODBC32.SQL_HANDLE.DESC:
                    // nothing to free on the handle
                    break;

                // case 0: ThreadAbortException setting handle before HandleType
                default:
                    Debug.Assert(ADP.PtrZero == handle, "unknown handle type");
                    break;
                }
            }

            // If we ended up getting released, then we have to release
            // our reference on our parent.
            OdbcHandle parentHandle = _parentHandle;
            _parentHandle = null;
            if (null != parentHandle) {
                parentHandle.DangerousRelease();
                parentHandle = null;
            }
            return true;
        }
Esempio n. 31
0
    	protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true) {
			throw new NotImplementedException ();
    	}
Esempio n. 32
0
 static internal extern /*SQLRETURN*/ODBC32.RetCode SQLGetDiagFieldW(
    /*SQLSMALLINT*/ ODBC32.SQL_HANDLE HandleType,
    /*SQLHANDLE*/   OdbcHandle Handle,
    /*SQLSMALLINT*/ Int16     RecNumber,
    /*SQLSMALLINT*/ Int16     DiagIdentifier,
    [MarshalAs(UnmanagedType.LPWStr)]
    /*SQLPOINTER*/  StringBuilder    rchState,
    /*SQLSMALLINT*/ Int16     BufferLength,
    /*SQLSMALLINT* */ out Int16 StringLength);
        // non-throwing HandleError
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode) {

            Debug.Assert(retcode!=ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");

            switch(retcode) {
            case ODBC32.RetCode.SUCCESS:
                break;
            case ODBC32.RetCode.SUCCESS_WITH_INFO: {
                //Optimize to only create the event objects and obtain error info if
                //the user is really interested in retriveing the events...
                if (infoMessageEventHandler != null) {
                    OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                    errors.SetSource(this.Driver);
                    OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                }
                break;
            }
            default:
                OdbcException e = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                if (e != null) {
                    e.Errors.SetSource(this.Driver);
                }
                ConnectionIsAlive(e);        // this will close and throw if the connection is dead
                return (Exception)e;
            }
            return null;
        }
 internal static extern ODBC32.RetCode SQLAllocHandle(ODBC32.SQL_HANDLE HandleType, OdbcHandle InputHandle, out IntPtr OutputHandle);