Skip to content

Commit

Permalink
增加StatementConfiguration,支持对Statement进行额外的配置。
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Nov 2, 2024
1 parent a87d524 commit 4babae6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.fantasy0v0.swift.jdbc;

public record StatementConfiguration(Integer queryTimeout,
Integer maxFieldSize,
Integer maxRows) {

public static class Builder {
private Integer queryTimeout;
private Integer maxFieldSize;
private Integer maxRows;

public Builder() {
}

public Builder queryTimeout(Integer queryTimeout) {
this.queryTimeout = queryTimeout;
return this;
}

public Builder maxFieldSize(Integer maxFieldSize) {
this.maxFieldSize = maxFieldSize;
return this;
}

public Builder maxRows(Integer maxRows) {
this.maxRows = maxRows;
return this;
}

public StatementConfiguration build() {
return new StatementConfiguration(queryTimeout, maxFieldSize, maxRows);
}
}

}
35 changes: 30 additions & 5 deletions jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static <T> List<T> executeQuery(Connection conn,
long startTime = System.nanoTime() / 1000;
String callerInfo = printCallerInfo();
LogUtil.sql().debug("executeQuery: {}, caller: {}", sql, callerInfo);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
try (PreparedStatement statement = prepareStatement(conn, sql, null)) {
fillStatementParams(conn, statement, params, parameterHandler);
try (ResultSet resultSet = statement.executeQuery()) {
return fetchByResultSet(resultSet, fetchMapper, firstOnly);
Expand All @@ -117,7 +117,7 @@ static <T> List<T> execute(Connection conn,
long startTime = System.nanoTime() / 1000;
String callerInfo = printCallerInfo();
LogUtil.sql().debug("execute: {}, caller: {}", sql, callerInfo);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
try (PreparedStatement statement = prepareStatement(conn, sql, null)) {
fillStatementParams(conn, statement, params, parameterHandler);
boolean result = statement.execute();
if (!result) {
Expand All @@ -141,7 +141,7 @@ static <T> List<T> executeBatch(Connection conn,
long startTime = System.nanoTime() / 1000;
String callerInfo = printCallerInfo();
LogUtil.sql().debug("executeBatch: {}, caller: {}", sql, callerInfo);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
try (PreparedStatement statement = prepareStatement(conn, sql, null)) {
if (null != batch) {
for (List<Object> params : batch) {
fillStatementParams(conn, statement, params, parameterHandler);
Expand Down Expand Up @@ -172,7 +172,7 @@ static int executeUpdate(Connection conn,
long startTime = System.nanoTime() / 1000;
String callerInfo = printCallerInfo();
LogUtil.sql().debug("executeUpdate: {}, caller: {}", sql, callerInfo);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
try (PreparedStatement statement = prepareStatement(conn, sql, null)) {
fillStatementParams(conn, statement, params, parameterHandler);
return statement.executeUpdate();
} finally {
Expand All @@ -189,7 +189,7 @@ static int[] executeUpdateBatch(Connection conn,
long startTime = System.nanoTime() / 1000;
String callerInfo = printCallerInfo();
LogUtil.sql().debug("executeUpdateBatch: {}, caller: {}", sql, callerInfo);
try (PreparedStatement statement = conn.prepareStatement(sql)) {
try (PreparedStatement statement = prepareStatement(conn, sql, null)) {
for (List<Object> params : batch) {
fillStatementParams(conn, statement, params, parameterHandler);
statement.addBatch();
Expand Down Expand Up @@ -240,4 +240,29 @@ static void fillStatementParams(Connection conn,
}
}

private static PreparedStatement prepareStatement(Connection conn,
String sql,
int autoGeneratedKeys,
StatementConfiguration configuration) throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql, autoGeneratedKeys);
if (null != configuration) {
if (null != configuration.queryTimeout()) {
ps.setQueryTimeout(configuration.queryTimeout());
}
if (null != configuration.maxFieldSize()) {
ps.setMaxFieldSize(configuration.maxFieldSize());
}
if (null != configuration.maxRows()) {
ps.setMaxRows(configuration.maxRows());
}
}
return ps;
}

private static PreparedStatement prepareStatement(Connection conn,
String sql,
StatementConfiguration configuration) throws SQLException {
return prepareStatement(conn, sql, Statement.NO_GENERATED_KEYS, configuration);
}

}

0 comments on commit 4babae6

Please sign in to comment.