FieldTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.FIELD) { throw new DalRuntimeException("Internal error: only FIELD token is supported by " + getClass()); } SimpleTagToken field = (SimpleTagToken) token; String fieldName = field.getAttribute("name", null); String tableName = field.getAttribute("table", ctx.getEntityInfo().getLogicalName()); String[] logicalNameAndAlias = ctx.getEntityInfo().getLogicalNameAndAlias(tableName); EntityInfo entityInfo = m_manager.getEntityInfo(logicalNameAndAlias[0]); Attribute attribute = entityInfo.getAttribute(fieldName); if (attribute != null) { switch (ctx.getQuery().getType()) { case SELECT: if (!ctx.isTableResolved()) { DataField f = entityInfo.getFieldByName(fieldName); ctx.addOutField(f); ctx.addOutSubObjectName(null); } if (attribute.selectExpr().length() > 0) { return m_expressionResolver.resolve(ctx, attribute.selectExpr()); } else { String tableAlias = logicalNameAndAlias[1]; return m_naming.getField(attribute.field(), tableAlias); } case INSERT: return m_naming.getField(attribute.field()); case UPDATE: return m_naming.getField(attribute.field()); case DELETE: return m_naming.getField(attribute.field()); default: throw new DalRuntimeException("TABLE token does not support query type: " + ctx.getQuery().getType()); } } else { throw new DalRuntimeException("DataField(" + fieldName + ") is not defined in " + ctx.getQuery().getEntityClass() + ". Query: " + ctx.getQuery()); } }  String resolve(Token token, QueryContext ctx);  }
@Test public void testField() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='user-id'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("u.user_id", ctx.getSqlStatement()); Assert.assertEquals(false, ctx.isTableResolved()); Assert.assertEquals("[user-id]", ctx.getOutFields().toString()); }
@Test public void testFieldAndWhereClause() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "SELECT <FIELD name='user-id'/> FROM <TABLE/> WHERE <FIELD name='user-name'/> IS NOT NULL"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("SELECT u.user_id FROM `user` u WHERE u.full_name IS NOT NULL", ctx.getSqlStatement()); Assert.assertEquals(true, ctx.isTableResolved()); Assert.assertEquals("[user-id]", ctx.getOutFields().toString()); }
@Test public void testFieldOfSelectExpr() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='upper-user-name'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("upper(full_name)", ctx.getSqlStatement()); Assert.assertEquals(false, ctx.isTableResolved()); Assert.assertEquals("[upper-user-name]", ctx.getOutFields().toString()); }
@Test public void testFieldOfSelectExpr2() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='user-name'/>,<FIELD name='upper-user-name'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("u.full_name,upper(full_name)", ctx.getSqlStatement()); Assert.assertEquals(false, ctx.isTableResolved()); Assert.assertEquals("[user-name, upper-user-name]", ctx.getOutFields().toString()); }
@Test public void testFieldWithAnotherTable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='user-id' table='home-address'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("hua.user_id", ctx.getSqlStatement()); Assert.assertEquals(false, ctx.isTableResolved()); Assert.assertEquals("[user-id]", ctx.getOutFields().toString()); }
@Test public void testFieldWithAnotherTableNonrelated() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='user-id' table='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 testFieldWithSameTable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD name='user-id' table='user'/>"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("u.user_id", ctx.getSqlStatement()); }
@Test public void testNonExistField() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<FIELD 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) { } }