Search in sources :

Example 11 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldDemonstrateSimpleExpectedTextWithNoLoopsOrConditionals.

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

Example 12 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldHandleOgnlExpression.

@Test
void shouldHandleOgnlExpression() throws Exception {
    final HashMap<String, String> parameterObject = new HashMap<String, String>() {

        {
            put("name", "Steve");
        }
    };
    final String expected = "Expression test: 3 / yes.";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("Expression test: ${name.indexOf('v')} / ${name in {'Bob', 'Steve'\\} ? 'yes' : 'no'}."));
    BoundSql boundSql = source.getBoundSql(parameterObject);
    assertEquals(expected, boundSql.getSql());
}
Also used : DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) HashMap(java.util.HashMap) BoundSql(org.apache.ibatis.mapping.BoundSql) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 13 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldDemonstrateMultipartExpectedTextWithNoLoopsOrConditionals.

@Test
void shouldDemonstrateMultipartExpectedTextWithNoLoopsOrConditionals() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE ID = ?";
    DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new TextSqlNode("WHERE ID = ?"));
    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) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 14 with DynamicSqlSource

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

the class DynamicSqlSourceTest method shouldTrimWHEREANDWithLFForFirstCondition.

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

Example 15 with DynamicSqlSource

use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project qiuyj-code by qiuyuanjun.

the class ReturnValueWrapper method generateSqlSource.

/**
 * 生成对应的SqlSource,这里仅仅会生成DynamicSqlSource和StaticSqlSource
 */
public SqlSource generateSqlSource(Configuration configuration) {
    if (Objects.isNull(sqlNode)) {
        throw new IllegalArgumentException("SqlNode can not be null");
    } else {
        SqlSource sqlSource;
        if (generateStaticSqlSource) {
            DynamicContext context = new DynamicContext(configuration, null);
            sqlNode.apply(context);
            sqlSource = new StaticSqlSource(configuration, context.getSql(), parameterMappings);
        } else {
            sqlSource = new DynamicSqlSource(configuration, sqlNode);
        }
        return new MapperSqlSource(sqlSource);
    }
}
Also used : StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) MapperSqlSource(com.qiuyj.mybatis.MapperSqlSource) SqlSource(org.apache.ibatis.mapping.SqlSource) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) MapperSqlSource(com.qiuyj.mybatis.MapperSqlSource) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) DynamicContext(org.apache.ibatis.scripting.xmltags.DynamicContext)

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