private int GetParameterValueSize()
        {
            NativeDBType dbType   = NativeDBType;
            Type         dataType = dbType.dataType;

            object value = GetParameterValue();

            if (typeof(string) == dataType)
            {
                if (value is String)
                {
                    if (NativeDBType.STR == dbType.wType)
                    {
                        // CONSIDER: doing slow method to compute correct ansi length
                        // user work-around is to specify a size
                        return(((String)value).Length * 2); // MDAC 69865
                    }
                    return(((String)value).Length);         // MDAC 69865
                }
            }
            else if ((typeof(byte[]) == dataType) && (value is byte[]))
            {
                return(((Byte[])value).Length); // MDAC 69865
            }
            return(0);
        }
 private static object CoerceValue(object value, NativeDBType destinationType)
 {
     if (((value != null) && (DBNull.Value != value)) && (typeof(object) != destinationType.dataType))
     {
         Type type = value.GetType();
         if (!(type != destinationType.dataType))
         {
             return(value);
         }
         try
         {
             if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == type))
             {
                 return(value);
             }
             if ((6 == destinationType.dbType) && (typeof(string) == type))
             {
                 value = decimal.Parse((string)value, NumberStyles.Currency, null);
                 return(value);
             }
             value = Convert.ChangeType(value, destinationType.dataType, null);
         }
         catch (Exception exception)
         {
             if (!ADP.IsCatchableExceptionType(exception))
             {
                 throw;
             }
             throw ADP.ParameterConversionFailed(value, destinationType.dataType, exception);
         }
     }
     return(value);
 }
        private static object CoerceValue(object value, NativeDBType destinationType)
        {
            Debug.Assert(null != destinationType, "null destinationType");
            if ((null != value) && (DBNull.Value != value) && (typeof(object) != destinationType.dataType))
            {
                Type currentType = value.GetType();
                if (currentType != destinationType.dataType)
                {
                    try {
                        if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == currentType))
                        {
                        }
                        else if ((NativeDBType.CY == destinationType.dbType) && (typeof(string) == currentType))
                        {
                            value = Decimal.Parse((string)value, NumberStyles.Currency, (IFormatProvider)null); // WebData 99376
                        }
                        else
                        {
                            value = Convert.ChangeType(value, destinationType.dataType, (IFormatProvider)null);
                        }
                    }
                    catch (Exception e) {
                        //
                        if (!ADP.IsCatchableExceptionType(e))
                        {
                            throw;
                        }

                        throw ADP.ParameterConversionFailed(value, destinationType.dataType, e); // WebData 75433
                    }
                }
            }
            return(value);
        }
 public void ResetOleDbType()
 {
     if (this._metaType != null)
     {
         this.PropertyTypeChanging();
         this._metaType = null;
     }
 }
 public void ResetOleDbType()
 {
     if (null != _metaType)
     {
         PropertyTypeChanging();
         _metaType = null;
     }
 }
        internal bool IsParameterComputed()
        {
            NativeDBType metaType = _metaType;

            return((null == metaType) ||
                   (!ShouldSerializeSize() && metaType.IsVariableLength) ||
                   ((NativeDBType.DECIMAL == metaType.dbType) || (NativeDBType.NUMERIC == metaType.dbType) &&
                    (!ShouldSerializeScale() || !ShouldSerializePrecision())
                   )
                   );  // MDAC 69299
        }
        internal bool BindParameter(int i, DBBindings bindings, UnsafeNativeMethods.tagDBPARAMBINDINFO[] bindInfo)
        {
            ValidateParameter();

            int wtype = NativeDBType.wType;

            int maxLen = GetParameterByteCount();

            NativeDBType dbType = NativeDBType;

            if (dbType.islong || (IsParameterVarLength() && (ODB.LargeDataSize < maxLen)))
            {
                maxLen = IntPtr.Size;
                wtype |= NativeDBType.BYREF;
            }
            byte precision = GetParameterPrecision();
            byte scale     = GetParameterScale();

            bindInfo[i].pwszDataSourceType = NativeDBType.dbString;
            bindInfo[i].pwszName           = IntPtr.Zero;
            bindInfo[i].ulParamSize        = new IntPtr(GetParameterSize());
            bindInfo[i].dwFlags            = GetParameterFlags();
            bindInfo[i].bPrecision         = precision;
            bindInfo[i].bScale             = scale;

            // tagDBBINDING info for CreateAccessor
            bindings.CurrentIndex = i;
            bindings.Ordinal      = i + 1;
            //bindings.ValueOffset  = bindings.DataBufferSize; // set via MaxLen
            //bindings.LengthOffset = i * sizeof_int64;
            //bindings.StatusOffset = i * sizeof_int64 + sizeof_int32;
            //bindings.TypeInfoPtr  = 0;
            //bindings.ObjectPtr    = 0;
            //bindings.BindExtPtr   = 0;
            bindings.Part = NativeDBType.dbPart;
            //bindings.MemOwner     = /*DBMEMOWNER_CLIENTOWNED*/0;
            bindings.ParamIO = GetParameterDirection();
            bindings.MaxLen  = maxLen;      // also increments databuffer size
            //bindings.Flags        = 0;
            bindings.DbType    = wtype;
            bindings.Precision = precision;
            bindings.Scale     = scale;

#if DEBUG
            if (AdapterSwitches.OleDbTrace.TraceVerbose)
            {
                ODB.Trace_Binding(i, bindings, ParameterName);
            }
#endif
            return(IsParameterComputed());
        }
        private NativeDBType GetBindType(object value)
        {
            NativeDBType type = this._metaType;

            if (type != null)
            {
                return(type);
            }
            if (ADP.IsNull(value))
            {
                return(NativeDBType.Default);
            }
            return(NativeDBType.FromSystemType(value));
        }
        private NativeDBType GetBindType(object value)
        {
            NativeDBType dbtype = _metaType;

            if (null == dbtype)
            {
                if (ADP.IsNull(value))
                {
                    dbtype = OleDb.NativeDBType.Default;
                }
                else
                {
                    dbtype = NativeDBType.FromSystemType(value);
                }
            }
            return(dbtype);
        }
        private int GetParameterSize()
        {
            NativeDBType dbType = NativeDBType;

            if (-1 != dbType.fixlen)
            {
                return(dbType.fixlen);
            }
            if (dbType.islong)   // MDAC 80657
            {
                return(~0);
            }
            if (0 < Size)
            {
                return(Size);
            }
            if (ParameterDirection.Input == Direction)   // MDAC 63571, 69475
            {
                object value = GetParameterValue();
                if (typeof(string) == dbType.dataType)
                {
                    if (value is String)
                    {
                        int len = ((String)value).Length;
                        if (NativeDBType.STR == dbType.dbType)   // MDAC 63961
                        {
                            len *= 2;
                        }
                        return(len);
                    }
                }
                else if (typeof(Byte[]) == dbType.dataType)
                {
                    if (value is Byte[])
                    {
                        return(((Byte[])value).Length);
                    }
                }
            }
            return(0);
        }
        internal bool IsParameterComputed()
        {
            NativeDBType type = this._metaType;

            if ((type != null) && (this.ShouldSerializeSize() || !type.IsVariableLength))
            {
                if (14 == type.dbType)
                {
                    return(true);
                }
                if (0x83 != type.dbType)
                {
                    return(false);
                }
                if (this.ShouldSerializeScale())
                {
                    return(!this.ShouldSerializePrecision());
                }
            }
            return(true);
        }
        private int GetParameterByteCount()
        {
            NativeDBType dbType = NativeDBType;
            int          tmp    = dbType.fixlen; // fixed length

            Debug.Assert(0 != tmp, "GetParameterByteCount: 0 length in lookup table");
            if (0 >= tmp)
            {
                tmp = Size;
                if ((0 == tmp) && (0 != (ParameterDirection.Input & Direction)))
                {
                    tmp = GetParameterValueSize();
                }
                tmp = Math.Max(tmp, 0);
                if (0 < tmp)
                {
                    if (NativeDBType.STR == dbType.wType)
                    {
                        tmp = Math.Min(tmp, Int32.MaxValue - 1) + 1;
                    }
                    else if (NativeDBType.WSTR == dbType.wType)
                    {
                        tmp = Math.Min(tmp, 1073741822 /*Int32.MaxValue/2-1*/) * 2 + 2;
                    }
                }
                else if (NativeDBType.STR == dbType.wType)   // allow space for null termination character
                {
                    tmp = 1;
                }
                else if (NativeDBType.WSTR == dbType.wType)   // allow space for null termination character
                {
                    tmp = 2;
                }
            }
            Debug.Assert(0 < tmp || NativeDBType.BYTES == dbType.wType, "GetParameterByteCount");
            return(tmp);
        }
Exemple #13
0
        private static object?CoerceValue(object?value, NativeDBType destinationType)
        {
            Debug.Assert(null != destinationType, "null destinationType");
            if ((null != value) && (DBNull.Value != value) && (typeof(object) != destinationType.dataType))
            {
                Type currentType = value.GetType();
                if (currentType != destinationType.dataType)
                {
                    try
                    {
                        if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == currentType))
                        {
                        }
                        else if ((NativeDBType.CY == destinationType.dbType) && (typeof(string) == currentType))
                        {
                            value = decimal.Parse((string)value, NumberStyles.Currency, null);
                        }
                        else
                        {
                            value = Convert.ChangeType(value, destinationType.dataType !, null);
                        }
                    }
                    catch (Exception e)
                    {
                        // UNDONE - should not be catching all exceptions!!!
                        if (!ADP.IsCatchableExceptionType(e))
                        {
                            throw;
                        }

                        throw ADP.ParameterConversionFailed(value, destinationType.dataType !, e);
                    }
                }
            }
            return(value);
        }
Exemple #14
0
        private static OleDbParameter[] DeriveParametersFromStoredProcedure(OleDbConnection connection, OleDbCommand command)
        {
            OleDbParameter[] parameterArray = new OleDbParameter[0];
            if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters))
            {
                string str3;
                string str4;
                connection.GetLiteralQuotes("DeriveParameters", out str4, out str3);
                object[] sourceArray = MultipartIdentifier.ParseMultipartIdentifier(command.CommandText, str4, str3, '.', 4, true, "OLEDB_OLEDBCommandText", false);
                if (sourceArray[3] == null)
                {
                    throw ADP.NoStoredProcedureExists(command.CommandText);
                }
                object[] destinationArray = new object[4];
                Array.Copy(sourceArray, 1, destinationArray, 0, 3);
                DataTable schemaRowset = connection.GetSchemaRowset(OleDbSchemaGuid.Procedure_Parameters, destinationArray);
                if (schemaRowset != null)
                {
                    DataColumnCollection columns = schemaRowset.Columns;
                    DataColumn           column6 = null;
                    DataColumn           column5 = null;
                    DataColumn           column4 = null;
                    DataColumn           column3 = null;
                    DataColumn           column2 = null;
                    DataColumn           column  = null;
                    DataColumn           column7 = null;
                    int index = columns.IndexOf("PARAMETER_NAME");
                    if (-1 != index)
                    {
                        column6 = columns[index];
                    }
                    index = columns.IndexOf("PARAMETER_TYPE");
                    if (-1 != index)
                    {
                        column5 = columns[index];
                    }
                    index = columns.IndexOf("DATA_TYPE");
                    if (-1 != index)
                    {
                        column4 = columns[index];
                    }
                    index = columns.IndexOf("CHARACTER_MAXIMUM_LENGTH");
                    if (-1 != index)
                    {
                        column3 = columns[index];
                    }
                    index = columns.IndexOf("NUMERIC_PRECISION");
                    if (-1 != index)
                    {
                        column2 = columns[index];
                    }
                    index = columns.IndexOf("NUMERIC_SCALE");
                    if (-1 != index)
                    {
                        column = columns[index];
                    }
                    index = columns.IndexOf("TYPE_NAME");
                    if (-1 != index)
                    {
                        column7 = columns[index];
                    }
                    DataRow[] rowArray = schemaRowset.Select(null, "ORDINAL_POSITION ASC", DataViewRowState.CurrentRows);
                    parameterArray = new OleDbParameter[rowArray.Length];
                    for (index = 0; index < rowArray.Length; index++)
                    {
                        DataRow        row       = rowArray[index];
                        OleDbParameter parameter = new OleDbParameter();
                        if ((column6 != null) && !row.IsNull(column6, DataRowVersion.Default))
                        {
                            parameter.ParameterName = Convert.ToString(row[column6, DataRowVersion.Default], CultureInfo.InvariantCulture).TrimStart(new char[] { '@', ' ', ':' });
                        }
                        if ((column5 != null) && !row.IsNull(column5, DataRowVersion.Default))
                        {
                            short num3 = Convert.ToInt16(row[column5, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.Direction = ConvertToParameterDirection(num3);
                        }
                        if ((column4 != null) && !row.IsNull(column4, DataRowVersion.Default))
                        {
                            short dbType = Convert.ToInt16(row[column4, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.OleDbType = NativeDBType.FromDBType(dbType, false, false).enumOleDbType;
                        }
                        if ((column3 != null) && !row.IsNull(column3, DataRowVersion.Default))
                        {
                            parameter.Size = Convert.ToInt32(row[column3, DataRowVersion.Default], CultureInfo.InvariantCulture);
                        }
                        switch (parameter.OleDbType)
                        {
                        case OleDbType.VarChar:
                        case OleDbType.VarWChar:
                        case OleDbType.VarBinary:
                        {
                            string str;
                            object obj2 = row[column7, DataRowVersion.Default];
                            if ((obj2 is string) && ((str = ((string)obj2).ToLower(CultureInfo.InvariantCulture)) != null))
                            {
                                if (str == "binary")
                                {
                                    break;
                                }
                                if (str == "image")
                                {
                                    goto Label_03B9;
                                }
                                if (str == "char")
                                {
                                    goto Label_03C6;
                                }
                                if (str == "text")
                                {
                                    goto Label_03D3;
                                }
                                if (str == "nchar")
                                {
                                    goto Label_03E0;
                                }
                                if (str == "ntext")
                                {
                                    goto Label_03ED;
                                }
                            }
                            goto Label_03F8;
                        }

                        case OleDbType.VarNumeric:
                        case OleDbType.Decimal:
                        case OleDbType.Numeric:
                            if ((column2 != null) && !row.IsNull(column2, DataRowVersion.Default))
                            {
                                parameter.PrecisionInternal = (byte)Convert.ToInt16(row[column2], CultureInfo.InvariantCulture);
                            }
                            if ((column != null) && !row.IsNull(column, DataRowVersion.Default))
                            {
                                parameter.ScaleInternal = (byte)Convert.ToInt16(row[column], CultureInfo.InvariantCulture);
                            }
                            goto Label_03F8;

                        default:
                            goto Label_03F8;
                        }
                        parameter.OleDbType = OleDbType.Binary;
                        goto Label_03F8;
Label_03B9:
                        parameter.OleDbType = OleDbType.LongVarBinary;
                        goto Label_03F8;
Label_03C6:
                        parameter.OleDbType = OleDbType.Char;
                        goto Label_03F8;
Label_03D3:
                        parameter.OleDbType = OleDbType.LongVarChar;
                        goto Label_03F8;
Label_03E0:
                        parameter.OleDbType = OleDbType.WChar;
                        goto Label_03F8;
Label_03ED:
                        parameter.OleDbType = OleDbType.LongVarWChar;
Label_03F8:
                        parameterArray[index] = parameter;
                    }
                }
                if ((parameterArray.Length == 0) && connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures))
                {
                    object[] objArray3 = new object[4];
                    objArray3[2]     = command.CommandText;
                    destinationArray = objArray3;
                    if (connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, destinationArray).Rows.Count == 0)
                    {
                        throw ADP.NoStoredProcedureExists(command.CommandText);
                    }
                }
                return(parameterArray);
            }
            if (!connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures))
            {
                throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider);
            }
            object[] objArray2 = new object[4];
            objArray2[2] = command.CommandText;
            object[] restrictions = objArray2;
            if (connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, restrictions).Rows.Count == 0)
            {
                throw ADP.NoStoredProcedureExists(command.CommandText);
            }
            throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider);
        }
 public void ResetOleDbType()
 {
     if (this._metaType != null)
     {
         this.PropertyTypeChanging();
         this._metaType = null;
     }
 }
 private void PropertyTypeChanging()
 {
     this.PropertyChanging();
     this._coerceMetaType = null;
     this.CoercedValue = null;
 }
 private static object CoerceValue(object value, NativeDBType destinationType)
 {
     if (((value != null) && (DBNull.Value != value)) && (typeof(object) != destinationType.dataType))
     {
         Type type = value.GetType();
         if (!(type != destinationType.dataType))
         {
             return value;
         }
         try
         {
             if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == type))
             {
                 return value;
             }
             if ((6 == destinationType.dbType) && (typeof(string) == type))
             {
                 value = decimal.Parse((string) value, NumberStyles.Currency, null);
                 return value;
             }
             value = Convert.ChangeType(value, destinationType.dataType, null);
         }
         catch (Exception exception)
         {
             if (!ADP.IsCatchableExceptionType(exception))
             {
                 throw;
             }
             throw ADP.ParameterConversionFailed(value, destinationType.dataType, exception);
         }
     }
     return value;
 }
 internal bool BindParameter(int index, Bindings bindings)
 {
     int size;
     int ptrSize;
     byte precisionInternal;
     byte scaleInternal;
     object obj2 = this.Value;
     NativeDBType bindType = this.GetBindType(obj2);
     if (bindType.enumOleDbType == System.Data.OleDb.OleDbType.Empty)
     {
         throw ODB.UninitializedParameters(index, bindType.enumOleDbType);
     }
     this._coerceMetaType = bindType;
     obj2 = CoerceValue(obj2, bindType);
     this.CoercedValue = obj2;
     ParameterDirection direction = this.Direction;
     if (this.ShouldSerializePrecision())
     {
         precisionInternal = this.PrecisionInternal;
     }
     else
     {
         precisionInternal = this.ValuePrecision(obj2);
     }
     if (precisionInternal == 0)
     {
         precisionInternal = bindType.maxpre;
     }
     if (this.ShouldSerializeScale())
     {
         scaleInternal = this.ScaleInternal;
     }
     else
     {
         scaleInternal = this.ValueScale(obj2);
     }
     int wType = bindType.wType;
     if (bindType.islong)
     {
         ptrSize = ADP.PtrSize;
         if (this.ShouldSerializeSize())
         {
             size = this.Size;
         }
         else if (0x81 == bindType.dbType)
         {
             size = 0x7fffffff;
         }
         else if (130 == bindType.dbType)
         {
             size = 0x3fffffff;
         }
         else
         {
             size = 0x7fffffff;
         }
         wType |= 0x4000;
     }
     else if (bindType.IsVariableLength)
     {
         bool flag;
         if (!this.ShouldSerializeSize() && ADP.IsDirection(this, ParameterDirection.Output))
         {
             throw ADP.UninitializedParameterSize(index, this._coerceMetaType.dataType);
         }
         if (this.ShouldSerializeSize())
         {
             size = this.Size;
             flag = false;
         }
         else
         {
             size = this.ValueSize(obj2);
             flag = true;
         }
         if (0 >= size)
         {
             if (size != 0)
             {
                 if (-1 != size)
                 {
                     throw ADP.InvalidSizeValue(size);
                 }
                 ptrSize = ADP.PtrSize;
                 wType |= 0x4000;
             }
             else if (130 == wType)
             {
                 ptrSize = 2;
             }
             else
             {
                 ptrSize = 0;
             }
         }
         else
         {
             if (130 == bindType.wType)
             {
                 ptrSize = (Math.Min(size, 0x3ffffffe) * 2) + 2;
             }
             else
             {
                 ptrSize = size;
             }
             if (flag && (0x81 == bindType.dbType))
             {
                 size = Math.Min(size, 0x3ffffffe) * 2;
             }
             if (0x2000 < ptrSize)
             {
                 ptrSize = ADP.PtrSize;
                 wType |= 0x4000;
             }
         }
     }
     else
     {
         ptrSize = bindType.fixlen;
         size = ptrSize;
     }
     bindings.CurrentIndex = index;
     bindings.DataSourceType = bindType.dbString.DangerousGetHandle();
     bindings.Name = ADP.PtrZero;
     bindings.ParamSize = new IntPtr(size);
     bindings.Flags = GetBindFlags(direction);
     bindings.Ordinal = (IntPtr) (index + 1);
     bindings.Part = bindType.dbPart;
     bindings.ParamIO = GetBindDirection(direction);
     bindings.Precision = precisionInternal;
     bindings.Scale = scaleInternal;
     bindings.DbType = wType;
     bindings.MaxLen = ptrSize;
     if (Bid.AdvancedOn)
     {
         Bid.Trace("<oledb.struct.tagDBPARAMBINDINFO|INFO|ADV> index=%d, parameterName='%ls'\n", index, this.ParameterName);
         Bid.Trace("<oledb.struct.tagDBBINDING|INFO|ADV>\n");
     }
     return this.IsParameterComputed();
 }
        private static object CoerceValue(object value, NativeDBType destinationType) {
            Debug.Assert(null != destinationType, "null destinationType");
            if ((null != value) && (DBNull.Value != value) && (typeof(object) != destinationType.dataType)) {
                Type currentType = value.GetType();
                if (currentType != destinationType.dataType) {
                    try {
                        if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == currentType)) {
                        }
                        else if ((NativeDBType.CY == destinationType.dbType) && (typeof(string) == currentType)) {
                            value = Decimal.Parse((string)value, NumberStyles.Currency, (IFormatProvider)null); // WebData 99376
                        }
                        else {
                            value = Convert.ChangeType(value, destinationType.dataType, (IFormatProvider)null);
                        }
                    }
                    catch (Exception e) {
                        // 
                        if (!ADP.IsCatchableExceptionType(e)) {
                            throw;
                        }

                        throw ADP.ParameterConversionFailed(value, destinationType.dataType, e); // WebData 75433
                    }
                }
            }
            return value;
        }
        // goal: call virtual property getters only once per parameter
        internal bool BindParameter(int index, Bindings bindings) {
            int changeID = _changeID;
            object value = Value;

            NativeDBType dbtype = GetBindType(value);
            if (OleDbType.Empty == dbtype.enumOleDbType) {
                throw ODB.UninitializedParameters(index, dbtype.enumOleDbType);
            }
            _coerceMetaType = dbtype;
            value = CoerceValue(value, dbtype);
            CoercedValue = value;

            ParameterDirection direction = Direction;

            byte precision;
            if (ShouldSerializePrecision()) {
                precision = PrecisionInternal;
            }
            else {
                precision = ValuePrecision(value);
            }
            if (0 == precision) {
                precision = dbtype.maxpre;
            }

            byte scale;
            if (ShouldSerializeScale()) {
                scale = ScaleInternal;
            }
            else {
                scale = ValueScale(value);
            }

            int wtype = dbtype.wType;
            int bytecount, size;

            if (dbtype.islong) { // long data (image, text, ntext)
                bytecount = ADP.PtrSize;
                if (ShouldSerializeSize()) {
                    size = Size;
                }
                else {
                    if (NativeDBType.STR == dbtype.dbType) {
                        size = Int32.MaxValue; // WebData 98940
                    }
                    else if (NativeDBType.WSTR == dbtype.dbType) {
                        size = Int32.MaxValue/2;
                    }
                    else {
                        size = Int32.MaxValue;
                    }
                }
                wtype |= NativeDBType.BYREF;
            }
            else if (dbtype.IsVariableLength) { // variable length data (varbinary, varchar, nvarchar)
                if (!ShouldSerializeSize() && ADP.IsDirection(this, ParameterDirection.Output)) {
                    throw ADP.UninitializedParameterSize(index, _coerceMetaType.dataType);
                }

                bool computedSize;
                if (ShouldSerializeSize()) {
                    size = Size;
                    computedSize = false;
                }
                else {
                    size = ValueSize(value);
                    computedSize = true;
                }
                if (0 < size) {
                    if (NativeDBType.WSTR == dbtype.wType) {
                        // maximum 0x3FFFFFFE characters, computed this way to avoid overflow exception
                        bytecount = Math.Min(size, 0x3FFFFFFE) * 2 + 2;
                    }
                    else {
                        Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
                        bytecount = size;
                    }

                    if (computedSize) {
                        if (NativeDBType.STR == dbtype.dbType) { // WebData 98140
                            // maximum 0x7ffffffe characters, computed this way to avoid overflow exception
                            size = Math.Min(size, 0x3FFFFFFE) * 2;
                        }
                    }

                    if (ODB.LargeDataSize < bytecount) {
                        bytecount = ADP.PtrSize;
                        wtype |= NativeDBType.BYREF;
                    }
                }
                else if (0 == size) {
                    if (NativeDBType.WSTR == wtype) { // allow space for null termination character
                        bytecount = 2;
                        // 0 == size, okay for (STR == dbType)
                    }
                    else {
                        Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
                        bytecount = 0;
                    }
                }
                else if (-1 == size) {
                    bytecount = ADP.PtrSize;
                    wtype |= NativeDBType.BYREF;
                }
                else {
                    throw ADP.InvalidSizeValue(size);
                }
            }
            else { // fixed length data
                bytecount = dbtype.fixlen;
                size = bytecount;
            }
            bindings.CurrentIndex = index;

            // tagDBPARAMBINDINFO info for SetParameterInfo
            bindings.DataSourceType = dbtype.dbString.DangerousGetHandle(); // NOTE: This is a constant and isn't exposed publicly, so there really isn't a potential for Handle Recycling.
            bindings.Name = ADP.PtrZero;
            bindings.ParamSize = new IntPtr(size);
            bindings.Flags = GetBindFlags(direction);
          //bindings.Precision    = precision;
          //bindings.Scale        = scale;

            // tagDBBINDING info for CreateAccessor
            bindings.Ordinal      = (IntPtr)(index+1);
            bindings.Part         = dbtype.dbPart;
            bindings.ParamIO      = GetBindDirection(direction);
            bindings.Precision    = precision;
            bindings.Scale        = scale;
            bindings.DbType       = wtype;
            bindings.MaxLen       = bytecount; // also increments databuffer size (uses DbType)
          //bindings.ValueOffset  = bindings.DataBufferSize; // set via MaxLen
          //bindings.LengthOffset = i * sizeof_int64;
          //bindings.StatusOffset = i * sizeof_int64 + sizeof_int32;
          //bindings.TypeInfoPtr  = 0;
          //bindings.ObjectPtr    = 0;
          //bindings.BindExtPtr   = 0;
          //bindings.MemOwner     = /*DBMEMOWNER_CLIENTOWNED*/0;
          //bindings.Flags        = 0;

          //bindings.ParameterChangeID = changeID; // bind until something changes
            Debug.Assert(_changeID == changeID, "parameter has unexpectedly changed");

            if (Bid.AdvancedOn) {
                Bid.Trace("<oledb.struct.tagDBPARAMBINDINFO|INFO|ADV> index=%d, parameterName='%ls'\n", index, ParameterName);//, bindings.BindInfo[index]);
                Bid.Trace("<oledb.struct.tagDBBINDING|INFO|ADV>\n");//, bindings.DBBinding[index]);
            }
            return IsParameterComputed();
        }
 private void PropertyTypeChanging() {
     PropertyChanging();
     _coerceMetaType = null;
     CoercedValue = null;
 }
 private void PropertyTypeChanging()
 {
     this.PropertyChanging();
     this._coerceMetaType = null;
     this.CoercedValue    = null;
 }
        private DataTable GetDataTypesTable(OleDbConnection connection)
        {
            if (base.CollectionDataSet.Tables[DbMetaDataCollectionNames.DataTypes] == null)
            {
                throw ADP.UnableToBuildCollection(DbMetaDataCollectionNames.DataTypes);
            }
            DataTable table            = base.CloneAndFilterCollection(DbMetaDataCollectionNames.DataTypes, null);
            DataTable oleDbSchemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Provider_Types, null);

            DataColumn[] columnArray4 = new DataColumn[] { table.Columns[DbMetaDataColumnNames.TypeName], table.Columns[DbMetaDataColumnNames.ColumnSize], table.Columns[DbMetaDataColumnNames.CreateParameters], table.Columns[DbMetaDataColumnNames.IsAutoIncrementable], table.Columns[DbMetaDataColumnNames.IsCaseSensitive], table.Columns[DbMetaDataColumnNames.IsFixedLength], table.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale], table.Columns[DbMetaDataColumnNames.IsLong], table.Columns[DbMetaDataColumnNames.IsNullable], table.Columns[DbMetaDataColumnNames.IsUnsigned], table.Columns[DbMetaDataColumnNames.MaximumScale], table.Columns[DbMetaDataColumnNames.MinimumScale], table.Columns[DbMetaDataColumnNames.LiteralPrefix], table.Columns[DbMetaDataColumnNames.LiteralSuffix], table.Columns[OleDbMetaDataColumnNames.NativeDataType] };
            DataColumn[] columnArray3 = new DataColumn[] { oleDbSchemaTable.Columns["TYPE_NAME"], oleDbSchemaTable.Columns["COLUMN_SIZE"], oleDbSchemaTable.Columns["CREATE_PARAMS"], oleDbSchemaTable.Columns["AUTO_UNIQUE_VALUE"], oleDbSchemaTable.Columns["CASE_SENSITIVE"], oleDbSchemaTable.Columns["IS_FIXEDLENGTH"], oleDbSchemaTable.Columns["FIXED_PREC_SCALE"], oleDbSchemaTable.Columns["IS_LONG"], oleDbSchemaTable.Columns["IS_NULLABLE"], oleDbSchemaTable.Columns["UNSIGNED_ATTRIBUTE"], oleDbSchemaTable.Columns["MAXIMUM_SCALE"], oleDbSchemaTable.Columns["MINIMUM_SCALE"], oleDbSchemaTable.Columns["LITERAL_PREFIX"], oleDbSchemaTable.Columns["LITERAL_SUFFIX"], oleDbSchemaTable.Columns["DATA_TYPE"] };
            DataColumn   column2      = table.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn   column       = table.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn   column8      = table.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn   column7      = table.Columns[DbMetaDataColumnNames.DataType];
            DataColumn   column6      = table.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn   column5      = table.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn   column4      = oleDbSchemaTable.Columns["DATA_TYPE"];
            DataColumn   column3      = oleDbSchemaTable.Columns["SEARCHABLE"];

            foreach (DataRow row2 in oleDbSchemaTable.Rows)
            {
                DataRow row = table.NewRow();
                for (int i = 0; i < columnArray3.Length; i++)
                {
                    if ((columnArray3[i] != null) && (columnArray4[i] != null))
                    {
                        row[columnArray4[i]] = row2[columnArray3[i]];
                    }
                }
                short        dbType = (short)Convert.ChangeType(row2[column4], typeof(short), CultureInfo.InvariantCulture);
                NativeDBType type   = NativeDBType.FromDBType(dbType, (bool)row[column6], (bool)row[column5]);
                row[column7] = type.dataType.FullName;
                row[column8] = type.enumOleDbType;
                if (((column2 != null) && (column != null)) && (column3 != null))
                {
                    row[column2] = DBNull.Value;
                    row[column]  = DBNull.Value;
                    if (DBNull.Value != row2[column3])
                    {
                        long num2 = (long)row2[column3];
                        if ((num2 <= 4L) && (num2 >= 1L))
                        {
                            switch (((int)(num2 - 1L)))
                            {
                            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;
                            }
                        }
                    }
                }
                table.Rows.Add(row);
            }
            table.AcceptChanges();
            return(table);
        }
 private void PropertyTypeChanging()
 {
     PropertyChanging();
     _coerceMetaType = null;
     CoercedValue    = null;
 }
        // goal: call virtual property getters only once per parameter
        internal bool BindParameter(int index, Bindings bindings)
        {
            int    changeID = _changeID;
            object value    = Value;

            NativeDBType dbtype = GetBindType(value);

            if (OleDbType.Empty == dbtype.enumOleDbType)
            {
                throw ODB.UninitializedParameters(index, dbtype.enumOleDbType);
            }
            _coerceMetaType = dbtype;
            value           = CoerceValue(value, dbtype);
            CoercedValue    = value;

            ParameterDirection direction = Direction;

            byte precision;

            if (ShouldSerializePrecision())
            {
                precision = PrecisionInternal;
            }
            else
            {
                precision = ValuePrecision(value);
            }
            if (0 == precision)
            {
                precision = dbtype.maxpre;
            }

            byte scale;

            if (ShouldSerializeScale())
            {
                scale = ScaleInternal;
            }
            else
            {
                scale = ValueScale(value);
            }

            int wtype = dbtype.wType;
            int bytecount, size;

            if (dbtype.islong)   // long data (image, text, ntext)
            {
                bytecount = ADP.PtrSize;
                if (ShouldSerializeSize())
                {
                    size = Size;
                }
                else
                {
                    if (NativeDBType.STR == dbtype.dbType)
                    {
                        size = Int32.MaxValue; // WebData 98940
                    }
                    else if (NativeDBType.WSTR == dbtype.dbType)
                    {
                        size = Int32.MaxValue / 2;
                    }
                    else
                    {
                        size = Int32.MaxValue;
                    }
                }
                wtype |= NativeDBType.BYREF;
            }
            else if (dbtype.IsVariableLength)   // variable length data (varbinary, varchar, nvarchar)
            {
                if (!ShouldSerializeSize() && ADP.IsDirection(this, ParameterDirection.Output))
                {
                    throw ADP.UninitializedParameterSize(index, _coerceMetaType.dataType);
                }

                bool computedSize;
                if (ShouldSerializeSize())
                {
                    size         = Size;
                    computedSize = false;
                }
                else
                {
                    size         = ValueSize(value);
                    computedSize = true;
                }
                if (0 < size)
                {
                    if (NativeDBType.WSTR == dbtype.wType)
                    {
                        // maximum 0x3FFFFFFE characters, computed this way to avoid overflow exception
                        bytecount = Math.Min(size, 0x3FFFFFFE) * 2 + 2;
                    }
                    else
                    {
                        Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
                        bytecount = size;
                    }

                    if (computedSize)
                    {
                        if (NativeDBType.STR == dbtype.dbType)   // WebData 98140
                        // maximum 0x7ffffffe characters, computed this way to avoid overflow exception
                        {
                            size = Math.Min(size, 0x3FFFFFFE) * 2;
                        }
                    }

                    if (ODB.LargeDataSize < bytecount)
                    {
                        bytecount = ADP.PtrSize;
                        wtype    |= NativeDBType.BYREF;
                    }
                }
                else if (0 == size)
                {
                    if (NativeDBType.WSTR == wtype)   // allow space for null termination character
                    {
                        bytecount = 2;
                        // 0 == size, okay for (STR == dbType)
                    }
                    else
                    {
                        Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
                        bytecount = 0;
                    }
                }
                else if (-1 == size)
                {
                    bytecount = ADP.PtrSize;
                    wtype    |= NativeDBType.BYREF;
                }
                else
                {
                    throw ADP.InvalidSizeValue(size);
                }
            }
            else   // fixed length data
            {
                bytecount = dbtype.fixlen;
                size      = bytecount;
            }
            bindings.CurrentIndex = index;

            // tagDBPARAMBINDINFO info for SetParameterInfo
            bindings.DataSourceType = dbtype.dbString.DangerousGetHandle(); // NOTE: This is a constant and isn't exposed publicly, so there really isn't a potential for Handle Recycling.
            bindings.Name           = ADP.PtrZero;
            bindings.ParamSize      = new IntPtr(size);
            bindings.Flags          = GetBindFlags(direction);
            //bindings.Precision    = precision;
            //bindings.Scale        = scale;

            // tagDBBINDING info for CreateAccessor
            bindings.Ordinal   = (IntPtr)(index + 1);
            bindings.Part      = dbtype.dbPart;
            bindings.ParamIO   = GetBindDirection(direction);
            bindings.Precision = precision;
            bindings.Scale     = scale;
            bindings.DbType    = wtype;
            bindings.MaxLen    = bytecount;    // also increments databuffer size (uses DbType)
            //bindings.ValueOffset  = bindings.DataBufferSize; // set via MaxLen
            //bindings.LengthOffset = i * sizeof_int64;
            //bindings.StatusOffset = i * sizeof_int64 + sizeof_int32;
            //bindings.TypeInfoPtr  = 0;
            //bindings.ObjectPtr    = 0;
            //bindings.BindExtPtr   = 0;
            //bindings.MemOwner     = /*DBMEMOWNER_CLIENTOWNED*/0;
            //bindings.Flags        = 0;

            //bindings.ParameterChangeID = changeID; // bind until something changes
            Debug.Assert(_changeID == changeID, "parameter has unexpectedly changed");

            if (Bid.AdvancedOn)
            {
                Bid.Trace("<oledb.struct.tagDBPARAMBINDINFO|INFO|ADV> index=%d, parameterName='%ls'\n", index, ParameterName); //, bindings.BindInfo[index]);
                Bid.Trace("<oledb.struct.tagDBBINDING|INFO|ADV>\n");                                                           //, bindings.DBBinding[index]);
            }
            return(IsParameterComputed());
        }
Exemple #26
0
        private DataTable GetDataTypesTable(OleDbConnection connection)
        {
            // verify the existance of the table in the data set
            DataTable dataTypesTable = CollectionDataSet.Tables[DbMetaDataCollectionNames.DataTypes];

            if (dataTypesTable == null)
            {
                throw ADP.UnableToBuildCollection(DbMetaDataCollectionNames.DataTypes);
            }

            // copy the table filtering out any rows that don't apply to tho current version of the prrovider
            dataTypesTable = CloneAndFilterCollection(DbMetaDataCollectionNames.DataTypes, null);

            DataTable providerTypesTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Provider_Types, null);

            DataColumn[] targetColumns = new DataColumn[] {
                dataTypesTable.Columns[DbMetaDataColumnNames.TypeName],
                dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize],
                dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsLong],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable],
                dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned],
                dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale],
                dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale],
                dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix],
                dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix],
                dataTypesTable.Columns[OleDbMetaDataColumnNames.NativeDataType]
            };

            DataColumn[] sourceColumns = new DataColumn[] {
                providerTypesTable.Columns["TYPE_NAME"],
                providerTypesTable.Columns["COLUMN_SIZE"],
                providerTypesTable.Columns["CREATE_PARAMS"],
                providerTypesTable.Columns["AUTO_UNIQUE_VALUE"],
                providerTypesTable.Columns["CASE_SENSITIVE"],
                providerTypesTable.Columns["IS_FIXEDLENGTH"],
                providerTypesTable.Columns["FIXED_PREC_SCALE"],
                providerTypesTable.Columns["IS_LONG"],
                providerTypesTable.Columns["IS_NULLABLE"],
                providerTypesTable.Columns["UNSIGNED_ATTRIBUTE"],
                providerTypesTable.Columns["MAXIMUM_SCALE"],
                providerTypesTable.Columns["MINIMUM_SCALE"],
                providerTypesTable.Columns["LITERAL_PREFIX"],
                providerTypesTable.Columns["LITERAL_SUFFIX"],
                providerTypesTable.Columns["DATA_TYPE"]
            };

            Debug.Assert(sourceColumns.Length == targetColumns.Length);

            DataColumn isSearchable         = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn isSearchableWithLike = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn providerDbType       = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn clrType         = dataTypesTable.Columns[DbMetaDataColumnNames.DataType];
            DataColumn isLong          = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn isFixed         = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn sourceOleDbType = providerTypesTable.Columns["DATA_TYPE"];

            DataColumn searchable = providerTypesTable.Columns["SEARCHABLE"];

            foreach (DataRow sourceRow in providerTypesTable.Rows)
            {
                DataRow newRow = dataTypesTable.NewRow();
                for (int i = 0; i < sourceColumns.Length; i++)
                {
                    if ((sourceColumns[i] != null) && (targetColumns[i] != null))
                    {
                        newRow[targetColumns[i]] = sourceRow[sourceColumns[i]];
                    }
                }

                short        nativeDataType = (short)Convert.ChangeType(sourceRow[sourceOleDbType], typeof(short), CultureInfo.InvariantCulture);
                NativeDBType nativeType     = NativeDBType.FromDBType(nativeDataType, (bool)newRow[isLong], (bool)newRow[isFixed]);

                newRow[clrType]        = nativeType.dataType.FullName;
                newRow[providerDbType] = nativeType.enumOleDbType;

                // searchable has to be special cased becasue it is not an eaxct mapping
                if ((isSearchable != null) && (isSearchableWithLike != null) && (searchable != null))
                {
                    newRow[isSearchable]         = DBNull.Value;
                    newRow[isSearchableWithLike] = DBNull.Value;
                    if (DBNull.Value != sourceRow[searchable])
                    {
                        long searchableValue = (long)(sourceRow[searchable]);
                        switch (searchableValue)
                        {
                        case ODB.DB_UNSEARCHABLE:
                            newRow[isSearchable]         = false;
                            newRow[isSearchableWithLike] = false;
                            break;

                        case ODB.DB_LIKE_ONLY:
                            newRow[isSearchable]         = false;
                            newRow[isSearchableWithLike] = true;
                            break;

                        case ODB.DB_ALL_EXCEPT_LIKE:
                            newRow[isSearchable]         = true;
                            newRow[isSearchableWithLike] = false;
                            break;

                        case ODB.DB_SEARCHABLE:
                            newRow[isSearchable]         = true;
                            newRow[isSearchableWithLike] = true;
                            break;
                        }
                    }
                }

                dataTypesTable.Rows.Add(newRow);
            }

            dataTypesTable.AcceptChanges();

            return(dataTypesTable);
        }
        // known difference: when getting the parameters for a sproc, the
        //   return value gets marked as a return value but for a sql stmt
        //   the return value gets marked as an output parameter.
        static private OleDbParameter[] DeriveParametersFromStoredProcedure(OleDbConnection connection, OleDbCommand command)
        {
            OleDbParameter[] plist = new OleDbParameter[0];

            if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters))
            {
                string quotePrefix, quoteSuffix;
                connection.GetLiteralQuotes(ADP.DeriveParameters, out quotePrefix, out quoteSuffix);

                Object[] parsed = MultipartIdentifier.ParseMultipartIdentifier(command.CommandText, quotePrefix, quoteSuffix, '.', 4, true, SR.OLEDB_OLEDBCommandText, false);
                if (null == parsed[3])
                {
                    throw ADP.NoStoredProcedureExists(command.CommandText);
                }

                Object[] restrictions = new object[4];
                object   value;

                // Parse returns an enforced 4 part array
                // 0) Server - ignored but removal would be a run-time breaking change from V1.0
                // 1) Catalog
                // 2) Schema
                // 3) ProcedureName

                // Restrictions array which is passed to OleDb API expects:
                // 0) Catalog
                // 1) Schema
                // 2) ProcedureName
                // 3) ParameterName (leave null)

                // Copy from Parse format to OleDb API format
                Array.Copy(parsed, 1, restrictions, 0, 3);

                //if (cmdConnection.IsServer_msdaora) {
                //    restrictions[1] = Convert.ToString(cmdConnection.UserId).ToUpper();
                //}
                DataTable table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedure_Parameters, restrictions);

                if (null != table)
                {
                    DataColumnCollection columns = table.Columns;

                    DataColumn parameterName      = null;
                    DataColumn parameterDirection = null;
                    DataColumn dataType           = null;
                    DataColumn maxLen             = null;
                    DataColumn numericPrecision   = null;
                    DataColumn numericScale       = null;
                    DataColumn backendtype        = null;

                    int index = columns.IndexOf(ODB.PARAMETER_NAME);
                    if (-1 != index)
                    {
                        parameterName = columns[index];
                    }

                    index = columns.IndexOf(ODB.PARAMETER_TYPE);
                    if (-1 != index)
                    {
                        parameterDirection = columns[index];
                    }

                    index = columns.IndexOf(ODB.DATA_TYPE);
                    if (-1 != index)
                    {
                        dataType = columns[index];
                    }

                    index = columns.IndexOf(ODB.CHARACTER_MAXIMUM_LENGTH);
                    if (-1 != index)
                    {
                        maxLen = columns[index];
                    }

                    index = columns.IndexOf(ODB.NUMERIC_PRECISION);
                    if (-1 != index)
                    {
                        numericPrecision = columns[index];
                    }

                    index = columns.IndexOf(ODB.NUMERIC_SCALE);
                    if (-1 != index)
                    {
                        numericScale = columns[index];
                    }

                    index = columns.IndexOf(ODB.TYPE_NAME);
                    if (-1 != index)
                    {
                        backendtype = columns[index];
                    }

                    DataRow[] dataRows = table.Select(null, ODB.ORDINAL_POSITION_ASC, DataViewRowState.CurrentRows);
                    plist = new OleDbParameter[dataRows.Length];
                    for (index = 0; index < dataRows.Length; ++index)
                    {
                        DataRow dataRow = dataRows[index];

                        OleDbParameter parameter = new OleDbParameter();

                        if ((null != parameterName) && !dataRow.IsNull(parameterName, DataRowVersion.Default))
                        {
                            // $CONSIDER - not trimming the @ from the beginning but to left the designer do that
                            parameter.ParameterName = Convert.ToString(dataRow[parameterName, DataRowVersion.Default], CultureInfo.InvariantCulture).TrimStart(new char[] { '@', ' ', ':' });
                        }
                        if ((null != parameterDirection) && !dataRow.IsNull(parameterDirection, DataRowVersion.Default))
                        {
                            short direction = Convert.ToInt16(dataRow[parameterDirection, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.Direction = ConvertToParameterDirection(direction);
                        }
                        if ((null != dataType) && !dataRow.IsNull(dataType, DataRowVersion.Default))
                        {
                            // need to ping FromDBType, otherwise WChar->WChar when the user really wants VarWChar
                            short wType = Convert.ToInt16(dataRow[dataType, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.OleDbType = NativeDBType.FromDBType(wType, false, false).enumOleDbType;
                        }
                        if ((null != maxLen) && !dataRow.IsNull(maxLen, DataRowVersion.Default))
                        {
                            parameter.Size = Convert.ToInt32(dataRow[maxLen, DataRowVersion.Default], CultureInfo.InvariantCulture);
                        }
                        switch (parameter.OleDbType)
                        {
                        case OleDbType.Decimal:
                        case OleDbType.Numeric:
                        case OleDbType.VarNumeric:
                            if ((null != numericPrecision) && !dataRow.IsNull(numericPrecision, DataRowVersion.Default))
                            {
                                // @devnote: unguarded cast from Int16 to Byte
                                parameter.PrecisionInternal = (Byte)Convert.ToInt16(dataRow[numericPrecision], CultureInfo.InvariantCulture);
                            }
                            if ((null != numericScale) && !dataRow.IsNull(numericScale, DataRowVersion.Default))
                            {
                                // @devnote: unguarded cast from Int16 to Byte
                                parameter.ScaleInternal = (Byte)Convert.ToInt16(dataRow[numericScale], CultureInfo.InvariantCulture);
                            }
                            break;

                        case OleDbType.VarBinary:
                        case OleDbType.VarChar:
                        case OleDbType.VarWChar:
                            value = dataRow[backendtype, DataRowVersion.Default];
                            if (value is string)
                            {
                                string backendtypename = ((string)value).ToLower(CultureInfo.InvariantCulture);
                                switch (backendtypename)
                                {
                                case "binary":
                                    parameter.OleDbType = OleDbType.Binary;
                                    break;

                                //case "varbinary":
                                //    parameter.OleDbType = OleDbType.VarBinary;
                                //    break;
                                case "image":
                                    parameter.OleDbType = OleDbType.LongVarBinary;
                                    break;

                                case "char":
                                    parameter.OleDbType = OleDbType.Char;
                                    break;

                                //case "varchar":
                                //case "varchar2":
                                //    parameter.OleDbType = OleDbType.VarChar;
                                //    break;
                                case "text":
                                    parameter.OleDbType = OleDbType.LongVarChar;
                                    break;

                                case "nchar":
                                    parameter.OleDbType = OleDbType.WChar;
                                    break;

                                //case "nvarchar":
                                //    parameter.OleDbType = OleDbType.VarWChar;
                                case "ntext":
                                    parameter.OleDbType = OleDbType.LongVarWChar;
                                    break;
                                }
                            }
                            break;
                        }
                        //if (AdapterSwitches.OleDbSql.TraceVerbose) {
                        //    ADP.Trace_Parameter("StoredProcedure", parameter);
                        //}
                        plist[index] = parameter;
                    }
                }
                if ((0 == plist.Length) && connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures))
                {
                    restrictions = new Object[4] {
                        null, null, command.CommandText, null
                    };
                    table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, restrictions);
                    if (0 == table.Rows.Count)
                    {
                        throw ADP.NoStoredProcedureExists(command.CommandText);
                    }
                }
            }
            else if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures))
            {
                Object[] restrictions = new Object[4] {
                    null, null, command.CommandText, null
                };
                DataTable table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, restrictions);
                if (0 == table.Rows.Count)
                {
                    throw ADP.NoStoredProcedureExists(command.CommandText);
                }
                // we don't ever expect a procedure with 0 parameters, they should have at least a return value
                throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider);
            }
            else
            {
                throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider);
            }
            return(plist);
        }
 public void ResetOleDbType() {
     if (null != _metaType) {
         PropertyTypeChanging();
         _metaType = null;
     }
 }
Exemple #29
0
        // known difference: when getting the parameters for a sproc, the
        //   return value gets marked as a return value but for a sql stmt
        //   the return value gets marked as an output parameter.
        private static OleDbParameter[] DeriveParametersFromStoredProcedure(OleDbConnection connection, OleDbCommand command)
        {
            OleDbParameter[] plist = Array.Empty <OleDbParameter>();

            if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters))
            {
                string quotePrefix, quoteSuffix;
                connection.GetLiteralQuotes(ADP.DeriveParameters, out quotePrefix, out quoteSuffix);

                object?[] parsed = MultipartIdentifier.ParseMultipartIdentifier(command.CommandText, quotePrefix, quoteSuffix, '.', 4, true, SR.OLEDB_OLEDBCommandText, false);
                if (null == parsed[3])
                {
                    throw ADP.NoStoredProcedureExists(command.CommandText);
                }

                object?[] restrictions = new object[4];
                object    value;

                // Parse returns an enforced 4 part array
                // 0) Server - ignored but removal would be a run-time breaking change from V1.0
                // 1) Catalog
                // 2) Schema
                // 3) ProcedureName

                // Restrictions array which is passed to OleDb API expects:
                // 0) Catalog
                // 1) Schema
                // 2) ProcedureName
                // 3) ParameterName (leave null)

                // Copy from Parse format to OleDb API format
                Array.Copy(parsed, 1, restrictions, 0, 3);

                //if (cmdConnection.IsServer_msdaora) {
                //    restrictions[1] = Convert.ToString(cmdConnection.UserId).ToUpper();
                //}
                DataTable?table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedure_Parameters, restrictions);

                if (null != table)
                {
                    DataColumnCollection columns = table.Columns;

                    DataColumn?parameterName      = null;
                    DataColumn?parameterDirection = null;
                    DataColumn?dataType           = null;
                    DataColumn?maxLen             = null;
                    DataColumn?numericPrecision   = null;
                    DataColumn?numericScale       = null;
                    DataColumn?backendtype        = null;

                    int index = columns.IndexOf(ODB.PARAMETER_NAME);
                    if (-1 != index)
                    {
                        parameterName = columns[index];
                    }

                    index = columns.IndexOf(ODB.PARAMETER_TYPE);
                    if (-1 != index)
                    {
                        parameterDirection = columns[index];
                    }

                    index = columns.IndexOf(ODB.DATA_TYPE);
                    if (-1 != index)
                    {
                        dataType = columns[index];
                    }

                    index = columns.IndexOf(ODB.CHARACTER_MAXIMUM_LENGTH);
                    if (-1 != index)
                    {
                        maxLen = columns[index];
                    }

                    index = columns.IndexOf(ODB.NUMERIC_PRECISION);
                    if (-1 != index)
                    {
                        numericPrecision = columns[index];
                    }

                    index = columns.IndexOf(ODB.NUMERIC_SCALE);
                    if (-1 != index)
                    {
                        numericScale = columns[index];
                    }

                    index = columns.IndexOf(ODB.TYPE_NAME);
                    if (-1 != index)
                    {
                        backendtype = columns[index];
                    }

                    DataRow[] dataRows = table.Select(null, ODB.ORDINAL_POSITION_ASC, DataViewRowState.CurrentRows);
                    plist = new OleDbParameter[dataRows.Length];
                    for (index = 0; index < dataRows.Length; ++index)
                    {
                        DataRow dataRow = dataRows[index];

                        OleDbParameter parameter = new OleDbParameter();

                        if ((null != parameterName) && !dataRow.IsNull(parameterName, DataRowVersion.Default))
                        {
                            // $CONSIDER - not trimming the @ from the beginning but to left the designer do that
                            parameter.ParameterName = Convert.ToString(dataRow[parameterName, DataRowVersion.Default], CultureInfo.InvariantCulture) !.TrimStart(new char[] { '@', ' ', ':' });
                        }
                        if ((null != parameterDirection) && !dataRow.IsNull(parameterDirection, DataRowVersion.Default))
                        {
                            short direction = Convert.ToInt16(dataRow[parameterDirection, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.Direction = ConvertToParameterDirection(direction);
                        }
                        if ((null != dataType) && !dataRow.IsNull(dataType, DataRowVersion.Default))
                        {
                            // need to ping FromDBType, otherwise WChar->WChar when the user really wants VarWChar
                            short wType = Convert.ToInt16(dataRow[dataType, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.OleDbType = NativeDBType.FromDBType(wType, false, false).enumOleDbType;
                        }
                        if ((null != maxLen) && !dataRow.IsNull(maxLen, DataRowVersion.Default))
                        {
                            parameter.Size = Convert.ToInt32(dataRow[maxLen, DataRowVersion.Default], CultureInfo.InvariantCulture);
                        }
                        switch (parameter.OleDbType)
                        {
                        case OleDbType.Decimal:
                        case OleDbType.Numeric:
                        case OleDbType.VarNumeric:
                            if ((null != numericPrecision) && !dataRow.IsNull(numericPrecision, DataRowVersion.Default))
                            {
                                // @devnote: unguarded cast from Int16 to Byte
                                parameter.PrecisionInternal = (byte)Convert.ToInt16(dataRow[numericPrecision], CultureInfo.InvariantCulture);
                            }
                            if ((null != numericScale) && !dataRow.IsNull(numericScale, DataRowVersion.Default))
                            {
                                // @devnote: unguarded cast from Int16 to Byte
                                parameter.ScaleInternal = (byte)Convert.ToInt16(dataRow[numericScale], CultureInfo.InvariantCulture);
                            }
                            break;

                        case OleDbType.VarBinary:
                        case OleDbType.VarChar:
                        case OleDbType.VarWChar:
                            value = dataRow[backendtype !, DataRowVersion.Default];
        internal bool BindParameter(int index, Bindings bindings)
        {
            int          size;
            int          ptrSize;
            byte         precisionInternal;
            byte         scaleInternal;
            object       obj2     = this.Value;
            NativeDBType bindType = this.GetBindType(obj2);

            if (bindType.enumOleDbType == System.Data.OleDb.OleDbType.Empty)
            {
                throw ODB.UninitializedParameters(index, bindType.enumOleDbType);
            }
            this._coerceMetaType = bindType;
            obj2 = CoerceValue(obj2, bindType);
            this.CoercedValue = obj2;
            ParameterDirection direction = this.Direction;

            if (this.ShouldSerializePrecision())
            {
                precisionInternal = this.PrecisionInternal;
            }
            else
            {
                precisionInternal = this.ValuePrecision(obj2);
            }
            if (precisionInternal == 0)
            {
                precisionInternal = bindType.maxpre;
            }
            if (this.ShouldSerializeScale())
            {
                scaleInternal = this.ScaleInternal;
            }
            else
            {
                scaleInternal = this.ValueScale(obj2);
            }
            int wType = bindType.wType;

            if (bindType.islong)
            {
                ptrSize = ADP.PtrSize;
                if (this.ShouldSerializeSize())
                {
                    size = this.Size;
                }
                else if (0x81 == bindType.dbType)
                {
                    size = 0x7fffffff;
                }
                else if (130 == bindType.dbType)
                {
                    size = 0x3fffffff;
                }
                else
                {
                    size = 0x7fffffff;
                }
                wType |= 0x4000;
            }
            else if (bindType.IsVariableLength)
            {
                bool flag;
                if (!this.ShouldSerializeSize() && ADP.IsDirection(this, ParameterDirection.Output))
                {
                    throw ADP.UninitializedParameterSize(index, this._coerceMetaType.dataType);
                }
                if (this.ShouldSerializeSize())
                {
                    size = this.Size;
                    flag = false;
                }
                else
                {
                    size = this.ValueSize(obj2);
                    flag = true;
                }
                if (0 >= size)
                {
                    if (size != 0)
                    {
                        if (-1 != size)
                        {
                            throw ADP.InvalidSizeValue(size);
                        }
                        ptrSize = ADP.PtrSize;
                        wType  |= 0x4000;
                    }
                    else if (130 == wType)
                    {
                        ptrSize = 2;
                    }
                    else
                    {
                        ptrSize = 0;
                    }
                }
                else
                {
                    if (130 == bindType.wType)
                    {
                        ptrSize = (Math.Min(size, 0x3ffffffe) * 2) + 2;
                    }
                    else
                    {
                        ptrSize = size;
                    }
                    if (flag && (0x81 == bindType.dbType))
                    {
                        size = Math.Min(size, 0x3ffffffe) * 2;
                    }
                    if (0x2000 < ptrSize)
                    {
                        ptrSize = ADP.PtrSize;
                        wType  |= 0x4000;
                    }
                }
            }
            else
            {
                ptrSize = bindType.fixlen;
                size    = ptrSize;
            }
            bindings.CurrentIndex   = index;
            bindings.DataSourceType = bindType.dbString.DangerousGetHandle();
            bindings.Name           = ADP.PtrZero;
            bindings.ParamSize      = new IntPtr(size);
            bindings.Flags          = GetBindFlags(direction);
            bindings.Ordinal        = (IntPtr)(index + 1);
            bindings.Part           = bindType.dbPart;
            bindings.ParamIO        = GetBindDirection(direction);
            bindings.Precision      = precisionInternal;
            bindings.Scale          = scaleInternal;
            bindings.DbType         = wType;
            bindings.MaxLen         = ptrSize;
            if (Bid.AdvancedOn)
            {
                Bid.Trace("<oledb.struct.tagDBPARAMBINDINFO|INFO|ADV> index=%d, parameterName='%ls'\n", index, this.ParameterName);
                Bid.Trace("<oledb.struct.tagDBBINDING|INFO|ADV>\n");
            }
            return(this.IsParameterComputed());
        }