Java Code Examples for org.apache.ibatis.scripting.xmltags.MixedSqlNode

The following examples show how to use org.apache.ibatis.scripting.xmltags.MixedSqlNode. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mybatis-test   Source File: SqlNodeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWhereSqlNode() throws IOException {
    String sqlFragment = "AND id = #{id}";
    MixedSqlNode msn = new MixedSqlNode(Arrays.asList(new StaticTextSqlNode(sqlFragment)));
    WhereSqlNode wsn = new WhereSqlNode(new Configuration(), msn);
    DynamicContext dc = new DynamicContext(new Configuration(), new ParamMap<>());
    wsn.apply(dc);
    System.out.println("解析前:" + sqlFragment);
    System.out.println("解析后:" + dc.getSql());
}
 
Example 2
Source Project: jig   Source File: MyBatisSqlReader.java    License: Apache License 2.0 5 votes vote down vote up
private Query getQuery(MappedStatement mappedStatement) throws NoSuchFieldException, IllegalAccessException {
    SqlSource sqlSource = mappedStatement.getSqlSource();

    if (!(sqlSource instanceof DynamicSqlSource)) {
        return new Query(mappedStatement.getBoundSql(null).getSql());
    }

    // 動的クエリ(XMLで組み立てるもの)をエミュレート
    DynamicSqlSource dynamicSqlSource = (DynamicSqlSource) sqlSource;

    Field rootSqlNode = DynamicSqlSource.class.getDeclaredField("rootSqlNode");
    rootSqlNode.setAccessible(true);
    SqlNode sqlNode = (SqlNode) rootSqlNode.get(dynamicSqlSource);


    if (sqlNode instanceof MixedSqlNode) {
        StringBuilder sql = new StringBuilder();
        MixedSqlNode mixedSqlNode = (MixedSqlNode) sqlNode;
        Field contents = mixedSqlNode.getClass().getDeclaredField("contents");
        contents.setAccessible(true);
        List list = (List) contents.get(mixedSqlNode);

        for (Object content : list) {
            if (content instanceof StaticTextSqlNode) {
                StaticTextSqlNode staticTextSqlNode = (StaticTextSqlNode) content;
                Field text = StaticTextSqlNode.class.getDeclaredField("text");
                text.setAccessible(true);
                String textSql = (String) text.get(staticTextSqlNode);
                sql.append(textSql);
            }
        }

        String sqlText = sql.toString().trim();
        LOGGER.debug("動的SQLの組み立てをエミュレートしました。ID={}", mappedStatement.getId());
        LOGGER.debug("組み立てたSQL: [{}]", sqlText);
        return new Query(sqlText);
    }
    return new Query(mappedStatement.getBoundSql(null).getSql());
}
 
Example 3
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMapNullStringsToEmptyStrings() {
  final String expected = "id=${id}";
  final MixedSqlNode sqlNode = mixedContents(new TextSqlNode(expected));
  final DynamicSqlSource source = new DynamicSqlSource(new Configuration(), sqlNode);
  String sql = source.getBoundSql(new Bean(null)).getSql();
  Assert.assertEquals("id=", sql);
}
 
Example 6
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 7
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMapNullStringsToEmptyStrings() {
  final String expected = "id=${id}";
  final MixedSqlNode sqlNode = mixedContents(new TextSqlNode(expected));
  final DynamicSqlSource source = new DynamicSqlSource(new Configuration(), sqlNode);
  String sql = source.getBoundSql(new Bean(null)).getSql();
  Assert.assertEquals("id=", sql);
}
 
Example 9
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 4 votes vote down vote up
private MixedSqlNode mixedContents(SqlNode... contents) {
  return new MixedSqlNode(Arrays.asList(contents));
}
 
Example 10
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 4 votes vote down vote up
private MixedSqlNode mixedContents(SqlNode... contents) {
  return new MixedSqlNode(Arrays.asList(contents));
}