-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: helper to retrieve column value from cursor #43
Comments
I've been meaning to add something like this. Could be changed to use I'm not sure it's worth generating getters for each table compared to what you gain over a simple helper class. |
@MyDogTom I use a similar approach, but mine's just a helper class: package ...;
import android.database.Cursor;
import android.support.annotation.Nullable;
import java.util.Date;
public abstract class CursorHelper {
public static long getLong(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
if (columnIndex >= 0 && !cursor.isNull(columnIndex)) {
return cursor.getLong(columnIndex);
}
return 0;
}
public static int getInt(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
if (columnIndex >= 0 && !cursor.isNull(columnIndex)) {
return cursor.getInt(columnIndex);
}
return 0;
}
@Nullable
public static Date getDate(Cursor cursor, String columnName) {
long since = getLong(cursor, columnName);
if (since > 0) {
return new Date(since);
}
return null;
}
@Nullable
public static String getString(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
if (columnIndex >= 0 && !cursor.isNull(columnIndex)) {
return cursor.getString(columnIndex);
}
return null;
}
private CursorHelper() {
}
} Then I just copy and paste this class for each project. Sometimes I extend it with more methods. And then, I call like this: final Date createdAt = CursorHelper.getDate(cursor, ActionColumns.CREATED_AT);
final String action = CursorHelper.getString(cursor, ActionColumns.ACTION); As schematic generates a class to populate final {@Database.className()}Cursors cursors = new {@Database.className()}Cursors();
final {Model Name}Cursor modelCursor = cursors.for{Model Name}(cursor);
modelCursor.createdAt();
modelCursor.action();
modelCursor.id(); |
Added the helper class in e020ecb at least. I think having Will keep it open in case someone comes up with an awesome solution. |
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 callString note = NotesGetter.noteString(cursor);
. It's more significant when you deal with nullable columns.Couple details about suggested implementation:
getColumnIndexOrThrow
instead ofgetColumnIndex
. I think this approach is more robust because let identify problems earlier.null
for wrappers. Example of generated code.null
support despite@NotNull
annotation.The text was updated successfully, but these errors were encountered: