@Override public void onDownloadFileUpdated(DownloadFileInfo downloadFileInfo, Type type) { if (downloadFileInfo != null && downloadFileInfo.getUrl() != null && downloadFileInfo.getUrl().equals (mCourseUrl)) { if (this.mDownloadFileInfo == null) { try { if (mCourseDbHelper == null) { return; } UpdateBuilder builder = mCourseDbHelper.getDao(CoursePreviewInfo.class).updateBuilder(); builder.where().eq(CoursePreviewInfo.COLUMN_NAME_OF_FIELD_COURSE_URL, downloadFileInfo.getUrl()); int result = builder.update(); if (result == 1) { this.mDownloadFileInfo = downloadFileInfo; } else { Dao<CoursePreviewInfo, Integer> dao = mCourseDbHelper.getDao(CoursePreviewInfo.class); CreateOrUpdateStatus status = dao.createOrUpdate(this); if (status.isCreated() || status.isUpdated()) { this.mDownloadFileInfo = downloadFileInfo; } } } catch (SQLException e) { e.printStackTrace(); } } } }
/** * Saves a single module data */ public boolean saveModuleData(ModuleData moduleData) throws SQLException { getModuleData(moduleData.getModule().getId(), moduleData.getName()).ifPresent((data) -> { moduleData.setId(data.getId()); }); return DB.MODULE_DATA_DAO.createOrUpdate(moduleData).getNumLinesChanged() == 1; }
assertTrue(status.isUpdated());
assertEquals(1, questionDao.createOrUpdate(question2).getNumLinesChanged()); assertEquals(1, questionDao.createOrUpdate(question2).getNumLinesChanged());
@Override public void onDownloadFileCreated(DownloadFileInfo downloadFileInfo) { if (downloadFileInfo != null && downloadFileInfo.getUrl() != null && downloadFileInfo.getUrl().equals (mCourseUrl)) { // add this CoursePreviewInfo in database download record // // the reason why to save this CoursePreviewInfo in course database is // that when the user enter the CourseDownloadFragment,the fragment need to show the course title to the // user,however the DownloadFileInfo in FileDownloader can not provide the course title,also the fragment // need to know how many course items need to show,so it is depends on the size of the course database, // because FileDownloader is just a tool to download, record and manage all the files which were // downloaded from the internet try { if (mCourseDbHelper == null) { return; } Dao<CoursePreviewInfo, Integer> dao = mCourseDbHelper.getDao(CoursePreviewInfo.class); CreateOrUpdateStatus status = dao.createOrUpdate(this); if (status.isCreated() || status.isUpdated()) { this.mDownloadFileInfo = downloadFileInfo; } } catch (SQLException e) { e.printStackTrace(); } } }
@Test public void testVersionFieldInsertOrUpdate() throws Exception { Dao<VersionField, Integer> dao = createDao(VersionField.class, true); VersionField foo1 = new VersionField(); assertEquals(1, dao.create(foo1)); assertEquals(1, foo1.id); assertEquals(0, foo1.version); CreateOrUpdateStatus status = dao.createOrUpdate(foo1); assertTrue(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); assertEquals(1, foo1.version); status = dao.createOrUpdate(foo1); assertTrue(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); assertEquals(2, foo1.version); VersionField result = dao.queryForId(foo1.id); // we update this one to a new version number assertEquals(1, dao.update(result)); assertEquals(3, result.version); // the old one doesn't change assertEquals(2, foo1.version); // but when we try to update the earlier foo, the version doesn't match assertEquals(0, dao.update(foo1)); }
@Test public void testCreateOrUpdateManualId() throws Exception { Dao<Child, Integer> childDao = createDao(Child.class, true); Dao<Parent, Object> parentDao = createDao(Parent.class, true); // this group doesn't exists in database Child child1 = new Child(); child1.id = 1; List<Parent> parents = new ArrayList<Parent>(); for (int i = 0; i < 5; i++) { Parent parent = new Parent(); parent.child = child1; parents.add(parent); } // this group doesn't exists in database Child child2 = new Child(); child2.id = 2; for (int i = 0; i < 5; i++) { Parent parent = new Parent(); parent.child = child2; parents.add(parent); } for (Parent parent : parents) { assertTrue(parentDao.createOrUpdate(parent).isCreated()); } assertEquals(0, childDao.countOf()); assertEquals(parents.size(), parentDao.countOf()); }
@Test public void testCreateOrUpdateMultiple2() throws Exception { Dao<Child, Integer> childDao = createDao(Child.class, true); Dao<Parent, Object> parentDao = createDao(Parent.class, true); Child child = new Child(); child.api_id = 1; // no create because of auto-create Parent parent1 = new Parent(); parent1.child = child; assertTrue(parentDao.createOrUpdate(parent1).isCreated()); Parent parent2 = new Parent(); parent2.child = child; assertTrue(parentDao.createOrUpdate(parent2).isCreated()); assertEquals(1, childDao.countOf()); List<Parent> results = parentDao.queryForAll(); assertNotNull(results); assertEquals(2, results.size()); Parent result = results.get(0); assertEquals(1, result.id); assertNotNull(result.child); assertEquals(child.id, result.child.id); result = results.get(1); assertEquals(2, result.id); assertNotNull(result.child); assertEquals(child.id, result.child.id); }
/** * Create or update the tile scaling * * @param tileScaling * tile scaling * @return true upon success */ public boolean createOrUpdate(TileScaling tileScaling) { boolean success = false; tileScaling.setTableName(tableName); getOrCreateExtension(); try { if (!tileScalingDao.isTableExists()) { geoPackage.createTileScalingTable(); } CreateOrUpdateStatus status = tileScalingDao .createOrUpdate(tileScaling); success = status.isCreated() || status.isUpdated(); } catch (SQLException e) { throw new GeoPackageException( "Failed to create or update tile scaling for GeoPackage: " + geoPackage.getName() + ", Tile Table: " + tableName, e); } return success; }
@Test public void testCreateOrUpdateMultipleLoop() throws Exception { Dao<Child, Integer> childDao = createDao(Child.class, true); Dao<Parent, Object> parentDao = createDao(Parent.class, true); Child child = new Child(); child.api_id = 1; // no create because of auto-create Parent[] parents = new Parent[10]; for (int i = 0; i < parents.length; i++) { parents[i] = new Parent(); parents[i].child = child; assertTrue(parentDao.createOrUpdate(parents[i]).isCreated()); } assertEquals(1, childDao.countOf()); assertEquals(10, parentDao.countOf()); for (int i = 0; i < parents.length; i++) { Parent result = parentDao.queryForId(parents[i].id); assertNotNull(result); assertNotNull(result.child); assertEquals(child.id, result.child.id); } }
@Test public void testCreateOrUpdateLoop() throws Exception { Dao<Question, Object> questionDao = createDao(Question.class, true); Dao<Answer, Object> answerDao = createDao(Answer.class, true); Answer answer = new Answer(); answer.val = 1234313123; assertEquals(1, answerDao.create(answer)); Question[] questions = new Question[10]; for (int i = 0; i < questions.length; i++) { // not failed questions[i] = new Question(); questions[i].name = "name_" + i; questions[i].bestAnswer = answer; assertTrue(questionDao.createOrUpdate(questions[i]).isCreated()); } for (int i = 0; i < questions.length; i++) { // not failed Question result = questionDao.queryForId(questions[i].id); assertNotNull(result); assertNotNull(result.bestAnswer); assertEquals(answer.id, result.bestAnswer.id); } }
@Test public void testCreateOrUpdateNullId() throws Exception { Dao<CreateOrUpdateObjectId, Integer> dao = createDao(CreateOrUpdateObjectId.class, true); CreateOrUpdateObjectId foo = new CreateOrUpdateObjectId(); String stuff = "21313"; foo.stuff = stuff; CreateOrUpdateStatus status = dao.createOrUpdate(foo); assertTrue(status.isCreated()); assertFalse(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); CreateOrUpdateObjectId result = dao.queryForId(foo.id); assertNotNull(result); assertEquals(stuff, result.stuff); String stuff2 = "pwojgfwe"; foo.stuff = stuff2; dao.createOrUpdate(foo); result = dao.queryForId(foo.id); assertNotNull(result); assertEquals(stuff2, result.stuff); }
@Test public void testCreateOrUpdate() throws Exception { Dao<NotQuiteFoo, Integer> dao = createDao(NotQuiteFoo.class, true); NotQuiteFoo foo1 = new NotQuiteFoo(); foo1.stuff = "wow"; CreateOrUpdateStatus status = dao.createOrUpdate(foo1); assertTrue(status.isCreated()); assertFalse(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); String stuff2 = "4134132"; foo1.stuff = stuff2; status = dao.createOrUpdate(foo1); assertFalse(status.isCreated()); assertTrue(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); NotQuiteFoo result = dao.queryForId(foo1.id); assertEquals(stuff2, result.stuff); }
@Test public void testCreateOrUpdateNullId() throws Exception { Dao<CreateOrUpdateObjectId, Integer> dao = createDao(CreateOrUpdateObjectId.class, true); CreateOrUpdateObjectId foo = new CreateOrUpdateObjectId(); String stuff = "21313"; foo.stuff = stuff; CreateOrUpdateStatus status = dao.createOrUpdate(foo); assertTrue(status.isCreated()); assertFalse(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); CreateOrUpdateObjectId result = dao.queryForId(foo.id); assertNotNull(result); assertEquals(stuff, result.stuff); String stuff2 = "pwojgfwe"; foo.stuff = stuff2; dao.createOrUpdate(foo); result = dao.queryForId(foo.id); assertNotNull(result); assertEquals(stuff2, result.stuff); }
@Test public void testCreateOrUpdateMultipleLoopManualId() throws Exception { Dao<Child, Integer> childDao = createDao(Child.class, true); Dao<Parent, Object> parentDao = createDao(Parent.class, true); // should have no entries assertEquals(0, childDao.countOf()); Parent[] parents = new Parent[10]; for (int i = 0; i < parents.length; i++) { parents[i] = new Parent(); Child child = new Child(); child.id = i + 1; parents[i].child = child; assertTrue(parentDao.createOrUpdate(parents[i]).isCreated()); } assertEquals(0, childDao.countOf()); assertEquals(10, parentDao.countOf()); }
@Test public void testCreateOrUpdate() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); int equal1 = 21313; foo1.equal = equal1; CreateOrUpdateStatus status = dao.createOrUpdate(foo1); assertTrue(status.isCreated()); assertFalse(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); int equal2 = 4134132; foo1.equal = equal2; status = dao.createOrUpdate(foo1); assertFalse(status.isCreated()); assertTrue(status.isUpdated()); assertEquals(1, status.getNumLinesChanged()); Foo fooResult = dao.queryForId(foo1.id); assertEquals(equal2, fooResult.equal); }
@Override public synchronized CreateOrUpdateStatus createOrUpdate(T data) throws SQLException { if (data == null) { return new CreateOrUpdateStatus(false, false, 0); } ID id = extractId(data); // assume we need to create it if there is no id if (id == null || !idExists(id)) { int numRows = create(data); return new CreateOrUpdateStatus(true, false, numRows); } else { int numRows = update(data); return new CreateOrUpdateStatus(false, true, numRows); } }
@Override public synchronized CreateOrUpdateStatus createOrUpdate(T data) throws SQLException { if (data == null) { return new CreateOrUpdateStatus(false, false, 0); } ID id = extractId(data); // assume we need to create it if there is no id if (id == null || !idExists(id)) { int numRows = create(data); return new CreateOrUpdateStatus(true, false, numRows); } else { int numRows = update(data); return new CreateOrUpdateStatus(false, true, numRows); } }
@Override public void createOrUpdate(Object object, boolean notify) { try { Dao.CreateOrUpdateStatus status = dao.createOrUpdate((T) object); if (notify) { notifyObservers((T) object, status.isCreated() ? CREATE_ACTION : UPDATE_ACTION); } } catch (SQLException e) { ErrorUtils.logError(TAG, "createOrUpdate(Object) - " + e.getMessage()); } }
@Override public void createOrUpdate(ResourcesGroupMap resourcesGroupMap) { try { CreateOrUpdateStatus status = this.getDao().createOrUpdate(resourcesGroupMap); _logger.debug("CreateOrUpdate ResourcesGroupMap:[{}],Create:{},Update:{},Lines Changed:{}", resourcesGroupMap, status.isCreated(), status.isUpdated(), status.getNumLinesChanged()); } catch (SQLException ex) { _logger.error("unable to CreateOrUpdate ResourcesGroup:[{}]", resourcesGroupMap, ex); throw new McDatabaseException(ex); } }