Exemplo n.º 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);
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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 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);
    }
Exemplo n.º 4
0
    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));
    }