Пример #1
0
        public static void PrepareNamedParameters(string paramName1, object value1, string paramName2, object value2)
        {
            var input1 = SqlObject.New(SqlValueUtil.FromObject(value1));
            var input2 = SqlObject.New(SqlValueUtil.FromObject(value2));

            var query = new SqlCommand($"SELECT * FROM a WHERE a.col1 = {paramName1} AND a.col2 = {paramName2}", SqlParameterNaming.Named);

            query.Parameters.Add(new SqlParameter(paramName1, input1));
            query.Parameters.Add(new SqlParameter(paramName2, input2));

            var preparer = query.ExpressionPreparer;

            Assert.NotNull(preparer);

            var param1 = SqlExpression.Variable(paramName1);
            var param2 = SqlExpression.Variable(paramName2);

            Assert.True(preparer.CanPrepare(param1));
            Assert.True(preparer.CanPrepare(param2));

            var exp1 = preparer.Prepare(param1);
            var exp2 = preparer.Prepare(param2);

            Assert.NotNull(exp1);
            Assert.NotNull(exp2);

            Assert.IsType <SqlConstantExpression>(exp1);
            Assert.IsType <SqlConstantExpression>(exp2);

            Assert.Equal(input1, ((SqlConstantExpression)exp1).Value);
            Assert.Equal(input2, ((SqlConstantExpression)exp2).Value);
        }
Пример #2
0
        public static void PrepareMarkerParameters(object value1, object value2)
        {
            var input1 = SqlObject.New(SqlValueUtil.FromObject(value1));
            var input2 = SqlObject.New(SqlValueUtil.FromObject(value2));

            var query = new SqlCommand("SELECT * FROM a WHERE a.col1 = ? AND a.col2 = ?", SqlParameterNaming.Marker);

            query.Parameters.Add(new SqlParameter("?", input1));
            query.Parameters.Add(new SqlParameter("?", input2));

            var preparer = query.ExpressionPreparer;

            Assert.NotNull(preparer);

            var param1 = SqlExpression.Parameter();
            var param2 = SqlExpression.Parameter();

            Assert.True(preparer.CanPrepare(param1));
            Assert.True(preparer.CanPrepare(param2));

            var exp1 = preparer.Prepare(param1);
            var exp2 = preparer.Prepare(param2);

            Assert.NotNull(exp1);
            Assert.NotNull(exp2);

            Assert.IsType <SqlConstantExpression>(exp1);
            Assert.IsType <SqlConstantExpression>(exp2);

            Assert.Equal(input1, ((SqlConstantExpression)exp1).Value);
            Assert.Equal(input2, ((SqlConstantExpression)exp2).Value);
        }
Пример #3
0
        private static SqlExpression NewExpression(object value)
        {
            var sqlValue = SqlValueUtil.FromObject(value);
            var obj      = SqlObject.New(sqlValue);

            return(SqlExpression.Constant(obj));
        }
Пример #4
0
        public static void EnumerateItems(params object[] values)
        {
            var array = new SqlArray(values.Select(NewExpression).ToArray());

            Assert.Equal(values.Length, array.Length);
            Assert.True(array.Length > 0);

            var expected = SqlObject.New(SqlValueUtil.FromObject(values[0]));
            var first    = array.First();

            Assert.IsType <SqlConstantExpression>(first);

            var itemValue = ((SqlConstantExpression)first).Value;

            Assert.Equal(expected, itemValue);
        }
Пример #5
0
        public static void Create(params object[] values)
        {
            var array = new SqlArray(values.Select(NewExpression).ToArray());

            Assert.Equal(values.Length, array.Length);
            Assert.True(array.Length > 0);

            var expected = SqlObject.New(SqlValueUtil.FromObject(values[0]));
            var item     = array[0];

            Assert.IsType <SqlConstantExpression>(item);

            var itemValue = ((SqlConstantExpression)item).Value;

            Assert.Equal(expected, itemValue);
        }