|
| 1 | +package org.seasar.doma.template; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import javax.sql.DataSource; |
| 10 | +import org.seasar.doma.internal.expr.ExpressionEvaluator; |
| 11 | +import org.seasar.doma.internal.expr.Value; |
| 12 | +import org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder; |
| 13 | +import org.seasar.doma.internal.jdbc.sql.SqlParser; |
| 14 | +import org.seasar.doma.jdbc.Config; |
| 15 | +import org.seasar.doma.jdbc.PreparedSql; |
| 16 | +import org.seasar.doma.jdbc.SqlKind; |
| 17 | +import org.seasar.doma.jdbc.SqlLogType; |
| 18 | +import org.seasar.doma.jdbc.SqlNode; |
| 19 | +import org.seasar.doma.jdbc.dialect.Dialect; |
| 20 | +import org.seasar.doma.jdbc.dialect.StandardDialect; |
| 21 | +import org.seasar.doma.wrapper.Wrapper; |
| 22 | + |
| 23 | +/** Represents a SQL template. */ |
| 24 | +public class SqlTemplate { |
| 25 | + private final String sql; |
| 26 | + private final Config config; |
| 27 | + private final Map<String, Value> values = new HashMap<>(); |
| 28 | + |
| 29 | + /** @param sql a template. Must not be null. */ |
| 30 | + public SqlTemplate(String sql) { |
| 31 | + this(sql, new StandardDialect()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param sql a template. Must not be null. |
| 36 | + * @param dialect a dialect. Must not be null. |
| 37 | + */ |
| 38 | + public SqlTemplate(String sql, Dialect dialect) { |
| 39 | + this( |
| 40 | + sql, |
| 41 | + new Config() { |
| 42 | + |
| 43 | + @Override |
| 44 | + public DataSource getDataSource() { |
| 45 | + throw new UnsupportedOperationException(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Dialect getDialect() { |
| 50 | + return dialect; |
| 51 | + } |
| 52 | + }); |
| 53 | + Objects.requireNonNull(dialect); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param sql a template. Must not be null. |
| 58 | + * @param config a configuration. Must not be null. |
| 59 | + */ |
| 60 | + public SqlTemplate(String sql, Config config) { |
| 61 | + this.sql = Objects.requireNonNull(sql); |
| 62 | + this.config = Objects.requireNonNull(config); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Adds a value. |
| 67 | + * |
| 68 | + * @param name the value name. Must not be null. |
| 69 | + * @param type the value type. Must not be null. |
| 70 | + * @param value the value. Can be null. |
| 71 | + * @return this instance. Must not be null. |
| 72 | + * @param <T> the value type |
| 73 | + */ |
| 74 | + public <T> SqlTemplate add(String name, Class<T> type, T value) { |
| 75 | + Objects.requireNonNull(name); |
| 76 | + Objects.requireNonNull(type); |
| 77 | + values.put(name, new Value(type, value)); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates a SQL statement from this template. |
| 83 | + * |
| 84 | + * @return a SQL statement. Must not be null. |
| 85 | + */ |
| 86 | + public SqlStatement execute() { |
| 87 | + SqlParser parser = new SqlParser(sql); |
| 88 | + SqlNode node = parser.parse(); |
| 89 | + NodePreparedSqlBuilder builder = createNodePreparedSqlBuilder(); |
| 90 | + PreparedSql preparedSql = builder.build(node, Function.identity()); |
| 91 | + return toSqlStatement(preparedSql); |
| 92 | + } |
| 93 | + |
| 94 | + private NodePreparedSqlBuilder createNodePreparedSqlBuilder() { |
| 95 | + ExpressionEvaluator evaluator = |
| 96 | + new ExpressionEvaluator( |
| 97 | + values, config.getDialect().getExpressionFunctions(), config.getClassHelper()); |
| 98 | + return new NodePreparedSqlBuilder( |
| 99 | + config, SqlKind.SCRIPT, null, evaluator, SqlLogType.FORMATTED); |
| 100 | + } |
| 101 | + |
| 102 | + private SqlStatement toSqlStatement(PreparedSql preparedSql) { |
| 103 | + List<SqlArgument> arguments = |
| 104 | + preparedSql.getParameters().stream() |
| 105 | + .map( |
| 106 | + it -> { |
| 107 | + Wrapper<?> w = it.getWrapper(); |
| 108 | + return new SqlArgument(w.getBasicClass(), w.get()); |
| 109 | + }) |
| 110 | + .collect(Collectors.toList()); |
| 111 | + return new SqlStatement(preparedSql.getRawSql(), preparedSql.getFormattedSql(), arguments); |
| 112 | + } |
| 113 | +} |
0 commit comments