| 
 | 1 | +// Licensed to the .NET Foundation under one or more agreements.  | 
 | 2 | +// The .NET Foundation licenses this file to you under the MIT license.  | 
 | 3 | +// See the LICENSE file in the project root for more information.  | 
 | 4 | + | 
 | 5 | +using System;  | 
 | 6 | +using System.Data;  | 
 | 7 | +using System.Data.SqlTypes;  | 
 | 8 | +using Microsoft.Data.Common;  | 
 | 9 | + | 
 | 10 | +namespace Microsoft.Data.SqlClient.Server  | 
 | 11 | +{  | 
 | 12 | +    /// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SqlDataRecord/*' />  | 
 | 13 | +    public partial class SqlDataRecord : IDataRecord  | 
 | 14 | +    {  | 
 | 15 | +        private readonly SmiContext _recordContext;  | 
 | 16 | +       | 
 | 17 | +        private Type GetFieldTypeFrameworkSpecific(int ordinal)  | 
 | 18 | +        {  | 
 | 19 | +            SqlMetaData md = GetSqlMetaData(ordinal);  | 
 | 20 | +            if (md.SqlDbType == SqlDbType.Udt)  | 
 | 21 | +            {  | 
 | 22 | +                return md.Type;  | 
 | 23 | +            }  | 
 | 24 | +            else  | 
 | 25 | +            {  | 
 | 26 | +                return MetaType.GetMetaTypeFromSqlDbType(md.SqlDbType, false).ClassType;  | 
 | 27 | +            }  | 
 | 28 | +        }  | 
 | 29 | +     | 
 | 30 | +        private object GetValueFrameworkSpecific(int ordinal)  | 
 | 31 | +        {  | 
 | 32 | +            SmiMetaData metaData = GetSmiMetaData(ordinal);  | 
 | 33 | +            if (SmiVersion >= SmiContextFactory.KatmaiVersion)  | 
 | 34 | +            {  | 
 | 35 | +                return ValueUtilsSmi.GetValue200(_eventSink, _recordBuffer, ordinal, metaData, _recordContext);  | 
 | 36 | +            }  | 
 | 37 | +            else  | 
 | 38 | +            {  | 
 | 39 | +                return ValueUtilsSmi.GetValue(_eventSink, _recordBuffer, ordinal, metaData, _recordContext);  | 
 | 40 | +            }  | 
 | 41 | +        }  | 
 | 42 | +      | 
 | 43 | +        private object GetSqlValueFrameworkSpecific(int ordinal)  | 
 | 44 | +        {  | 
 | 45 | +            SmiMetaData metaData = GetSmiMetaData(ordinal);  | 
 | 46 | +            if (SmiVersion >= SmiContextFactory.KatmaiVersion)  | 
 | 47 | +            {  | 
 | 48 | +                return ValueUtilsSmi.GetSqlValue200(_eventSink, _recordBuffer, ordinal, metaData, _recordContext);  | 
 | 49 | +            }  | 
 | 50 | +            return ValueUtilsSmi.GetSqlValue(_eventSink, _recordBuffer, ordinal, metaData, _recordContext);  | 
 | 51 | +        }  | 
 | 52 | + | 
 | 53 | +        private SqlBytes GetSqlBytesFrameworkSpecific(int ordinal) => ValueUtilsSmi.GetSqlBytes(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), _recordContext);  | 
 | 54 | +   | 
 | 55 | +        private SqlXml GetSqlXmlFrameworkSpecific(int ordinal) => ValueUtilsSmi.GetSqlXml(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), _recordContext);  | 
 | 56 | +          | 
 | 57 | +        private SqlChars GetSqlCharsFrameworkSpecific(int ordinal) => ValueUtilsSmi.GetSqlChars(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), _recordContext);  | 
 | 58 | +   | 
 | 59 | +        private int SetValuesFrameworkSpecific(params object[] values)  | 
 | 60 | +        {  | 
 | 61 | +            if (values == null)  | 
 | 62 | +            {  | 
 | 63 | +                throw ADP.ArgumentNull(nameof(values));  | 
 | 64 | +            }  | 
 | 65 | + | 
 | 66 | +            // Allow values array longer than FieldCount, just ignore the extra cells.  | 
 | 67 | +            int copyLength = (values.Length > FieldCount) ? FieldCount : values.Length;  | 
 | 68 | + | 
 | 69 | +            ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode[copyLength];  | 
 | 70 | + | 
 | 71 | +            // Verify all data values as acceptable before changing current state.  | 
 | 72 | +            for (int i = 0; i < copyLength; i++)  | 
 | 73 | +            {  | 
 | 74 | +                SqlMetaData metaData = GetSqlMetaData(i);  | 
 | 75 | +                typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(  | 
 | 76 | +                    metaData.SqlDbType,  | 
 | 77 | +                    isMultiValued: false,  | 
 | 78 | +                    values[i],  | 
 | 79 | +                    metaData.Type,  | 
 | 80 | +                    SmiVersion  | 
 | 81 | +                );  | 
 | 82 | +                if (typeCodes[i] == ExtendedClrTypeCode.Invalid)  | 
 | 83 | +                {  | 
 | 84 | +                    throw ADP.InvalidCast();  | 
 | 85 | +                }  | 
 | 86 | +            }  | 
 | 87 | + | 
 | 88 | +            // Now move the data (it'll only throw if someone plays with the values array between  | 
 | 89 | +            //      the validation loop and here, or if an invalid UDT was sent).  | 
 | 90 | +            for (int i = 0; i < copyLength; i++)  | 
 | 91 | +            {  | 
 | 92 | +                if (SmiVersion >= SmiContextFactory.KatmaiVersion)  | 
 | 93 | +                {  | 
 | 94 | +                    ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], offset: 0, length: 0, peekAhead: null);  | 
 | 95 | +                }  | 
 | 96 | +                else  | 
 | 97 | +                {  | 
 | 98 | +                    ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], offset: 0);  | 
 | 99 | +                }  | 
 | 100 | +            }  | 
 | 101 | + | 
 | 102 | +            return copyLength;  | 
 | 103 | +        }  | 
 | 104 | +        | 
 | 105 | +        private void SetValueFrameworkSpecific(int ordinal, object value)  | 
 | 106 | +        {  | 
 | 107 | +            SqlMetaData metaData = GetSqlMetaData(ordinal);  | 
 | 108 | +            ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(  | 
 | 109 | +                metaData.SqlDbType,  | 
 | 110 | +                isMultiValued: false,  | 
 | 111 | +                value,  | 
 | 112 | +                metaData.Type,  | 
 | 113 | +                SmiVersion  | 
 | 114 | +            );  | 
 | 115 | +            if (typeCode == ExtendedClrTypeCode.Invalid)  | 
 | 116 | +            {  | 
 | 117 | +                throw ADP.InvalidCast();  | 
 | 118 | +            }  | 
 | 119 | + | 
 | 120 | +            if (SmiVersion >= SmiContextFactory.KatmaiVersion)  | 
 | 121 | +            {  | 
 | 122 | +                ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0, length: 0, peekAhead: null);  | 
 | 123 | +            }  | 
 | 124 | +            else  | 
 | 125 | +            {  | 
 | 126 | +                ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0);  | 
 | 127 | +            }  | 
 | 128 | +        }  | 
 | 129 | +    | 
 | 130 | +        private void SetTimeSpanFrameworkSpecific(int ordinal, TimeSpan value) => ValueUtilsSmi.SetTimeSpan(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion);  | 
 | 131 | +        private void SetDateTimeOffsetFrameworkSpecific(int ordinal, DateTimeOffset value) => ValueUtilsSmi.SetDateTimeOffset(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion);  | 
 | 132 | + | 
 | 133 | +        private ulong SmiVersion => InOutOfProcHelper.InProc ? SmiContextFactory.Instance.NegotiatedSmiVersion : SmiContextFactory.LatestVersion;  | 
 | 134 | +    }  | 
 | 135 | +}  | 
0 commit comments