JoinsTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.JOINS) { throw new DalRuntimeException("Internal error: only JOINS token is supported by " + getClass()); } switch (ctx.getQuery().getType()) { case SELECT: EntityInfo entityInfo = ctx.getEntityInfo(); SubObjects subobject = entityInfo.getSubobjects(ctx.getReadset()); StringBuilder sb = new StringBuilder(256); if (subobject != null) { String[] names = subobject.value(); for (String name : names) { if (name != null && name.length() > 0) { Relation relation = entityInfo.getRelation(name); if (sb.length() > 0) { sb.append(" and "); } sb.append(relation.join()); } } } if (sb.length() == 0) { sb.append("1=1"); } return sb.toString(); case INSERT: throw new DalRuntimeException("TABLES token does not support query type: " + ctx.getQuery().getType()); case UPDATE: throw new DalRuntimeException("TABLES token does not support query type: " + ctx.getQuery().getType()); case DELETE: throw new DalRuntimeException("TABLES token does not support query type: " + ctx.getQuery().getType()); default: throw new DalRuntimeException("TABLES token does not support query type: " + ctx.getQuery().getType()); } }  String resolve(Token token, QueryContext ctx);  }
@Test public void testSelectOneTable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<JOINS/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("1=1", ctx.getSqlStatement()); }
@Test public void testSelectOneTable2() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<JOINS/>"); Readset<?> readset = UserEntity.READSET_FULL_U; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("1=1", ctx.getSqlStatement()); }
@Test public void testSelectTwoTables() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<JOINS/>"); Readset<?> readset = UserEntity.READSET_FULL_WITH_HOME_ADDRESS_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("u.user_id=hua.user_id and hua.type='H'", ctx.getSqlStatement()); }
@Test public void testInsert() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.INSERT, "<JOINS/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }
@Test public void testUpdate() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.UPDATE, "<JOINS/>"); Updateset<User> updateset = UserEntity.UPDATESET_FULL; User user = new User(); QueryContext ctx = getUpdateContext(query, user, updateset); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }
@Test public void testDelete() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.DELETE, "<JOINS/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }