Esempio n. 1
0
        /// <summary>
        /// 条件筛选 例如:expression 为 it=>it.a  inValues值为 new string[]{"a" ,"b"} 生成的SQL就是  a in('a','b')
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Queryable <T> In <T, FieldType>(this Queryable <T> queryable, Expression <Func <T, object> > expression, List <FieldType> inValues)
        {
            ResolveExpress re          = new ResolveExpress();
            var            InFieldName = re.GetExpressionRightFiled(expression);

            return(In <T, FieldType>(queryable, InFieldName, inValues));
        }
Esempio n. 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);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取最小值
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="minField">列</param>
        /// <returns></returns>
        public static object Min <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression)
        {
            ResolveExpress re       = new ResolveExpress();
            var            minField = re.GetExpressionRightFiled(expression);

            return(Min <T, object>(queryable, minField));
        }
Esempio n. 4
0
        /// <summary>
        /// 分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="groupFileds">如:id,name </param>
        /// <returns></returns>
        public static Queryable <T> GroupBy <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression)
        {
            ResolveExpress re    = new ResolveExpress();
            var            field = re.GetExpressionRightFiled(expression);
            var            pre   = queryable.GroupBy.IsValuable() ? "," : "";

            queryable.GroupBy += pre + field;
            return(queryable);
        }
Esempio n. 5
0
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="orderFileds">如:id asc,name desc </param>
        /// <returns></returns>
        public static Queryable <T> OrderBy <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression, OrderByType type = OrderByType.asc)
        {
            ResolveExpress re    = new ResolveExpress();
            var            field = re.GetExpressionRightFiled(expression);
            var            pre   = queryable.OrderBy.IsValuable() ? "," : "";

            queryable.OrderBy += pre + field + " " + type.ToString().ToUpper();
            return(queryable);
        }
Esempio n. 6
0
        /// <summary>
        /// 条件筛选 例如:InFieldName 为 a inValues 值为 new string[]{"a" ,"b"} 生成的SQL就是  a in('a','b')
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Queryable <T> In <T, FieldType>(this Queryable <T> queryable, string InFieldName, params FieldType[] inValues)
        {
            var type = queryable.Type;

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

            queryable.Where.Add(string.Format(" AND {0} IN ({1})", InFieldName, inValues.ToJoinSqlInVal()));
            return(queryable);
        }
Esempio n. 7
0
        /// <summary>
        ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static T Single <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.ToList().Single());
        }
Esempio n. 8
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);
            string typeName = type.Name;

            typeName = GetTableNameByClassType(typeName);
            ResolveExpress re = new ResolveExpress();

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

            return(isSuccess);
        }
Esempio n. 9
0
        public void ResolveExpression(ResolveExpress re, Expression exp)
        {
            ResolveExpress.MemberType type = ResolveExpress.MemberType.None;
            var expStr        = exp.ToString();
            var isNotBool     = !expStr.Contains("True") && !expStr.Contains("False");
            var isContainsNot = expStr.Contains("Not");

            if (isContainsNot && expStr.IsMatch(@" => Not\(.+?\)") && !expStr.Contains("Contains"))
            {
                this.SqlWhere = string.Format(" AND {0}=0 or {0} is null ", Regex.Match(expStr, @" => Not\(.+\.(.+?)\)").Groups[1].Value);
            }
            else if (isNotBool)
            {
                this.SqlWhere = string.Format(" AND {0} ", re.CreateSqlElements(exp, ref type));
            }
            else
            {
                var isTrue  = Regex.IsMatch(expStr, @"\=\> True$");
                var isFalse = Regex.IsMatch(expStr, @"\=\> False$");
                if (isFalse)
                {
                    this.SqlWhere = string.Format(" AND 1<>1 ");
                }
                else if (isTrue)
                {
                }
                else
                {
                    this.SqlWhere = string.Format(" AND {0} ", re.CreateSqlElements(exp, ref type));
                }
            }
            foreach (var par in Paras)
            {
                SqlSugarTool.SetParSize(par);
            }
        }
Esempio n. 10
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 <SQLiteParameter> parsList = new List <SQLiteParameter>();

            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);
            }
        }