Skip to content

Proposal: helper to retrieve column value from cursor #43

Open
@MyDogTom

Description

@MyDogTom

Hi, @SimonVT .
I'm thinking about creating merge request with "column getter" functionality (see description below). Please tell me if it suitable for library or not.

Motivation: Instead of writing String note = cursor.getString(cursor.getColumnIndex(NoteColumns.NOTE)); just call String note = NotesGetter.noteString(cursor);. It's more significant when you deal with nullable columns.

Couple details about suggested implementation:

  • Use getColumnIndexOrThrow instead of getColumnIndex. I think this approach is more robust because let identify problems earlier.
  • Create implementation for all primitive types and for wrappers. Support null for wrappers. Example of generated code.
  public static Integer getIdIntOrNull(Cursor cursor) {
    int index = cursor.getColumnIndexOrThrow(NoteColumns.ID);
    if (cursor.isNull(index)) {
      return null;
    } else {
      return cursor.getInt(index);
    }
  }

  public int getIdInt(Cursor cursor) {
    return cursor.getInt(cursor.getColumnIndexOrThrow(NoteColumns.ID));
  }
  • I'll always generate null support despite @NotNull annotation.
  • I'm still looking for better class and method names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions