Example #1
0
        protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var result = SqlExpressionCompiler.Evaluate(expression);
            var inArgs = (result as IEnumerable).Flatten();

            sqlGenerate += "(";
            if (inArgs?.Count > 0)
            {
                foreach (var item in inArgs)
                {
                    sqlGenerate.AddDbParameter(item);
                    sqlGenerate += ",";
                }
                if (sqlGenerate.Sql.ToString().EndsWith(","))
                {
                    sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1);
                }
            }
            else
            {
                sqlGenerate += "NULL";
            }
            sqlGenerate += ")";
            return(sqlGenerate);
        }
Example #2
0
        protected override SqlGenerate Where(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            if (expression.Expression != null)
            {
                if (expression.Member.DeclaringType.IsNullableType())
                {
                    if (expression.Member.Name == "Value") //Can't use C# 6 yet: nameof(Nullable<bool>.Value)
                    {
                        SqlExpressionProvider.Where(expression.Expression, sqlGenerate);
                        return(sqlGenerate);
                    }
                    if (expression.Member.Name == "HasValue")
                    {
                        var doesNotEqualNull = Expression.MakeBinary(ExpressionType.NotEqual, expression.Expression, Expression.Constant(null));
                        SqlExpressionProvider.Where(doesNotEqualNull, sqlGenerate);
                        return(sqlGenerate);
                    }
                    throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
                }

                if (expression.IsParameterOrConvertAccess())
                {
                    sqlGenerate += $" {expression.Member.Name.ParamSql(sqlGenerate)}";
                    return(sqlGenerate);
                }
            }

            var val = SqlExpressionCompiler.Evaluate(expression);

            sqlGenerate.AddDbParameter(val);
            return(sqlGenerate);
        }
        private static void StartsWith(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            SqlExpressionProvider.Where(expression.Arguments[0], sqlGenerate);
            sqlGenerate += " like ";

            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"{val}%");
        }
Example #4
0
        protected override SqlGenerate Update(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var obj = SqlExpressionCompiler.Evaluate(expression);

            if (obj == null)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
            }

            if (obj.GetType().IsValueType)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported valuetype");
            }

            if (obj is IDictionary)
            {
                IDictionary dictionary = (IDictionary)obj;
                foreach (string key in dictionary.Keys)
                {
                    sqlGenerate += $"{key.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(dictionary[key]);
                    sqlGenerate += ",";
                }
            }
            else
            {
                var pis = obj.GetType().GetProperties();
                foreach (var p in pis)
                {
                    sqlGenerate += $"{p.Name.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(p.GetValue(obj));
                    sqlGenerate += ",";
                }
            }

            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Example #5
0
        protected override SqlGenerate Update(NewExpression expression, SqlGenerate sqlGenerate)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                var m = expression.Members[i];
                sqlGenerate += $"{m.Name.ParamSql(sqlGenerate)} = ";

                var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]);
                sqlGenerate.AddDbParameter(val);
                sqlGenerate += ",";
            }
            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
        private static void Contains(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            if (IsStaticArrayMethod(expression))
            {
                DoStaticArrayMethodCall(expression, sqlGenerate);
                return;
            }
            if (IsEnumerableMethod(expression))
            {
                DoEnumerableMethodCall(expression, sqlGenerate);
                return;
            }

            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            sqlGenerate += " like ";
            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"%{val}%");
        }
 protected override SqlGenerate Where(ConstantExpression expression, SqlGenerate sqlGenerate)
 {
     sqlGenerate.AddDbParameter(expression.Value);
     return(sqlGenerate);
 }