Search in sources :

Example 1 with BoundSql

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

the class DefaultResultSetHandlerTest method shouldRetainColumnNameCase.

/**
   * Contrary to the spec, some drivers require case-sensitive column names when getting result.
   * 
   * @see <a href="http://code.google.com/p/mybatis/issues/detail?id=557">Issue 557</a>
   */
@Test
public void shouldRetainColumnNameCase() throws Exception {
    final MappedStatement ms = getMappedStatement();
    final Executor executor = null;
    final ParameterHandler parameterHandler = null;
    final ResultHandler resultHandler = null;
    final BoundSql boundSql = null;
    final RowBounds rowBounds = new RowBounds(0, 100);
    final DefaultResultSetHandler fastResultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler, resultHandler, boundSql, rowBounds);
    when(stmt.getResultSet()).thenReturn(rs);
    when(rs.getMetaData()).thenReturn(rsmd);
    when(rs.getType()).thenReturn(ResultSet.TYPE_FORWARD_ONLY);
    when(rs.next()).thenReturn(true).thenReturn(false);
    when(rs.getInt("CoLuMn1")).thenReturn(100);
    when(rs.wasNull()).thenReturn(false);
    when(rsmd.getColumnCount()).thenReturn(1);
    when(rsmd.getColumnLabel(1)).thenReturn("CoLuMn1");
    when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
    when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
    when(stmt.getConnection()).thenReturn(conn);
    when(conn.getMetaData()).thenReturn(dbmd);
    // for simplicity.
    when(dbmd.supportsMultipleResultSets()).thenReturn(false);
    final List<Object> results = fastResultSetHandler.handleResultSets(stmt);
    assertEquals(1, results.size());
    assertEquals(Integer.valueOf(100), ((HashMap) results.get(0)).get("cOlUmN1"));
}
Also used : ParameterHandler(org.apache.ibatis.executor.parameter.ParameterHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test)

Example 2 with BoundSql

use of org.apache.ibatis.mapping.BoundSql 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 3 with BoundSql

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

the class DynamicSqlSourceTest method shouldTrimWHEREInsteadOfANDForBothConditions.

@Test
public void shouldTrimWHEREInsteadOfANDForBothConditions() throws Exception {
    final String expected = "SELECT * FROM BLOG WHERE  ID = ?   OR NAME = ?";
    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 = ?  ")), "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 4 with BoundSql

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

the class DynamicSqlSourceTest method shouldTrimWHEREORWithLFForFirstCondition.

@Test
public void shouldTrimWHEREORWithLFForFirstCondition() 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("   or\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 5 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)

Aggregations

BoundSql (org.apache.ibatis.mapping.BoundSql)36 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 IfSqlNode (org.apache.ibatis.scripting.xmltags.IfSqlNode)17 Configuration (org.apache.ibatis.session.Configuration)16 WhereSqlNode (org.apache.ibatis.scripting.xmltags.WhereSqlNode)10 MappedStatement (org.apache.ibatis.mapping.MappedStatement)9 CacheKey (org.apache.ibatis.cache.CacheKey)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Executor (org.apache.ibatis.executor.Executor)4 RowBounds (org.apache.ibatis.session.RowBounds)4 Map (java.util.Map)3 ChooseSqlNode (org.apache.ibatis.scripting.xmltags.ChooseSqlNode)3 ForEachSqlNode (org.apache.ibatis.scripting.xmltags.ForEachSqlNode)3 ResultHandler (org.apache.ibatis.session.ResultHandler)3 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2