public static List<Record> find(Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.config, conn, sql, paras); }
public static List<Record> find(String configName, Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.getConfig(configName), conn, sql, paras); }
@Override public List<ArticleCategory> findListByArticleId(long articleId) { List<Record> mappings = Db.find("select * from article_category_mapping where article_id = ?", articleId); if (mappings == null || mappings.isEmpty()) { return null; } return mappings .stream() .map(record -> DAO.findById((long) record.get("category_id"))) .filter(Objects::nonNull) .collect(Collectors.toList()); }
@Override public Long[] findCategoryIdsByArticleId(long articleId) { List<Record> records = Db.find("select * from article_category_mapping where article_id = ?", articleId); if (records == null || records.isEmpty()) return null; return ArrayUtils.toObject(records.stream().mapToLong(record -> record.get("category_id")).toArray()); }
@Cacheable(name = "user_permission", key = "role:#(roleId)", nullCacheEnable = true) public List<Permission> findPermissionListByRoleId(long roleId) { String sql = "select * from role_permission_mapping where role_id = ? "; List<Record> rolePermissionRecords = Db.find(sql, roleId); if (rolePermissionRecords == null || rolePermissionRecords.isEmpty()) { return null; } List<Permission> permissionList = new ArrayList<>(); for (Record rolePermissionRecord : rolePermissionRecords) { Permission permission = findById(rolePermissionRecord.getLong("permission_id")); if (permission != null) permissionList.add(permission); } return permissionList; }
@Cacheable(name = "user_role", key = "user_roles:#(userId)", nullCacheEnable = true) public List<Role> findRoleListByUserId(long userId) { String sql = "select * from user_role_mapping where user_id = ?"; List<Record> records = Db.find(sql, userId); if (records == null || records.isEmpty()) { return null; } List<Role> roles = new ArrayList<>(); for (Record record : records) { Role role = findById(record.getLong("role_id")); if (role != null) roles.add(role); } return roles; }
@Cacheable(name = "user_permission", key = "user_permissions:#(userId)", nullCacheEnable = true) public List<Permission> findPermissionListByUserId(long userId) { Set<Permission> permissions = new HashSet<>(); String sql = "select * from user_role_mapping where user_id = ? "; List<Record> userRoleRecords = Db.find(sql, userId); if (userRoleRecords != null) { for (Record userRoleRecord : userRoleRecords) { List<Permission> rolePermissions = findPermissionListByRoleId(userRoleRecord.getLong("role_id")); if (rolePermissions != null) { permissions.addAll(rolePermissions); } } } // sql = "select * from user_permission where user_id = ?"; // List<Record> userPermissionList = Db.find(sql, userId); // if (userPermissionList != null) { // for (Record userPermission : userPermissionList) { // if (userPermission.getInt("own") > 0) { // permissions.add(findById(userPermission.getLong("permission_id"))); // } else { // permissions.remove(findById(userPermission.getLong("permission_id"))); // } // } // } return new ArrayList<>(permissions); }
@Override public boolean deleteById(Object id) { return Db.tx(() -> { boolean delOk = ArticleServiceProvider.super.deleteById(id); if (delOk == false) { return false; } List<Record> records = Db.find("select * from article_category_mapping where article_id = ? ", id); if (records == null || records.isEmpty()) { return true; } Db.update("delete from article_category_mapping where article_id = ?", id); records.stream().forEach(record -> { categoryService.updateCount(record.get("category_id")); }); return true; }); }
/** * Return the columns map of the record * @param record the Record object * @return the columns map of the record public static final Map<String, Object> getColumns(Record record) { return record.getColumns(); } */ public static List<Record> find(Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.config, conn, sql, paras); }
public static List<Record> find(Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.config, conn, sql, paras); }
public static List<Record> find(String configName, Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.getConfig(configName), conn, sql, paras); }
public static List<Record> find(String configName, Connection conn, String sql, Object... paras) throws SQLException { return Db.find(DbKit.getConfig(configName), conn, sql, paras); }
@Override public List<ProductDTO> listDetailByProductIds(String productIds) { String[] productIdArr = productIds.split(","); SqlPara sqlPara = Db.getSqlPara("product.listDetailByProductIds", Kv.by("productIds", productIdArr)); List<Record> recordList = Db.find(sqlPara); List<ProductDTO> productDTOList = RecordUtils.converModel(recordList, ProductDTO.class); return productDTOList; }
public List<String> queryAllPerms(Long userId) { SqlPara sqlPara = Db.getSqlPara("sysUser.queryAllPerms", Kv.by("userId", userId)); List<Record> sysUserList = Db.find(sqlPara); List<String> perms = new ArrayList<>(); for (Record r:sysUserList ) { if(r == null || r.get("perms") == null) { continue; } perms.add(r.get("perms")); } return perms; }
public List<Long> queryAllMenuId(Long userId) { SqlPara sqlPara = Db.getSqlPara("sysUser.queryAllMenuId", Kv.by("userId", userId)); List<Record> sysMenuList = Db.find(sqlPara); List<Long> menuIds = new ArrayList<>(); for (Record r:sysMenuList ) { if(r == null || r.get("menu_id") == null) { continue; } menuIds.add(r.get("menu_id")); } return menuIds; }
@Override public List<CartDTO> listDetail(String userId) { SqlPara sqlPara = Db.getSqlPara("cart.listDetail", Kv.by("userId", userId)); List<Record> recordList = Db.find(sqlPara); List<CartDTO> cartDTOList = RecordUtils.converModel(recordList, CartDTO.class); return cartDTOList; } }
@Override public List<FavoriteGoodsDTO> list(String userId) { SqlPara sqlPara = Db.getSqlPara("favoriteGoods.list", Kv.by("userId", userId)); List<Record> recordList = Db.find(sqlPara); List<FavoriteGoodsDTO> favoriteGoodsDTOList = RecordUtils.converModel(recordList, FavoriteGoodsDTO.class); return favoriteGoodsDTOList; } }
protected static String dynamicUpdate(String database, String tableName, Model<?> model) { DynamicSQL dsql = new DynamicSQL(); dsql.append(" update " + tableName); dsql.append(" set " ); List<Record> columns = Db.find(columnSql, database, tableName); for (Record rd : columns) if (StrKit.notNull(rd.getStr("columnName"))) dsql.isNotEmpty(String.format(" %s = %s , ", rd.getStr("columnName"), placeholder(rd.getStr("dataType"))), model.get(rd.getStr("columnName"))); String sql = dsql.toString(); sql = sql.substring(0, sql.lastIndexOf(',')); dsql.clear(); return sql; }
protected static String selectSql(String database, String tableName, Model<?> model) { DynamicSQL dsql = new DynamicSQL(); dsql.append(" select * from " + tableName); dsql.append(" where 1= 1 " ); List<Record> columns = Db.find(columnSql, database, tableName); for (Record rd : columns) if (StrKit.isNull(rd.getStr("columnName"))) dsql.isNotEmpty(String.format(" and %s = %s ", rd.getStr("columnName"), placeholder(rd.getStr("dataType"))), model.get(rd.getStr("columnName"))); String sql = dsql.toString(); dsql.clear(); return sql; }
protected static String insertSql(String database, String tableName, Model<?> model) { DynamicSQL dsql = new DynamicSQL(); dsql.append(" insert into " + tableName); dsql.append(" ( " ); List<Record> columns = Db.find(columnSql, database, tableName); // INSERT INTO table_name (列1, 列2,...) for (Record rd : columns) if (ModelKit.recordNotBlank(model.get(rd.getStr("columnName")))) dsql.isNotEmpty(" $, ", rd.getStr("columnName")); dsql.append(" ) values( " ); // VALUES (值1, 值2,....) for (Record rd : columns) if (StrKit.notNull(rd.getStr("columnName"))) dsql.isNotEmpty(String.format(" %s, ", placeholder(rd.getStr("dataType"))), model.get(rd.getStr("columnName"))); dsql.append(" ) " ); String sql = dsql.toString(); sql = sql.replaceAll(", \\)", ")"); dsql.clear(); return sql; }