001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbutils;
018
019/**
020 * Configuration options for a {@link java.sql.Statement} when preparing statements in {@code QueryRunner}.
021 */
022public class StatementConfiguration {
023    private final Integer fetchDirection;
024    private final Integer fetchSize;
025    private final Integer maxFieldSize;
026    private final Integer maxRows;
027    private final Integer queryTimeout;
028
029    /**
030     * Constructor for {@code StatementConfiguration}.  For more flexibility, use {@link Builder}.
031     *
032     * @param fetchDirection The direction for fetching rows from database tables.
033     * @param fetchSize The number of rows that should be fetched from the database when more rows are needed.
034     * @param maxFieldSize The maximum number of bytes that can be returned for character and binary column values.
035     * @param maxRows The maximum number of rows that a {@code ResultSet} can produce.
036     * @param queryTimeout The number of seconds the driver will wait for execution.
037     */
038    public StatementConfiguration(final Integer fetchDirection, final Integer fetchSize,
039                                  final Integer maxFieldSize, final Integer maxRows,
040                                  final Integer queryTimeout) {
041        this.fetchDirection = fetchDirection;
042        this.fetchSize = fetchSize;
043        this.maxFieldSize = maxFieldSize;
044        this.maxRows = maxRows;
045        this.queryTimeout = queryTimeout;
046    }
047
048    /**
049     * Get the fetch direction.
050     *
051     * @return The direction to fetch or null if not set.
052     */
053    public Integer getFetchDirection() {
054        return fetchDirection;
055    }
056
057    /**
058     * Whether fetch direction is set.
059     *
060     * @return true if set, false otherwise.
061     */
062    public boolean isFetchDirectionSet() {
063        return fetchDirection != null;
064    }
065
066    /**
067     * Get the fetch size.
068     *
069     * @return The fetch size or null if not set.
070     */
071    public Integer getFetchSize() {
072        return fetchSize;
073    }
074
075    /**
076     * Whether fetch size is set.
077     *
078     * @return true if set, false otherwise.
079     */
080    public boolean isFetchSizeSet() {
081        return fetchSize != null;
082    }
083
084    /**
085     * Get the max field size.
086     *
087     * @return The max field size or null if not set.
088     */
089    public Integer getMaxFieldSize() {
090        return maxFieldSize;
091    }
092
093    /**
094     * Whether max field size is set.
095     *
096     * @return true if set, false otherwise.
097     */
098    public boolean isMaxFieldSizeSet() {
099        return maxFieldSize != null;
100    }
101
102    /**
103     * Get the max rows.
104     *
105     * @return The max rows or null if not set.
106     */
107    public Integer getMaxRows() {
108        return maxRows;
109    }
110
111    /**
112     * Whether max rows is set.
113     *
114     * @return true if set, false otherwise.
115     */
116    public boolean isMaxRowsSet() {
117        return maxRows != null;
118    }
119
120    /**
121     * Get the query timeout.
122     *
123     * @return The query timeout or null if not set.
124     */
125    public Integer getQueryTimeout() {
126        return queryTimeout;
127    }
128
129    /**
130     * Whether query timeout is set.
131     *
132     * @return true if set, false otherwise.
133     */
134    public boolean isQueryTimeoutSet() {
135        return queryTimeout != null;
136    }
137
138    /**
139     * Builder class for {@code StatementConfiguration} for more flexible construction.
140     */
141    public static final class Builder {
142        private Integer fetchDirection;
143        private Integer fetchSize;
144        private Integer maxRows;
145        private Integer queryTimeout;
146        private Integer maxFieldSize;
147
148        /**
149         * @param fetchDirection The direction for fetching rows from database tables.
150         * @return This builder for chaining.
151         * @see StatementConfiguration#getFetchDirection()
152         */
153        public Builder fetchDirection(final Integer fetchDirection) {
154            this.fetchDirection = fetchDirection;
155            return this;
156        }
157
158        /**
159         * @param fetchSize The number of rows that should be fetched from the database when more rows are needed.
160         * @return This builder for chaining.
161         * @see StatementConfiguration#getFetchSize()
162         */
163        public Builder fetchSize(final Integer fetchSize) {
164            this.fetchSize = fetchSize;
165            return this;
166        }
167
168        /**
169         * @param maxRows The maximum number of rows that a {@code ResultSet} can produce.
170         * @return This builder for chaining.
171         * @see StatementConfiguration#getMaxRows()
172         */
173        public Builder maxRows(final Integer maxRows) {
174            this.maxRows = maxRows;
175            return this;
176        }
177
178        /**
179         * @param queryTimeout The number of seconds the driver will wait for execution.
180         * @return This builder for chaining.
181         * @see StatementConfiguration#getQueryTimeout()
182         */
183        public Builder queryTimeout(final Integer queryTimeout) {
184            this.queryTimeout = queryTimeout;
185            return this;
186        }
187
188        /**
189         * @param maxFieldSize The maximum number of bytes that can be returned for character and binary column values.
190         * @return This builder for chaining.
191         * @see StatementConfiguration#getMaxFieldSize()
192         */
193        public Builder maxFieldSize(final Integer maxFieldSize) {
194            this.maxFieldSize = maxFieldSize;
195            return this;
196        }
197
198        /**
199         * @return A new and configured {@link StatementConfiguration}.
200         */
201        public StatementConfiguration build() {
202            return new StatementConfiguration(fetchDirection, fetchSize, maxFieldSize, maxRows, queryTimeout);
203        }
204    }
205}