StatementConfiguration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils;

  18. import java.time.Duration;

  19. /**
  20.  * Configuration options for a {@link java.sql.Statement} when preparing statements in {@code QueryRunner}.
  21.  */
  22. public class StatementConfiguration {
  23.     /**
  24.      * Builder class for {@code StatementConfiguration} for more flexible construction.
  25.      */
  26.     public static final class Builder {
  27.         private Integer fetchDirection;
  28.         private Integer fetchSize;
  29.         private Integer maxRows;
  30.         private Duration queryTimeout;
  31.         private Integer maxFieldSize;

  32.         /**
  33.          * @return A new and configured {@link StatementConfiguration}.
  34.          */
  35.         public StatementConfiguration build() {
  36.             return new StatementConfiguration(fetchDirection, fetchSize, maxFieldSize, maxRows, queryTimeout);
  37.         }

  38.         /**
  39.          * @param fetchDirection The direction for fetching rows from database tables.
  40.          * @return This builder for chaining.
  41.          * @see StatementConfiguration#getFetchDirection()
  42.          */
  43.         public Builder fetchDirection(final Integer fetchDirection) {
  44.             this.fetchDirection = fetchDirection;
  45.             return this;
  46.         }

  47.         /**
  48.          * @param fetchSize The number of rows that should be fetched from the database when more rows are needed.
  49.          * @return This builder for chaining.
  50.          * @see StatementConfiguration#getFetchSize()
  51.          */
  52.         public Builder fetchSize(final Integer fetchSize) {
  53.             this.fetchSize = fetchSize;
  54.             return this;
  55.         }

  56.         /**
  57.          * @param maxFieldSize The maximum number of bytes that can be returned for character and binary column values.
  58.          * @return This builder for chaining.
  59.          * @see StatementConfiguration#getMaxFieldSize()
  60.          */
  61.         public Builder maxFieldSize(final Integer maxFieldSize) {
  62.             this.maxFieldSize = maxFieldSize;
  63.             return this;
  64.         }

  65.         /**
  66.          * @param maxRows The maximum number of rows that a {@code ResultSet} can produce.
  67.          * @return This builder for chaining.
  68.          * @see StatementConfiguration#getMaxRows()
  69.          */
  70.         public Builder maxRows(final Integer maxRows) {
  71.             this.maxRows = maxRows;
  72.             return this;
  73.         }

  74.         /**
  75.          * @param queryTimeout The number of seconds the driver will wait for execution.
  76.          * @return This builder for chaining.
  77.          * @see StatementConfiguration#getQueryTimeoutDuration()
  78.          * @since 1.8.0
  79.          */
  80.         public Builder queryTimeout(final Duration queryTimeout) {
  81.             this.queryTimeout = queryTimeout;
  82.             return this;
  83.         }

  84.         /**
  85.          * @param queryTimeout The number of seconds the driver will wait for execution.
  86.          * @return This builder for chaining.
  87.          * @see StatementConfiguration#getQueryTimeout()
  88.          * @deprecated Use {@link #queryTimeout(Duration)}.
  89.          */
  90.         @Deprecated
  91.         public Builder queryTimeout(final Integer queryTimeout) {
  92.             this.queryTimeout = queryTimeout != null ? Duration.ofSeconds(queryTimeout) : null;
  93.             return this;
  94.         }
  95.     }

  96.     private final Integer fetchDirection;
  97.     private final Integer fetchSize;
  98.     private final Integer maxFieldSize;
  99.     private final Integer maxRows;
  100.     private final Duration queryTimeout;

  101.     /**
  102.      * Constructor for {@code StatementConfiguration}.  For more flexibility, use {@link Builder}.
  103.      *
  104.      * @param fetchDirection The direction for fetching rows from database tables.
  105.      * @param fetchSize The number of rows that should be fetched from the database when more rows are needed.
  106.      * @param maxFieldSize The maximum number of bytes that can be returned for character and binary column values.
  107.      * @param maxRows The maximum number of rows that a {@code ResultSet} can produce.
  108.      * @param queryTimeout The number of seconds the driver will wait for execution.
  109.      * @since 1.8.0
  110.      */
  111.     public StatementConfiguration(final Integer fetchDirection, final Integer fetchSize,
  112.                                   final Integer maxFieldSize, final Integer maxRows,
  113.                                   final Duration queryTimeout) {
  114.         this.fetchDirection = fetchDirection;
  115.         this.fetchSize = fetchSize;
  116.         this.maxFieldSize = maxFieldSize;
  117.         this.maxRows = maxRows;
  118.         if (queryTimeout != null && queryTimeout.getSeconds() > Integer.MAX_VALUE) {
  119.             throw new IllegalArgumentException(String.format("queryTimeout overflow: %d > %,d", queryTimeout.getSeconds(), Integer.MAX_VALUE));
  120.         }
  121.         this.queryTimeout = queryTimeout;
  122.     }

  123.     /**
  124.      * Constructor for {@code StatementConfiguration}.  For more flexibility, use {@link Builder}.
  125.      *
  126.      * @param fetchDirection The direction for fetching rows from database tables.
  127.      * @param fetchSize The number of rows that should be fetched from the database when more rows are needed.
  128.      * @param maxFieldSize The maximum number of bytes that can be returned for character and binary column values.
  129.      * @param maxRows The maximum number of rows that a {@code ResultSet} can produce.
  130.      * @param queryTimeout The number of seconds the driver will wait for execution.
  131.      * @deprecated Use {@link StatementConfiguration#StatementConfiguration(Integer, Integer, Integer, Integer, Duration)}.
  132.      */
  133.     @Deprecated
  134.     public StatementConfiguration(final Integer fetchDirection, final Integer fetchSize,
  135.                                   final Integer maxFieldSize, final Integer maxRows,
  136.                                   final Integer queryTimeout) {
  137.         this(fetchDirection, fetchSize, maxFieldSize, maxRows, Duration.ofSeconds(queryTimeout));
  138.     }

  139.     /**
  140.      * Get the fetch direction.
  141.      *
  142.      * @return The direction to fetch or null if not set.
  143.      */
  144.     public Integer getFetchDirection() {
  145.         return fetchDirection;
  146.     }

  147.     /**
  148.      * Get the fetch size.
  149.      *
  150.      * @return The fetch size or null if not set.
  151.      */
  152.     public Integer getFetchSize() {
  153.         return fetchSize;
  154.     }

  155.     /**
  156.      * Get the max field size.
  157.      *
  158.      * @return The max field size or null if not set.
  159.      */
  160.     public Integer getMaxFieldSize() {
  161.         return maxFieldSize;
  162.     }

  163.     /**
  164.      * Get the max rows.
  165.      *
  166.      * @return The max rows or null if not set.
  167.      */
  168.     public Integer getMaxRows() {
  169.         return maxRows;
  170.     }

  171.     /**
  172.      * Get the query timeout.
  173.      *
  174.      * @return The query timeout or null if not set.
  175.      * @deprecated Use {@link #getQueryTimeoutDuration()}.
  176.      */
  177.     @Deprecated
  178.     public Integer getQueryTimeout() {
  179.         return queryTimeout != null ? (int) queryTimeout.getSeconds() : null;
  180.     }

  181.     /**
  182.      * Get the query timeout.
  183.      *
  184.      * @return The query timeout or null if not set.
  185.      * @since 1.8.0
  186.      */
  187.     public Duration getQueryTimeoutDuration() {
  188.         return queryTimeout;
  189.     }

  190.     /**
  191.      * Whether fetch direction is set.
  192.      *
  193.      * @return true if set, false otherwise.
  194.      */
  195.     public boolean isFetchDirectionSet() {
  196.         return fetchDirection != null;
  197.     }

  198.     /**
  199.      * Whether fetch size is set.
  200.      *
  201.      * @return true if set, false otherwise.
  202.      */
  203.     public boolean isFetchSizeSet() {
  204.         return fetchSize != null;
  205.     }

  206.     /**
  207.      * Whether max field size is set.
  208.      *
  209.      * @return true if set, false otherwise.
  210.      */
  211.     public boolean isMaxFieldSizeSet() {
  212.         return maxFieldSize != null;
  213.     }

  214.     /**
  215.      * Whether max rows is set.
  216.      *
  217.      * @return true if set, false otherwise.
  218.      */
  219.     public boolean isMaxRowsSet() {
  220.         return maxRows != null;
  221.     }

  222.     /**
  223.      * Whether query timeout is set.
  224.      *
  225.      * @return true if set, false otherwise.
  226.      */
  227.     public boolean isQueryTimeoutSet() {
  228.         return queryTimeout != null;
  229.     }
  230. }