ResolveExpression() публичный Метод

解析表达式
public ResolveExpression ( ResolveExpress re, Expression exp, SqlSugarClient db ) : void
re ResolveExpress 当前解析对象
exp System.Linq.Expressions.Expression 要解析的表达式
db SqlSugarClient 数据库访问对象
Результат void
Пример #1
2
        protected void Page_Load(object sender, EventArgs e)
        {
            PerformanceTest pt = new PerformanceTest();
            pt.SetCount(10000);//设置循环次数
            Models.Student ss = new Models.Student() { id=1 };
            pt.Execute(i =>
            {
                ResolveExpress r = new ResolveExpress();
                Expression<Func<Models.InsertTest, bool>> func = x => x.id>ss.id;
                r.ResolveExpression(r,func);

            }, m => { }, "lambda");

         
            //输出测试页面
            GridView gv = new GridView();
            gv.DataSource = pt.GetChartSource();
            gv.DataBind();
            Form.Controls.Add(gv);
        }
Пример #2
0
        /// <summary>
        /// 假删除,根据表达示
        /// 使用说明::
        /// FalseDelete《T》(new int[]{1,2,3})或者Delete《T》(3)
        /// </summary>
        /// <param name="field">更新删除状态字段</param>
        /// <param name="expression">筛选表达示</param>
        public bool FalseDelete <T>(string field, Expression <Func <T, bool> > expression)
        {
            Type   type     = typeof(T);
            string typeName = type.Name;

            typeName = GetTableNameByClassType(typeName);
            //属性缓存
            string cachePropertiesKey     = "db." + typeName + ".GetProperties";
            var    cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance();

            PropertyInfo[] props = null;
            if (cachePropertiesManager.ContainsKey(cachePropertiesKey))
            {
                props = cachePropertiesManager[cachePropertiesKey];
            }
            else
            {
                props = type.GetProperties();
                cachePropertiesManager.Add(cachePropertiesKey, props, cachePropertiesManager.Day);
            }
            bool           isSuccess = false;
            ResolveExpress re        = new ResolveExpress();

            re.ResolveExpression(re, expression);
            string sql            = string.Format("UPDATE  {0} SET {1}=1 WHERE  1=1 {2}", typeName, field, re.SqlWhere);
            int    deleteRowCount = ExecuteCommand(sql, re.Paras.ToArray());

            isSuccess = deleteRowCount > 0;
            return(isSuccess);
        }
Пример #3
0
        /// <summary>
        /// 更新
        /// 注意:rowObj为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列
        /// 使用说明:sqlSugar.Update《T》(rowObj,whereObj);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rowObj">new T(){name="张三",sex="男"}或者new {name="张三",sex="男"}</param>
        /// <param name="expression">it.id=100</param>
        /// <returns></returns>
        public bool Update <T>(object rowObj, Expression <Func <T, bool> > expression) where T : class
        {
            if (rowObj == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.rowObj");
            }
            if (expression == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.expression");
            }


            Type   type     = typeof(T);
            string typeName = type.Name;

            typeName = GetTableNameByClassType(typeName);
            StringBuilder sbSql  = new StringBuilder(string.Format(" UPDATE {0} SET ", typeName));
            var           rows   = SqlSugarTool.GetParameters(rowObj);
            string        pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName);

            foreach (var r in rows)
            {
                if (pkName == r.ParameterName.TrimStart('@'))
                {
                    if (rowObj.GetType() == type)
                    {
                        continue;
                    }
                }
                sbSql.Append(string.Format(" [{0}] =@{0}  ,", r.ParameterName.TrimStart('@')));
            }
            sbSql.Remove(sbSql.Length - 1, 1);
            sbSql.Append(" WHERE  1=1  ");
            ResolveExpress re = new ResolveExpress();

            re.ResolveExpression(re, expression);
            sbSql.Append(re.SqlWhere);;

            List <SqlParameter> parsList = new List <SqlParameter>();

            parsList.AddRange(re.Paras);
            var pars = rows;

            if (pars != null)
            {
                foreach (var par in pars)
                {
                    if (par.SqlDbType == SqlDbType.Udt)
                    {
                        par.UdtTypeName = "HIERARCHYID";
                    }
                    parsList.Add(par);
                }
            }
            var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray());

            return(updateRowCount > 0);
        }
Пример #4
0
        /// <summary>
        /// 条件筛选
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static SqlSugar.Queryable <T> Where <T>(this SqlSugar.Queryable <T> queryable, Expression <Func <T, bool> > expression)
        {
            var            type = queryable.Type;
            ResolveExpress re   = new ResolveExpress();

            re.ResolveExpression(re, expression);
            queryable.Params.AddRange(re.Paras);
            queryable.Where.Add(re.SqlWhere);
            return(queryable);
        }
Пример #5
0
        /// <summary>
        /// 删除,根据表达示
        /// 使用说明:
        /// Delete《T》(it=>it.id=100) 或者Delete《T》(3)
        /// </summary>
        /// <param name="expression">筛选表达示</param>
        public bool Delete <T>(Expression <Func <T, bool> > expression)
        {
            Type           type = typeof(T);
            ResolveExpress re   = new ResolveExpress();

            re.ResolveExpression(re, expression);
            string sql       = string.Format("DELETE FROM {0} WHERE 1=1 {1}", type.Name, re.SqlWhere);
            bool   isSuccess = ExecuteCommand(sql, re.Paras.ToArray()) > 0;

            return(isSuccess);
        }
        /// <summary>
        ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool Any <T>(this Queryable <T> queryable, Expression <Func <T, bool> > expression)
        {
            var type = queryable.Type;

            queryable.WhereIndex = queryable.WhereIndex + 100;
            ResolveExpress re = new ResolveExpress(queryable.WhereIndex);

            re.ResolveExpression(re, expression);
            queryable.Where.Add(re.SqlWhere);
            queryable.Params.AddRange(re.Paras);
            return(queryable.Count() > 0);
        }
        /// <summary>
        /// 返回序列中的第一个元素,如果序列为NULL返回default(T)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static T FirstOrDefault <T>(this Queryable <T> queryable, Expression <Func <T, bool> > expression)
        {
            var type = queryable.Type;

            queryable.WhereIndex = queryable.WhereIndex + 100;
            ResolveExpress re = new ResolveExpress(queryable.WhereIndex);

            re.ResolveExpression(re, expression);
            queryable.Where.Add(re.SqlWhere);
            queryable.Params.AddRange(re.Paras);
            return(FirstOrDefault <T>(queryable));
        }
Пример #8
0
        /// <summary>
        ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,否则返回null。
        /// </summary>
        /// <typeparam name="T">表实体类型</typeparam>
        /// <param name="queryable">查询对象</param>
        /// <param name="expression">表达式条件</param>
        /// <returns>T</returns>
        public static T SingleOrDefault <T>(this  Queryable <T> queryable, Expression <Func <T, bool> > expression)
        {
            var type = queryable.Type;

            queryable.WhereIndex = queryable.WhereIndex + 100;
            ResolveExpress re = new ResolveExpress(queryable.WhereIndex);

            re.ResolveExpression(re, expression, queryable.DB);
            queryable.WhereValue.Add(re.SqlWhere);
            queryable.Params.AddRange(re.Paras);
            return(queryable.ToList().SingleOrDefault());
        }
        /// <summary>
        /// 条件筛选
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Queryable <T> Where <T, T2, T3>(this Queryable <T> queryable, Expression <Func <T, T2, T3, object> > expression)
        {
            var type = queryable.Type;

            queryable.WhereIndex = queryable.WhereIndex + 100;
            ResolveExpress re = new ResolveExpress(queryable.WhereIndex);

            re.Type = ResolveExpressType.nT;
            re.ResolveExpression(re, expression);
            queryable.Params.AddRange(re.Paras);
            queryable.Where.Add(re.SqlWhere);
            return(queryable);
        }
        /// <summary>
        /// 条件筛选
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Queryable <T> Where <T>(this Queryable <T> queryable, Expression <Func <T, bool> > expression)
        {
            var type = queryable.Type;

            queryable.WhereIndex = queryable.WhereIndex + 100;
            ResolveExpress re = new ResolveExpress(queryable.WhereIndex);

            if (queryable.JoinTable.IsValuable())
            {
                re.Type = ResolveExpressType.nT;
            }
            re.ResolveExpression(re, expression);
            queryable.Params.AddRange(re.Paras);
            queryable.Where.Add(re.SqlWhere);
            return(queryable);
        }
Пример #11
0
        /// <summary>
        /// 联表查询
        /// </summary>
        /// <typeparam name="T">第一个表的对象</typeparam>
        /// <typeparam name="T2">联接表的对象</typeparam>
        /// <typeparam name="T3">联接表的对象</typeparam>
        /// <param name="queryable">查询对象</param>
        /// <param name="expression">条件表达式</param>
        /// <param name="type">Join的类型</param>
        /// <returns>Queryable</returns>
        public static Queryable <T> JoinTable <T, T2, T3>(this Queryable <T> queryable, Expression <Func <T, T2, T3, object> > expression, JoinType type = JoinType.LEFT)
        {
            queryable.DB.InitAttributes <T3>();

            ResolveExpress re = new ResolveExpress();

            queryable.WhereIndex = queryable.WhereIndex + 100;
            re.Type = ResolveExpressType.nT;
            var exLeftStr   = Regex.Match(expression.ToString(), @"\((.+?)\).+").Groups[1].Value;
            var exLeftArray = exLeftStr.Split(',');
            var shortName1  = exLeftArray[1];
            var shortName2  = exLeftArray[2];

            re.ResolveExpression(re, expression, queryable.DB);
            string joinTypeName  = type.ToString();
            string joinTableName = null;

            if (queryable.DB._mappingTableList.IsValuable())
            {
                //别名表
                if (queryable.DB._mappingTableList.IsValuable())
                {
                    string name = typeof(T3).Name;
                    if (queryable.DB._mappingTableList.Any(it => it.Key == name))
                    {
                        joinTableName = queryable.DB._mappingTableList.First(it => it.Key == name).Value;
                    }
                }
            }
            if (joinTableName.IsNullOrEmpty())
            {
                joinTableName = typeof(T3).Name;
            }
            string joinStr = string.Format(" {0} JOIN {1} {2} ON {3}  ",
                                           /*0*/ queryable.JoinTableValue.Count == 0 ? (" " + shortName1 + " " + joinTypeName) : joinTypeName.ToString(),
                                           /*1*/ joinTableName.GetTranslationSqlName(),
                                           /*2*/ shortName2,
                                           /*3*/ re.SqlWhere.Trim().TrimStart('A').TrimStart('N').TrimStart('D')
                                           );

            queryable.JoinTableValue.Add(joinStr);
            queryable.Params.AddRange(re.Paras);
            return(queryable);
        }
Пример #12
0
        /// <summary>
        /// 更新
        /// 注意:rowObj为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列
        /// 使用说明:sqlSugar.Update《T》(rowObj,whereObj);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rowObj">new T(){name="张三",sex="男"}或者new {name="张三",sex="男"}</param>
        /// <param name="expression">new {id=100}</param>
        /// <returns></returns>
        public bool Update <T>(object rowObj, Expression <Func <T, bool> > expression) where T : class
        {
            if (rowObj == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.rowObj");
            }
            if (expression == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.expression");
            }


            Type          type  = typeof(T);
            StringBuilder sbSql = new StringBuilder(string.Format(" UPDATE {0} SET ", type.Name));
            Dictionary <string, string> rows = SqlSugarTool.GetObjectToDictionary(rowObj);
            string pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, type.Name);

            foreach (var r in rows)
            {
                if (pkName == r.Key)
                {
                    if (rowObj.GetType() == type)
                    {
                        continue;
                    }
                }
                sbSql.Append(string.Format(" {0} =@{0}  ,", r.Key));
            }
            sbSql.Remove(sbSql.Length - 1, 1);
            sbSql.Append(" WHERE  1=1  ");
            ResolveExpress re = new ResolveExpress();

            re.ResolveExpression(re, expression);
            sbSql.Append(re.SqlWhere);;

            List <SqlParameter> parsList = new List <SqlParameter>();

            parsList.AddRange(re.Paras);
            parsList.AddRange(rows.Select(c => new SqlParameter("@" + c.Key, c.Value)));
            var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray());

            return(updateRowCount > 0);
        }
        /// <summary>
        /// 联表查询
        /// </summary>
        /// <typeparam name="T">第一个表的对象</typeparam>
        /// <typeparam name="T2">联接的表对象</typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression">表达示</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Queryable <T> JoinTable <T, T2, T3>(this Queryable <T> queryable, Expression <Func <T, T2, T3, object> > expression, JoinType type = JoinType.LEFT)
        {
            ResolveExpress re = new ResolveExpress();

            queryable.WhereIndex = queryable.WhereIndex + 100;
            re.Type = ResolveExpressType.nT;
            var exLeftStr   = Regex.Match(expression.ToString(), @"\((.+?)\).+").Groups[1].Value;
            var exLeftArray = exLeftStr.Split(',');
            var shortName1  = exLeftArray[1];
            var shortName2  = exLeftArray[2];

            re.ResolveExpression(re, expression);
            string joinTableName = type.ToString();
            string joinStr       = string.Format(" {0} JOIN {1} {2} ON {3}  ",
                                                 /*0*/ queryable.JoinTable.Count == 0 ? (" " + shortName1 + " " + joinTableName) : joinTableName.ToString(),
                                                 /*1*/ typeof(T3).Name,
                                                 /*2*/ shortName2,
                                                 /*3*/ re.SqlWhere.Trim().TrimStart('A').TrimStart('N').TrimStart('D')
                                                 );

            queryable.JoinTable.Add(joinStr);
            queryable.Params.AddRange(re.Paras);
            return(queryable);
        }
Пример #14
0
        /// <summary>
        /// 更新
        /// 注意:rowObj为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列
        /// 使用说明:sqlSugar.Update《T》(rowObj,whereObj);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rowObj">new T(){name="张三",sex="男"}或者new {name="张三",sex="男"}</param>
        /// <param name="expression">it.id=100</param>
        /// <returns></returns>
        public bool Update <T>(object rowObj, Expression <Func <T, bool> > expression) where T : class
        {
            if (rowObj == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.rowObj");
            }
            if (expression == null)
            {
                throw new ArgumentNullException("SqlSugarClient.Update.expression");
            }


            Type   type     = typeof(T);
            string typeName = type.Name;

            typeName = GetTableNameByClassType(typeName);
            var rows = SqlSugarTool.GetParameters(rowObj);

            //sql语句缓存
            string cacheSqlKey     = "db.update." + typeName + rows.Length;
            var    cacheSqlManager = CacheManager <StringBuilder> .GetInstance();



            string pkName        = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName);
            var    identityNames = SqlSugarTool.GetIdentitiesKeyByTableName(this, typeName);


            ResolveExpress re = new ResolveExpress();

            re.ResolveExpression(re, expression);


            StringBuilder sbSql = new StringBuilder();

            if (cacheSqlManager.ContainsKey(cacheSqlKey))
            {
                sbSql = cacheSqlManager[cacheSqlKey];
            }
            else
            {
                sbSql = new StringBuilder(string.Format(" UPDATE [{0}] SET ", typeName));
                foreach (var r in rows)
                {
                    var name                   = r.ParameterName.TrimStart('@');
                    var isPk                   = pkName != null && pkName.ToLower() == name.ToLower();
                    var isIdentity             = identityNames.Any(it => it.Value.ToLower() == name.ToLower());
                    var isDisableUpdateColumns = DisableUpdateColumns != null && DisableUpdateColumns.Any(it => it.ToLower() == name.ToLower());
                    if (isPk || isIdentity || isDisableUpdateColumns)
                    {
                        if (rowObj.GetType() == type)
                        {
                            continue;
                        }
                    }
                    sbSql.Append(string.Format(" [{0}] =@{0}  ,", name));
                }
                sbSql.Remove(sbSql.Length - 1, 1);
                sbSql.Append(" WHERE  1=1  ");
                sbSql.Append(re.SqlWhere);;
                cacheSqlManager.Add(cacheSqlKey, sbSql, cacheSqlManager.Day);
            }

            List <SqlParameter> parsList = new List <SqlParameter>();

            parsList.AddRange(re.Paras);
            var pars = rows;

            if (pars != null)
            {
                foreach (var par in pars)
                {
                    if (par.SqlDbType == SqlDbType.Udt)
                    {
                        par.UdtTypeName = "HIERARCHYID";
                    }
                    SqlSugarTool.SetParSize(par);
                    parsList.Add(par);
                }
            }
            try
            {
                var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray());
                return(updateRowCount > 0);
            }
            catch (Exception ex)
            {
                var cacheManager = CacheManager <string> .GetInstance();

                cacheManager.RemoveAll(it => it.Contains("KeyBy"));
                throw new Exception("sql:" + sbSql.ToString() + "\n" + ex.Message);
            }
        }