Java Code Examples for com.j256.ormlite.dao.BaseDaoImpl#queryBuilder()

The following examples show how to use com.j256.ormlite.dao.BaseDaoImpl#queryBuilder() . 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: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testInSubQuery() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	qb.selectColumns(ID_COLUMN_NAME);
	where.in(ID_COLUMN_NAME, qb);
	StringBuilder whereSb = new StringBuilder();
	where.appendSql(null, whereSb, new ArrayList<ArgumentHolder>());
	StringBuilder sb = new StringBuilder();
	databaseType.appendEscapedEntityName(sb, ID_COLUMN_NAME);
	sb.append(" IN (");
	sb.append("SELECT ");
	databaseType.appendEscapedEntityName(sb, ID_COLUMN_NAME);
	sb.append(" FROM ");
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" ) ");
	assertEquals(sb.toString(), whereSb.toString());
}
 
Example 2
Source File: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testExistsSubQuery() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	where.exists(qb);
	StringBuilder whereSb = new StringBuilder();
	where.appendSql(null, whereSb, new ArrayList<ArgumentHolder>());
	StringBuilder sb = new StringBuilder();
	sb.append("EXISTS (");
	sb.append("SELECT * FROM ");
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" ) ");
	assertEquals(sb.toString(), whereSb.toString());
}
 
Example 3
Source File: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testNotExistsSubQuery() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	where.not().exists(qb);
	StringBuilder whereSb = new StringBuilder();
	where.appendSql(null, whereSb, new ArrayList<ArgumentHolder>());
	StringBuilder sb = new StringBuilder();
	sb.append("(NOT EXISTS (");
	sb.append("SELECT * FROM ");
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" ) ) ");
	assertEquals(sb.toString(), whereSb.toString());
}
 
Example 4
Source File: HsqldbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Override
@Test
public void testLimitFormat() throws Exception {
	connectionSource.setDatabaseType(databaseType);
	BaseDaoImpl<StringId, String> dao = new BaseDaoImpl<StringId, String>(connectionSource, StringId.class) {
	};
	dao.initialize();
	QueryBuilder<StringId, String> qb = dao.queryBuilder();
	long limit = 1232;
	qb.limit(limit);
	String query = qb.prepareStatementString();
	assertTrue(query + " should start with stuff", query.startsWith("SELECT LIMIT 0 " + limit + " "));
}
 
Example 5
Source File: InSubQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testTwoResultsInSubQuery() throws Exception {
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qbInner = foreignDao.queryBuilder();
	qbInner.selectColumns(ID_COLUMN_NAME);
	QueryBuilder<ForeignFoo, Integer> qbOuter = foreignDao.queryBuilder();
	qbInner.selectColumns(FOREIGN_COLUMN_NAME);

	Where<ForeignFoo, Integer> where = qbOuter.where();
	where.in(ID_COLUMN_NAME, qbInner);
	where.prepare();
}
 
Example 6
Source File: InSubQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testResultColumnNoMatchWhere() throws Exception {
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qbInner = foreignDao.queryBuilder();
	qbInner.selectColumns(STRING_COLUMN_NAME);
	QueryBuilder<ForeignFoo, Integer> qbOuter = foreignDao.queryBuilder();
	Where<ForeignFoo, Integer> where = qbOuter.where();
	where.in(ID_COLUMN_NAME, qbInner);
	where.prepare();
}
 
Example 7
Source File: WhereTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testInSubQueryToManySubColumns() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	qb.selectColumns(ID_COLUMN_NAME, FOREIGN_COLUMN_NAME);
	where.in(ID_COLUMN_NAME, qb);
}