ValueTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.VALUE) { throw new DalRuntimeException("Internal error: only VALUE token is supported by " + getClass()); } EntityInfo entityInfo = ctx.getEntityInfo(); StringBuilder sb = new StringBuilder(1024); String fieldName = ((SimpleTagToken) token).getAttribute("name", null); DataField field = ctx.getEntityInfo().getFieldByName(fieldName); if (field != null) { switch (ctx.getQuery().getType()) { case SELECT: throw new DalRuntimeException("VALUE token does not support query type: " + ctx.getQuery().getType()); case INSERT: DataObject proto = ctx.getProto(); Attribute attribute = entityInfo.getAttribute(field); if (!proto.isFieldUsed(field) && attribute.insertExpr().length() > 0) { sb.append(m_expressionResolver.resolve(ctx, attribute.insertExpr())); } else { ctx.addParameter(new Parameter(field)); sb.append('?'); } break; case UPDATE: throw new DalRuntimeException("VALUES token does not support query type: " + ctx.getQuery().getType()); case DELETE: throw new DalRuntimeException("VALUES token does not support query type: " + ctx.getQuery().getType()); default: throw new DalRuntimeException("VALUES 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()); } return sb.toString(); }  String resolve(Token token, QueryContext ctx);  }
@Test public void testUserName() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.INSERT, "<value name='user-name'/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); user.setUserName("test"); m_queryResolver.resolve(ctx); Assert.assertEquals("?", ctx.getSqlStatement()); Assert.assertEquals("[${user-name}]", ctx.getParameters().toString()); }
@Test public void testEncryptedPassword() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.INSERT, "<value name='encrypted-password'/>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); user.setPassword("test"); m_queryResolver.resolve(ctx); Assert.assertEquals("password(?)", ctx.getSqlStatement()); Assert.assertEquals("[${password}]", ctx.getParameters().toString()); }