public static OciErrorInfo HandleError(OciHandle hand)
        {
            OciErrorInfo info;

            info.ErrorCode    = 0;
            info.ErrorMessage = String.Empty;

            ulong   errbufSize  = 4096;
            UIntPtr errbufSizep = new UIntPtr(errbufSize);
            IntPtr  errbuf      = OciCalls.AllocateClear((int)errbufSize);

            OciCalls.OCIErrorGet(hand,
                                 1,
                                 IntPtr.Zero,
                                 out info.ErrorCode,
                                 errbuf,
                                 (uint)errbufSize,
                                 OciHandleType.Error);

            byte[] bytea = new byte[errbufSize];
            Marshal.Copy(errbuf, bytea, 0, (int)errbufSize);
            errbufSize = 0;

            OciHandle h = hand.Parent;

            if (h == null)
            {
                h = hand;
            }
            if (h == null)
            {
                throw new Exception("Internal driver error: handle is null.");
            }

            // first call to OCICharSetToUnicode gets the size
            OciCalls.OCICharSetToUnicode(h, null, bytea, ref errbufSizep);
            errbufSize = errbufSizep.ToUInt64();
            StringBuilder str = new StringBuilder((int)errbufSize);

            // second call to OCICharSetToUnicode gets the string
            OciCalls.OCICharSetToUnicode(h, str, bytea, ref errbufSizep);

            string errmsg = String.Empty;

            if (errbufSize > 0)
            {
                errmsg = str.ToString();
            }
            else
            {
                errmsg = "Internal driver error. Could not retrieve error message.";
            }

            info.ErrorMessage = String.Copy(errmsg);
            Marshal.FreeHGlobal(errbuf);

            return(info);
        }
        public static OciErrorInfo HandleError(OciHandle hwnd, int status)
        {
            OciErrorInfo info;

            info.ErrorCode    = status;
            info.ErrorMessage = OciGlue.ReturnCodeToString(status);

            if (status == OciGlue.OCI_ERROR || status == OciGlue.OCI_SUCCESS_WITH_INFO)
            {
                OciHandle h = hwnd;
                if (h == null)
                {
                    throw new Exception("Internal driver error: handle is null.");
                }

                ulong   errbufSize  = 4096;
                UIntPtr errbufSizep = new UIntPtr(errbufSize);
                IntPtr  errbuf      = OciCalls.AllocateClear((int)errbufSize);

                OciCalls.OCIErrorGet(hwnd,
                                     1,
                                     IntPtr.Zero,
                                     out info.ErrorCode,
                                     errbuf,
                                     (uint)errbufSize,
                                     OciHandleType.Error);

                byte[] bytea = new byte[errbufSize];
                Marshal.Copy(errbuf, bytea, 0, (int)errbufSize);
                errbufSize = 0;

                // first call to OCICharSetToUnicode gets the size
                OciCalls.OCICharSetToUnicode(h, null, bytea, ref errbufSizep);
                errbufSize = errbufSizep.ToUInt64();
                StringBuilder str = new StringBuilder((int)errbufSize);

                // second call to OCICharSetToUnicode gets the string
                OciCalls.OCICharSetToUnicode(h, str, bytea, ref errbufSizep);

                string errmsg = String.Empty;
                if (errbufSize > 0)
                {
                    errmsg            = str.ToString();
                    info.ErrorMessage = String.Copy(errmsg);
                }
                Marshal.FreeHGlobal(errbuf);
            }
            return(info);
        }
Exemplo n.º 3
0
        internal object GetValue(IFormatProvider formatProvider, OracleConnection conn)
        {
            object tmp;

            byte [] buffer = null;

            switch (DataType)
            {
            case OciDataType.VarChar2:
            case OciDataType.String:
            case OciDataType.VarChar:
            case OciDataType.Char:
            case OciDataType.CharZ:
            case OciDataType.OciString:
            case OciDataType.RowIdDescriptor:
                buffer = new byte [Size];
                Marshal.Copy(Value, buffer, 0, Size);

                // Get length of returned string
                int rsize = 0;
                //IntPtr	env = Parent.Parent;	// Parent is statement, grandparent is environment
                IntPtr env    = conn.Environment;
                int    status = OciCalls.OCICharSetToUnicode(env, null, buffer, out rsize);
                OciErrorHandle.ThrowExceptionIfError(ErrorHandle, status);

                // Get string
                StringBuilder ret = new StringBuilder(rsize);
                status = OciCalls.OCICharSetToUnicode(env, ret, buffer, out rsize);
                OciErrorHandle.ThrowExceptionIfError(ErrorHandle, status);

                return(ret.ToString(0, rsize));

            case OciDataType.LongVarChar:
            case OciDataType.Long:
                buffer = new byte [LongVarCharMaxValue];
                Marshal.Copy(Value, buffer, 0, buffer.Length);

                int longSize = 0;
                if (BitConverter.IsLittleEndian)
                {
                    longSize = BitConverter.ToInt32(new byte[] { buffer[0], buffer[1], buffer[2], buffer[3] }, 0);
                }
                else
                {
                    longSize = BitConverter.ToInt32(new byte[] { buffer[3], buffer[2], buffer[1], buffer[0] }, 0);
                }

                ASCIIEncoding encoding = new ASCIIEncoding();
                string        e        = encoding.GetString(buffer, 4, longSize);
                return(e);

            case OciDataType.Integer:
            case OciDataType.Number:
            case OciDataType.Float:
            case OciDataType.VarNum:
            case OciDataType.UnsignedInt:
                tmp = Marshal.PtrToStringAnsi(Value, Size);
                if (tmp != null)
                {
                    return(Decimal.Parse(String.Copy((string)tmp), formatProvider));
                }
                break;

            case OciDataType.TimeStamp:
                return(dateTimeDesc.GetDateTime(conn.Environment, dateTimeDesc.ErrorHandle));

            case OciDataType.Date:
                return(UnpackDate());

            case OciDataType.Raw:
            case OciDataType.VarRaw:
                byte [] raw_buffer = new byte [Size];
                Marshal.Copy(Value, raw_buffer, 0, Size);
                return(raw_buffer);

            case OciDataType.LongRaw:
            case OciDataType.LongVarRaw:
                buffer = new byte [LongVarRawMaxValue];
                Marshal.Copy(Value, buffer, 0, buffer.Length);

                int longrawSize = 0;
                if (BitConverter.IsLittleEndian)
                {
                    longrawSize = BitConverter.ToInt32(new byte[] { buffer[0], buffer[1], buffer[2], buffer[3] }, 0);
                }
                else
                {
                    longrawSize = BitConverter.ToInt32(new byte[] { buffer[3], buffer[2], buffer[1], buffer[0] }, 0);
                }

                byte[] longraw_buffer = new byte [longrawSize];
                Array.ConstrainedCopy(buffer, 4, longraw_buffer, 0, longrawSize);
                return(longraw_buffer);

            case OciDataType.Blob:
            case OciDataType.Clob:
                return(GetOracleLob());

            case OciDataType.IntervalDayToSecond:
                return(new OracleTimeSpan(intervalDesc.GetDayToSecond(conn.Environment, intervalDesc.ErrorHandle)));

            case OciDataType.IntervalYearToMonth:
                return(new OracleMonthSpan(intervalDesc.GetYearToMonth(conn.Environment, intervalDesc.ErrorHandle)));

            default:
                throw new Exception("OciDataType not implemented: " + DataType.ToString());
            }

            return(DBNull.Value);
        }