示例#1
0
        public static SqlExpression GenericPad(SqlFunctionCall node)
        {
            string paddingFunction;

            switch (node.FunctionType)
            {
            case SqlFunctionType.PadLeft:
                paddingFunction = "lpad";
                break;

            case SqlFunctionType.PadRight:
                paddingFunction = "rpad";
                break;

            default:
                throw new InvalidOperationException();
            }
            var operand = node.Arguments[0];
            var result  = SqlDml.Case();

            result.Add(
                SqlDml.CharLength(operand) < node.Arguments[1],
                SqlDml.FunctionCall(paddingFunction, node.Arguments));
            result.Else = operand;
            return(result);
        }
示例#2
0
        private SqlExpression GenericPad(SqlFunctionCall node)
        {
            var operand           = node.Arguments[0];
            var actualLength      = SqlDml.CharLength(operand);
            var requiredLength    = node.Arguments[1];
            var paddingExpression = node.Arguments.Count > 2
        ? SqlDml.FunctionCall("REPLICATE", node.Arguments[2], requiredLength - actualLength)
        : SqlDml.FunctionCall("SPACE", requiredLength - actualLength);
            SqlExpression resultExpression;

            switch (node.FunctionType)
            {
            case SqlFunctionType.PadLeft:
                resultExpression = paddingExpression + operand;
                break;

            case SqlFunctionType.PadRight:
                resultExpression = operand + paddingExpression;
                break;

            default:
                throw new InvalidOperationException();
            }
            var result = SqlDml.Case();

            result.Add(actualLength < requiredLength, resultExpression);
            result.Else = operand;
            return(result);
        }
示例#3
0
        public void SqlFunctionCallReplacingTest()
        {
            SqlFunctionCall fc          = SqlDml.CharLength(" text ");
            SqlFunctionCall fcReplacing = SqlDml.Substring("text", 0, 2);

            fc.ReplaceWith(fcReplacing);

            bool passed = false;

            try {
                fc.ReplaceWith(1);
            }
            catch {
                passed = true;
            }

            Assert.IsTrue(passed);
            Assert.AreNotEqual(fc, fcReplacing);
            Assert.AreEqual(fc.NodeType, fcReplacing.NodeType);
            Assert.AreEqual(fc.FunctionType, fcReplacing.FunctionType);
            Assert.AreEqual(fc.Arguments.Count, fcReplacing.Arguments.Count);
            for (int i = 0, l = fc.Arguments.Count; i < l; i++)
            {
                Assert.AreEqual(fc.Arguments[i], fcReplacing.Arguments[i]);
            }
        }
        public void SqlFunctionCallCloneTest()
        {
            {
                SqlFunctionCall fc      = SqlDml.FunctionCall("Function", 1, 2, 4);
                SqlFunctionCall fcClone = (SqlFunctionCall)fc.Clone();

                Assert.AreNotEqual(fc, fcClone);
                Assert.AreNotEqual(fc.Arguments, fcClone.Arguments);
                Assert.AreEqual(fc.NodeType, fcClone.NodeType);
                Assert.AreEqual(fc.Arguments.Count, fcClone.Arguments.Count);
                for (int i = 0, l = fc.Arguments.Count; i < l; i++)
                {
                    Assert.AreNotEqual(fc.Arguments[i], fcClone.Arguments[i]);
                    Assert.AreEqual(fc.Arguments[i].NodeType, fcClone.Arguments[i].NodeType);
                }
                Assert.AreEqual(fc.FunctionType, fcClone.FunctionType);
                Assert.AreEqual(((SqlUserFunctionCall)fc).Name, ((SqlUserFunctionCall)fcClone).Name);
            }
            Console.WriteLine();
            {
                SqlFunctionCall fc      = SqlDml.CharLength("string");
                SqlFunctionCall fcClone = (SqlFunctionCall)fc.Clone();

                Assert.AreNotEqual(fc, fcClone);
                Assert.AreNotEqual(fc.Arguments, fcClone.Arguments);
                for (int i = 0, l = fc.Arguments.Count; i < l; i++)
                {
                    Assert.AreNotEqual(fc.Arguments[i], fcClone.Arguments[i]);
                    Assert.AreEqual(fc.Arguments[i].NodeType, fcClone.Arguments[i].NodeType);
                }
                Assert.AreEqual(fc.NodeType, fcClone.NodeType);
                Assert.AreEqual(fc.Arguments.Count, fcClone.Arguments.Count);
                Assert.AreEqual(fc.FunctionType, fcClone.FunctionType);
            }
        }
示例#5
0
 public static SqlExpression StringInsert(SqlExpression _this,
                                          [Type(typeof(int))] SqlExpression startIndex,
                                          [Type(typeof(string))] SqlExpression value)
 {
     return(SqlDml.Concat(SqlDml.Concat(
                              SqlDml.Substring(_this, 0, startIndex), value),
                          SqlDml.Substring(_this, startIndex, SqlDml.CharLength(_this) - startIndex)));
 }
示例#6
0
        public void AddTest()
        {
            SqlLiteral <int> l1 = SqlDml.Literal(1);
            SqlLiteral <int> l2 = SqlDml.Literal(2);
            SqlBinary        b  = l1 + l2;

            Assert.AreEqual(b.NodeType, SqlNodeType.Add);

            b = b - ~l1;
            Assert.AreEqual(b.NodeType, SqlNodeType.Subtract);
            Assert.AreEqual(b.Right.NodeType, SqlNodeType.BitNot);

            SqlSelect s = SqlDml.Select();

            s.Columns.Add(1, "id");
            b = b / s;
            Assert.AreEqual(b.NodeType, SqlNodeType.Divide);
            Assert.AreEqual(b.Right.NodeType, SqlNodeType.SubSelect);

            SqlCast c = SqlDml.Cast(l1, SqlType.Decimal);

            Assert.AreEqual(c.NodeType, SqlNodeType.Cast);

            SqlFunctionCall l = SqlDml.CharLength(SqlDml.Literal("name"));

            b = c % l;
            Assert.AreEqual(b.NodeType, SqlNodeType.Modulo);
            Assert.AreEqual(b.Right.NodeType, SqlNodeType.FunctionCall);

            b = l1 * (-l2);
            Assert.AreEqual(b.NodeType, SqlNodeType.Multiply);
            Assert.AreEqual(b.Right.NodeType, SqlNodeType.Negate);

            SqlBatch    batch = SqlDml.Batch();
            SqlVariable v1    = SqlDml.Variable("v1", SqlType.Double);

            batch.Add(v1.Declare());
            batch.Add(SqlDml.Assign(v1, 1.0));
            s = SqlDml.Select();
            s.Columns.Add(b, "value");
            batch.Add(s);
        }
示例#7
0
        /// <inheritdoc/>
        public override void Visit(SqlFunctionCall node)
        {
            switch (node.FunctionType)
            {
            case SqlFunctionType.CharLength:
                (SqlDml.FunctionCall("DATALENGTH", node.Arguments) / 2).AcceptVisitor(this);
                return;

            case SqlFunctionType.PadLeft:
            case SqlFunctionType.PadRight:
                GenericPad(node).AcceptVisitor(this);
                return;

            case SqlFunctionType.Round:
                // Round should always be called with 2 arguments
                if (node.Arguments.Count == 1)
                {
                    Visit(SqlDml.FunctionCall(
                              translator.Translate(SqlFunctionType.Round),
                              node.Arguments[0],
                              SqlDml.Literal(0)));
                    return;
                }
                break;

            case SqlFunctionType.Truncate:
                // Truncate is implemented as round(arg, 0, 1) call in MSSQL.
                // It's stupid, isn't it?
                Visit(SqlDml.FunctionCall(
                          translator.Translate(SqlFunctionType.Round),
                          node.Arguments[0],
                          SqlDml.Literal(0),
                          SqlDml.Literal(1)));
                return;

            case SqlFunctionType.Substring:
                if (node.Arguments.Count == 2)
                {
                    node = SqlDml.Substring(node.Arguments[0], node.Arguments[1]);
                    SqlExpression len = SqlDml.CharLength(node.Arguments[0]);
                    node.Arguments.Add(len);
                    Visit(node);
                    return;
                }
                break;

            case SqlFunctionType.IntervalToMilliseconds:
                Visit(CastToLong(node.Arguments[0]) / NanosecondsPerMillisecond);
                return;

            case SqlFunctionType.IntervalConstruct:
            case SqlFunctionType.IntervalToNanoseconds:
                Visit(CastToLong(node.Arguments[0]));
                return;

            case SqlFunctionType.DateTimeAddMonths:
                Visit(DateAddMonth(node.Arguments[0], node.Arguments[1]));
                return;

            case SqlFunctionType.DateTimeAddYears:
                Visit(DateAddYear(node.Arguments[0], node.Arguments[1]));
                return;

            case SqlFunctionType.DateTimeTruncate:
                DateTimeTruncate(node.Arguments[0]).AcceptVisitor(this);
                return;

            case SqlFunctionType.DateTimeConstruct:
                Visit(DateAddDay(DateAddMonth(DateAddYear(SqlDml.Literal(new DateTime(2001, 1, 1)),
                                                          node.Arguments[0] - 2001),
                                              node.Arguments[1] - 1),
                                 node.Arguments[2] - 1));
                return;

            case SqlFunctionType.DateTimeToStringIso:
                Visit(DateTimeToStringIso(node.Arguments[0]));
                return;
            }

            base.Visit(node);
        }
示例#8
0
 public static SqlExpression Len([Type(typeof(string))] SqlExpression stringExpression)
 {
     return(SqlDml.CharLength(stringExpression));
 }
示例#9
0
 public static SqlExpression StringLength(SqlExpression _this)
 {
     return(SqlDml.CharLength(_this));
 }