コード例 #1
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." + type.FullName + ".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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
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));
        }
コード例 #4
0
        /// <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);
        }
コード例 #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);
            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);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
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);
        }
コード例 #8
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);
            var isDynamic = rowObj.GetType() != type;
            var isClass   = !isDynamic;

            //sql语句缓存
            string cacheSqlKey     = "db.update." + type.FullName + 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) && isClass)
            {
                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 (this.IsIgnoreErrorColumns)
                    {
                        if (!SqlSugarTool.GetColumnsByTableName(this, typeName).Any(it => it.ToLower() == name.ToLower()))
                        {
                            continue;
                        }
                    }

                    if (isPk || isIdentity || isDisableUpdateColumns)
                    {
                        if (isClass)
                        {
                            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 <OracleParameter> parsList = new List <OracleParameter>();

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

            if (pars != null)
            {
                foreach (var par in pars)
                {
                    SqlSugarTool.SetParSize(par);
                    parsList.Add(par);
                }
            }
            try
            {
                var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray());
                return(updateRowCount > 0);
            }
            catch (Exception ex)
            {
                throw new Exception("sql:" + sbSql.ToString() + "\n" + ex.Message);
            }
        }