Search code examples
javaandroidsqlitestored-proceduressqldelight

SqlDelight not generating SQL Query String for SQL Statement


In the example in the SqlDelight documentation, the HockeyPlayerModel generated by SqlDelight from a HockeyPlayer.sq file is implemented in an abstract class public abstract class HockeyPlayer implements HockeyPlayerModel

In this class, the string SELECT_ALL_INFO is passed in as the query to db.rawQuery(SELECT_ALL_INFO, new String[0]). The string SELECT_ALL_INFO was generated by the select_all_info statement inside HockeyPlayer.sq. However, in my situation, my statements are not generating strings. Why is this?

My statement

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 ;

My implementation of the ExerciseSetModel being generated by SqlDelight

@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 {}
}

To be clear, the variable reference NAMES_FOR_GROUPS is not found in the line db.rawQuery(NAMES_FOR_GROUPS, new String[0])


Solution

  • The documentation needs to be updated, I'll do that. We no longer generate Strings in SQLDelight 0.6.+ instead there is a method on the factory which returns a SQLDelightStatement that is used to query:

    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;
    }