Esempio n. 1
0
        public void WhereOperationAdd()
        {
            var c = new ObservableCollection<NotificationObject<string>>()
            {
                new NotificationObject<string>() { Value1 = "Test1" },
                new NotificationObject<string>() { Value1 = "Test2" },
                new NotificationObject<string>() { Value1 = "Test3" },
            };

            var op = new WhereOperation<NotificationObject<string>>(new OperationContext(),
                Expression.Call(
                    typeof(Queryable).GetMethods()
                        .Where(i => i.Name == "Where")
                        .Where(i => i.IsGenericMethodDefinition)
                        .Where(i => i.GetGenericArguments().Length == 1)
                        .Select(i => i.MakeGenericMethod(typeof(NotificationObject<string>)))
                        .Where(i => i.GetParameters().Length == 2)
                        .Where(i => i.GetParameters()[1].ParameterType == typeof(Expression<Func<NotificationObject<string>, bool>>))
                        .Single(),
                    new ObservableQuery<NotificationObject<string>>(c).Expression,
                    Expression.Lambda<Func<NotificationObject<string>, bool>>(
                        Expression.Constant(true),
                        Expression.Parameter(typeof(NotificationObject<string>), "i"))));
            Assert.AreEqual(3, op.Value.Count());

            c.Add(new NotificationObject<string>() { Value1 = "Test4" });
            Assert.AreEqual(4, op.Value.Count());
        }
Esempio n. 2
0
 //internal WhereCondition()
 //{
 //}
 public WhereCondition(string field, WhereOperation operation, object value)
 {
     Field = field;
     Operation = operation;
     if (value != null)
     {
         Value = operation == WhereOperation.In ? value : value.ToString().Replace("'", "''").Replace("_", "\\_");
     }
 }
Esempio n. 3
0
        public void WhereOperationPredicate()
        {
            var c = new ObservableCollection<NotificationObject<string>>()
            {
                new NotificationObject<string>() { Value1 = "False" },
                new NotificationObject<string>() { Value1 = "True" },
                new NotificationObject<string>() { Value1 = "False" },
            };

            var op = new WhereOperation<NotificationObject<string>>(new OperationContext(),
                Expression.Call(
                    typeof(Queryable).GetMethods()
                        .Where(i => i.Name == "Where")
                        .Where(i => i.IsGenericMethodDefinition)
                        .Where(i => i.GetGenericArguments().Length == 1)
                        .Select(i => i.MakeGenericMethod(typeof(NotificationObject<string>)))
                        .Where(i => i.GetParameters().Length == 2)
                        .Where(i => i.GetParameters()[1].ParameterType == typeof(Expression<Func<NotificationObject<string>, bool>>))
                        .Single(),
                    new ObservableQuery<NotificationObject<string>>(c).Expression,
                    Expression.Lambda<Func<NotificationObject<string>, bool>>(
                        Expression.Equal(
                            Expression.MakeMemberAccess(
                                Expression.Parameter(typeof(NotificationObject<string>), "i"),
                                typeof(NotificationObject<string>).GetProperty("Value1")),
                            Expression.Constant("True", typeof(string))),
                        Expression.Parameter(typeof(NotificationObject<string>), "i"))));
            Assert.AreEqual(1, op.Value.Count());

            c.Add(new NotificationObject<string>() { Value1 = "False" });
            Assert.AreEqual(1, op.Value.Count());

            c.Add(new NotificationObject<string>() { Value1 = "True" });
            Assert.AreEqual(2, op.Value.Count());

            c.Add(new NotificationObject<string>() { Value1 = "True" });
            Assert.AreEqual(3, op.Value.Count());

            c.RemoveAt(1);
            Assert.AreEqual(2, op.Value.Count());

            c[0].Value1 = "True";
            Assert.AreEqual(3, op.Value.Count());

            c[1].Value1 = "True";
            Assert.AreEqual(4, op.Value.Count());
        }
Esempio n. 4
0
        public static IQueryable <T> Where <T>(this IQueryable <T> query,
                                               string column, object value, WhereOperation operation)
        {
            if (string.IsNullOrEmpty(column))
            {
                return(query);
            }

            ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");

            MemberExpression memberAccess = null;

            foreach (var property in column.Split('.'))
            {
                memberAccess = MemberExpression.Property
                                   (memberAccess ?? (parameter as Expression), property);
            }

            //change param value type
            //necessary to getting bool from string
            ConstantExpression filter = Expression.Constant
                                        (
                Convert.ChangeType(value, memberAccess.Type)
                                        );

            //switch operation
            Expression       condition = null;
            LambdaExpression lambda    = null;

            switch (operation)
            {
            //equal ==
            case WhereOperation.Equal:
                condition = Expression.Equal(memberAccess, filter);
                lambda    = Expression.Lambda(condition, parameter);
                break;

            //not equal !=
            case WhereOperation.NotEqual:
                condition = Expression.NotEqual(memberAccess, filter);
                lambda    = Expression.Lambda(condition, parameter);
                break;

            //string.Contains()
            case WhereOperation.Contains:
                condition = Expression.Call(memberAccess,
                                            typeof(string).GetMethod("Contains"),
                                            Expression.Constant(value));
                lambda = Expression.Lambda(condition, parameter);
                break;
            }


            MethodCallExpression result = Expression.Call(
                typeof(Queryable), "Where",
                new[] { query.ElementType },
                query.Expression,
                lambda);

            return(query.Provider.CreateQuery <T>(result));
        }
Esempio n. 5
0
 /// <summary>
 /// Inner Join。Lambda写法:.InnerJoin&lt;Model2>((a, b) => a.ID == b.ID)
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="where"></param>
 /// <param name="asName"></param>
 /// <param name="asName2"></param>
 /// <returns></returns>
 public Search <T> InnerJoin <TEntity>(WhereOperation where, string asName = "", string asName2 = "")
     where TEntity : Entity
 {
     return(Join(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>(), where, JoinType.InnerJoin));
 }
 public TextualWhereCondition(WhereOperation op, string column, object value)
 {
     WhereOperation = op;
     Column         = column;
     Value          = value;
 }
Esempio n. 7
0
 /// <summary>
 /// Having
 /// </summary>
 public new Search <T> Having(WhereOperation havingWhere)
 {
     return((Search <T>)base.Having(havingWhere));
 }
Esempio n. 8
0
 /// <summary>
 /// whereclip
 /// </summary>
 public new Search <T> Where(WhereOperation where)
 {
     return((Search <T>)base.Where(where));
 }
Esempio n. 9
0
 /// <summary>
 /// 连接
 /// </summary>
 /// <param name="tableName"></param>
 /// <param name="userName"></param>
 /// <param name="where"></param>
 /// <param name="joinType"></param>
 /// <returns></returns>
 private Search <T> Join(string tableName, string userName, WhereOperation where, JoinType joinType)
 {
     return((Search <T>)base.join(tableName, userName, where, joinType));
 }
Esempio n. 10
0
 /// <summary>
 /// Full Join
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="where"></param>
 /// <returns></returns>
 public Search <T> FullJoin <TEntity>(WhereOperation where)
     where TEntity : Entity
 {
     return(Join(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>(), where, JoinType.FullJoin));
 }
Esempio n. 11
0
        /// <summary>
        /// 创建更新DbCommand
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DbCommand CreateUpdateCommand <TEntity>(Field[] fields, object[] values, WhereOperation where)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            if (null == fields || fields.Length == 0 || null == values || values.Length == 0)
            {
                return(null);
            }

            Check.Require(fields.Length == values.Length, "fields.Length must be equal values.Length");

            var length = fields.Length;

            if (WhereOperation.IsNullOrEmpty(where))
            {
                where = WhereOperation.All;
            }

            var sql = new StringBuilder();

            sql.Append("UPDATE ");
            sql.Append(db.DbProvider.BuildTableName(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>()));
            sql.Append(" SET ");

            var identityField = EntityCache.GetIdentityField <TEntity>();
            var identityExist = !Field.IsNullOrEmpty(identityField);
            var list          = new List <Parameter>();
            var colums        = new StringBuilder();

            for (var i = 0; i < length; i++)
            {
                if (identityExist)
                {
                    //标识列  排除
                    if (fields[i].PropertyName.Equals(identityField.PropertyName))
                    {
                        continue;
                    }
                }

                colums.Append(",");
                colums.Append(fields[i].FieldName);
                colums.Append("=");

                if (values[i] is Expression)
                {
                    var expression = (Expression)values[i];
                    colums.Append(expression);
                    list.AddRange(expression.Parameters);
                }
                else if (values[i] is Field)
                {
                    var fieldValue = (Field)values[i];
                    colums.Append(fieldValue.TableFieldName);
                }
                else
                {
                    var pname = DataUtils.MakeUniqueKey(fields[i]);
                    //var pname = string.Concat("@", fields[i].Name, i);
                    colums.Append(pname);
                    var p = new Parameter(pname, values[i], fields[i].ParameterDbType, fields[i].ParameterSize);
                    list.Add(p);
                }
            }
            sql.Append(colums.ToString().Substring(1));
            sql.Append(where.WhereString);
            list.AddRange(where.Parameters);

            var cmd = db.GetSqlStringCommand(sql.ToString());

            db.AddCommandParameter(cmd, list.ToArray());
            return(cmd);
        }
Esempio n. 12
0
 /// <summary>
 /// 创建删除DbCommand
 /// </summary>
 /// <param name="where"></param>
 /// <returns></returns>
 public DbCommand CreateDeleteCommand <TEntity>(WhereOperation where)
     where TEntity : Entity
 {
     return(CreateDeleteCommand(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>(), where));
 }
        public static IQueryable <T> Where <T>(this IQueryable <T> query,
                                               string column, object value, WhereOperation operation)
        {
            if (string.IsNullOrEmpty(column))
            {
                return(query);
            }

            var parameter = Expression.Parameter(query.ElementType, "p");

            var memberAccess = column.Split('.').Aggregate <string, MemberExpression>(null, (current, property) => Expression.Property(current ?? (parameter as Expression), property));

            //change param value type necessary to getting bool from string
            ConstantExpression filter = null;

            try
            {
                filter = Expression.Constant
                         (
                    Convert.ChangeType(value, memberAccess.Type, CultureInfo.InvariantCulture)
                         );
            }
            catch
            {
                return(query);
            }

            //switch operation
            LambdaExpression lambda = null;

            try
            {
                Expression condition = null;
                switch (operation)
                {
                case WhereOperation.Equal:
                    condition = Expression.Equal(memberAccess, filter);
                    lambda    = Expression.Lambda(condition, parameter);
                    break;

                case WhereOperation.NotEqual:
                    condition = Expression.NotEqual(memberAccess, filter);
                    lambda    = Expression.Lambda(condition, parameter);
                    break;

                case WhereOperation.Contains:
                    condition = Expression.Call(memberAccess,
                                                typeof(string).GetMethod("Contains"),
                                                Expression.Constant(value));
                    lambda = Expression.Lambda(condition, parameter);
                    break;
                }
            }
            catch
            {
                return(query);
            }

            var result = Expression.Call(
                typeof(Queryable), "Where",
                new[] { query.ElementType },
                query.Expression,
                lambda);

            return(query.Provider.CreateQuery <T>(result));
        }