Example #1
0
        public virtual ISqlNode VisitCast(SqlCastNode n)
        {
            var expr = Visit(n.Expression);
            var type = Visit(n.DataType) as SqlDataTypeNode;

            return(n.Update(expr, type));
        }
        public void Optimize_CastStringToNumeric()
        {
            // CAST("123.45" as NUMERIC) => 123.45M
            var ast = new SqlCastNode
            {
                Expression = new SqlStringNode("123.45"),
                DataType   = new SqlDataTypeNode
                {
                    DataType = new SqlKeywordNode("NUMERIC")
                }
            };

            var target = new ExpressionOptimizeVisitor();
            var result = target.Visit(ast);

            result.Should().MatchAst(new SqlNumberNode(123.45M));
        }
        public void Optimize_CastStringToBigint()
        {
            // CAST("12345" as BIGINT) => 12345L
            var ast = new SqlCastNode
            {
                Expression = new SqlStringNode("12345"),
                DataType   = new SqlDataTypeNode
                {
                    DataType = new SqlKeywordNode("BIGINT")
                }
            };

            var target = new ExpressionOptimizeVisitor();
            var result = target.Visit(ast);

            result.Should().MatchAst(new SqlNumberNode(12345L));
        }
        public void Optimize_CastNumberToVarcharMax()
        {
            // CAST(5 as VARCHAR(MAX)) => "5"
            var ast = new SqlCastNode
            {
                Expression = new SqlNumberNode(5),
                DataType   = new SqlDataTypeNode
                {
                    DataType = new SqlKeywordNode("VARCHAR"),
                    Size     = new SqlKeywordNode("MAX")
                }
            };

            var target = new ExpressionOptimizeVisitor();
            var result = target.Visit(ast);

            result.Should().MatchAst(new SqlStringNode("5"));
        }
        public void Optimize_CastStringToVarcharNoPadRight()
        {
            // CAST("12345" as VARCHAR(3)) => "123"
            var ast = new SqlCastNode
            {
                Expression = new SqlStringNode("ab"),
                DataType   = new SqlDataTypeNode
                {
                    DataType = new SqlKeywordNode("VARCHAR"),
                    Size     = new SqlNumberNode(4)
                }
            };

            var target = new ExpressionOptimizeVisitor();
            var result = target.Visit(ast);

            result.Should().MatchAst(new SqlStringNode("ab"));
        }
 public ISqlNode VisitCast(SqlCastNode n)
 {
     Append("CAST(", n.Expression, " AS ", n.DataType, ")");
     return(n);
 }
 public override ISqlNode VisitCast(SqlCastNode n)
 {
     return(n.TryReduce());
 }