示例#1
0
        public override DbParameter CreateParameter(string parameterName, AbstractDbType dbType, string?udtTypeName, bool nullable, object?value)
        {
            if (dbType.IsDate())
            {
                if (value is DateTime dt)
                {
                    AssertDateTime(dt);
                }
                else if (value is Date d)
                {
                    value = new NpgsqlDate((DateTime)d);
                }
            }

            var result = new Npgsql.NpgsqlParameter(parameterName, value ?? DBNull.Value)
            {
                IsNullable = nullable
            };

            result.NpgsqlDbType = dbType.PostgreSql;
            if (udtTypeName != null)
            {
                result.DataTypeName = udtTypeName;
            }


            return(result);
        }
    public override DbParameter CreateParameter(string parameterName, AbstractDbType dbType, string?udtTypeName, bool nullable, object?value)
    {
        if (dbType.IsDate())
        {
            if (value is DateTime dt)
            {
                AssertDateTime(dt);
            }
            else if (value is DateOnly d)
            {
                value = d.ToDateTime();
            }
        }
        else if (dbType.IsTime())
        {
            if (value is TimeOnly to)
            {
                value = to.ToTimeSpan();
            }
        }

        var result = new SqlParameter(parameterName, value ?? DBNull.Value)
        {
            IsNullable = nullable
        };

        result.SqlDbType = dbType.SqlServer;
        if (udtTypeName != null)
        {
            result.UdtTypeName = udtTypeName;
        }

        return(result);
    }
    public override MemberInitExpression ParameterFactory(Expression parameterName, AbstractDbType dbType, int?size, byte?precision, byte?scale, string?udtTypeName, bool nullable, Expression value)
    {
        var uType = value.Type.UnNullify();

        var exp =
            uType == typeof(DateTime) ? Expression.Call(miAsserDateTime, Expression.Convert(value, typeof(DateTime?))) :
                //https://github.com/dotnet/SqlClient/issues/1009
                uType == typeof(DateOnly) ? Expression.Call(miToDateTimeKind, Expression.Convert(value, typeof(DateOnly)), Expression.Constant(Schema.Current.DateTimeKind)) :
                uType == typeof(TimeOnly) ? Expression.Call(Expression.Convert(value, typeof(TimeOnly)), miToTimeSpan) :
                value;

        Expression valueExpr = Expression.Convert(exp, typeof(object));

        if (nullable)
        {
            valueExpr = Expression.Condition(Expression.Equal(value, Expression.Constant(null, value.Type)),
                                             Expression.Constant(DBNull.Value, typeof(object)),
                                             valueExpr);
        }

        NewExpression newExpr = Expression.New(typeof(SqlParameter).GetConstructor(new[] { typeof(string), typeof(object) }) !, parameterName, valueExpr);


        List <MemberBinding> mb = new()
        {
            Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.IsNullable)) !, Expression.Constant(nullable)),
            Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.SqlDbType)) !, Expression.Constant(dbType.SqlServer)),
        };

        if (size != null)
        {
            mb.Add(Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.Size)) !, Expression.Constant(size)));
        }

        if (precision != null)
        {
            mb.Add(Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.Precision)) !, Expression.Constant(precision)));
        }

        if (scale != null)
        {
            mb.Add(Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.Scale)) !, Expression.Constant(scale)));
        }

        if (udtTypeName != null)
        {
            mb.Add(Expression.Bind(typeof(SqlParameter).GetProperty(nameof(SqlParameter.UdtTypeName)) !, Expression.Constant(udtTypeName)));
        }

        return(Expression.MemberInit(newExpr, mb));
    }
    public override MemberInitExpression ParameterFactory(Expression parameterName, AbstractDbType dbType, int?size, byte?precision, byte?scale, string?udtTypeName, bool nullable, Expression value)
    {
        Expression valueExpr = Expression.Convert(
            !dbType.IsDate() ? value :
            value.Type.UnNullify() == typeof(DateTime) ? Expression.Call(miAsserDateTime, Expression.Convert(value, typeof(DateTime?))) :
                value.Type.UnNullify() == typeof(DateOnly) ? Expression.Call(miToDateTimeKind, Expression.Convert(value, typeof(DateOnly?)), Expression.Constant(Schema.Current.DateTimeKind)) :
                    value,
                    typeof(object));

        if (nullable)
        {
            valueExpr = Expression.Condition(Expression.Equal(value, Expression.Constant(null, value.Type)),
                                             Expression.Constant(DBNull.Value, typeof(object)),
                                             valueExpr);
        }

        NewExpression newExpr = Expression.New(typeof(NpgsqlParameter).GetConstructor(new[] { typeof(string), typeof(object) }) !, parameterName, valueExpr);


        List <MemberBinding> mb = new List <MemberBinding>()
        {
            Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.IsNullable)) !, Expression.Constant(nullable)),
            Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.NpgsqlDbType)) !, Expression.Constant(dbType.PostgreSql)),
        };

        if (size != null)
        {
            mb.Add(Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.Size)) !, Expression.Constant(size)));
        }

        if (precision != null)
        {
            mb.Add(Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.Precision)) !, Expression.Constant(precision)));
        }

        if (scale != null)
        {
            mb.Add(Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.Scale)) !, Expression.Constant(scale)));
        }

        if (udtTypeName != null)
        {
            mb.Add(Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.DataTypeName)) !, Expression.Constant(udtTypeName)));
        }

        return(Expression.MemberInit(newExpr, mb));
    }
示例#5
0
 public SqlCastExpression(Type type, Expression expression, AbstractDbType dbType)
     : base(DbExpressionType.SqlCast, type)
 {
     this.Expression = expression;
     this.DbType     = dbType;
 }
示例#6
0
 public abstract MemberInitExpression ParameterFactory(Expression parameterName, AbstractDbType dbType, string?udtTypeName, bool nullable, Expression value);
示例#7
0
 public abstract DbParameter CreateParameter(string parameterName, AbstractDbType dbType, string?udtTypeName, bool nullable, object?value);
示例#8
0
        public override MemberInitExpression ParameterFactory(Expression parameterName, AbstractDbType dbType, string?udtTypeName, bool nullable, Expression value)
        {
            Expression valueExpr = Expression.Convert(
                !dbType.IsDate() ? value :
                value.Type.UnNullify() == typeof(DateTime) ? Expression.Call(miAsserDateTime, Expression.Convert(value, typeof(DateTime?))) :
                    value.Type.UnNullify() == typeof(Date) ? Expression.Convert(Expression.Convert(value, typeof(Date?)), typeof(DateTime?)) : //Converting from Date -> DateTime? directly produces null always
                        value,
                        typeof(object));

            if (nullable)
            {
                valueExpr = Expression.Condition(Expression.Equal(value, Expression.Constant(null, value.Type)),
                                                 Expression.Constant(DBNull.Value, typeof(object)),
                                                 valueExpr);
            }

            NewExpression newExpr = Expression.New(typeof(NpgsqlParameter).GetConstructor(new[] { typeof(string), typeof(object) }), parameterName, valueExpr);


            List <MemberBinding> mb = new List <MemberBinding>()
            {
                Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.IsNullable)), Expression.Constant(nullable)),
                Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.NpgsqlDbType)), Expression.Constant(dbType.PostgreSql)),
            };

            if (udtTypeName != null)
            {
                mb.Add(Expression.Bind(typeof(NpgsqlParameter).GetProperty(nameof(NpgsqlParameter.DataTypeName)), Expression.Constant(udtTypeName)));
            }

            return(Expression.MemberInit(newExpr, mb));
        }
示例#9
0
 public DbTypePair(AbstractDbType type, string?udtTypeName)
 {
     this.DbType = type;
     this.UserDefinedTypeName = udtTypeName;
 }