@Override protected final Long updateKeyAfterInsert(User entity, long rowId) { entity.setId(rowId); return rowId; }
@Override public Long getKey(User entity) { if(entity != null) { return entity.getId(); } else { return null; } }
@Override public void readEntity(Cursor cursor, User entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setUid(cursor.getLong(offset + 1)); entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); entity.setAge(cursor.getInt(offset + 3)); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), "test.db", null); DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); mDaoSession = daoMaster.newSession(); mUserDao = mDaoSession.getUserDao(); User user = new User(); user.setUid(112111); user.setName("hello"); user.setAge(1); mUserDao.insert(user); long count = mUserDao.count(); Log.d(TAG, "onCreate: count - " + count); List<User> users = mUserDao.queryBuilder().build().list(); Log.d(TAG, "onCreate: size - " + users.size()); } }
@Test public void testUserDao() { mUserDao.deleteAll(); long count = mUserDao.count(); assertEquals(0, count); long uid = 0; User user = new User(); user.setUid(11111); user.setName("hello"); user.setAge(1); mUserDao.insert(user); count = mUserDao.count(); assertEquals(1, count); }
@Before @Override public void setUp() throws Exception { super.setUp(); injectInstrumentation(InstrumentationRegistry.getInstrumentation()); DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getInstrumentation().getContext(), "test.db", null); assertNotNull(helper); DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); assertNotNull(daoMaster); mDaoSession = daoMaster.newSession(); assertNotNull(mDaoSession); mUserDao = mDaoSession.getUserDao(); assertNotNull(mUserDao); }
@Override protected final void bindValues(SQLiteStatement stmt, User entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } stmt.bindLong(2, entity.getUid()); String name = entity.getName(); if (name != null) { stmt.bindString(3, name); } stmt.bindLong(4, entity.getAge()); }
@Override public User readEntity(Cursor cursor, int offset) { User entity = new User( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.getLong(offset + 1), // uid cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name cursor.getInt(offset + 3) // age ); return entity; }
@Override protected final void bindValues(DatabaseStatement stmt, User entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } stmt.bindLong(2, entity.getUid()); String name = entity.getName(); if (name != null) { stmt.bindString(3, name); } stmt.bindLong(4, entity.getAge()); }