use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project mybatis-3 by mybatis.
the class DynamicSqlSourceTest method shouldTrimWHEREInsteadOfANDForBothConditions.
@Test
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());
}
use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project mybatis-3 by mybatis.
the class DynamicSqlSourceTest method createDynamicSqlSource.
private DynamicSqlSource createDynamicSqlSource(SqlNode... contents) throws IOException, SQLException {
createBlogDataSource();
final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
final Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
Configuration configuration = sqlMapper.getConfiguration();
MixedSqlNode sqlNode = mixedContents(contents);
return new DynamicSqlSource(configuration, sqlNode);
}
use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project mybatis-3 by mybatis.
the class DynamicSqlSourceTest method shouldIterateOnceForEachItemInCollection.
@Test
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());
}
use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project mybatis-3 by mybatis.
the class DynamicSqlSourceTest method shouldTrimNoWhereClause.
@Test
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());
}
use of org.apache.ibatis.scripting.xmltags.DynamicSqlSource in project mybatis-3 by mybatis.
the class DynamicSqlSourceTest method shouldTrimWHEREORWithLFForFirstCondition.
@Test
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());
}
Aggregations