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

The following examples show how to use org.apache.ibatis.scripting.xmltags.SqlNode. 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-boost   Source File: ListParameterEnhancement.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private boolean filter(List<SqlNode> contents) {
    for (SqlNode content : contents) {
        if (content instanceof ForEachSqlNode) return false;
        for (Field field : content.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try {
                if (SqlNode.class.isAssignableFrom(field.getType())) {
                    if (!filter(Collections.singletonList((SqlNode) field.get(content)))) return false;
                } else if (Objects.equals(field.getGenericType().getTypeName(),
                        "java.util.List<org.apache.ibatis.scripting.xmltags.SqlNode>")) {
                    if (!filter((List<SqlNode>) field.get(content))) return false;
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return true;
}
 
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 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());
}
 
Example 4
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConditionallyChooseFirst() throws Exception {
  final String expected = "SELECT * FROM BLOG WHERE CATEGORY = ?";
  DynamicSqlSource source = createDynamicSqlSource(
      new TextSqlNode("SELECT * FROM BLOG"),
      new ChooseSqlNode(new ArrayList<SqlNode>() {{
        add(new IfSqlNode(mixedContents(new TextSqlNode("WHERE CATEGORY = ?")), "true"
        ));
        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());
}
 
Example 5
Source Project: mybaties   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConditionallyChooseSecond() throws Exception {
  final String expected = "SELECT * FROM BLOG WHERE CATEGORY = 'NONE'";
  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'")), "true"
        ));
      }}, mixedContents(new TextSqlNode("WHERE CATEGORY = 'DEFAULT'"))));
  BoundSql boundSql = source.getBoundSql(null);
  assertEquals(expected, boundSql.getSql());
}
 
Example 6
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 7
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 8
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConditionallyChooseFirst() throws Exception {
  final String expected = "SELECT * FROM BLOG WHERE CATEGORY = ?";
  DynamicSqlSource source = createDynamicSqlSource(
      new TextSqlNode("SELECT * FROM BLOG"),
      new ChooseSqlNode(new ArrayList<SqlNode>() {{
        add(new IfSqlNode(mixedContents(new TextSqlNode("WHERE CATEGORY = ?")), "true"
        ));
        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());
}
 
Example 9
Source Project: mybatis   Source File: DynamicSqlSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConditionallyChooseSecond() throws Exception {
  final String expected = "SELECT * FROM BLOG WHERE CATEGORY = 'NONE'";
  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'")), "true"
        ));
      }}, mixedContents(new TextSqlNode("WHERE CATEGORY = 'DEFAULT'"))));
  BoundSql boundSql = source.getBoundSql(null);
  assertEquals(expected, boundSql.getSql());
}
 
Example 10
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 11
Source Project: mybatis-boost   Source File: ListParameterEnhancement.java    License: MIT License 4 votes vote down vote up
private boolean filter(MappedStatement mappedStatement) {
    return !(mappedStatement.getSqlSource() instanceof DynamicSqlSource) ||
            filterCache.computeIfAbsent(mappedStatement.getId(), k -> filter(Collections.singletonList((SqlNode)
                    MyBatisUtils.getMetaObject(mappedStatement.getSqlSource()).getValue("rootSqlNode"))));
}
 
Example 12
Source Project: genericdao   Source File: SqlUtil.java    License: Artistic License 2.0 4 votes vote down vote up
public MyDynamicSqlSource(Configuration configuration, SqlNode rootSqlNode, Boolean count) {
    this.configuration = configuration;
    this.rootSqlNode = rootSqlNode;
    this.count = count;
}
 
Example 13
Source Project: mybaties   Source File: RawSqlSource.java    License: Apache License 2.0 4 votes vote down vote up
public RawSqlSource(Configuration configuration, SqlNode rootSqlNode, Class<?> parameterType) {
  this(configuration, getSql(configuration, rootSqlNode), parameterType);
}
 
Example 14
Source Project: mybaties   Source File: RawSqlSource.java    License: Apache License 2.0 4 votes vote down vote up
private static String getSql(Configuration configuration, SqlNode rootSqlNode) {
  DynamicContext context = new DynamicContext(configuration, null);
  rootSqlNode.apply(context);
  return context.getSql();
}
 
Example 15
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 16
Source Project: mybatis   Source File: RawSqlSource.java    License: Apache License 2.0 4 votes vote down vote up
public RawSqlSource(Configuration configuration, SqlNode rootSqlNode, Class<?> parameterType) {
  this(configuration, getSql(configuration, rootSqlNode), parameterType);
}
 
Example 17
Source Project: mybatis   Source File: RawSqlSource.java    License: Apache License 2.0 4 votes vote down vote up
private static String getSql(Configuration configuration, SqlNode rootSqlNode) {
  DynamicContext context = new DynamicContext(configuration, null);
  rootSqlNode.apply(context);
  return context.getSql();
}
 
Example 18
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));
}