ParameterTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.PARAM) { throw new DalRuntimeException("Internal error: only PARAM token is supported by " + getClass()); } ParameterToken parameter = (ParameterToken) token; String fieldName = parameter.getParameterName(); DataField dataField = ctx.getEntityInfo().getFieldByName(fieldName); if (parameter.isIn()) { Object value = m_accessor.getFieldValue(ctx.getProto(), dataField); if (ctx.isWithinInToken() && value != null && value.getClass().isArray()) { int length = Array.getLength(value); StringBuilder sb = new StringBuilder(); if (length > 0) { ctx.addParameter(new Parameter(dataField).setType(Parameter.TYPE_ARRAY)); for (int i = 0; i < length; i++) { if (i > 0) { sb.append(','); } sb.append('?'); } } else { sb.append("null"); } return sb.toString(); } else if (ctx.isWithinInToken() && value instanceof Iterable) { Iterable<?> iterable = (Iterable<?>) value; Iterator<?> i = iterable.iterator(); StringBuilder sb = new StringBuilder(); if (i.hasNext()) { ctx.addParameter(new Parameter(dataField).setType(Parameter.TYPE_ITERABLE)); for (; i.hasNext(); i.next()) { if (sb.length() > 0) { sb.append(','); } sb.append('?'); } } else { sb.append("null"); } return sb.toString(); } else { ctx.addParameter(new Parameter(dataField).setType(Parameter.TYPE_SINGLE_VALUE)); return "?"; } } else if (parameter.isInOut()) { Variable variable = ctx.getEntityInfo().getVariable(dataField); ctx.addParameter(new Parameter(dataField, variable.sqlType(), variable.scale(), true).setType(Parameter.TYPE_SINGLE_VALUE)); return "?"; } else { Variable variable = ctx.getEntityInfo().getVariable(dataField); ctx.addParameter(new Parameter(dataField, variable.sqlType(), variable.scale()).setType(Parameter.TYPE_SINGLE_VALUE)); return "?"; } }  String resolve(Token token, QueryContext ctx);  }
@Test public void testNonExistField() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "${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 testAttribute() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "${user-id}"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); user.setUserId(1234L); m_queryResolver.resolve(ctx); Assert.assertEquals("?", ctx.getSqlStatement()); Assert.assertEquals(1234L, getParameterValue(ctx, 0)); }
@Test public void testAttributeWithDefaultValue() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "${user-id}"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); m_queryResolver.resolve(ctx); Assert.assertEquals("?", ctx.getSqlStatement()); Assert.assertEquals(0L, getParameterValue(ctx, 0)); }
@Test public void testVariable() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "${key-user-id}"); Readset<?> readset = UserEntity.READSET_FULL; User user = new User(); QueryContext ctx = getSelectContext(query, user, readset); user.setKeyUserId(1234L); m_queryResolver.resolve(ctx); Assert.assertEquals("?", ctx.getSqlStatement()); Assert.assertEquals(1234L, getParameterValue(ctx, 0)); }