Created
May 30, 2018 18:40
-
-
Save AdamMc331/150908d1be78fec8d638542502e61839 to your computer and use it in GitHub Desktop.
Shows an implementation of a Room database DAO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountDAO_Impl implements AccountDAO { | |
private final RoomDatabase __db; | |
private final EntityInsertionAdapter __insertionAdapterOfAccount; | |
private final EntityDeletionOrUpdateAdapter __deletionAdapterOfAccount; | |
private final SharedSQLiteStatement __preparedStmtOfDeleteAll; | |
public AccountDAO_Impl(RoomDatabase __db) { | |
this.__db = __db; | |
this.__insertionAdapterOfAccount = new EntityInsertionAdapter<Account>(__db) { | |
@Override | |
public String createQuery() { | |
return "INSERT OR ABORT INTO `Account`(`name`,`balance`) VALUES (?,?)"; | |
} | |
@Override | |
public void bind(SupportSQLiteStatement stmt, Account value) { | |
if (value.getName() == null) { | |
stmt.bindNull(1); | |
} else { | |
stmt.bindString(1, value.getName()); | |
} | |
stmt.bindDouble(2, value.getBalance()); | |
} | |
}; | |
this.__deletionAdapterOfAccount = new EntityDeletionOrUpdateAdapter<Account>(__db) { | |
@Override | |
public String createQuery() { | |
return "DELETE FROM `Account` WHERE `name` = ?"; | |
} | |
@Override | |
public void bind(SupportSQLiteStatement stmt, Account value) { | |
if (value.getName() == null) { | |
stmt.bindNull(1); | |
} else { | |
stmt.bindString(1, value.getName()); | |
} | |
} | |
}; | |
this.__preparedStmtOfDeleteAll = new SharedSQLiteStatement(__db) { | |
@Override | |
public String createQuery() { | |
final String _query = "DELETE FROM account"; | |
return _query; | |
} | |
}; | |
} | |
@Override | |
public long insert(Account account) { | |
__db.beginTransaction(); | |
try { | |
long _result = __insertionAdapterOfAccount.insertAndReturnId(account); | |
__db.setTransactionSuccessful(); | |
return _result; | |
} finally { | |
__db.endTransaction(); | |
} | |
} | |
@Override | |
public int delete(Account account) { | |
int _total = 0; | |
__db.beginTransaction(); | |
try { | |
_total +=__deletionAdapterOfAccount.handle(account); | |
__db.setTransactionSuccessful(); | |
return _total; | |
} finally { | |
__db.endTransaction(); | |
} | |
} | |
@Override | |
public int deleteAll() { | |
final SupportSQLiteStatement _stmt = __preparedStmtOfDeleteAll.acquire(); | |
__db.beginTransaction(); | |
try { | |
final int _result = _stmt.executeUpdateDelete(); | |
__db.setTransactionSuccessful(); | |
return _result; | |
} finally { | |
__db.endTransaction(); | |
__preparedStmtOfDeleteAll.release(_stmt); | |
} | |
} | |
@Override | |
public Flowable<List<Account>> getAll() { | |
final String _sql = "SELECT * FROM account ORDER BY name"; | |
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0); | |
return RxRoom.createFlowable(__db, new String[]{"account"}, new Callable<List<Account>>() { | |
@Override | |
public List<Account> call() throws Exception { | |
final Cursor _cursor = __db.query(_statement); | |
try { | |
final int _cursorIndexOfName = _cursor.getColumnIndexOrThrow("name"); | |
final int _cursorIndexOfBalance = _cursor.getColumnIndexOrThrow("balance"); | |
final List<Account> _result = new ArrayList<Account>(_cursor.getCount()); | |
while(_cursor.moveToNext()) { | |
final Account _item; | |
_item = new Account(); | |
final String _tmpName; | |
_tmpName = _cursor.getString(_cursorIndexOfName); | |
_item.setName(_tmpName); | |
final double _tmpBalance; | |
_tmpBalance = _cursor.getDouble(_cursorIndexOfBalance); | |
_item.setBalance(_tmpBalance); | |
_result.add(_item); | |
} | |
return _result; | |
} finally { | |
_cursor.close(); | |
} | |
} | |
@Override | |
protected void finalize() { | |
_statement.release(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment