private void addSync() { QueryBuilder<Donor, Integer> queryBuilder = databaseHelper.getDonorListDao().queryBuilder(); // get the WHERE object to build our query Where<Donor, Integer> where = queryBuilder.where(); try { where.eq("status", new Integer(DhenupaApplication.STATUS_DONOR_ADDED)); // and where.or(); where.eq("status", new Integer(DhenupaApplication.STATUS_DONOR_UPDATED)); PreparedQuery<Donor> preparedQuery = queryBuilder.prepare(); List<Donor> donorList = databaseHelper.getDonorListDao().query(preparedQuery); for (Donor donor : donorList) { new DonorManager(this).addNewDonor(donor); // Sleep is added to maintain a different Timestamp between multiple entries try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (SQLException e) { e.printStackTrace(); } }
public void deleteByDataset(final int dataset) { try { DeleteBuilder<Data, Integer> deleteBuilder = getDataDao().deleteBuilder(); Where<Data, Integer> where = deleteBuilder.where(); where.eq("dataset_id", dataset); deleteBuilder.delete(); } catch (SQLException e) { e.printStackTrace(); } }
public List<Data> getDataByDataset(final int datasetId) { try { QueryBuilder<Data, Integer> queryBuilder = getDataDao().queryBuilder(); Where<Data, Integer> where = queryBuilder.where(); where.eq("dataset_id", datasetId); PreparedQuery<Data> preparedQuery = queryBuilder.prepare(); return getDataDao().query(preparedQuery); } catch (SQLException e) { e.printStackTrace(); } return null; }
public void update(final Dataset dataset, final Field field, final String data) { try { UpdateBuilder<Data, Integer> updateBuilder = getDataDao().updateBuilder(); Where<Data, Integer> where = updateBuilder.where(); where.eq("dataset_id", dataset); where.and(); where.eq("field_id", field); updateBuilder.updateColumnValue("data" /* column */, data /* value */); updateBuilder.update(); } catch (SQLException e) { e.printStackTrace(); } }
public Data getData(final int datasetId, final int fieldId) { try { QueryBuilder<Data, Integer> queryBuilder = getDataDao().queryBuilder(); Where<Data, Integer> where = queryBuilder.where(); where.eq("dataset_id", datasetId); where.and(); where.eq("field_id", fieldId); PreparedQuery<Data> preparedQuery = queryBuilder.prepare(); return getDataDao().queryForFirst(preparedQuery); } catch (SQLException e) { e.printStackTrace(); } return null; }
public CloseableIterator<PlayerWarnData> findWarnings(long fromTime) throws SQLException { if (fromTime == 0) { return iterator(); } long checkTime = fromTime + DateUtils.getTimeDiff(); QueryBuilder<PlayerWarnData, Integer> query = queryBuilder(); Where<PlayerWarnData, Integer> where = query.where(); where.ge("created", checkTime); query.setWhere(where); return query.iterator(); }
@Test public void testWhere() throws Exception { QueryBuilder<Foo, Integer> qb = new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, null); Where<Foo, Integer> where = qb.where(); String val = "1"; where.eq(Foo.ID_COLUMN_NAME, val); StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM "); databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName()); sb.append(" WHERE "); databaseType.appendEscapedEntityName(sb, Foo.ID_COLUMN_NAME); sb.append(" = ").append(val).append(' '); assertEquals(sb.toString(), qb.prepareStatementString()); }
@SuppressWarnings("unchecked") public int deleteMarkStarredAndUnStarred(Article article) { try { DeleteBuilder<ArticleSyncAction, Integer> deleteBuilder = deleteBuilder(); Where<ArticleSyncAction, Integer> where = deleteBuilder.where(); where.and( where.eq(COLUMN_ARTICLEID, article.getId()), where .eq(COLUMN_ACTION, ArticleSyncAction.Action.MarkStarred) .or() .eq(COLUMN_ACTION, ArticleSyncAction.Action.MarkUnstarred)); return deleteBuilder.delete(); } catch (SQLException e) { throw new RuntimeException(e); } }
public static ArrayList<Colleage> queryAll(String userId) { try { QueryBuilder<Colleage, String> queryBuilder = getDao().queryBuilder(); Where<Colleage, String> where = queryBuilder.where(); where.eq(Colleage.USER_ID, userId); queryBuilder.orderBy(Colleage.CREATETIME, false); List<Colleage> resultList = getDao().query(queryBuilder.prepare()); if (TextUtil.isValidate(resultList)) { return (ArrayList<Colleage>) resultList; } } catch (SQLException e) { e.printStackTrace(); } catch (DBNotInitializeException e) { e.printStackTrace(); } return null; }
@Override public List<Photocategory> getCategories(String id) { if (noUid()) { return null; } Where<Photocategory, String> where = this.category.queryBuilder().where(); List<Photocategory> datas = null; try { datas = where.eq("_Users_uid", this.uid).and().eq("parentid", id).query(); // 找子类 return datas; } catch (SQLException e) { log.setExcetion(id, e); return null; } }
/** * 查询标题和描述一致的专辑 * * @param pDiggerAlbum 专辑对象 * @return */ public ArrayList<DiggerAlbum> existedDiggerAlbum(DiggerAlbum pDiggerAlbum) { List<DiggerAlbum> list = new ArrayList<>(); QueryBuilder<DiggerAlbum, String> builder = mDiggerAlbumDao.queryBuilder(); Where<DiggerAlbum, String> where = builder.where(); try { where .eq(COLUMN_ALBUM_TITLE, pDiggerAlbum.getAlbum_title()) .and() .eq(COLUMN_ALBUM_DES, pDiggerAlbum.getAlbum_des()); list = builder.query(); if (TextUtil.isListEmpty(list)) { return new ArrayList<>(); } } catch (SQLException e) { e.printStackTrace(); } return new ArrayList<>(list); }
public void delete(final Dataset dataset, final Field field, final String data) { try { DeleteBuilder<Data, Integer> deleteBuilder = getDataDao().deleteBuilder(); Where<Data, Integer> where = deleteBuilder.where(); where.eq("dataset_id", dataset); where.and(); where.eq("field_id", field); where.and(); where.eq("data", data); deleteBuilder.delete(); } catch (SQLException e) { e.printStackTrace(); } }
private List<KnouNoticeInfo> getListKnouNoticeInfo(String blngDc, String blngCd, String anncNo) { List<KnouNoticeInfo> knouNoticeInfoList = null; try { Dao<KnouNoticeInfo, Long> knouNoticeInfoDao = this.getHelper().getKnouNoticeInfoDao(); QueryBuilder<KnouNoticeInfo, Long> qb = knouNoticeInfoDao.queryBuilder(); Where where = qb.where(); where.eq(KNOU_NOTICE.blngDc, blngDc); where.and(); where.eq(KNOU_NOTICE.blngCd, blngCd); where.and(); where.eq(KNOU_NOTICE.anncNo, anncNo); System.out.println("sql:" + qb.prepareStatementString()); knouNoticeInfoList = qb.query(); } catch (SQLException e) { e.printStackTrace(); } return knouNoticeInfoList; }
public <E extends SyncEntity> void synObjects( final Class<E> entityClass, boolean userAware, final SyncCallback<E> callback) throws SyncException { try { final String parseClass = extractParseClass(entityClass); // get updated remote objects ParseQuery query = ParseQuery.getQuery(parseClass); query.whereGreaterThan(PARSE_UPDATED_AT_FIELD, lastSyncDate); if (userAware && userId != null) query.whereEqualTo(PARSE_USER_ID_FIELD, userId); // query.orderByAscending(PARSE_OBJECT_ID_FIELD); final List<ParseObject> remoteObjects = ParseTools.findAllParseObjects(query); // get updated local objects final SyncDAO<E, ?> syncDao = dbHelper.getDao(entityClass); QueryBuilder<E, ?> dbQuery = syncDao.queryBuilder(); // dbQuery.orderBy(DB_OBJECT_ID_FIELD, true); Where<E, ?> where = dbQuery.where().gt(DB_UPDATED_AT_FIELD, lastSyncDate); if (userAware && userId != null) where.and().eq(DB_USER_ID_FIELD, userId); final List<E> localObjects = where.query(); // create local object map Map<String, E> localObjectMap = new HashMap<String, E>(localObjects.size()); for (E localObject : localObjects) { localObjectMap.put(localObject.getSyncId(), localObject); } List<Pair<E, ParseObject>> toSaveLocally = new ArrayList<>(); List<Pair<E, ParseObject>> toSaveRemotely = new ArrayList<>(); for (ParseObject remoteObject : remoteObjects) { String syncId = remoteObject.getObjectId(); E localObject = localObjectMap.get(syncId); if (localObject == null) { localObject = findBySyncId(syncDao, syncId); if (localObject == null) { // this object was created on the server but doesn't exist locally localObject = SyncEntity.fromParseObject(remoteObject, entityClass); } else { // the object exists locally but out-of-date SyncEntity.fromParseObject(remoteObject, localObject); } toSaveLocally.add(new Pair<>(localObject, remoteObject)); continue; } if (localObject != null) { long localTime = (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime(); long remoteTime = (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime(); if (remoteTime > localTime) { // the remote object is newer SyncEntity.fromParseObject(remoteObject, localObject); toSaveLocally.add(new Pair<>(localObject, remoteObject)); } else if (remoteTime < localTime) { // the local objects is newer SyncEntity.toParseObject(localObject, remoteObject); toSaveRemotely.add(new Pair<>(localObject, remoteObject)); } } } localObjectMap = null; // create remote object map Map<String, ParseObject> remoteObjectMap = new HashMap<String, ParseObject>(remoteObjects.size()); for (ParseObject remoteObject : remoteObjects) { remoteObjectMap.put(remoteObject.getObjectId(), remoteObject); } for (E localObject : localObjects) { String syncId = localObject.getSyncId(); if (syncId == null) { // a brand new object! ParseObject remoteObject = SyncEntity.toParseObject(localObject); toSaveRemotely.add(new Pair<>(localObject, remoteObject)); continue; } ParseObject remoteObject = remoteObjectMap.get(syncId); if (remoteObject == null && !localObject.isDeleted()) { // object was created locally but doesn't exist or too old on the server // this is weird because syncId is not null // try to get it from server remoteObject = ParseObject.createWithoutData(parseClass, syncId).fetch(); toSaveRemotely.add(new Pair<>(localObject, remoteObject)); continue; } if (remoteObject != null) { long localTime = (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime(); long remoteTime = (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime(); if (remoteTime > localTime) { // the remote object is newer SyncEntity.fromParseObject(remoteObject, localObject); toSaveLocally.add(new Pair<>(localObject, remoteObject)); } else if (remoteTime < localTime) { // the local objects is newer SyncEntity.toParseObject(localObject, remoteObject); toSaveRemotely.add(new Pair<>(localObject, remoteObject)); } } } if (callback != null) { callback.beforeSync(toSaveLocally, toSaveRemotely); } for (Pair<E, ParseObject> p : toSaveLocally) { final E localObject = p.first; final ParseObject remoteObject = p.second; TransactionManager.callInTransaction( dbHelper.getConnectionSource(), new Callable<Object>() { @Override public Object call() throws Exception { if (callback != null) { callback.onSaveLocally(localObject, remoteObject); } syncDao.createOrUpdate(localObject); return null; } }); } for (Pair<E, ParseObject> p : toSaveRemotely) { final E localObject = p.first; final ParseObject remoteObject = p.second; TransactionManager.callInTransaction( dbHelper.getConnectionSource(), new Callable<Object>() { @Override public Object call() throws Exception { if (callback != null) { callback.onSaveRemotely(localObject, remoteObject); } remoteObject.save(); if (localObject.getSyncId() == null) { SyncEntity.fromParseObject(remoteObject, localObject); syncDao.createOrUpdate(localObject); } return null; } }); } if (callback != null) { callback.afterSync(); } } catch (Exception ex) { throw new SyncException("Synchronization failed", ex); } }