Java Code Examples for com.j256.ormlite.stmt.QueryBuilder#setCountOf()

The following examples show how to use com.j256.ormlite.stmt.QueryBuilder#setCountOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBFutureAlert.java    From passopolis-server with GNU General Public License v3.0 6 votes vote down vote up
public static void addNew(Manager manager,
    AlertPredicate alertType, DBProcessedAudit audit, DBIdentity userToAlert, long minTimestampMs) throws SQLException {
  // These must be unique on (secretId, userId, alertType)  
  // e.g. If I share, then unshare, then re-share something 
  // with a user, only one of these events should ever be added.
  
  QueryBuilder<DBFutureAlert,Integer> builder = manager.futureAlertDao.queryBuilder();
  builder.where().eq(INACTIVE, false)
      .and().eq(ALERT_TYPE, new SelectArg(alertType))
      .and().eq(USER_ID_TO_ALERT, userToAlert.getId());
  
  builder.setCountOf(true);
  if (manager.futureAlertDao.countOf(builder.prepare()) > 0) {
    logger.info("Trying to add new alert for {}, {}. Existing alert already exists. Ignoring",
        alertType, userToAlert.getId());
  } else {
    logger.info("Inserting future alert for {}, {}.",
        alertType, userToAlert.getId());
    DBFutureAlert a = new DBFutureAlert();
    a.userIdToAlert = userToAlert.getId();
    a.transactionId = audit.getTransactionId();
    a.enqueueTimestampMs = minTimestampMs;
    a.alertType = alertType;
    manager.futureAlertDao.create(a);
  }
}
 
Example 2
Source File: NewInactiveUserEmailer.java    From passopolis-server with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected boolean internalProcess(AlerterContext context) throws SQLException {
  // get all actions that user did after joining.
  if (System.currentTimeMillis() < TIME_TO_WAIT_MS + context.dbAlertObject.enqueueTimestampMs) {
    context.dbAlertObject.nextCheckTimestampMs = context.dbAlertObject.enqueueTimestampMs + TIME_TO_WAIT_MS;
    return false;
  }
  QueryBuilder<DBProcessedAudit,Integer> builder = getBuilder(context, CANCEL_ALERT_ACTIONS, AlertUserType.ACTOR);

  // for some reason limit(int) is deprecated, WTF.
  builder.limit(1L);
  builder.setCountOf(true);
  
  if (context.manager.processedAuditDao.countOf(builder.prepare()) == 0) {
    // no such actions have happened. we should trigger the alert.
    logger.warn("should send unimplemented email for idle user");
    // TODO: send idle user email
  }
  return true;
}
 
Example 3
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 获取满足指定条件的记录数
 *
 * @param map 查询条件键值组合
 * @return
 */
public long getCount(Map<String, Object> map) throws SQLException {
    QueryBuilder queryBuilder = ormLiteDao.queryBuilder();
    queryBuilder.setCountOf(true);
    Where where = queryBuilder.where();
    where.isNotNull("id");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        where.and().eq(entry.getKey(), entry.getValue());
    }
    PreparedQuery<T> preparedQuery = queryBuilder.prepare();
    return ormLiteDao.countOf(preparedQuery);
}
 
Example 4
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
public long getCount() throws SQLException {
    QueryBuilder queryBuilder = ormLiteDao.queryBuilder();
    queryBuilder.setCountOf(true);
    Where where = queryBuilder.where();
    where.isNotNull("id");
    PreparedQuery<T> preparedQuery = queryBuilder.prepare();
    return ormLiteDao.countOf(preparedQuery);
}
 
Example 5
Source File: FirstSecretEmailer.java    From passopolis-server with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean internalProcess(AlerterContext context) throws SQLException {
  QueryBuilder<DBProcessedAudit,Integer> builder = getBuilder(context,
      ImmutableList.of(ActionType.CREATE_SECRET),
      AlertUserType.ACTOR);
  builder.setCountOf(true);
  if (context.manager.processedAuditDao.countOf(builder.prepare()) > 0) {
    enqueueEmail(context, Collections.<String,String>emptyMap(), DBEmailQueue.Type.MANDRILL_SAVE_FIRST_SECRET);
    return true;
  }
  return false;
}
 
Example 6
Source File: RockyDao.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
/**
 * 判断是否存在
 * 
 * @return
 */
public boolean exists(Map<String, Object> map)
{
	if (map == null || map.isEmpty())
	{
		throw new NullPointerException("Map can not be null or empty!!!");
	}

	try
	{
		QueryBuilder<Model, ?> queryBuilder = mRuntimeDao.queryBuilder();
		queryBuilder.setCountOf(true);
		Where<Model, ?> where = null;

		Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
		Entry<String, Object> entry = null;
		boolean isFirst = true;
		while (iterator.hasNext())
		{
			entry = iterator.next();

			if (isFirst)
			{
				where = queryBuilder.where().eq(entry.getKey(), entry.getValue());
				isFirst = false;
			}
			else
			{
				where.and().eq(entry.getKey(), entry.getValue());
			}

			// 此法仅能保证一个
			// queryBuilder.where().eq(entry.getKey(), entry.getValue());
		}

		PreparedQuery<Model> preparedQuery = queryBuilder.prepare();
		long count = mRuntimeDao.countOf(preparedQuery);

		return count > 0 ? true : false;
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}

	return false;
}