TableTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.TABLE) { throw new DalRuntimeException("Internal error: only TABLE token is supported by " + getClass()); } SimpleTagToken table = (SimpleTagToken) token; String tableName = table.getAttribute("name", ctx.getEntityInfo().getLogicalName()); String[] logicalNameAndAlias = ctx.getEntityInfo().getLogicalNameAndAlias(tableName); TableProvider tableProvider = m_manager.getTableProvider(logicalNameAndAlias[0]); String physicalTableName = tableProvider.getPhysicalTableName(ctx.getQueryHints(), tableName); switch (ctx.getQuery().getType()) { case SELECT: String alias = table.getAttribute("alias", logicalNameAndAlias[1]); ctx.setTableResolved(true); return m_naming.getTable(physicalTableName, alias); case INSERT: return m_naming.getTable(physicalTableName); case UPDATE: return m_naming.getTable(physicalTableName); case DELETE: return m_naming.getTable(physicalTableName); default: throw new DalRuntimeException("TABLE token does not support query type: " + ctx.getQuery().getType()); } }  String resolve(Token token, QueryContext ctx);  }
@Test public void testAnotherTableRelated() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE name='home-address'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("user_address hua", ctx.getSqlStatement()); }
@Test public void testAnotherTableRelatedWithAlias() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE name='home-address' alias='address'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("user_address address", ctx.getSqlStatement()); }
@Test public void testAnotherTableNonrelated() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE name='user2'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }
@Test public void testCurrentTable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE/>"); 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 testCurrentTable2() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE name='user'/>"); 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 testCurrentTableWithAlias() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE alias='user'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("`user` user", ctx.getSqlStatement()); Assert.assertEquals(true, ctx.isTableResolved()); }
@Test public void testNonExistTable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<TABLE name='unknown'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); try { m_queryResolver.resolve(ctx); Assert.fail("DalRuntimeException expected"); } catch (DalRuntimeException e) { } }
@Test public void testCurrentTableForInsert() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.INSERT, "<TABLE/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); m_queryResolver.resolve(ctx); Assert.assertEquals("`user`", ctx.getSqlStatement()); }
@Test public void testCurrentTableForUpdate() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.UPDATE, "<TABLE/>"); Updateset<User> updateset = UserEntity.UPDATESET_FULL; User user = new User(); QueryContext ctx = getUpdateContext(query, user, updateset); m_queryResolver.resolve(ctx); Assert.assertEquals("`user`", ctx.getSqlStatement()); }
@Test public void testCurrentTableForDelete() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.DELETE, "<TABLE/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); m_queryResolver.resolve(ctx); Assert.assertEquals("`user`", ctx.getSqlStatement()); }