Ejemplo n.º 1
0
        internal void PrepareForBind(OdbcCommand command, short ordinal, ref int parameterBufferSize) {
            // make a snapshot of the current properties. Properties may change while we work on them
            //
            CopyParameterInternal();

            object value  = ProcessAndGetParameterValue();
            int    offset = _internalOffset;
            int    size   = _internalSize;
            ODBC32.SQL_C sql_c_type;


            // offset validation based on the values type
            //
            if (offset > 0) {
                if (value is string) {
                    if (offset > ((string)value).Length) {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (value is char[]) {
                    if (offset > ((char[])value).Length) {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (value is byte[]) {
                    if (offset > ((byte[])value).Length) {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else {
                    // for all other types offset has no meaning
                    // this is important since we might upgrade some types to strings
                    offset = 0;
                }
            }

            // type support verification for certain data types
            //
            switch(_bindtype._sql_type) {
                case ODBC32.SQL_TYPE.DECIMAL:
                case ODBC32.SQL_TYPE.NUMERIC:
                    if (
                        !command.Connection.IsV3Driver                                      // for non V3 driver we always do the conversion
                        || !command.Connection.TestTypeSupport(ODBC32.SQL_TYPE.NUMERIC)     // otherwise we convert if the driver does not support numeric
                        || command.Connection.TestRestrictedSqlBindType(_bindtype._sql_type)// or the type is not supported
                    ){
                        // No support for NUMERIC
                        // Change the type
                        _bindtype = TypeMap._VarChar;
                        if ((null != value) && !Convert.IsDBNull(value)) {
                            value = ((Decimal)value).ToString(CultureInfo.CurrentCulture);
                            size = ((string)value).Length;
                            offset = 0;
                        }
                    }
                    break;
                case ODBC32.SQL_TYPE.BIGINT:
                    if (!command.Connection.IsV3Driver){
                        // No support for BIGINT
                        // Change the type
                        _bindtype = TypeMap._VarChar;
                        if ((null != value) && !Convert.IsDBNull(value)) {
                            value = ((Int64)value).ToString(CultureInfo.CurrentCulture);
                            size = ((string)value).Length;
                            offset = 0;
                        }
                    }
                    break;
                case ODBC32.SQL_TYPE.WCHAR: // MDAC 68993
                case ODBC32.SQL_TYPE.WVARCHAR:
                case ODBC32.SQL_TYPE.WLONGVARCHAR:
                    if (value is Char) {
                        value = value.ToString();
                        size = ((string)value).Length;
                        offset = 0;
                    }
                    if (!command.Connection.TestTypeSupport (_bindtype._sql_type)) {
                        // No support for WCHAR, WVARCHAR or WLONGVARCHAR
                        // Change the type
                        if (ODBC32.SQL_TYPE.WCHAR == _bindtype._sql_type) { _bindtype = TypeMap._Char; }
                        else if (ODBC32.SQL_TYPE.WVARCHAR == _bindtype._sql_type) { _bindtype = TypeMap._VarChar; }
                        else if (ODBC32.SQL_TYPE.WLONGVARCHAR == _bindtype._sql_type) {
                            _bindtype = TypeMap._Text;
                        }
                    }
                    break;
            } // end switch

            // Conversation from WCHAR to CHAR, VARCHAR or LONVARCHAR (AnsiString) is different for some providers
            // we need to chonvert WCHAR to CHAR and bind as sql_c_type = CHAR
            //
            sql_c_type = _bindtype._sql_c;

            if (!command.Connection.IsV3Driver) {
                  if (sql_c_type == ODBC32.SQL_C.WCHAR) {
                    sql_c_type = ODBC32.SQL_C.CHAR;

                    if (null != value){
                        if (!Convert.IsDBNull(value) && value is string) {
                            int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
                            CultureInfo culInfo = new CultureInfo(lcid);
                            Encoding cpe = System.Text.Encoding.GetEncoding(culInfo.TextInfo.ANSICodePage);
                            value = cpe.GetBytes(value.ToString());
                            size = ((byte[])value).Length;
                        }
                    }
                }
            };

            int cbParameterSize = GetParameterSize(value, offset, ordinal);      // count of bytes for the data, for SQLBindParameter

            // here we upgrade the datatypes if the given values size is bigger than the types columnsize
            //
            switch(_bindtype._sql_type) {
                case ODBC32.SQL_TYPE.VARBINARY: // MDAC 74372
                    // Note: per definition DbType.Binary does not support more than 8000 bytes so we change the type for binding
                    if ((cbParameterSize > 8000))
                        { _bindtype = TypeMap._Image; } // will change to LONGVARBINARY
                    break;
                case ODBC32.SQL_TYPE.VARCHAR: // MDAC 74372
                    // Note: per definition DbType.Binary does not support more than 8000 bytes so we change the type for binding
                    if ((cbParameterSize > 8000))
                        { _bindtype = TypeMap._Text; }  // will change to LONGVARCHAR
                    break;
                case ODBC32.SQL_TYPE.WVARCHAR : // MDAC 75099
                    // Note: per definition DbType.Binary does not support more than 8000 bytes so we change the type for binding
                    if ((cbParameterSize > 4000))
                        { _bindtype = TypeMap._NText; }  // will change to WLONGVARCHAR
                    break;
            }

            _prepared_Sql_C_Type = sql_c_type;
            _preparedOffset      = offset;
            _preparedSize        = size;
            _preparedValue       = value;
            _preparedBufferSize  = cbParameterSize;
            _preparedIntOffset   = parameterBufferSize;
            _preparedValueOffset = _preparedIntOffset + IntPtr.Size;
            parameterBufferSize += (cbParameterSize + IntPtr.Size);
        }
Ejemplo n.º 2
0
        internal void GetOutputValue(CNativeBuffer parameterBuffer) { //Handle any output params

            // No value is available if the user fiddles with the parameters properties
            //
            if (_hasChanged) return;

            if ((null != _bindtype) && (_internalDirection != ParameterDirection.Input)) {

               TypeMap typemap = _bindtype;
                _bindtype = null;

                int cbActual = (int)parameterBuffer.ReadIntPtr(_preparedIntOffset);
                if (ODBC32.SQL_NULL_DATA == cbActual) {
                    Value = DBNull.Value;
                }
                else if ((0 <= cbActual)  || (cbActual == ODBC32.SQL_NTS)){ // safeguard
                    Value = parameterBuffer.MarshalToManaged(_preparedValueOffset, _boundSqlCType, cbActual);

                if (_boundSqlCType== ODBC32.SQL_C.CHAR) {
                    if ((null != Value) && !Convert.IsDBNull(Value)) {
                        int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
                        CultureInfo culInfo = new CultureInfo(lcid);
                        Encoding cpe = System.Text.Encoding.GetEncoding(culInfo.TextInfo.ANSICodePage);
                        Value = cpe.GetString((Byte[])Value);
                    }
                }

                    if ((typemap != _typemap) && (null != Value) && !Convert.IsDBNull(Value) && (Value.GetType() != _typemap._type)) {
                        Debug.Assert(ODBC32.SQL_C.NUMERIC == _typemap._sql_c, "unexpected");
                        Value = Decimal.Parse((string)Value, System.Globalization.CultureInfo.CurrentCulture);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public void ResetOdbcType() {
     PropertyTypeChanging();
     _typemap = null;
     _userSpecifiedType = false;
 }
Ejemplo n.º 4
0
 internal void ClearBinding() {
     if (!_userSpecifiedType) {
         _typemap = null;
     }
     _bindtype = null;
 }
Ejemplo n.º 5
0
        internal void PrepareForBind(OdbcCommand command, short ordinal, ref int parameterBufferSize)
        {
            this.CopyParameterInternal();
            object bytes  = this.ProcessAndGetParameterValue();
            int    offset = this._internalOffset;
            int    length = this._internalSize;

            if (offset > 0)
            {
                if (bytes is string)
                {
                    if (offset > ((string)bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (bytes is char[])
                {
                    if (offset > ((char[])bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (bytes is byte[])
                {
                    if (offset > ((byte[])bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else
                {
                    offset = 0;
                }
            }
            switch (this._bindtype._sql_type)
            {
            case ODBC32.SQL_TYPE.WLONGVARCHAR:
            case ODBC32.SQL_TYPE.WVARCHAR:
            case ODBC32.SQL_TYPE.WCHAR:
                if (bytes is char)
                {
                    bytes  = bytes.ToString();
                    length = ((string)bytes).Length;
                    offset = 0;
                }
                if (!command.Connection.TestTypeSupport(this._bindtype._sql_type))
                {
                    if (ODBC32.SQL_TYPE.WCHAR == this._bindtype._sql_type)
                    {
                        this._bindtype = TypeMap._Char;
                    }
                    else if (ODBC32.SQL_TYPE.WVARCHAR == this._bindtype._sql_type)
                    {
                        this._bindtype = TypeMap._VarChar;
                    }
                    else if (ODBC32.SQL_TYPE.WLONGVARCHAR == this._bindtype._sql_type)
                    {
                        this._bindtype = TypeMap._Text;
                    }
                }
                break;

            case ODBC32.SQL_TYPE.BIGINT:
                if (!command.Connection.IsV3Driver)
                {
                    this._bindtype = TypeMap._VarChar;
                    if ((bytes != null) && !Convert.IsDBNull(bytes))
                    {
                        bytes  = ((long)bytes).ToString(CultureInfo.CurrentCulture);
                        length = ((string)bytes).Length;
                        offset = 0;
                    }
                }
                break;

            case ODBC32.SQL_TYPE.NUMERIC:
            case ODBC32.SQL_TYPE.DECIMAL:
                if ((!command.Connection.IsV3Driver || !command.Connection.TestTypeSupport(ODBC32.SQL_TYPE.NUMERIC)) || command.Connection.TestRestrictedSqlBindType(this._bindtype._sql_type))
                {
                    this._bindtype = TypeMap._VarChar;
                    if ((bytes != null) && !Convert.IsDBNull(bytes))
                    {
                        bytes  = ((decimal)bytes).ToString(CultureInfo.CurrentCulture);
                        length = ((string)bytes).Length;
                        offset = 0;
                    }
                }
                break;
            }
            ODBC32.SQL_C cHAR = this._bindtype._sql_c;
            if (!command.Connection.IsV3Driver && (cHAR == ODBC32.SQL_C.WCHAR))
            {
                cHAR = ODBC32.SQL_C.CHAR;
                if (((bytes != null) && !Convert.IsDBNull(bytes)) && (bytes is string))
                {
                    CultureInfo info = new CultureInfo(CultureInfo.CurrentCulture.LCID);
                    bytes  = Encoding.GetEncoding(info.TextInfo.ANSICodePage).GetBytes(bytes.ToString());
                    length = ((byte[])bytes).Length;
                }
            }
            int num2 = this.GetParameterSize(bytes, offset, ordinal);

            switch (this._bindtype._sql_type)
            {
            case ODBC32.SQL_TYPE.WVARCHAR:
                if (num2 > 0xfa0)
                {
                    this._bindtype = TypeMap._NText;
                }
                break;

            case ODBC32.SQL_TYPE.VARBINARY:
                if (num2 > 0x1f40)
                {
                    this._bindtype = TypeMap._Image;
                }
                break;

            case ODBC32.SQL_TYPE.VARCHAR:
                if (num2 > 0x1f40)
                {
                    this._bindtype = TypeMap._Text;
                }
                break;
            }
            this._prepared_Sql_C_Type = cHAR;
            this._preparedOffset      = offset;
            this._preparedSize        = length;
            this._preparedValue       = bytes;
            this._preparedBufferSize  = num2;
            this._preparedIntOffset   = parameterBufferSize;
            this._preparedValueOffset = this._preparedIntOffset + IntPtr.Size;
            parameterBufferSize      += num2 + IntPtr.Size;
        }
Ejemplo n.º 6
0
        internal object GetValue(int i, TypeMap typemap) {
            switch(typemap._sql_type) {

                case ODBC32.SQL_TYPE.CHAR:
                case ODBC32.SQL_TYPE.VARCHAR:
                case ODBC32.SQL_TYPE.LONGVARCHAR:
                case ODBC32.SQL_TYPE.WCHAR:
                case ODBC32.SQL_TYPE.WVARCHAR:
                case ODBC32.SQL_TYPE.WLONGVARCHAR:
                    return internalGetString(i);

                case ODBC32.SQL_TYPE.DECIMAL:
                case ODBC32.SQL_TYPE.NUMERIC:
                    return   internalGetDecimal(i);

                case ODBC32.SQL_TYPE.SMALLINT:
                    return  internalGetInt16(i);

                case ODBC32.SQL_TYPE.INTEGER:
                    return internalGetInt32(i);

                case ODBC32.SQL_TYPE.REAL:
                    return  internalGetFloat(i);

                case ODBC32.SQL_TYPE.FLOAT:
                case ODBC32.SQL_TYPE.DOUBLE:
                    return  internalGetDouble(i);

                case ODBC32.SQL_TYPE.BIT:
                    return  internalGetBoolean(i);

                case ODBC32.SQL_TYPE.TINYINT:
                    return  internalGetByte(i);

                case ODBC32.SQL_TYPE.BIGINT:
                    return  internalGetInt64(i);

                case ODBC32.SQL_TYPE.BINARY:
                case ODBC32.SQL_TYPE.VARBINARY:
                case ODBC32.SQL_TYPE.LONGVARBINARY:
                    return  internalGetBytes(i);

                case ODBC32.SQL_TYPE.TYPE_DATE:
                    return  internalGetDate(i);

                case ODBC32.SQL_TYPE.TYPE_TIME:
                    return  internalGetTime(i);

//                  case ODBC32.SQL_TYPE.TIMESTAMP:
                case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                    return  internalGetDateTime(i);

                case ODBC32.SQL_TYPE.GUID:
                    return  internalGetGuid(i);

                case ODBC32.SQL_TYPE.SS_VARIANT:
                    //Note: SQL Variant is not an ODBC defined type.
                    //Instead of just binding it as a byte[], which is not very useful,
                    //we will actually code this specific for SQL Server.

                    //To obtain the sub-type, we need to first load the context (obtaining the length
                    //will work), and then query for a speicial SQLServer specific attribute.
                    if (_isRead) {
                        if(this.dataCache.AccessIndex(i) == null) {
                            int dummy;
                            bool isNotDbNull = QueryFieldInfo(i, ODBC32.SQL_C.BINARY, out dummy);
                            // if the value is DBNull, QueryFieldInfo will cache it
                            if (isNotDbNull) {
                                //Delegate (for the sub type)
                                ODBC32.SQL_TYPE subtype = (ODBC32.SQL_TYPE)(int)GetColAttribute(i, (ODBC32.SQL_DESC)ODBC32.SQL_CA_SS.VARIANT_SQL_TYPE, (ODBC32.SQL_COLUMN)(-1), ODBC32.HANDLER.THROW);
                                return  GetValue(i, TypeMap.FromSqlType(subtype));
                            }
                        }
                        return this.dataCache[i];
                    }
                    throw ADP.DataReaderNoData();



                default:
                    //Unknown types are bound strictly as binary
                    return  internalGetBytes(i);
             }
        }
 internal void GetOutputValue(CNativeBuffer parameterBuffer)
 {
     if (!this._hasChanged && ((this._bindtype != null) && (this._internalDirection != ParameterDirection.Input)))
     {
         TypeMap map = this._bindtype;
         this._bindtype = null;
         int cb = (int) parameterBuffer.ReadIntPtr(this._preparedIntOffset);
         if (-1 == cb)
         {
             this.Value = DBNull.Value;
         }
         else if ((0 <= cb) || (cb == -3))
         {
             this.Value = parameterBuffer.MarshalToManaged(this._preparedValueOffset, this._boundSqlCType, cb);
             if (((this._boundSqlCType == ODBC32.SQL_C.CHAR) && (this.Value != null)) && !Convert.IsDBNull(this.Value))
             {
                 CultureInfo info = new CultureInfo(CultureInfo.CurrentCulture.LCID);
                 this.Value = Encoding.GetEncoding(info.TextInfo.ANSICodePage).GetString((byte[]) this.Value);
             }
             if (((map != this._typemap) && (this.Value != null)) && (!Convert.IsDBNull(this.Value) && (this.Value.GetType() != this._typemap._type)))
             {
                 this.Value = decimal.Parse((string) this.Value, CultureInfo.CurrentCulture);
             }
         }
     }
 }
                public static TypeMap GetTypeMap (SQL_TYPE sqlType)
                {
                        TypeMap map;
                        try {
                                map  = OdbcTypeMap [sqlType];
                                return map;
                        } catch (ArgumentException) {
                                
                        }

                        // If not in default translation
                        map = new TypeMap ();
                        map.SqlType = sqlType;
                        switch (sqlType) {
			case SQL_TYPE.DATE:                      
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.TYPE_DATE;
                                return map;

			case SQL_TYPE.DECIMAL:                      
                                map.OdbcType = OdbcType.Decimal;
                                map.SqlCType = SQL_C_TYPE.CHAR;
                                return map;

			case SQL_TYPE.INTERVAL_DAY:              
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_DAY;
                                return map;

			case SQL_TYPE.INTERVAL_DAY_TO_HOUR:      
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_DAY_TO_HOUR;
                                return map;

			case SQL_TYPE.INTERVAL_DAY_TO_MINUTE:    
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_DAY_TO_MINUTE;
                                return map;

			case SQL_TYPE.INTERVAL_DAY_TO_SECOND:    
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_DAY_TO_SECOND;
                                return map;

			case SQL_TYPE.INTERVAL_HOUR:             
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_HOUR;
                                return map;

			case SQL_TYPE.INTERVAL_HOUR_TO_MINUTE:   
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_HOUR_TO_MINUTE;
                                return map;

			case SQL_TYPE.INTERVAL_HOUR_TO_SECOND:   
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_HOUR_TO_SECOND;
                                return map;

			case SQL_TYPE.INTERVAL_MINUTE:           
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_MINUTE;
                                return map;

			case SQL_TYPE.INTERVAL_MINUTE_TO_SECOND: 
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_MINUTE_TO_SECOND;
                                return map;

			case SQL_TYPE.INTERVAL_MONTH:            
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_MONTH;
                                return map;

			case SQL_TYPE.INTERVAL_SECOND:           
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_SECOND;
                                return map;

			case SQL_TYPE.INTERVAL_YEAR:             
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_YEAR;
                                return map;

			case SQL_TYPE.INTERVAL_YEAR_TO_MONTH:    
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.INTERVAL_YEAR_TO_MONTH;
                                return map;

                        case SQL_TYPE.LONGVARBINARY:    
                                map.OdbcType = OdbcType.Binary;
                                map.SqlCType = SQL_C_TYPE.BINARY;
                                return map;

			case SQL_TYPE.TIME:                      
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.TIME;
                                return map;

			case SQL_TYPE.TYPE_TIMESTAMP:            
                                map.OdbcType = OdbcType.DateTime;
                                map.SqlCType = SQL_C_TYPE.TIMESTAMP;
                                return map;

			case SQL_TYPE.VARCHAR:                   
                                map.OdbcType = OdbcType.VarChar;
                                map.SqlCType = SQL_C_TYPE.CHAR;
                                return map;

                        default:                        
                                map.OdbcType = OdbcType.NVarChar;
                                map.SqlCType = SQL_C_TYPE.WCHAR;
                                return map;
                        }
                }
        internal static TypeMap UpgradeSignedType(TypeMap typeMap, bool unsigned)
        {
            if (unsigned)
            {
                switch (typeMap._dbType)
                {
                    case DbType.Int16:
                        return _Int;

                    case DbType.Int32:
                        return _BigInt;

                    case DbType.Int64:
                        return _Decimal;
                }
                return typeMap;
            }
            if (typeMap._dbType == DbType.Byte)
            {
                return _SmallInt;
            }
            return typeMap;
        }
 internal void ClearBinding()
 {
     if (!this._userSpecifiedType)
     {
         this._typemap = null;
     }
     this._bindtype = null;
 }
Ejemplo n.º 11
0
        internal void PrepareForBind(OdbcCommand command, short ordinal, ref int parameterBufferSize)
        {
            // make a snapshot of the current properties. Properties may change while we work on them
            //
            CopyParameterInternal();

            object value  = ProcessAndGetParameterValue();
            int    offset = _internalOffset;
            int    size   = _internalSize;

            ODBC32.SQL_C sql_c_type;


            // offset validation based on the values type
            //
            if (offset > 0)
            {
                if (value is string)
                {
                    if (offset > ((string)value).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (value is char[])
                {
                    if (offset > ((char[])value).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (value is byte[])
                {
                    if (offset > ((byte[])value).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else
                {
                    // for all other types offset has no meaning
                    // this is important since we might upgrade some types to strings
                    offset = 0;
                }
            }

            // type support verification for certain data types
            //
            switch (_bindtype._sql_type)
            {
            case ODBC32.SQL_TYPE.DECIMAL:
            case ODBC32.SQL_TYPE.NUMERIC:
                if (
                    !command.Connection.IsV3Driver ||                                       // for non V3 driver we always do the conversion
                    !command.Connection.TestTypeSupport(ODBC32.SQL_TYPE.NUMERIC) ||         // otherwise we convert if the driver does not support numeric
                    command.Connection.TestRestrictedSqlBindType(_bindtype._sql_type)       // or the type is not supported
                    )
                {
                    // No support for NUMERIC
                    // Change the type
                    _bindtype = TypeMap._VarChar;
                    if ((null != value) && !Convert.IsDBNull(value))
                    {
                        value  = ((Decimal)value).ToString(CultureInfo.CurrentCulture);
                        size   = ((string)value).Length;
                        offset = 0;
                    }
                }
                break;

            case ODBC32.SQL_TYPE.BIGINT:
                if (!command.Connection.IsV3Driver)
                {
                    // No support for BIGINT
                    // Change the type
                    _bindtype = TypeMap._VarChar;
                    if ((null != value) && !Convert.IsDBNull(value))
                    {
                        value  = ((Int64)value).ToString(CultureInfo.CurrentCulture);
                        size   = ((string)value).Length;
                        offset = 0;
                    }
                }
                break;

            case ODBC32.SQL_TYPE.WCHAR:     // MDAC 68993
            case ODBC32.SQL_TYPE.WVARCHAR:
            case ODBC32.SQL_TYPE.WLONGVARCHAR:
                if (value is Char)
                {
                    value  = value.ToString();
                    size   = ((string)value).Length;
                    offset = 0;
                }
                if (!command.Connection.TestTypeSupport(_bindtype._sql_type))
                {
                    // No support for WCHAR, WVARCHAR or WLONGVARCHAR
                    // Change the type
                    if (ODBC32.SQL_TYPE.WCHAR == _bindtype._sql_type)
                    {
                        _bindtype = TypeMap._Char;
                    }
                    else if (ODBC32.SQL_TYPE.WVARCHAR == _bindtype._sql_type)
                    {
                        _bindtype = TypeMap._VarChar;
                    }
                    else if (ODBC32.SQL_TYPE.WLONGVARCHAR == _bindtype._sql_type)
                    {
                        _bindtype = TypeMap._Text;
                    }
                }
                break;
            } // end switch

            // Conversation from WCHAR to CHAR, VARCHAR or LONVARCHAR (AnsiString) is different for some providers
            // we need to chonvert WCHAR to CHAR and bind as sql_c_type = CHAR
            //
            sql_c_type = _bindtype._sql_c;

            if (!command.Connection.IsV3Driver)
            {
                if (sql_c_type == ODBC32.SQL_C.WCHAR)
                {
                    sql_c_type = ODBC32.SQL_C.CHAR;

                    if (null != value)
                    {
                        if (!Convert.IsDBNull(value) && value is string)
                        {
                            int         lcid    = System.Globalization.CultureInfo.CurrentCulture.LCID;
                            CultureInfo culInfo = new CultureInfo(lcid);
                            Encoding    cpe     = System.Text.Encoding.GetEncoding(culInfo.TextInfo.ANSICodePage);
                            value = cpe.GetBytes(value.ToString());
                            size  = ((byte[])value).Length;
                        }
                    }
                }
            }
            ;

            int cbParameterSize = GetParameterSize(value, offset, ordinal); // count of bytes for the data, for SQLBindParameter

            // Upgrade input value type if the size of input value is bigger than the max size of the input value type.
            switch (_bindtype._sql_type)
            {
            case ODBC32.SQL_TYPE.VARBINARY:
                // Max length of VARBINARY is 8,000 of byte array.
                if (size > 8000)
                {
                    _bindtype = TypeMap._Image;     // will change to LONGVARBINARY
                }
                break;

            case ODBC32.SQL_TYPE.VARCHAR:
                // Max length of VARCHAR is 8,000 of non-unicode characters.
                if (size > 8000)
                {
                    _bindtype = TypeMap._Text;     // will change to LONGVARCHAR
                }
                break;

            case ODBC32.SQL_TYPE.WVARCHAR:
                // Max length of WVARCHAR (NVARCHAR) is 4,000 of unicode characters.
                if (size > 4000)
                {
                    _bindtype = TypeMap._NText;     // will change to WLONGVARCHAR
                }
                break;
            }

            _prepared_Sql_C_Type = sql_c_type;
            _preparedOffset      = offset;
            _preparedSize        = size;
            _preparedValue       = value;
            _preparedBufferSize  = cbParameterSize;
            _preparedIntOffset   = parameterBufferSize;
            _preparedValueOffset = _preparedIntOffset + IntPtr.Size;
            parameterBufferSize += (cbParameterSize + IntPtr.Size);
        }
Ejemplo n.º 12
0
 public void ResetOdbcType()
 {
     PropertyTypeChanging();
     _typemap           = null;
     _userSpecifiedType = false;
 }
        private void DataTableFromDataReaderDataTypes(DataTable dataTypesTable, OdbcDataReader dataReader, OdbcConnection connection)
        {
            DataTable schemaTable = null;

            schemaTable = dataReader.GetSchemaTable();
            if (schemaTable == null)
            {
                throw ADP.OdbcNoTypesFromProvider();
            }
            object[]   values   = new object[schemaTable.Rows.Count];
            DataColumn column19 = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn column18 = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn column17 = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn column16 = dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters];
            DataColumn column15 = dataTypesTable.Columns[DbMetaDataColumnNames.DataType];
            DataColumn column6  = dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable];
            DataColumn column14 = dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive];
            DataColumn column5  = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn column13 = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale];
            DataColumn column4  = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn column3  = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];
            DataColumn column2  = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn column   = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn column12 = dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned];
            DataColumn column11 = dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale];
            DataColumn column10 = dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale];
            DataColumn column9  = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix];
            DataColumn column8  = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix];
            DataColumn column7  = dataTypesTable.Columns[OdbcMetaDataColumnNames.SQLType];

            while (dataReader.Read())
            {
                TypeMap map;
                dataReader.GetValues(values);
                DataRow row = dataTypesTable.NewRow();
                row[column19] = values[0];
                row[column7]  = values[1];
                ODBC32.SQL_TYPE sqltype = (ODBC32.SQL_TYPE)((short)((int)Convert.ChangeType(values[1], typeof(int), null)));
                if (!connection.IsV3Driver)
                {
                    if (sqltype == ~ODBC32.SQL_TYPE.WLONGVARCHAR)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_DATE;
                    }
                    else if (sqltype == ~ODBC32.SQL_TYPE.GUID)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_TIME;
                    }
                }
                try
                {
                    map = TypeMap.FromSqlType(sqltype);
                }
                catch (ArgumentException)
                {
                    map = null;
                }
                if (map != null)
                {
                    row[column18] = map._odbcType;
                    row[column15] = map._type.FullName;
                    switch (sqltype)
                    {
                    case ODBC32.SQL_TYPE.SS_TIME_EX:
                    case ODBC32.SQL_TYPE.SS_UTCDATETIME:
                    case ODBC32.SQL_TYPE.SS_VARIANT:
                    case ODBC32.SQL_TYPE.GUID:
                    case ODBC32.SQL_TYPE.WCHAR:
                    case ODBC32.SQL_TYPE.BIT:
                    case ODBC32.SQL_TYPE.TINYINT:
                    case ODBC32.SQL_TYPE.BIGINT:
                    case ODBC32.SQL_TYPE.BINARY:
                    case ODBC32.SQL_TYPE.CHAR:
                    case ODBC32.SQL_TYPE.NUMERIC:
                    case ODBC32.SQL_TYPE.DECIMAL:
                    case ODBC32.SQL_TYPE.INTEGER:
                    case ODBC32.SQL_TYPE.SMALLINT:
                    case ODBC32.SQL_TYPE.FLOAT:
                    case ODBC32.SQL_TYPE.REAL:
                    case ODBC32.SQL_TYPE.DOUBLE:
                    case ODBC32.SQL_TYPE.TIMESTAMP:
                    case ODBC32.SQL_TYPE.TYPE_DATE:
                    case ODBC32.SQL_TYPE.TYPE_TIME:
                    case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                        goto Label_02F8;

                    case ODBC32.SQL_TYPE.SS_XML:
                    case ODBC32.SQL_TYPE.WLONGVARCHAR:
                    case ODBC32.SQL_TYPE.LONGVARBINARY:
                    case ODBC32.SQL_TYPE.LONGVARCHAR:
                        goto Label_02BC;

                    case ODBC32.SQL_TYPE.WVARCHAR:
                    case ODBC32.SQL_TYPE.VARBINARY:
                    case ODBC32.SQL_TYPE.VARCHAR:
                        goto Label_02DA;
                    }
                }
                goto Label_0314;
Label_02BC:
                row[column4] = true;
                row[column5] = false;
                goto Label_0314;
Label_02DA:
                row[column4] = false;
                row[column5] = false;
                goto Label_0314;
Label_02F8:
                row[column4] = false;
                row[column5] = true;
Label_0314:
                row[column17] = values[2];
                row[column16] = values[5];
                if ((values[11] == DBNull.Value) || (Convert.ToInt16(values[11], null) == 0))
                {
                    row[column6] = false;
                }
                else
                {
                    row[column6] = true;
                }
                row[column14] = this.BooleanFromODBC(values[7]);
                row[column13] = this.BooleanFromODBC(values[10]);
                if (values[6] != DBNull.Value)
                {
                    switch (((ODBC32.SQL_NULLABILITY)((ushort)Convert.ToInt16(values[6], null))))
                    {
                    case ODBC32.SQL_NULLABILITY.NO_NULLS:
                        row[column3] = false;
                        break;

                    case ODBC32.SQL_NULLABILITY.NULLABLE:
                        row[column3] = true;
                        break;

                    case ODBC32.SQL_NULLABILITY.UNKNOWN:
                        row[column3] = DBNull.Value;
                        break;
                    }
                }
                if (DBNull.Value != values[8])
                {
                    switch (Convert.ToInt16(values[8], null))
                    {
                    case 0:
                        row[column2] = false;
                        row[column]  = false;
                        break;

                    case 1:
                        row[column2] = false;
                        row[column]  = true;
                        break;

                    case 2:
                        row[column2] = true;
                        row[column]  = false;
                        break;

                    case 3:
                        row[column2] = true;
                        row[column]  = true;
                        break;
                    }
                }
                row[column12] = this.BooleanFromODBC(values[9]);
                if (values[14] != DBNull.Value)
                {
                    row[column11] = values[14];
                }
                if (values[13] != DBNull.Value)
                {
                    row[column10] = values[13];
                }
                if (values[3] != DBNull.Value)
                {
                    row[column9] = values[3];
                }
                if (values[4] != DBNull.Value)
                {
                    row[column8] = values[4];
                }
                dataTypesTable.Rows.Add(row);
            }
        }
Ejemplo n.º 14
0
        private object ProcessAndGetParameterValue() {
            object value = _internalValue;
            if (_internalUserSpecifiedType) {
                if ((null != value) && !Convert.IsDBNull(value)) {
                    Type valueType = value.GetType();
                    if (!valueType.IsArray) {
                        if (valueType != _typemap._type) {
                            try {
                                value = Convert.ChangeType (value, _typemap._type, (System.IFormatProvider)null);
                            }
                            catch(Exception e) {
                                // Don't know which exception to expect from ChangeType so we filter out the serious ones
                                // 
                                if (!ADP.IsCatchableExceptionType(e)) {
                                    throw;
                                }
                                throw ADP.ParameterConversionFailed(value, _typemap._type, e); // WebData 75433
                            }
                        }
                    }
                    else if (valueType == typeof(char[])) {
                        value = new String((char[])value);
                    }
                }
            }
            else if (null == _typemap) {
                if ((null == value) || Convert.IsDBNull (value)) {
                    _typemap = TypeMap._NVarChar; // default type
                }
                else {
                    Type type = value.GetType ();

                    _typemap = TypeMap.FromSystemType (type);
                }
            }
            Debug.Assert(null != _typemap, "GetParameterValue: null _typemap");
            _originalbindtype = _bindtype = _typemap;
            return value;
        }
        internal void PrepareForBind(OdbcCommand command, short ordinal, ref int parameterBufferSize)
        {
            this.CopyParameterInternal();
            object bytes = this.ProcessAndGetParameterValue();
            int offset = this._internalOffset;
            int length = this._internalSize;
            if (offset > 0)
            {
                if (bytes is string)
                {
                    if (offset > ((string) bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (bytes is char[])
                {
                    if (offset > ((char[]) bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else if (bytes is byte[])
                {
                    if (offset > ((byte[]) bytes).Length)
                    {
                        throw ADP.OffsetOutOfRangeException();
                    }
                }
                else
                {
                    offset = 0;
                }
            }
            switch (this._bindtype._sql_type)
            {
                case ODBC32.SQL_TYPE.WLONGVARCHAR:
                case ODBC32.SQL_TYPE.WVARCHAR:
                case ODBC32.SQL_TYPE.WCHAR:
                    if (bytes is char)
                    {
                        bytes = bytes.ToString();
                        length = ((string) bytes).Length;
                        offset = 0;
                    }
                    if (!command.Connection.TestTypeSupport(this._bindtype._sql_type))
                    {
                        if (ODBC32.SQL_TYPE.WCHAR == this._bindtype._sql_type)
                        {
                            this._bindtype = TypeMap._Char;
                        }
                        else if (ODBC32.SQL_TYPE.WVARCHAR == this._bindtype._sql_type)
                        {
                            this._bindtype = TypeMap._VarChar;
                        }
                        else if (ODBC32.SQL_TYPE.WLONGVARCHAR == this._bindtype._sql_type)
                        {
                            this._bindtype = TypeMap._Text;
                        }
                    }
                    break;

                case ODBC32.SQL_TYPE.BIGINT:
                    if (!command.Connection.IsV3Driver)
                    {
                        this._bindtype = TypeMap._VarChar;
                        if ((bytes != null) && !Convert.IsDBNull(bytes))
                        {
                            bytes = ((long) bytes).ToString(CultureInfo.CurrentCulture);
                            length = ((string) bytes).Length;
                            offset = 0;
                        }
                    }
                    break;

                case ODBC32.SQL_TYPE.NUMERIC:
                case ODBC32.SQL_TYPE.DECIMAL:
                    if ((!command.Connection.IsV3Driver || !command.Connection.TestTypeSupport(ODBC32.SQL_TYPE.NUMERIC)) || command.Connection.TestRestrictedSqlBindType(this._bindtype._sql_type))
                    {
                        this._bindtype = TypeMap._VarChar;
                        if ((bytes != null) && !Convert.IsDBNull(bytes))
                        {
                            bytes = ((decimal) bytes).ToString(CultureInfo.CurrentCulture);
                            length = ((string) bytes).Length;
                            offset = 0;
                        }
                    }
                    break;
            }
            ODBC32.SQL_C cHAR = this._bindtype._sql_c;
            if (!command.Connection.IsV3Driver && (cHAR == ODBC32.SQL_C.WCHAR))
            {
                cHAR = ODBC32.SQL_C.CHAR;
                if (((bytes != null) && !Convert.IsDBNull(bytes)) && (bytes is string))
                {
                    CultureInfo info = new CultureInfo(CultureInfo.CurrentCulture.LCID);
                    bytes = Encoding.GetEncoding(info.TextInfo.ANSICodePage).GetBytes(bytes.ToString());
                    length = ((byte[]) bytes).Length;
                }
            }
            int num2 = this.GetParameterSize(bytes, offset, ordinal);
            switch (this._bindtype._sql_type)
            {
                case ODBC32.SQL_TYPE.WVARCHAR:
                    if (num2 > 0xfa0)
                    {
                        this._bindtype = TypeMap._NText;
                    }
                    break;

                case ODBC32.SQL_TYPE.VARBINARY:
                    if (num2 > 0x1f40)
                    {
                        this._bindtype = TypeMap._Image;
                    }
                    break;

                case ODBC32.SQL_TYPE.VARCHAR:
                    if (num2 > 0x1f40)
                    {
                        this._bindtype = TypeMap._Text;
                    }
                    break;
            }
            this._prepared_Sql_C_Type = cHAR;
            this._preparedOffset = offset;
            this._preparedSize = length;
            this._preparedValue = bytes;
            this._preparedBufferSize = num2;
            this._preparedIntOffset = parameterBufferSize;
            this._preparedValueOffset = this._preparedIntOffset + IntPtr.Size;
            parameterBufferSize += num2 + IntPtr.Size;
        }
 public int Add (TypeMap map)
 {
         return List.Add (map);
 }
 private object ProcessAndGetParameterValue()
 {
     object obj2 = this._internalValue;
     if (this._internalUserSpecifiedType)
     {
         if ((obj2 != null) && !Convert.IsDBNull(obj2))
         {
             Type type = obj2.GetType();
             if (!type.IsArray)
             {
                 if (!(type != this._typemap._type))
                 {
                     goto Label_00D5;
                 }
                 try
                 {
                     obj2 = Convert.ChangeType(obj2, this._typemap._type, null);
                     goto Label_00D5;
                 }
                 catch (Exception exception)
                 {
                     if (!ADP.IsCatchableExceptionType(exception))
                     {
                         throw;
                     }
                     throw ADP.ParameterConversionFailed(obj2, this._typemap._type, exception);
                 }
             }
             if (type == typeof(char[]))
             {
                 obj2 = new string((char[]) obj2);
             }
         }
     }
     else if (this._typemap == null)
     {
         if ((obj2 == null) || Convert.IsDBNull(obj2))
         {
             this._typemap = TypeMap._NVarChar;
         }
         else
         {
             Type dataType = obj2.GetType();
             this._typemap = TypeMap.FromSystemType(dataType);
         }
     }
 Label_00D5:
     this._originalbindtype = this._bindtype = this._typemap;
     return obj2;
 }
Ejemplo n.º 18
0
 // Upgrade integer datatypes to missinterpretaion of the highest bit
 // (e.g. 0xff could be 255 if unsigned but is -1 if signed)
 //
 static internal TypeMap UpgradeSignedType(TypeMap typeMap, bool unsigned) {
     // upgrade unsigned types to be able to hold data that has the highest bit set
     //
     if (unsigned == true) {
         switch (typeMap._dbType) {
             case DbType.Int64:
                 return _Decimal;        // upgrade to decimal
             case DbType.Int32:
                 return _BigInt;         // upgrade to 64 bit
             case DbType.Int16:
                 return _Int;            // upgrade to 32 bit
             default:
                 return typeMap;
         } // end switch
     }
     else {
         switch (typeMap._dbType) {
             case DbType.Byte:
                 return _SmallInt;       // upgrade to 16 bit
             default:
                 return typeMap;
         } // end switch
     }
 } // end UpgradeSignedType
        internal object GetValue(int i, TypeMap typemap)
        {
            switch (typemap._sql_type)
            {
                case ODBC32.SQL_TYPE.GUID:
                    return this.internalGetGuid(i);

                case ODBC32.SQL_TYPE.WLONGVARCHAR:
                case ODBC32.SQL_TYPE.WVARCHAR:
                case ODBC32.SQL_TYPE.WCHAR:
                case ODBC32.SQL_TYPE.LONGVARCHAR:
                case ODBC32.SQL_TYPE.CHAR:
                case ODBC32.SQL_TYPE.VARCHAR:
                    return this.internalGetString(i);

                case ODBC32.SQL_TYPE.BIT:
                    return this.internalGetBoolean(i);

                case ODBC32.SQL_TYPE.TINYINT:
                    return this.internalGetByte(i);

                case ODBC32.SQL_TYPE.BIGINT:
                    return this.internalGetInt64(i);

                case ODBC32.SQL_TYPE.LONGVARBINARY:
                case ODBC32.SQL_TYPE.VARBINARY:
                case ODBC32.SQL_TYPE.BINARY:
                    return this.internalGetBytes(i);

                case ODBC32.SQL_TYPE.NUMERIC:
                case ODBC32.SQL_TYPE.DECIMAL:
                    return this.internalGetDecimal(i);

                case ODBC32.SQL_TYPE.INTEGER:
                    return this.internalGetInt32(i);

                case ODBC32.SQL_TYPE.SMALLINT:
                    return this.internalGetInt16(i);

                case ODBC32.SQL_TYPE.FLOAT:
                case ODBC32.SQL_TYPE.DOUBLE:
                    return this.internalGetDouble(i);

                case ODBC32.SQL_TYPE.REAL:
                    return this.internalGetFloat(i);

                case ODBC32.SQL_TYPE.SS_VARIANT:
                    int num;
                    if (!this._isRead)
                    {
                        throw ADP.DataReaderNoData();
                    }
                    if ((this.dataCache.AccessIndex(i) == null) && this.QueryFieldInfo(i, ODBC32.SQL_C.BINARY, out num))
                    {
                        ODBC32.SQL_TYPE sqltype = (ODBC32.SQL_TYPE) ((short) this.GetColAttribute(i, (ODBC32.SQL_DESC) 0x4c0, ~ODBC32.SQL_COLUMN.COUNT, ODBC32.HANDLER.THROW));
                        return this.GetValue(i, TypeMap.FromSqlType(sqltype));
                    }
                    return this.dataCache[i];

                case ODBC32.SQL_TYPE.TYPE_DATE:
                    return this.internalGetDate(i);

                case ODBC32.SQL_TYPE.TYPE_TIME:
                    return this.internalGetTime(i);

                case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                    return this.internalGetDateTime(i);
            }
            return this.internalGetBytes(i);
        }