Exemple #1
0
        static void Main(string[] args)
        {
            var temp    = typeof(DTS_Metadata).ToTemplate();
            var service = new TableTemplateService();

            System.Diagnostics.Debug.WriteLine(typeof(DTS_TableDefine).ToTemplate().CreateType().SQLGenerateCreateTable());
            System.Diagnostics.Debug.WriteLine(typeof(DTS_Template).ToTemplate().CreateType().SQLGenerateCreateTable());
            System.Diagnostics.Debug.WriteLine(typeof(DTS_TemplateDetail).ToTemplate().CreateType().SQLGenerateCreateTable());

            /*service.PostTableTemplate(typeof(DTS_TableDefine).ToTemplate());
             * service.PostTableTemplate(typeof(DTS_Template).ToTemplate());
             * service.PostTableTemplate(typeof(DTS_TemplateDetail).ToTemplate());*/
            Console.ReadKey();
            return;

            var s = "(Name %% \"rrr\\\"dddd\" or Id==1) or (Id>=100 or Name =% \"\")and IsDelete==false";

            Console.WriteLine(s);
            Console.WriteLine("-=解析=-");
            var c    = DynamicCondition.Parse(s);
            var exp  = c.GenerateExperssion <TableTemplate>();
            var db   = new TemplateModel();
            var list = db.TableTemplates.Where(exp);

            Console.WriteLine(list.ToString());
            Console.WriteLine("-=Linq=-");
            Console.WriteLine(exp);
            Console.ReadKey();

            /*for (int i = 0; i < 10000; i++)
             * {
             *  Console.WriteLine(i);
             *  DoSomething();
             * }*/
            return;

            var sql = typeof(IT_GeneralTable).SQLGenerateCreateTable("dddddddddddd");

            /*var db = new Model2();
             * var template = db.TableTemplates.FirstOrDefault(p=>p.Id == 3005);
             * var sql = template.SQLGenerateCreateTable(template.Name);*/
            Console.Write(sql);
            //Console.ReadKey();

            /*
             * var template = DynamicEntityService.GetTemplate(3005);
             * var type = template.CreateType("T_Metadata", new Type[] { typeof(IT_GeneralTable) });
             * var service1 = new DynamicEntityService( type, "T_Metadata");
             *
             * //var service = new DynamicEntityService(typeof(TableTemplate), "TableTemplate");
             * var list = service1.GetEntities();
             * var json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
             * Console.WriteLine(json);
             *
             * service1.PostEntity(Activator.CreateInstance(type));
             * list = service1.GetEntities();
             * json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
             */
            //Console.ReadKey();
        }
        public static DynamicCondition operator +(DynamicCondition condition1, DynamicCondition condition2)
        {
            DynamicCondition condition3 = new DynamicCondition();

            condition3.Operation = DynamicConditionOperation.Or;
            if (condition1.Operation == DynamicConditionOperation.Or)
            {
                condition3.Branchs.AddRange(condition1.Branchs);
            }
            else
            {
                condition3.Branchs.Add(condition1);
            }
            if (condition2.Operation == DynamicConditionOperation.Or)
            {
                condition3.Branchs.AddRange(condition2.Branchs);
            }
            else
            {
                condition3.Branchs.Add(condition2);
            }
            return(condition3);
        }
        static public Expression GenerateExperssion(this DynamicCondition condition, Expression param = null)
        {
            /*var param = Expression.Parameter(typeof(T));
             * var property = Expression.PropertyOrField(param, field);
             * var body = Expression.Equal(property, Expression.Constant(value, typeof(V)));
             * return Expression.Lambda<Func<T, bool>>(body, param);*/
            Expression body = null;
            Expression left = null;

            if (param == null)
            {
            }
            if (condition.Operation == DynamicConditionOperation.And)
            {
                foreach (var branch in condition.Branchs)
                {
                    if (left == null)
                    {
                        left = branch.GenerateExperssion(param);
                    }
                    else if (body == null)
                    {
                        body = Expression.AndAlso(left, branch.GenerateExperssion(param));
                    }
                    else
                    {
                        body = Expression.AndAlso(body, branch.GenerateExperssion(param));
                    }
                }
            }
            else if (condition.Operation == DynamicConditionOperation.Or)
            {
                foreach (var branch in condition.Branchs)
                {
                    if (left == null)
                    {
                        left = branch.GenerateExperssion(param);
                    }
                    else if (body == null)
                    {
                        body = Expression.OrElse(left, branch.GenerateExperssion(param));
                    }
                    else
                    {
                        body = Expression.OrElse(body, branch.GenerateExperssion(param));
                    }
                }
            }
            else
            {
                var property = Expression.PropertyOrField(param, condition.Field);
                switch (condition.Operation)
                {
                case DynamicConditionOperation.Eq:
                    body = Expression.Equal(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.NE:
                    body = Expression.NotEqual(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.Gt:
                    body = Expression.GreaterThan(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.GE:
                    body = Expression.GreaterThanOrEqual(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.Lt:
                    body = Expression.LessThan(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.LE:
                    body = Expression.LessThanOrEqual(property, Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.Ct:
                    //Expression<Func<String, bool>> exp = s => s.Contains((string)condition.Value);


                    body = Expression.Call(
                        property,
                        typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
                        Expression.Constant(condition.Value));

                    break;

                case DynamicConditionOperation.Sw:
                    body = Expression.Call(
                        property,
                        typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
                        Expression.Constant(condition.Value));
                    break;

                case DynamicConditionOperation.Ew:
                    body = Expression.Call(
                        property,
                        typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }),
                        Expression.Constant(condition.Value));
                    break;

                default:
                    throw new Exception();
                }
            }
            return(body);
        }
        static public Expression <Func <T, bool> > GenerateExperssion <T>(this DynamicCondition condition)
        {
            var param = Expression.Parameter(typeof(T));

            return(Expression.Lambda <Func <T, bool> >(condition.GenerateExperssion(param), param));
        }
 public static DynamicCondition Or(this DynamicCondition condition, string field, string operation, object value)
 {
     return(condition + new DynamicCondition(field, operation, value));
 }
 static public DynamicCondition Or(this DynamicCondition condition, DynamicCondition condition2)
 {
     return(condition + condition2);
 }
 public static DynamicCondition And(this DynamicCondition condition, string field, DynamicConditionOperation operation, object value)
 {
     return(condition * new DynamicCondition(field, operation, value));
 }
 static public DynamicCondition And(this DynamicCondition condition, DynamicCondition condition2)
 {
     return(condition * condition2);
 }