Skip to content

Commit

Permalink
Refactor configuration into separate public object
Browse files Browse the repository at this point in the history
Also make the configuration object easier to use
  • Loading branch information
sykesd committed Jun 9, 2016
1 parent ea9dedc commit 3b9e533
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<property name="lib" value="${basedir}/lib" />
<property name="test-lib" value="${basedir}/lib-test" />

<property name="version" value="0.8.0" />
<property name="version" value="0.9.0" />

<property name="product-jar" value="${basedir}/${ant.project.name}-${version}.jar" />
</target>
Expand Down
44 changes: 16 additions & 28 deletions src/org/dt/japper/Japper.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,7 @@ public class Japper {

private static final Log log = LogFactory.getLog(Japper.class);

public static class Config {
private int fetchSize = 500;

public void setFetchSize(int fetchSize) {
this.fetchSize = fetchSize;
}

public int getFetchSize() {
return fetchSize;
}
}

public static final Config DEFAULT_CONFIG = new Config();
public static final JapperConfig DEFAULT_CONFIG = new JapperConfig();



Expand Down Expand Up @@ -112,15 +100,15 @@ public static <T> JapperStreamingResult<T> streamableOf(Connection conn, Class<T
* {@link #DEFAULT_CONFIG} will used for the configuration.
* </p>
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param resultType the result to map the query results to
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @param <T>
* @return a {@link JapperStreamingResult} which allows the caller to treat the results as an {@link Iterable} or as a {@link java.util.stream.Stream}.
*/
public static <T> JapperStreamingResult<T> streamableOf(Config config, Connection conn, Class<T> resultType, String sql, Object...params) {
public static <T> JapperStreamingResult<T> streamableOf(JapperConfig config, Connection conn, Class<T> resultType, String sql, Object...params) {
return streamableOf(config, conn, resultType, null, sql, params);
}

Expand All @@ -129,7 +117,7 @@ public static <T> JapperStreamingResult<T> streamableOf(Config config, Connectio
* in a form that they can be streamed.
* <p>
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param resultType the result to map the query results to
* @param rowProcessor an (optional) {@link org.dt.japper.RowProcessor} to perform additional per-row processing on the result
Expand All @@ -138,7 +126,7 @@ public static <T> JapperStreamingResult<T> streamableOf(Config config, Connectio
* @param <T>
* @return a {@link JapperStreamingResult} which allows the caller to treat the results as an {@link Iterable} or as a {@link java.util.stream.Stream}.
*/
public static <T> JapperStreamingResult<T> streamableOf(Config config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
public static <T> JapperStreamingResult<T> streamableOf(JapperConfig config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
Profile profile = new Profile(resultType, sql);

boolean needsCleanup = true;
Expand Down Expand Up @@ -221,15 +209,15 @@ public static <T> List<T> query(Connection conn, Class<T> resultType, RowProcess
* Execute the given SQL query on the given connection, mapping the result to the given
* resultType
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param resultType the result to map the query results to
* @param rowProcessor an (optional) {@link org.dt.japper.RowProcessor} to perform additional per-row processing on the result
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @return the list of resultType instances containing the results of the query, or an empty list of the query returns no results
*/
public static <T> List<T> query(Config config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
public static <T> List<T> query(JapperConfig config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
Profile profile = new Profile(resultType, sql);

List<T> result = new ArrayList<T>();
Expand Down Expand Up @@ -317,14 +305,14 @@ public static <T> List<T> query(Connection conn, Class<T> resultType, String sql
* Execute the given SQL query on the given connection, mapping the result to the given
* resultType
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param resultType the result to map the query results to
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @return the list of resultType instances containing the results of the query, or an empty list of the query returns no results
*/
public static <T> List<T> query(Config config, Connection conn, Class<T> resultType, String sql, Object...params) {
public static <T> List<T> query(JapperConfig config, Connection conn, Class<T> resultType, String sql, Object...params) {
return query(config, conn, resultType, null, sql, params);
}

Expand Down Expand Up @@ -359,15 +347,15 @@ public static <T> T queryOne(Connection conn, Class<T> resultType, RowProcessor<
* and then returns the first element of the returned list. It is assumed the caller
* is not issuing a query that returns thousands of rows and then only wants the first one
* <p>
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param resultType the result to map the query results to
* @param rowProcessor an (optional) {@link org.dt.japper.RowProcessor} to perform additional per-row processing on the result
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @return the first result of the query mapped to a resultType instances, or null if the query returns no results
*/
public static <T> T queryOne(Config config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
public static <T> T queryOne(JapperConfig config, Connection conn, Class<T> resultType, RowProcessor<T> rowProcessor, String sql, Object...params) {
List<T> results = query(conn, resultType, rowProcessor, sql, params);
if (results.size() > 0) {
return results.get(0);
Expand Down Expand Up @@ -418,13 +406,13 @@ public static QueryResult query(Connection conn, String sql, Object...params) {
* Execute the given SQL query on the given connection, returning the result as a
* {@link QueryResult}.
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @return the result set as a {@link QueryResult}
*/
public static QueryResult query(Config config, Connection conn, String sql, Object...params) {
public static QueryResult query(JapperConfig config, Connection conn, String sql, Object...params) {
Profile profile = new Profile(ResultSet.class, sql);

PreparedStatement ps = null;
Expand Down Expand Up @@ -485,13 +473,13 @@ public static int execute(Connection conn, String sql, Object...params) {
* on the database, but taking advantage of all of the parameter parsing, setting and
* conversions offered by Japper.
*
* @param config the {@link Config} to use when executing this query
* @param config the {@link JapperConfig} to use when executing this query
* @param conn the connection to execute the query on
* @param sql the SQL statement to execute
* @param params the parameters to the query
* @return the number of rows affected by the given statement
*/
public static int execute(Config config, Connection conn, String sql, Object...params) {
public static int execute(JapperConfig config, Connection conn, String sql, Object...params) {
Profile profile = new Profile("statement", int.class, sql);

PreparedStatement ps = null;
Expand Down Expand Up @@ -683,7 +671,7 @@ private static CallableStatement prepareCallSql(Profile profile, Connection conn
return cs;
}

private static PreparedStatement prepareSql(Profile profile, Config config, Connection conn, String sql, Object...params) throws SQLException {
private static PreparedStatement prepareSql(Profile profile, JapperConfig config, Connection conn, String sql, Object...params) throws SQLException {
profile.startPrep();
ParameterParser parser = new ParameterParser(sql, params).parse();

Expand Down
61 changes: 61 additions & 0 deletions src/org/dt/japper/JapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.dt.japper;

/*
* Copyright (c) 2012-2016, David Sykes and Tomasz Orzechowski
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name David Sykes nor Tomasz Orzechowski may be used to endorse
* or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/

/**
* Configuration object for Japper queries.
* <p>
* Holds configuration information that is used by {@link Japper} when
* executing queries.
* </p>
*/
public class JapperConfig {
private int fetchSize = 500; // Default JDBC fetch size

public JapperConfig(int fetchSize) {
setFetchSize(fetchSize);
}

public JapperConfig() {
// use default fetch size
}

public void setFetchSize(int fetchSize) {
this.fetchSize = fetchSize;
}

public int getFetchSize() {
return fetchSize;
}
}

0 comments on commit 3b9e533

Please sign in to comment.