public static async Task <ListResp <TType> > GetList <TType>(this IDbConnection con, string tableName, Expression <Func <TType, bool> > whereExp) { if (string.IsNullOrEmpty(tableName)) { tableName = typeof(TType).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 <TType>(sqlStr, paras)).ToList(); return(listRes.Count == 0 ? new ListResp <TType>().WithResp(RespTypes.ObjectNull, "没有查到相关信息!") : new ListResp <TType>(listRes.ToList())); }
internal 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 { 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, "操作失败!")); }
/// <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); }