Skip to content

Instantly share code, notes, and snippets.

@maxlord
Last active April 18, 2016 07:48
Show Gist options
  • Save maxlord/ba5b4d8db57e987468bb8b5630ab7cac to your computer and use it in GitHub Desktop.
Save maxlord/ba5b4d8db57e987468bb8b5630ab7cac to your computer and use it in GitHub Desktop.
RxSqlite Table
public class EventNotification$$Table implements RxSQLiteTable<EventNotification> {
private final CustomTypes mTypes;
public EventNotification$$Table(CustomTypes types) {
mTypes = types;
}
@Override
public void create(SQLiteDb db) {
db.exec("CREATE TABLE IF NOT EXISTS event_notification("
+ "_id INTEGER PRIMARY KEY ON CONFLICT REPLACE"
+ ", event_name TEXT"
+ ", date INTEGER"
+ ", content TEXT"
+ ", is_new INTEGER"
+ ");");
}
@Override
public Observable<EventNotification> query(SQLiteDb db, String selection, Iterable<Object> bindValues) {
final SQLiteStmt stmt = db.prepare("SELECT * FROM event_notification" + selection);
try {
final List<EventNotification> objects = new ArrayList<>();
int index = 0;
for (final Object value : bindValues) {
mTypes.bindValue(stmt, ++index, value);
}
final SQLiteCursor cursor = stmt.executeQuery();
while (cursor.step()) {
objects.add(instantiate(db, cursor));
}
return Observable.from(objects);
} finally {
stmt.close();
}
}
@Override
public Observable<EventNotification> save(SQLiteDb db, Iterable<EventNotification> objects) {
blockingSave(db, objects);
return Observable.from(objects);
}
@Override
public List<Long> blockingSave(SQLiteDb db, Iterable<EventNotification> objects) {
final SQLiteStmt stmt = db.prepare("INSERT INTO event_notification(_id, event_name, date, content, is_new) VALUES(?, ?, ?, ?, ?);");
try {
final List<Long> rowIds = new ArrayList<>();
for (final EventNotification object : objects) {
stmt.clearBindings();
bindStmtValues(stmt, object);
object.id = stmt.executeInsert();
rowIds.add(object.id);
}
return rowIds;
} finally {
stmt.close();
}
}
@Override
public Observable<Integer> remove(SQLiteDb db, Iterable<EventNotification> objects) {
final SQLiteStmt stmt = db.prepare("DELETE FROM event_notification WHERE _id = ?;");
try {
int affectedRows = 0;
for (final EventNotification object : objects) {
stmt.clearBindings();
stmt.bindLong(1, object.id);
affectedRows += stmt.executeUpdateDelete();
}
return Observable.just(affectedRows);
} finally {
stmt.close();
}
}
@Override
public Observable<Integer> clear(SQLiteDb db, String selection, Iterable<Object> bindValues) {
final SQLiteStmt stmt = db.prepare("DELETE FROM event_notification" + selection);
try {
int index = 0;
for (final Object value : bindValues) {
mTypes.bindValue(stmt, ++index, value);
}
return Observable.just(stmt.executeUpdateDelete());
} finally {
stmt.close();
}
}
@Override
public EventNotification instantiate(SQLiteDb db, SQLiteCursor cursor) {
final EventNotification object = new EventNotification();
object.id = (long) cursor.getColumnLong(0);
object.eventName = cursor.getColumnString(1);
object.date = (long) cursor.getColumnLong(2);
object.content = cursor.getColumnString(3);
object.isNew = (int) cursor.getColumnLong(4);
return object;
}
private void bindStmtValues(SQLiteStmt stmt, EventNotification object) {
if (object.id > 0) {
stmt.bindLong(1, object.id);
} else {
stmt.bindNull(1);
}
stmt.bindString(2, object.eventName);
stmt.bindLong(3, object.date);
stmt.bindString(4, object.content);
stmt.bindLong(5, object.isNew);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment