Skip to content

Commit

Permalink
upgrade mybatis dynamic sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ningpp committed May 3, 2024
1 parent 786f33c commit 6a912b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,15 +37,13 @@ private DateTimeFormat(BindableColumn<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,17 +43,25 @@ 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<LocalDateTime> dtfFunction = DateTimeFormat.of(createTime, "yyyy-MM-dd HH:mm:ss", new H2TimeFunction());
assertNotNull(dtfFunction.copy());
assertTrue(dtfFunction.typeHandler().isEmpty());
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();
}

}

0 comments on commit 6a912b3

Please sign in to comment.