/**
* Tests query creation with qualifier and ordering.
*/
public void testDbEntityQualifier() throws Exception {
ObjectContext context = createDataContext();
SelectQuery q = new SelectQuery(Artist.class);
final DbEntity entity = getNode().getEntityResolver().getDbEntity("ARTIST");
final DbEntity middleEntity = getNode().getEntityResolver().getDbEntity("ARTIST_GROUP");
entity.setQualifier(Expression.fromString("ARTIST_NAME = \"123\""));
middleEntity.setQualifier(Expression.fromString("GROUP_ID = 1987"));
try {
Template test = new Template() {
@Override
void test(SelectTranslator transl) throws Exception {
String generatedSql = transl.createSqlString();
// do some simple assertions to make sure all parts are in
assertNotNull(generatedSql);
assertTrue(generatedSql.startsWith("SELECT "));
assertTrue(generatedSql.indexOf(" FROM ") > 0);
if (generatedSql.contains("RTRIM")) {
assertTrue(generatedSql.indexOf("ARTIST_NAME) = ") > generatedSql.indexOf("RTRIM("));
}
else {
assertTrue(generatedSql.indexOf("ARTIST_NAME = ") > 0);
}
}
};
test.test(q);
context.performQuery(q);
//testing outer join!!
q = new SelectQuery(Painting.class);
q.addOrdering("toArtist+.artistName", SortOrder.ASCENDING);
test.test(q);
context.performQuery(q);
//testing quering from related table
q = new SelectQuery(Painting.class,
ExpressionFactory.matchExp("toArtist.artistName", "foo"));
test.test(q);
context.performQuery(q);
//testing flattened rels
q = new SelectQuery(Artist.class, ExpressionFactory.matchExp("groupArray.name", "bar"));
new Template() {
@Override
void test(SelectTranslator transl) throws Exception {
assertTrue(transl.createSqlString().indexOf("GROUP_ID = ") > 0);
}
}.test(q);
context.performQuery(q);
}
finally {
entity.setQualifier(null);
middleEntity.setQualifier(null);
}