Search in sources :

Example 11 with TextSqlNode

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

the class DynamicSqlSourceTest method shouldTrimWHEREORWithTABForFirstCondition.

@Test
public void shouldTrimWHEREORWithTABForFirstCondition() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE \t ID = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new WhereSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode("   or\t 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.Test)

Example 12 with TextSqlNode

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

the class DynamicSqlSourceTest method shouldDemonstrateSimpleExpectedTextWithNoLoopsOrConditionals.

@Test
public void shouldDemonstrateSimpleExpectedTextWithNoLoopsOrConditionals() throws Exception {
    final String expected = "SELECT * FROM BLOG";
    final MixedSqlNode sqlNode = mixedContents(new TextSqlNode(expected));
    DynamicSqlSource source = createDynamicSqlSource(sqlNode);
    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) MixedSqlNode(org.apache.ibatis.scripting.xmltags.MixedSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 13 with TextSqlNode

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

the class DynamicSqlSourceTest method shouldConditionallyChooseSecond.

@Test
public void shouldConditionallyChooseSecond() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE CATEGORY = 'NONE'";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new ChooseSqlNode(new ArrayList<SqlNode>() {

        {
            add(new IfSqlNode(mixedContents(new TextSqlNode("WHERE CATEGORY = ?")), "false"));
            add(new IfSqlNode(mixedContents(new TextSqlNode("WHERE CATEGORY = 'NONE'")), "true"));
        }
    }, mixedContents(new TextSqlNode("WHERE CATEGORY = 'DEFAULT'"))));
    BoundSql boundSql = source.getBoundSql(null);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) ChooseSqlNode(org.apache.ibatis.scripting.xmltags.ChooseSqlNode) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) BoundSql(org.apache.ibatis.mapping.BoundSql) ArrayList(java.util.ArrayList) IfSqlNode(org.apache.ibatis.scripting.xmltags.IfSqlNode) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 14 with TextSqlNode

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

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithBeforeAutoKey.

public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>()).build();
    MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config, "SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT).keyProperty("id").resultMaps(new ArrayList<ResultMap>() {

        {
            add(rm);
        }
    }).build();
    config.addMappedStatement(kms);
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
            add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
            add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
        }
    }).build()).cache(authorCache).keyGenerator(new SelectKeyGenerator(kms, true)).keyProperty("id").build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) ArrayList(java.util.ArrayList) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Section(org.apache.ibatis.domain.blog.Section) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 15 with TextSqlNode

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

the class DynamicSqlSourceTest method shouldTrimWHEREInsteadOfANDForFirstCondition.

@Test
public 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.Test)

Aggregations

DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)25 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)25 BaseDataTest (org.apache.ibatis.BaseDataTest)24 Test (org.junit.Test)24 BoundSql (org.apache.ibatis.mapping.BoundSql)23 IfSqlNode (org.apache.ibatis.scripting.xmltags.IfSqlNode)17 Configuration (org.apache.ibatis.session.Configuration)16 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)2 SetSqlNode (org.apache.ibatis.scripting.xmltags.SetSqlNode)2 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)1 Section (org.apache.ibatis.domain.blog.Section)1 SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 ResultMap (org.apache.ibatis.mapping.ResultMap)1