TablesTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.TABLES) { throw new DalRuntimeException("Internal error: only TABLES 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); String table = getPhysicalName(ctx, entityInfo.getLogicalName()); sb.append(m_naming.getTable(table, entityInfo.getAlias())); if (subobject != null) { String[] names = subobject.value(); Set<String> done = new HashSet<String>(); done.add(entityInfo.getLogicalName()); for (String name : names) { if (name != null && name.length() > 0) { if (done.contains(name)) { continue; } else { done.add(name); } Relation relation = entityInfo.getRelation(name); sb.append(", "); sb.append(m_naming.getTable(getPhysicalName(ctx, relation.logicalName()), relation.alias())); } } } ctx.setTableResolved(true); 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, "<TABLES/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("`user` u", ctx.getSqlStatement()); Assert.assertEquals(true, ctx.isTableResolved()); }
@Test public void testSelectTwoTables() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLES/>"); 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("`user` u, user_address hua", ctx.getSqlStatement()); Assert.assertEquals(true, ctx.isTableResolved()); }
@Test public void testSelectThreeTables() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLES/>"); Readset<?> readset = UserEntity.READSET_FULL_WITH_HOME_OFFICE_ADDRESS_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("`user` u, user_address hua, user_address oua", ctx.getSqlStatement()); Assert.assertEquals(true, ctx.isTableResolved()); }
@Test public void testInsert() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.INSERT, "<TABLES/>"); 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, "<TABLES/>"); 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, "<TABLES/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }