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</code>.
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</code>.  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</code> can produce.
036     * @param queryTimeout The number of seconds the driver will wait for execution.
037     */
038    public StatementConfiguration(Integer fetchDirection, Integer fetchSize, Integer maxFieldSize, Integer maxRows,
039                                  Integer queryTimeout) {
040        this.fetchDirection = fetchDirection;
041        this.fetchSize = fetchSize;
042        this.maxFieldSize = maxFieldSize;
043        this.maxRows = maxRows;
044        this.queryTimeout = queryTimeout;
045    }
046
047    /**
048     * Get the fetch direction.
049     *
050     * @return The direction to fetch or null if not set.
051     */
052    public Integer getFetchDirection() {
053        return fetchDirection;
054    }
055
056    /**
057     * Whether fetch direction is set.
058     *
059     * @return true if set, false otherwise.
060     */
061    public boolean isFetchDirectionSet() {
062        return fetchDirection != null;
063    }
064
065    /**
066     * Get the fetch size.
067     *
068     * @return The fetch size or null if not set.
069     */
070    public Integer getFetchSize() {
071        return fetchSize;
072    }
073
074    /**
075     * Whether fetch size is set.
076     *
077     * @return true if set, false otherwise.
078     */
079    public boolean isFetchSizeSet() {
080        return fetchSize != null;
081    }
082
083    /**
084     * Get the max field size.
085     *
086     * @return The max field size or null if not set.
087     */
088    public Integer getMaxFieldSize() {
089        return maxFieldSize;
090    }
091
092    /**
093     * Whether max field size is set.
094     *
095     * @return true if set, false otherwise.
096     */
097    public boolean isMaxFieldSizeSet() {
098        return maxFieldSize != null;
099    }
100
101    /**
102     * Get the max rows.
103     *
104     * @return The max rows or null if not set.
105     */
106    public Integer getMaxRows() {
107        return maxRows;
108    }
109
110    /**
111     * Whether max rows is set.
112     *
113     * @return true if set, false otherwise.
114     */
115    public boolean isMaxRowsSet() {
116        return maxRows != null;
117    }
118
119    /**
120     * Get the query timeout.
121     *
122     * @return The query timeout or null if not set.
123     */
124    public Integer getQueryTimeout() {
125        return queryTimeout;
126    }
127
128    /**
129     * Whether query timeout is set.
130     *
131     * @return true if set, false otherwise.
132     */
133    public boolean isQueryTimeoutSet() {
134        return queryTimeout != null;
135    }
136
137    /**
138     * Builder class for <code>StatementConfiguration</code> for more flexible construction.
139     */
140    public static final class Builder {
141        private Integer fetchDirection;
142        private Integer fetchSize;
143        private Integer maxRows;
144        private Integer queryTimeout;
145        private Integer maxFieldSize;
146
147        public Builder fetchDirection(final Integer fetchDirection) {
148            this.fetchDirection = fetchDirection;
149            return this;
150        }
151
152        public Builder fetchSize(final Integer fetchSize) {
153            this.fetchSize = fetchSize;
154            return this;
155        }
156
157        public Builder maxRows(final Integer maxRows) {
158            this.maxRows = maxRows;
159            return this;
160        }
161
162        public Builder queryTimeout(final Integer queryTimeout) {
163            this.queryTimeout = queryTimeout;
164            return this;
165        }
166
167        public Builder maxFieldSize(final Integer maxFieldSize) {
168            this.maxFieldSize = maxFieldSize;
169            return this;
170        }
171
172        public StatementConfiguration build() {
173            return new StatementConfiguration(fetchDirection, fetchSize, maxFieldSize, maxRows, queryTimeout);
174        }
175    }
176}