Search in sources :

Example 1 with ForEachSqlNode

use of org.apache.ibatis.scripting.xmltags.ForEachSqlNode in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldIterateOnceForEachItemInCollection.

@Test
public void shouldIterateOnceForEachItemInCollection() throws Exception {
    final HashMap<String, String[]> parameterObject = new HashMap<String, String[]>() {

        {
            put("array", new String[] { "one", "two", "three" });
        }
    };
    final String expected = "SELECT * FROM BLOG WHERE ID in (  one = ? AND two = ? AND three = ? )";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG WHERE ID in"), new ForEachSqlNode(new Configuration(), mixedContents(new TextSqlNode("${item} = #{item}")), "array", "index", "item", "(", ")", "AND"));
    BoundSql boundSql = source.getBoundSql(parameterObject);
    assertEquals(expected, boundSql.getSql());
    assertEquals(3, boundSql.getParameterMappings().size());
    assertEquals("__frch_item_0", boundSql.getParameterMappings().get(0).getProperty());
    assertEquals("__frch_item_1", boundSql.getParameterMappings().get(1).getProperty());
    assertEquals("__frch_item_2", boundSql.getParameterMappings().get(2).getProperty());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) Configuration(org.apache.ibatis.session.Configuration) HashMap(java.util.HashMap) BoundSql(org.apache.ibatis.mapping.BoundSql) ForEachSqlNode(org.apache.ibatis.scripting.xmltags.ForEachSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 2 with ForEachSqlNode

use of org.apache.ibatis.scripting.xmltags.ForEachSqlNode in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldPerformStrictMatchOnForEachVariableSubstitution.

@Test
public void shouldPerformStrictMatchOnForEachVariableSubstitution() throws Exception {
    final Map<String, Object> param = new HashMap<String, Object>();
    final Map<String, String> uuu = new HashMap<String, String>();
    uuu.put("u", "xyz");
    List<Bean> uuuu = new ArrayList<Bean>();
    uuuu.add(new Bean("bean id"));
    param.put("uuu", uuu);
    param.put("uuuu", uuuu);
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("INSERT INTO BLOG (ID, NAME, NOTE, COMMENT) VALUES"), new ForEachSqlNode(new Configuration(), mixedContents(new TextSqlNode("#{uuu.u}, #{u.id}, #{ u,typeHandler=org.apache.ibatis.type.StringTypeHandler}," + " #{u:VARCHAR,typeHandler=org.apache.ibatis.type.StringTypeHandler}")), "uuuu", "uu", "u", "(", ")", ","));
    BoundSql boundSql = source.getBoundSql(param);
    assertEquals(4, boundSql.getParameterMappings().size());
    assertEquals("uuu.u", boundSql.getParameterMappings().get(0).getProperty());
    assertEquals("__frch_u_0.id", boundSql.getParameterMappings().get(1).getProperty());
    assertEquals("__frch_u_0", boundSql.getParameterMappings().get(2).getProperty());
    assertEquals("__frch_u_0", boundSql.getParameterMappings().get(3).getProperty());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) Configuration(org.apache.ibatis.session.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ForEachSqlNode(org.apache.ibatis.scripting.xmltags.ForEachSqlNode) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) BoundSql(org.apache.ibatis.mapping.BoundSql) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 3 with ForEachSqlNode

use of org.apache.ibatis.scripting.xmltags.ForEachSqlNode in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldSkipForEachWhenCollectionIsEmpty.

@Test
public void shouldSkipForEachWhenCollectionIsEmpty() throws Exception {
    final HashMap<String, Integer[]> parameterObject = new HashMap<String, Integer[]>() {

        {
            put("array", new Integer[] {});
        }
    };
    final String expected = "SELECT * FROM BLOG";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new ForEachSqlNode(new Configuration(), mixedContents(new TextSqlNode("#{item}")), "array", null, "item", "WHERE id in (", ")", ","));
    BoundSql boundSql = source.getBoundSql(parameterObject);
    assertEquals(expected, boundSql.getSql());
    assertEquals(0, boundSql.getParameterMappings().size());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) Configuration(org.apache.ibatis.session.Configuration) HashMap(java.util.HashMap) BoundSql(org.apache.ibatis.mapping.BoundSql) ForEachSqlNode(org.apache.ibatis.scripting.xmltags.ForEachSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)3 BaseDataTest (org.apache.ibatis.BaseDataTest)3 BoundSql (org.apache.ibatis.mapping.BoundSql)3 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)3 ForEachSqlNode (org.apache.ibatis.scripting.xmltags.ForEachSqlNode)3 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)3 Configuration (org.apache.ibatis.session.Configuration)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1