Java Code Examples for com.j256.ormlite.stmt.Where#and()

The following examples show how to use com.j256.ormlite.stmt.Where#and() . These examples are extracted from open source projects. 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
public List<DialogOccupant> getActualDialogOccupantsByIds(String dialogId, List<Integer> userIdsList) {
    List<DialogOccupant> dialogOccupantsList = Collections.emptyList();

    try {
        QueryBuilder<DialogOccupant, Long> queryBuilder = dao.queryBuilder();
        Where<DialogOccupant, Long> where = queryBuilder.where();
        where.and(
                where.in(QMUserColumns.ID, userIdsList),
                where.eq(DialogOccupant.Column.STATUS, DialogOccupant.Status.ACTUAL),
                where.eq(Dialog.Column.ID, dialogId)
        );
        PreparedQuery<DialogOccupant> preparedQuery = queryBuilder.prepare();
        dialogOccupantsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogOccupantsList;
}
 
Example 2
public List<DialogOccupant> getActualDialogOccupantsByDialog(String dialogId) {
    List<DialogOccupant> dialogOccupantsList = Collections.emptyList();

    try {
        QueryBuilder<DialogOccupant, Long> queryBuilder = dao.queryBuilder();
        Where<DialogOccupant, Long> where = queryBuilder.where();
        where.and(
                where.eq(DialogOccupant.Column.STATUS, DialogOccupant.Status.ACTUAL),
                where.eq(Dialog.Column.ID, dialogId)
        );
        PreparedQuery<DialogOccupant> preparedQuery = queryBuilder.prepare();
        dialogOccupantsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogOccupantsList;
}
 
Example 3
public DialogNotification getDialogNotificationByDialogId(boolean firstMessage, List<Long> dialogOccupantsList) {
    DialogNotification dialogNotification = null;

    try {
        QueryBuilder<DialogNotification, Long> queryBuilder = dao.queryBuilder();
        Where<DialogNotification, Long> where = queryBuilder.where();
        where.and(
                where.in(DialogOccupant.Column.ID, dialogOccupantsList),
                where.eq(DialogNotification.Column.STATE, State.READ)
        );
        queryBuilder.orderBy(DialogNotification.Column.CREATED_DATE, firstMessage);
        PreparedQuery<DialogNotification> preparedQuery = queryBuilder.prepare();
        dialogNotification = dao.queryForFirst(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotification;
}
 
Example 4
public Message getMessageByDialogId(boolean firstMessage, List<Long> dialogOccupantsList) {
    Message message = null;

    try {
        QueryBuilder<Message, Long> queryBuilder = dao.queryBuilder();
        Where<Message, Long> where = queryBuilder.where();
        where.and(
                where.in(DialogOccupant.Column.ID, dialogOccupantsList),
                where.eq(Message.Column.STATE, State.READ)
        );
        queryBuilder.orderBy(Message.Column.CREATED_DATE, firstMessage);
        PreparedQuery<Message> preparedQuery = queryBuilder.prepare();
        message = dao.queryForFirst(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return message;
}
 
Example 5
public void deleteTempMessages(List<Long> dialogOccupantsIdsList) {
    try {
        DeleteBuilder<Message, Long> deleteBuilder = dao.deleteBuilder();

        Where<Message, Long> where = deleteBuilder.where();
        where.and(
                where.in(DialogOccupant.Column.ID, dialogOccupantsIdsList),
                where.or(
                        where.eq(Message.Column.STATE, State.TEMP_LOCAL),
                        where.eq(Message.Column.STATE, State.TEMP_LOCAL_UNREAD)
                )
        );

        if (deleteBuilder.delete() > 0) {
            //TODO VT need to think how to send IDs to observers
            notifyObserversDeletedById(dialogOccupantsIdsList);
        }
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

}
 
Example 6
/**
 * help to build a where to query in database;;
 * @return
 */
private PreparedQuery buildWhere(String wantDay,int schoolWeek,String schoolWeekDsz) throws SQLException {
    QueryBuilder<ClassModel,Integer> queryBuilder = classDao.queryBuilder();
    queryBuilder.clear();
    Where<ClassModel,Integer> where = queryBuilder.where();
    where.and(
            where.like("day",wantDay),
            where.le("strWeek", schoolWeek),
            where.ge("endWeek", schoolWeek),
            where.or(
                    where.isNull("dsz"),
                    where.like("dsz",""),
                    where.like("dsz",schoolWeekDsz)
            )
    );
    return queryBuilder.prepare();
}
 
Example 7
Source Project: ormlite-core   File: BaseDaoImplTest.java    License: ISC License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testUseOfAndMany() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	assertEquals(0, dao.countOf());
	Foo foo1 = new Foo();
	foo1.val = 1231231;
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	foo2.val = foo1.val + 1;
	assertEquals(1, dao.create(foo2));

	Where<Foo, Integer> where = dao.queryBuilder().where();
	where.and(where.eq(Foo.VAL_COLUMN_NAME, foo1.val), where.eq(Foo.ID_COLUMN_NAME, foo1.id));

	List<Foo> results = where.query();
	assertEquals(1, results.size());
	assertEquals(foo1.id, results.get(0).id);

	// this should match none
	where.reset();
	where.and(where.eq(Foo.ID_COLUMN_NAME, foo1.id), where.eq(Foo.ID_COLUMN_NAME, foo2.id),
			where.eq(Foo.VAL_COLUMN_NAME, foo1.val), where.eq(Foo.VAL_COLUMN_NAME, foo2.val));
	results = where.query();
	assertEquals(0, results.size());
}
 
Example 8
Source Project: mage-android   File: DateTimeFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void and(Where<? extends Temporal, Long> where) throws SQLException {
	where.and();

	if (start != null && end != null) {
		where.between(columnName, start, end);
	} else if (start != null) {
		where.ge(columnName, start);
	} else {
		where.lt(columnName, end);
	}
}
 
Example 9
/**
 * Add to the where clause, either as new or with an and
 * 
 * @param qb
 *            query builder
 * @param where
 *            where clause
 * @return where clause
 */
private Where<ExtendedRelation, Long> addToWhere(
		QueryBuilder<ExtendedRelation, Long> qb,
		Where<ExtendedRelation, Long> where) {
	if (where == null) {
		where = qb.where();
	} else {
		where.and();
	}
	return where;
}
 
Example 10
public DialogOccupant getDialogOccupant(String dialogId, int userId) {
    DialogOccupant dialogOccupant = null;

    try {
        QueryBuilder<DialogOccupant, Long> queryBuilder = dao.queryBuilder();
        Where<DialogOccupant, Long> where = queryBuilder.where();
        where.and(where.eq(Dialog.Column.ID, dialogId), where.eq(QMUserColumns.ID, userId));
        PreparedQuery<DialogOccupant> preparedQuery = queryBuilder.prepare();
        dialogOccupant = dao.queryForFirst(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogOccupant;
}
 
Example 11
public List<DialogNotification> getDialogNotificationsByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao.queryBuilder();

        Where<DialogNotification, Long> where = messageQueryBuilder.where();
        where.and(where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL),
                where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(DialogNotification.Column.CREATED_DATE, createdDate)
                        : where.lt(DialogNotification.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 12
public List<DialogNotification> getDialogNotificationsByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate, long limit) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao.queryBuilder();

        Where<DialogNotification, Long> where = messageQueryBuilder.where();
        where.and(where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL),
                where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(DialogNotification.Column.CREATED_DATE, createdDate)
                        : where.lt(DialogNotification.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(DialogNotification.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 13
public List<Message> getMessagesByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate){
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        Where<Message, Long> where = messageQueryBuilder.where();
        where.and(where.ne(Message.Column.STATE, State.TEMP_LOCAL),
                where.ne(Message.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(Message.Column.CREATED_DATE, createdDate)
                        : where.lt(Message.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 14
public List<Message> getMessagesByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate, long limit){
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        Where<Message, Long> where = messageQueryBuilder.where();
        where.and(where.ne(Message.Column.STATE, State.TEMP_LOCAL),
                where.ne(Message.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(Message.Column.CREATED_DATE, createdDate)
                        : where.lt(Message.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(Message.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 15
Source Project: ormlite-jdbc   File: JdbcBaseDaoImplTest.java    License: ISC License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testUseOfAndMany() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	assertEquals(0, dao.countOf());
	Foo foo = new Foo();
	int id = 1;
	foo.id = id;
	int val = 1231231;
	foo.val = val;
	assertEquals(1, dao.create(foo));
	int notId = id + 1;
	foo.id = notId;
	foo.val = val + 1;
	assertEquals(1, dao.create(foo));

	Where<Foo, Integer> where = dao.queryBuilder().where();
	where.and(where.eq(Foo.VAL_FIELD_NAME, val), where.eq(Foo.ID_FIELD_NAME, id));

	List<Foo> results = where.query();
	assertEquals(1, results.size());
	assertEquals(id, results.get(0).id);

	// this should match none
	where.reset();
	where.and(where.eq(Foo.VAL_FIELD_NAME, val), where.eq(Foo.ID_FIELD_NAME, id),
			where.eq(Foo.ID_FIELD_NAME, notId));
	results = where.query();
	assertEquals(0, results.size());
}
 
Example 16
Source Project: ormlite-jdbc   File: JdbcBaseDaoImplTest.java    License: ISC License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testUseOfAndInt() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	assertEquals(0, dao.countOf());
	Foo foo = new Foo();
	int id = 1;
	foo.id = id;
	int val = 1231231;
	foo.val = val;
	assertEquals(1, dao.create(foo));
	int notId = id + 1;
	foo.id = notId;
	foo.val = val + 1;
	assertEquals(1, dao.create(foo));

	Where<Foo, Integer> where = dao.queryBuilder().where();
	where.and(where.eq(Foo.VAL_FIELD_NAME, val), where.eq(Foo.ID_FIELD_NAME, id));

	List<Foo> results = where.query();
	assertEquals(1, results.size());
	assertEquals(id, results.get(0).id);

	// this should match none
	where.reset();
	where.eq(Foo.VAL_FIELD_NAME, val);
	where.eq(Foo.ID_FIELD_NAME, id);
	where.eq(Foo.ID_FIELD_NAME, notId);
	where.and(3);
	results = where.query();
	assertEquals(0, results.size());
}
 
Example 17
Source Project: ormlite-core   File: BaseDaoImplTest.java    License: ISC License 5 votes vote down vote up
@Test
public void testUseOfAndInt() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	assertEquals(0, dao.countOf());
	Foo foo1 = new Foo();
	foo1.val = 1231231;
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	foo2.val = foo1.val + 1;
	assertEquals(1, dao.create(foo1));

	Where<Foo, Integer> where = dao.queryBuilder().where();
	where.eq(Foo.VAL_COLUMN_NAME, foo1.val);
	where.eq(Foo.ID_COLUMN_NAME, foo1.id);
	where.and(2);

	List<Foo> results = where.query();
	assertEquals(1, results.size());
	assertEquals(foo1.id, results.get(0).id);

	// this should match none
	where.reset();
	where.eq(Foo.ID_COLUMN_NAME, foo1.id);
	where.eq(Foo.ID_COLUMN_NAME, foo2.id);
	where.eq(Foo.VAL_COLUMN_NAME, foo1.val);
	where.eq(Foo.VAL_COLUMN_NAME, foo2.val);
	where.and(4);

	results = where.query();
	assertEquals(0, results.size());
}
 
Example 18
@SuppressWarnings({ "unchecked" })
protected List<RPC.AuditEvent> queryAuditEvents(
    Manager manager,
    Set<Integer> userIds,
    Set<Integer> secretIds,
    Set<Integer> groupIds,
    Long limit, Long offset,
    Long startTimeMs, Long endTimeMs) throws SQLException, MitroServletException {
  List<RPC.AuditEvent> events = Lists.newArrayList();

  QueryBuilder<DBProcessedAudit, Integer> query = manager.processedAuditDao.queryBuilder();
  Where<DBProcessedAudit, Integer> where = query.where();

  List<Where<DBProcessedAudit, Integer>> clauses = new ArrayList<>();
  if (!userIds.isEmpty()) {
    clauses.add(where.and(where.in(DBProcessedAudit.ACTOR_FIELD_NAME, userIds),
                          where.in(DBProcessedAudit.ACTION_FIELD_NAME, 
                              Manager.makeSelectArgsFromList(USER_ACTION_TYPES))));
  }
  if (!secretIds.isEmpty()) {
    clauses.add(where.and(where.in(DBProcessedAudit.AFFECTED_SECRET_FIELD_NAME, secretIds),
                          where.in(DBProcessedAudit.ACTION_FIELD_NAME, 
                              Manager.makeSelectArgsFromList(SECRET_ACTION_TYPES))));
  }
  if (!groupIds.isEmpty()) {
    clauses.add(where.and(where.in(DBProcessedAudit.AFFECTED_GROUP_FIELD_NAME, groupIds),
                          where.in(DBProcessedAudit.ACTION_FIELD_NAME, 
                              Manager.makeSelectArgsFromList(GROUP_ACTION_TYPES))));
  }

  if (clauses.size() > 0) {
    orWhereClauses(where, clauses);

    if (startTimeMs != null) {
      where.and(where, where.ge(DBProcessedAudit.TIMESTAMP_FIELD_NAME, startTimeMs));
    }
    if (endTimeMs != null) {
      if (startTimeMs != null && startTimeMs > endTimeMs) {
        throw new MitroServletException("start time must be before end time");
      }
      where.and(where, where.le(DBProcessedAudit.TIMESTAMP_FIELD_NAME, endTimeMs));
    }
    // offset and limit of null interpreted as no offset and no limit.
    // Boolean param of orderBy specifies DESC order.
    query.offset(offset).limit(limit).orderBy(DBProcessedAudit.TIMESTAMP_FIELD_NAME, false);
    for (DBProcessedAudit dbAudit : query.query()) {
      RPC.AuditEvent auditEvent = new RPC.AuditEvent();
      fillRPCAuditEvent(manager, dbAudit, auditEvent);
      events.add(auditEvent);
    }
  }
  return events;
}