|
|
|
@ -45,6 +45,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
|
|
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
|
|
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
|
|
|
import org.springframework.jdbc.core.namedparam.SimplePropertySqlParameterSource; |
|
|
|
import org.springframework.jdbc.core.namedparam.SimplePropertySqlParameterSource; |
|
|
|
import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
|
|
|
import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
|
|
|
|
|
|
|
import org.springframework.jdbc.support.JdbcAccessor; |
|
|
|
import org.springframework.jdbc.support.KeyHolder; |
|
|
|
import org.springframework.jdbc.support.KeyHolder; |
|
|
|
import org.springframework.jdbc.support.rowset.SqlRowSet; |
|
|
|
import org.springframework.jdbc.support.rowset.SqlRowSet; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
@ -62,8 +63,6 @@ import org.springframework.util.Assert; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
final class DefaultJdbcClient implements JdbcClient { |
|
|
|
final class DefaultJdbcClient implements JdbcClient { |
|
|
|
|
|
|
|
|
|
|
|
private final JdbcOperations classicOps; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final NamedParameterJdbcOperations namedParamOps; |
|
|
|
private final NamedParameterJdbcOperations namedParamOps; |
|
|
|
|
|
|
|
|
|
|
|
private final ConversionService conversionService; |
|
|
|
private final ConversionService conversionService; |
|
|
|
@ -81,7 +80,6 @@ final class DefaultJdbcClient implements JdbcClient { |
|
|
|
|
|
|
|
|
|
|
|
public DefaultJdbcClient(NamedParameterJdbcOperations jdbcTemplate, @Nullable ConversionService conversionService) { |
|
|
|
public DefaultJdbcClient(NamedParameterJdbcOperations jdbcTemplate, @Nullable ConversionService conversionService) { |
|
|
|
Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null"); |
|
|
|
Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null"); |
|
|
|
this.classicOps = jdbcTemplate.getJdbcOperations(); |
|
|
|
|
|
|
|
this.namedParamOps = jdbcTemplate; |
|
|
|
this.namedParamOps = jdbcTemplate; |
|
|
|
this.conversionService = |
|
|
|
this.conversionService = |
|
|
|
(conversionService != null ? conversionService : DefaultConversionService.getSharedInstance()); |
|
|
|
(conversionService != null ? conversionService : DefaultConversionService.getSharedInstance()); |
|
|
|
@ -90,7 +88,7 @@ final class DefaultJdbcClient implements JdbcClient { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public StatementSpec sql(String sql) { |
|
|
|
public StatementSpec sql(String sql) { |
|
|
|
return new DefaultStatementSpec(sql); |
|
|
|
return new DefaultStatementSpec(sql, this.namedParamOps); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -98,14 +96,55 @@ final class DefaultJdbcClient implements JdbcClient { |
|
|
|
|
|
|
|
|
|
|
|
private final String sql; |
|
|
|
private final String sql; |
|
|
|
|
|
|
|
|
|
|
|
private final List<Object> indexedParams = new ArrayList<>(); |
|
|
|
private JdbcOperations classicOps; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private NamedParameterJdbcOperations namedParamOps; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private @Nullable JdbcTemplate customTemplate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final List<@Nullable Object> indexedParams = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
private final MapSqlParameterSource namedParams = new MapSqlParameterSource(); |
|
|
|
private final MapSqlParameterSource namedParams = new MapSqlParameterSource(); |
|
|
|
|
|
|
|
|
|
|
|
private SqlParameterSource namedParamSource = this.namedParams; |
|
|
|
private SqlParameterSource namedParamSource = this.namedParams; |
|
|
|
|
|
|
|
|
|
|
|
public DefaultStatementSpec(String sql) { |
|
|
|
public DefaultStatementSpec(String sql, NamedParameterJdbcOperations namedParamOps) { |
|
|
|
this.sql = sql; |
|
|
|
this.sql = sql; |
|
|
|
|
|
|
|
this.classicOps = namedParamOps.getJdbcOperations(); |
|
|
|
|
|
|
|
this.namedParamOps = namedParamOps; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JdbcTemplate enforceCustomTemplate() { |
|
|
|
|
|
|
|
if (this.customTemplate == null) { |
|
|
|
|
|
|
|
if (!(this.classicOps instanceof JdbcAccessor original)) { |
|
|
|
|
|
|
|
throw new IllegalStateException( |
|
|
|
|
|
|
|
"Needs to be bound to a JdbcAccessor for custom settings support: " + this.classicOps); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.customTemplate = new JdbcTemplate(original); |
|
|
|
|
|
|
|
this.classicOps = this.customTemplate; |
|
|
|
|
|
|
|
this.namedParamOps = (this.namedParamOps instanceof NamedParameterJdbcTemplate originalNamedParam ? |
|
|
|
|
|
|
|
new NamedParameterJdbcTemplate(originalNamedParam, this.customTemplate) : |
|
|
|
|
|
|
|
new NamedParameterJdbcTemplate(this.customTemplate)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this.customTemplate; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public StatementSpec withFetchSize(int fetchSize) { |
|
|
|
|
|
|
|
enforceCustomTemplate().setFetchSize(fetchSize); |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public StatementSpec withMaxRows(int maxRows) { |
|
|
|
|
|
|
|
enforceCustomTemplate().setMaxRows(maxRows); |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public StatementSpec withQueryTimeout(int queryTimeout) { |
|
|
|
|
|
|
|
enforceCustomTemplate().setQueryTimeout(queryTimeout); |
|
|
|
|
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -220,18 +259,18 @@ final class DefaultJdbcClient implements JdbcClient { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void query(RowCallbackHandler rch) { |
|
|
|
public void query(RowCallbackHandler rch) { |
|
|
|
if (useNamedParams()) { |
|
|
|
if (useNamedParams()) { |
|
|
|
namedParamOps.query(this.sql, this.namedParamSource, rch); |
|
|
|
this.namedParamOps.query(this.sql, this.namedParamSource, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
classicOps.query(statementCreatorForIndexedParams(), rch); |
|
|
|
this.classicOps.query(statementCreatorForIndexedParams(), rch); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public <T> T query(ResultSetExtractor<T> rse) { |
|
|
|
public <T> T query(ResultSetExtractor<T> rse) { |
|
|
|
T result = (useNamedParams() ? |
|
|
|
T result = (useNamedParams() ? |
|
|
|
namedParamOps.query(this.sql, this.namedParamSource, rse) : |
|
|
|
this.namedParamOps.query(this.sql, this.namedParamSource, rse) : |
|
|
|
classicOps.query(statementCreatorForIndexedParams(), rse)); |
|
|
|
this.classicOps.query(statementCreatorForIndexedParams(), rse)); |
|
|
|
Assert.state(result != null, "No result from ResultSetExtractor"); |
|
|
|
Assert.state(result != null, "No result from ResultSetExtractor"); |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -239,22 +278,22 @@ final class DefaultJdbcClient implements JdbcClient { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int update() { |
|
|
|
public int update() { |
|
|
|
return (useNamedParams() ? |
|
|
|
return (useNamedParams() ? |
|
|
|
namedParamOps.update(this.sql, this.namedParamSource) : |
|
|
|
this.namedParamOps.update(this.sql, this.namedParamSource) : |
|
|
|
classicOps.update(statementCreatorForIndexedParams())); |
|
|
|
this.classicOps.update(statementCreatorForIndexedParams())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int update(KeyHolder generatedKeyHolder) { |
|
|
|
public int update(KeyHolder generatedKeyHolder) { |
|
|
|
return (useNamedParams() ? |
|
|
|
return (useNamedParams() ? |
|
|
|
namedParamOps.update(this.sql, this.namedParamSource, generatedKeyHolder) : |
|
|
|
this.namedParamOps.update(this.sql, this.namedParamSource, generatedKeyHolder) : |
|
|
|
classicOps.update(statementCreatorForIndexedParamsWithKeys(null), generatedKeyHolder)); |
|
|
|
this.classicOps.update(statementCreatorForIndexedParamsWithKeys(null), generatedKeyHolder)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int update(KeyHolder generatedKeyHolder, String... keyColumnNames) { |
|
|
|
public int update(KeyHolder generatedKeyHolder, String... keyColumnNames) { |
|
|
|
return (useNamedParams() ? |
|
|
|
return (useNamedParams() ? |
|
|
|
namedParamOps.update(this.sql, this.namedParamSource, generatedKeyHolder, keyColumnNames) : |
|
|
|
this.namedParamOps.update(this.sql, this.namedParamSource, generatedKeyHolder, keyColumnNames) : |
|
|
|
classicOps.update(statementCreatorForIndexedParamsWithKeys(keyColumnNames), generatedKeyHolder)); |
|
|
|
this.classicOps.update(statementCreatorForIndexedParamsWithKeys(keyColumnNames), generatedKeyHolder)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean useNamedParams() { |
|
|
|
private boolean useNamedParams() { |
|
|
|
|