Beispiel #1
0
        private static void EmitDefaultValueConstant(ILGenerator code, FieldMapDefinition field)
        {
            if (field.DefaultValue == null)
            {
                return;
            }

            var defaultValueType = field.DefaultValue.GetType();

            if (defaultValueType == typeof(bool))
            {
                if ((bool)field.DefaultValue)
                {
                    code.Emit(OpCodes.Ldc_I4_1);
                }
                else
                {
                    code.Emit(OpCodes.Ldc_I4_0);
                }
            }
            else if (defaultValueType == typeof(byte))
            {
                code.Emit(OpCodes.Ldc_I4, (int)(byte)field.DefaultValue);
            }
            else if (defaultValueType == typeof(short))
            {
                code.Emit(OpCodes.Ldc_I4, (int)(short)field.DefaultValue);
            }
            else if (defaultValueType == typeof(int))
            {
                code.Emit(OpCodes.Ldc_I4, (int)field.DefaultValue);
            }
            else if (defaultValueType == typeof(long))
            {
                code.Emit(OpCodes.Ldc_I8, (long)field.DefaultValue);
            }
            else if (defaultValueType == typeof(char))
            {
                code.Emit(OpCodes.Ldc_I4_S, (char)field.DefaultValue);
            }
            else if (defaultValueType == typeof(string))
            {
                code.Emit(OpCodes.Ldstr, (string)field.DefaultValue);
            }
            else if (defaultValueType == typeof(float))
            {
                code.Emit(OpCodes.Ldc_R4, (float)field.DefaultValue);
            }
            else if (defaultValueType == typeof(double))
            {
                code.Emit(OpCodes.Ldc_R8, (double)field.DefaultValue);
            }
        }
Beispiel #2
0
        public void AddDebugGetValue(
            ILGenerator code,
            FieldMapDefinition field,
            MethodInfo getMethod,
            FieldInfo ordinalField)
        {
            var defaultValue = GetDefaultValue(field);

            this.AddDebugSymbolLine(
                code,
                $"            model.{field.DestinationProperty.Name} = reader.{getMethod.Name}({ordinalField.Name}{defaultValue});");
        }
Beispiel #3
0
        private static void WrapWithNullableIfNeeded(ILGenerator code, FieldMapDefinition field)
        {
            var propertyType = field.DestinationProperty.PropertyType;

            if (field.DefaultValue == null || propertyType.IsNullableType() == false)
            {
                return;
            }

            var constructor = MapEmitAssembly.SupportedConstantType[propertyType.GetGenericArguments()[0]];

            code.Emit(OpCodes.Newobj, constructor);
        }
Beispiel #4
0
        private void EmitParameter(
            FieldMapDefinition fieldDefinition,
            ILGenerator code,
            LocalBuilder parametersLocal)
        {
            var sourceColumn = fieldDefinition.SourceColumn;

            sourceColumn = sourceColumn.StartsWith("@") ? sourceColumn : $"@{sourceColumn}";
            if (parametersLocal == null)
            {
                this.EmitAddParameter(code, fieldDefinition, sourceColumn);
            }
            else
            {
                this.EmitSetParameter(code, fieldDefinition, parametersLocal, sourceColumn);
            }
        }
Beispiel #5
0
        private void EmitAddParameter(ILGenerator code, FieldMapDefinition field, string sourceColumn)
        {
            var destinationProperty = field.DestinationProperty;
            var propertyType        = destinationProperty.PropertyType;
            var commandMethods      = MapEmitAssembly.CommandExtensionMethods;
            var addParameterMethod  = field.AllowNulls ? commandMethods.AddParameterOrDbNull : commandMethods.AddParameter;

            this.mapEmitDebugInfo?.AddDebugAddParameter(code, field, addParameterMethod, sourceColumn);
            code.Emit(OpCodes.Ldarg_1);
            code.Emit(OpCodes.Ldstr, sourceColumn);
            code.Emit(OpCodes.Ldarg_2);
            code.Emit(OpCodes.Callvirt, destinationProperty.GetMethod);

            if (propertyType.IsValueType())
            {
                code.Emit(OpCodes.Box, propertyType);
            }

            code.Emit(OpCodes.Call, addParameterMethod);
        }
Beispiel #6
0
        private void EmitGetValue(ILGenerator code, FieldMapDefinition field)
        {
            var ordinalField = this.fieldOrdinals[field.SourceColumn];
            var getMethod    = GetReaderGetDataMethodForField(field);

            this.mapEmitDebugInfo?.AddDebugGetValue(code, field, getMethod, ordinalField);
            code.Emit(OpCodes.Ldarg_1);
            code.Emit(OpCodes.Ldarg_2);
            code.Emit(OpCodes.Ldarg_0);
            code.Emit(OpCodes.Ldfld, ordinalField);

            EmitDefaultValueConstant(code, field);

            ////code.EmitCall(field.AllowNulls || field.IsSqlServerSpecific ? OpCodes.Call : OpCodes.Callvirt, getMethod, null);
            code.EmitCall(getMethod.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, getMethod, null);

            WrapWithNullableIfNeeded(code, field);

            code.EmitCall(OpCodes.Callvirt, field.DestinationProperty.GetSetMethod(), null);
        }
Beispiel #7
0
        private static MethodInfo GetReaderGetDataMethodForField(FieldMapDefinition field)
        {
            Dictionary <Type, MethodInfo> methodDictionary;

            if (field.IsSqlServerSpecific)
            {
                methodDictionary = MapEmitAssembly.SqlSpecificGetMethods;
                return(methodDictionary[field.DestinationProperty.PropertyType]);
            }

            if (field.AllowNulls)
            {
                methodDictionary = field.DefaultValue == null ? MapEmitAssembly.NullableGetMethods : MapEmitAssembly.NullableGetWithDefaultMethods;
            }
            else
            {
                methodDictionary = MapEmitAssembly.DataRecordGetMethods;
            }

            return(methodDictionary[field.DestinationProperty.PropertyType]);
        }
Beispiel #8
0
        private static string GetDefaultValue(FieldMapDefinition field)
        {
            var value = field.DefaultValue;

            if (value is bool)
            {
                value = value.ToString().ToLower();
            }

            if (value is string)
            {
                value = $"\"{value}\"";
            }

            if (value is char)
            {
                value = $"'{value}'";
            }

            var defaultValue = value == null ? string.Empty : $", {value}";

            return(defaultValue);
        }
Beispiel #9
0
 public void AddDebugAddParameter(ILGenerator code, FieldMapDefinition field, MethodInfo addParameterMethod, string sourceColumn)
 {
     this.AddDebugSymbolLine(
         code,
         $"            command.{addParameterMethod.Name}(\"{sourceColumn}\", model.{field.DestinationProperty.Name});");
 }