gpt4 book ai didi

java - SqlDelight 不为 SQL 语句生成 SQL 查询字符串

转载 作者:行者123 更新时间:2023-12-02 13:19:04 29 4
gpt4 key购买 nike

SqlDelight documentation 中的示例中,SqlDelight 从 HockeyPlayer.sq 文件生成的 HockeyPlayerModel 在抽象类中实现 public 抽象类 HockeyPlayer 实现 HockeyPlayerModel

在此类中,字符串 SELECT_ALL_INFO 作为查询传入 db.rawQuery(SELECT_ALL_INFO, new String[0])。字符串 SELECT_ALL_INFO 是由 HockeyPlayer.sq 内的 select_all_info 语句生成的。但是,在我的情况下,我的语句不会生成字符串。这是为什么?

我的声明

names_for_groups:
SELECT DISTINCT name, exercise_set_group FROM exercise_set JOIN exercises
USING (exercise_id) WHERE workout_FK = ? ORDER BY exercise_set_group ASC ;

我对 SqlDelight 生成的 ExerciseSetModel 的实现

@AutoValue
public abstract class DbExerciseSet implements ExerciseSetModel, DbItem {
public static final Factory<DbExerciseSet> FACTORY = new Factory<>(AutoValue_DbExerciseSet::new);
public static final RowMapper<DbExerciseSet> MAPPER = FACTORY.select_allMapper();

public static final RowMapper<NamesForGroups> NAMES_FOR_GROUPS_MAPPER =
FACTORY.names_for_groupsMapper(AutoValue_DbExerciseSet_NamesForGroups::new);

public List<NamesForGroups> namesForGroups(SQLiteDatabase db) {
List<NamesForGroups> namesForGroupsList= new ArrayList<>();
Cursor cursor = db.rawQuery(NAMES_FOR_GROUPS, new String[0]);
while (cursor.moveToNext() && NAMES_FOR_GROUPS_MAPPER.map(cursor) != null) {
//NamesForGroups namesForGroups = NAMES_FOR_GROUPS_MAPPER.map(cursor);
namesForGroupsList.add(NAMES_FOR_GROUPS_MAPPER.map(cursor));
}

return namesForGroupsList;
}

@AutoValue
public abstract static class NamesForGroups implements Names_for_groupsModel {}

@AutoValue
public abstract static class Exercises implements ExerciseModel {}
}

需要明确的是,在 db.rawQuery(NAMES_FOR_GROUPS, new String[0]) 行中找不到变量引用 NAMES_FOR_GROUPS

最佳答案

文档需要更新,我会更新。我们不再在 SQLDelight 0.6.+ 中生成字符串,而是工厂中有一个方法返回用于查询的 SQLDelightStatement:

public List<NamesForGroups> namesForGroups(SQLiteDatabase db) {
List<NamesForGroups> namesForGroupsList= new ArrayList<>();
SQLDelightStatement query = FACTORY.name_for_groups();
Cursor cursor = db.rawQuery(query.statement, query.args);
while (cursor.moveToNext() && NAMES_FOR_GROUPS_MAPPER.map(cursor) != null) {
//NamesForGroups namesForGroups = NAMES_FOR_GROUPS_MAPPER.map(cursor);
namesForGroupsList.add(NAMES_FOR_GROUPS_MAPPER.map(cursor));
}

return namesForGroupsList;
}

关于java - SqlDelight 不为 SQL 语句生成 SQL 查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658678/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com