Search in sources :

Example 11 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldConditionallyDefault.

@Test
public void shouldConditionallyDefault() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE CATEGORY = 'DEFAULT'";
    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'")), "false"));
        }
    }, 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 12 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldTrimWHEREANDWithTABForFirstCondition.

@Test
public void shouldTrimWHEREANDWithTABForFirstCondition() 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("   and\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 13 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldTrimWHEREANDWithLFForFirstCondition.

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

Example 14 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldTrimNoWhereClause.

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

Example 15 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSourceTest method shouldHandleOgnlExpression.

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

Aggregations

BoundSql (org.apache.ibatis.mapping.BoundSql)39 Test (org.junit.Test)24 BaseDataTest (org.apache.ibatis.BaseDataTest)23 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)23 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)23 Configuration (org.apache.ibatis.session.Configuration)18 IfSqlNode (org.apache.ibatis.scripting.xmltags.IfSqlNode)17 MappedStatement (org.apache.ibatis.mapping.MappedStatement)12 WhereSqlNode (org.apache.ibatis.scripting.xmltags.WhereSqlNode)10 CacheKey (org.apache.ibatis.cache.CacheKey)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Executor (org.apache.ibatis.executor.Executor)5 MetaObject (org.apache.ibatis.reflection.MetaObject)5 RowBounds (org.apache.ibatis.session.RowBounds)5 ResultHandler (org.apache.ibatis.session.ResultHandler)4 Connection (java.sql.Connection)3 Map (java.util.Map)3 SqlSource (org.apache.ibatis.mapping.SqlSource)3 ChooseSqlNode (org.apache.ibatis.scripting.xmltags.ChooseSqlNode)3