以下のクラスは、API 呼び出しから返された構造体から、primaryKey、parentId、および depth を除いたものです。私の目標は、再帰 CTE を使用して Room DB にクロージャ テーブルを作成することです。
class Category {
public Integer primaryKey;
public String parentId;
public Integer depth;
public String categoryId;
public String name;
public String pluralName;
public String shortName;
@Ignore public List<Category> categories;
}
返された POJO がオブザーバブルから発行されたときに、ツリーをトラバースしようとしました。問題は、categoryId==parentId である推移閉包の一部で深さが正しくないことです。
for(Category category: categories){
List<Category> tree = new ArrayList<>();
buildCategoryClosureTable(category, tree, 0);
}
void buildCategoryClosureTable(Category target, List<Category> tree, Integer depth){
tree.add(target);
if(!target.categories.isEmpty()){
depth++;
do{
Category category = target.categories.remove(0);
buildFoursquareCategoryClosureTable(category, tree, depth);
}while(!target.categories.isEmpty());
}
for(Category category : tree){
FoursquareCategory foursquareCategory = new FoursquareCategory(target, category.categoryId, depth);
insertFoursquareCategoryIntoBaseTable(foursquareCategory);
depth--;
}
tree.remove(target);
}
だから...再帰CTEを使用してRoom DBでクロージャテーブルを作成するにはどうすればよいですか?