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

The following examples show how to use org.apache.ibatis.scripting.xmltags.SqlNode. 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 File: ListParameterEnhancement.java    From mybatis-boost with 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 File: DynamicSqlSourceTest.java    From mybaties with 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 #3
Source File: DynamicSqlSourceTest.java    From mybatis with 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 #4
Source File: DynamicSqlSourceTest.java    From mybatis with 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 #5
Source File: DynamicSqlSourceTest.java    From mybatis with 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 #6
Source File: DynamicSqlSourceTest.java    From mybatis with 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 #7
Source File: DynamicSqlSourceTest.java    From mybaties with 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 #8
Source File: DynamicSqlSourceTest.java    From mybaties with 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 File: DynamicSqlSourceTest.java    From mybaties with 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 #10
Source File: MyBatisSqlReader.java    From jig with 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 #11
Source File: ListParameterEnhancement.java    From mybatis-boost with 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 File: DynamicSqlSourceTest.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private MixedSqlNode mixedContents(SqlNode... contents) {
  return new MixedSqlNode(Arrays.asList(contents));
}
 
Example #13
Source File: RawSqlSource.java    From mybatis with 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 File: RawSqlSource.java    From mybatis with 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 File: RawSqlSource.java    From mybaties with 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 #16
Source File: RawSqlSource.java    From mybaties with 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 File: SqlUtil.java    From genericdao with 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 #18
Source File: DynamicSqlSourceTest.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private MixedSqlNode mixedContents(SqlNode... contents) {
  return new MixedSqlNode(Arrays.asList(contents));
}