Search in sources :

Example 21 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldTrimWHEREInsteadOfANDForFirstCondition.

@Test
void shouldTrimWHEREInsteadOfANDForFirstCondition() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE  ID = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new WhereSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode("   and ID = ?  ")), "true"), new IfSqlNode(mixedContents(new TextSqlNode("   or NAME = ?  ")), "false"))));
    BoundSql boundSql = source.getBoundSql(null);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) Configuration(org.apache.ibatis.session.Configuration) BoundSql(org.apache.ibatis.mapping.BoundSql) WhereSqlNode(org.apache.ibatis.scripting.xmltags.WhereSqlNode) IfSqlNode(org.apache.ibatis.scripting.xmltags.IfSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 22 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldPerformStrictMatchOnForEachVariableSubstitution.

@Test
void shouldPerformStrictMatchOnForEachVariableSubstitution() throws Exception {
    final Map<String, Object> param = new HashMap<>();
    final Map<String, String> uuu = new HashMap<>();
    uuu.put("u", "xyz");
    List<Bean> uuuu = new ArrayList<>();
    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.jupiter.api.Test)

Example 23 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldConditionallyIncludeWhere.

@Test
void shouldConditionallyIncludeWhere() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE ID = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new IfSqlNode(mixedContents(new TextSqlNode("WHERE ID = ?")), "true"));
    BoundSql boundSql = source.getBoundSql(null);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) BoundSql(org.apache.ibatis.mapping.BoundSql) IfSqlNode(org.apache.ibatis.scripting.xmltags.IfSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 24 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldTrimCommaAfterSET.

@Test
void shouldTrimCommaAfterSET() throws Exception {
    final String expected = "UPDATE BLOG SET  NAME = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("UPDATE BLOG"), new SetSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode("ID = ?")), "false"), new IfSqlNode(mixedContents(new TextSqlNode(", NAME = ?")), "true"))));
    BoundSql boundSql = source.getBoundSql(null);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) Configuration(org.apache.ibatis.session.Configuration) BoundSql(org.apache.ibatis.mapping.BoundSql) IfSqlNode(org.apache.ibatis.scripting.xmltags.IfSqlNode) SetSqlNode(org.apache.ibatis.scripting.xmltags.SetSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 25 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldTrimWHEREORWithCRLFForFirstCondition.

@Test
void shouldTrimWHEREORWithCRLFForFirstCondition() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE \r\n ID = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new WhereSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode("   or\r\n ID = ?  ")), "true"))));
    BoundSql boundSql = source.getBoundSql(null);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) Configuration(org.apache.ibatis.session.Configuration) BoundSql(org.apache.ibatis.mapping.BoundSql) WhereSqlNode(org.apache.ibatis.scripting.xmltags.WhereSqlNode) IfSqlNode(org.apache.ibatis.scripting.xmltags.IfSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)28 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)26 BaseDataTest (org.apache.ibatis.BaseDataTest)25 Test (org.junit.jupiter.api.Test)25 BoundSql (org.apache.ibatis.mapping.BoundSql)24 IfSqlNode (org.apache.ibatis.scripting.xmltags.IfSqlNode)18 Configuration (org.apache.ibatis.session.Configuration)18 WhereSqlNode (org.apache.ibatis.scripting.xmltags.WhereSqlNode)10 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 ChooseSqlNode (org.apache.ibatis.scripting.xmltags.ChooseSqlNode)3 ForEachSqlNode (org.apache.ibatis.scripting.xmltags.ForEachSqlNode)3 MixedSqlNode (org.apache.ibatis.scripting.xmltags.MixedSqlNode)3 SetSqlNode (org.apache.ibatis.scripting.xmltags.SetSqlNode)3 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)2 MapperSqlSource (com.qiuyj.mybatis.MapperSqlSource)1 Reader (java.io.Reader)1 Section (org.apache.ibatis.domain.blog.Section)1 SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1