Пример #1
0
        public static async Task <Resp> UpdatePartial <TType>(this IDbConnection con, string tableName,
                                                              Expression <Func <TType, object> > update, Expression <Func <TType, bool> > where, object mo)
        //where TType : BaseMo<IdType>
        {
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = typeof(TType).Name;
            }

            var visitor = new SqlExpressionVisitor();

            var updateSql = GetVisitExpressSql(visitor, update, SqlVistorType.Update);
            var whereSql  = GetVisitExpressSql(visitor, where, SqlVistorType.Where);
            var sql       = string.Concat("UPDATE ", tableName, " SET ", updateSql, whereSql);

            var paras = GetExecuteParas(mo, visitor);
            var row   = await con.ExecuteAsync(sql, paras);

            return(row > 0 ? new Resp() : new Resp().WithResp(RespTypes.OperateFailed, "更新失败!"));
        }
Пример #2
0
        public static async Task <ListResp <RType> > GetList <PType, RType>(this IDbConnection con, string tableName,
                                                                            Expression <Func <PType, bool> > whereExp)
            where PType : class
        {
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = typeof(PType).Name;
            }

            var sqlVisitor = new SqlExpressionVisitor();
            var whereSql   = GetVisitExpressSql(sqlVisitor, whereExp, SqlVistorType.Where);

            var sqlStr = string.Concat("SELECT * FROM ", tableName, whereSql);
            var paras  = GetExecuteParas(null, sqlVisitor);

            var listRes = (await con.QueryAsync <RType>(sqlStr, paras)).ToList();

            return(listRes.Count == 0
                ? new ListResp <RType>().WithResp(RespTypes.ObjectNull, "没有查到相关信息!")
                : new ListResp <RType>(listRes.ToList()));
        }
Пример #3
0
        /// <summary>
        ///   处理where条件表达式,如果表达式为空,默认使用Id
        /// </summary>
        /// <param name="visitor"></param>
        /// <param name="exp"></param>
        /// <param name="visType"></param>
        private static string GetVisitExpressSql(SqlExpressionVisitor visitor, Expression exp, SqlVistorType visType)
        {
            if (visType == SqlVistorType.Update)
            {
                var updateFlag = new SqlVistorFlag(SqlVistorType.Update);
                visitor.Visit(exp, updateFlag);
                return(updateFlag.sql);
            }

            string sql;

            if (exp == null)
            {
                throw new ArgumentNullException("whereExp", "where表达式不能为空!");
            }
            else
            {
                var whereFlag = new SqlVistorFlag(SqlVistorType.Where);
                visitor.Visit(exp, whereFlag);
                sql = string.Concat(" WHERE ", whereFlag.sql);
            }

            return(sql);
        }