diff --git a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/StatementConfiguration.java b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/StatementConfiguration.java new file mode 100644 index 0000000..aac60aa --- /dev/null +++ b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/StatementConfiguration.java @@ -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); + } + } + +} diff --git a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java index 7d51ece..10eb5b6 100644 --- a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java +++ b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java @@ -97,7 +97,7 @@ static List 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); @@ -117,7 +117,7 @@ static List 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) { @@ -141,7 +141,7 @@ static List 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 params : batch) { fillStatementParams(conn, statement, params, parameterHandler); @@ -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 { @@ -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 params : batch) { fillStatementParams(conn, statement, params, parameterHandler); statement.addBatch(); @@ -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); + } + }