diff --git a/mmegp-mybatis-ext/src/main/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormat.java b/mmegp-mybatis-ext/src/main/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormat.java index 8027ff5..f7eaa68 100644 --- a/mmegp-mybatis-ext/src/main/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormat.java +++ b/mmegp-mybatis-ext/src/main/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormat.java @@ -17,8 +17,9 @@ import me.ningpp.mmegp.sql.time.SqlTimeFunction; import org.mybatis.dynamic.sql.BindableColumn; -import org.mybatis.dynamic.sql.render.TableAliasCalculator; +import org.mybatis.dynamic.sql.render.RenderingContext; import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction; +import org.mybatis.dynamic.sql.util.FragmentAndParameters; import java.sql.JDBCType; import java.util.Objects; @@ -36,15 +37,13 @@ private DateTimeFormat(BindableColumn column, String javaPattern, SqlTimeFunc } @Override - public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) { + public FragmentAndParameters render(RenderingContext renderingContext) { String format = timeFunction.tranlateFormat(javaPattern); if (format.indexOf('\'') != -1) { throw new IllegalArgumentException("SQL injection???, javaPattern = " + javaPattern); } - return timeFunction.formatFunctionName() - + "(" - + column.renderWithTableAlias(tableAliasCalculator) - + ", '" + format + "')"; + return column.render(renderingContext).mapFragment(s -> timeFunction.formatFunctionName() + + "(" + s + ", '" + format + "')"); } @Override diff --git a/mmegp-mybatis-ext/src/test/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormatTest.java b/mmegp-mybatis-ext/src/test/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormatTest.java index c81d589..8c26098 100644 --- a/mmegp-mybatis-ext/src/test/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormatTest.java +++ b/mmegp-mybatis-ext/src/test/java/me/ningpp/mmegp/mybatis/dsql/function/DateTimeFormatTest.java @@ -20,7 +20,10 @@ import org.junit.jupiter.api.Test; import org.mybatis.dynamic.sql.SqlColumn; import org.mybatis.dynamic.sql.SqlTable; +import org.mybatis.dynamic.sql.configuration.StatementConfiguration; import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator; +import org.mybatis.dynamic.sql.render.RenderingContext; +import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.render.TableAliasCalculator; import java.time.LocalDateTime; @@ -40,12 +43,12 @@ void formatTest() { TableAliasCalculator calculatorAlias = ExplicitTableAliasCalculator.of(table, "_article_"); assertThrows(IllegalArgumentException.class, () -> DateTimeFormat.of(createTime, - "yyyy-MM-dd'T'HH:mm:ss", new H2TimeFunction()).renderWithTableAlias(calculator), + "yyyy-MM-dd'T'HH:mm:ss", new H2TimeFunction()).render(buildRenderingContext(calculator)), "SQL injection???, javaPattern = yyyy-MM-dd'T'HH:mm:ss"); assertEquals("DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s')", DateTimeFormat.of(createTime, - "yyyy-MM-dd HH:mm:ss", new MySqlTimeFunction()).renderWithTableAlias(calculator)); + "yyyy-MM-dd HH:mm:ss", new MySqlTimeFunction()).render(buildRenderingContext(calculator)).fragment()); assertEquals("DATE_FORMAT(_article_.create_time, '%Y-%m-%d %H:%i:%s')", DateTimeFormat.of(createTime, - "yyyy-MM-dd HH:mm:ss", new MySqlTimeFunction()).renderWithTableAlias(calculatorAlias)); + "yyyy-MM-dd HH:mm:ss", new MySqlTimeFunction()).render(buildRenderingContext(calculatorAlias)).fragment()); DateTimeFormat dtfFunction = DateTimeFormat.of(createTime, "yyyy-MM-dd HH:mm:ss", new H2TimeFunction()); assertNotNull(dtfFunction.copy()); @@ -53,4 +56,12 @@ void formatTest() { assertTrue(dtfFunction.jdbcType().isEmpty()); } + private static final StatementConfiguration STATEMENT_CONFIGURATION = new StatementConfiguration(); + + private RenderingContext buildRenderingContext(TableAliasCalculator calculator) { + return RenderingContext.withRenderingStrategy(RenderingStrategies.MYBATIS3) + .withStatementConfiguration(STATEMENT_CONFIGURATION) + .withTableAliasCalculator(calculator).build(); + } + }