InTokenResolver implements TokenResolver { public String resolve(Token token, QueryContext ctx) { if (token.getType() != TokenType.IN) { throw new DalRuntimeException("Internal error: only IN token is supported by " + getClass()); } if (token instanceof StartTagToken) { if (ctx.isWithinInToken()) { throw new DalRuntimeException("IN token can't be nested"); } ctx.setWithinInToken(true); return "("; } else if (token instanceof EndTagToken) { ctx.setWithinInToken(false); return ")"; } else { throw new DalRuntimeException("Internal error: IN token can only be used as <IN> or </IN>"); } }  String resolve(Token token, QueryContext ctx);  }
@Test public void testInConstant() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>...</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(...)", ctx.getSqlStatement()); Assert.assertEquals(0, ctx.getParameters().size()); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInNormalParameter() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); user.setUserId(1234L); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(?)", ctx.getSqlStatement()); Assert.assertEquals(1, ctx.getParameters().size()); Assert.assertEquals(1234L, getParameterValue(ctx, 0)); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInArrayParameter() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id-array}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); long[] userIdArray = new long[] { 1L, 2L, 3L, 4L }; user.setUserIdArray(userIdArray); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(?,?,?,?)", ctx.getSqlStatement()); Assert.assertEquals(1, ctx.getParameters().size()); Assert.assertEquals(userIdArray, getParameterValue(ctx, 0)); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInEmptyArrayParameter() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id-array}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); user.setUserIdArray(new long[0]); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(null)", ctx.getSqlStatement()); Assert.assertEquals(0, ctx.getParameters().size()); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInListParameter() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id-list}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); user.setUserIdList(Arrays.asList(1L, 2L, 3L, 4L)); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(?,?,?,?)", ctx.getSqlStatement()); Assert.assertEquals(1, ctx.getParameters().size()); Assert.assertEquals("[1, 2, 3, 4]", getParameterValue(ctx, 0).toString()); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInEmptyListParameter() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id-list}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); List<Long> emptyList = Collections.emptyList(); user.setUserIdList(emptyList); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(null)", ctx.getSqlStatement()); Assert.assertEquals(0, ctx.getParameters().size()); Assert.assertEquals(false, ctx.isWithinInToken()); }
@Test public void testInBothParameters() throws Exception { QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<IN>${user-id},${user-id-array}</IN>"); User user = new User(); QueryContext ctx = getSelectContext(query, user, null); long[] userIdArray = new long[] { 1, 2, 3, 4 }; user.setUserId(1234L); user.setUserIdArray(userIdArray); Assert.assertEquals(false, ctx.isWithinInToken()); m_queryResolver.resolve(ctx); Assert.assertEquals("(?,?,?,?,?)", ctx.getSqlStatement()); Assert.assertEquals(2, ctx.getParameters().size()); Assert.assertEquals(1234L, getParameterValue(ctx, 0)); Assert.assertEquals(userIdArray, getParameterValue(ctx, 1)); Assert.assertEquals(false, ctx.isWithinInToken()); }