diff --git a/querydsl-sql/src/main/java/com/mysema/query/sql/AbstractSQLQuery.java b/querydsl-sql/src/main/java/com/mysema/query/sql/AbstractSQLQuery.java index e84e95705..f48118cc3 100644 --- a/querydsl-sql/src/main/java/com/mysema/query/sql/AbstractSQLQuery.java +++ b/querydsl-sql/src/main/java/com/mysema/query/sql/AbstractSQLQuery.java @@ -13,7 +13,6 @@ */ package com.mysema.query.sql; -import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.sql.Connection; import java.sql.PreparedStatement; @@ -24,6 +23,12 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + import com.google.common.collect.ImmutableList; import com.mysema.commons.lang.CloseableIterator; import com.mysema.query.*; @@ -31,9 +36,6 @@ import com.mysema.query.support.QueryMixin; import com.mysema.query.types.*; import com.mysema.query.types.expr.Wildcard; import com.mysema.util.ResultSetAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; /** * AbstractSQLQuery is the base type for SQL query implementations @@ -59,6 +61,8 @@ public abstract class AbstractSQLQuery> extends Pr private Object lastCell; + private StatementOptions statementOptions = StatementOptions.DEFAULT; + public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration) { this(conn, configuration, new DefaultQueryMetadata().noValidate()); } @@ -176,7 +180,7 @@ public abstract class AbstractSQLQuery> extends Pr constants = serializer.getConstants(); listeners.prePrepare(context); - final PreparedStatement stmt = conn.prepareStatement(queryString); + final PreparedStatement stmt = getPreparedStatement(queryString); setParameters(stmt, constants, serializer.getConstantPaths(), getMetadata().getParams()); context.addPreparedStatement(stmt); listeners.prepared(context); @@ -204,6 +208,23 @@ public abstract class AbstractSQLQuery> extends Pr } } + private PreparedStatement getPreparedStatement(String queryString) throws SQLException { + PreparedStatement statement = conn.prepareStatement(queryString); + if (statementOptions.getFetchSize() != null) { + statement.setFetchSize(statementOptions.getFetchSize()); + } + if (statementOptions.getMaxFieldSize() != null) { + statement.setMaxFieldSize(statementOptions.getMaxFieldSize()); + } + if (statementOptions.getQueryTimeout() != null) { + statement.setQueryTimeout(statementOptions.getQueryTimeout()); + } + if (statementOptions.getMaxRows() != null) { + statement.setMaxRows(statementOptions.getMaxRows()); + } + return statement; + } + protected Configuration getConfiguration() { return configuration; } @@ -233,7 +254,7 @@ public abstract class AbstractSQLQuery> extends Pr constants = serializer.getConstants(); listeners.prePrepare(context); - final PreparedStatement stmt = conn.prepareStatement(queryString); + final PreparedStatement stmt = getPreparedStatement(queryString); setParameters(stmt, constants, serializer.getConstantPaths(), metadata.getParams()); context.addPreparedStatement(stmt); listeners.prepared(context); @@ -305,7 +326,7 @@ public abstract class AbstractSQLQuery> extends Pr constants = serializer.getConstants(); listeners.prePrepare(context); - final PreparedStatement stmt = conn.prepareStatement(queryString); + final PreparedStatement stmt = getPreparedStatement(queryString); try { setParameters(stmt, constants, serializer.getConstantPaths(), queryMixin.getMetadata().getParams()); context.addPreparedStatement(stmt); @@ -482,7 +503,7 @@ public abstract class AbstractSQLQuery> extends Pr constants = serializer.getConstants(); listeners.prePrepare(context); - stmt = conn.prepareStatement(queryString); + stmt = getPreparedStatement(queryString); setParameters(stmt, constants, serializer.getConstantPaths(), getMetadata().getParams()); context.addPreparedStatement(stmt); @@ -548,4 +569,8 @@ public abstract class AbstractSQLQuery> extends Pr public abstract Q clone(Connection connection); + public void setStatementOptions(StatementOptions statementOptions) { + this.statementOptions = statementOptions; + } + } diff --git a/querydsl-sql/src/main/java/com/mysema/query/sql/StatementOptions.java b/querydsl-sql/src/main/java/com/mysema/query/sql/StatementOptions.java new file mode 100644 index 000000000..d7ecb7e7d --- /dev/null +++ b/querydsl-sql/src/main/java/com/mysema/query/sql/StatementOptions.java @@ -0,0 +1,96 @@ +/* + * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mysema.query.sql; + +import java.sql.Statement; + +import javax.annotation.concurrent.Immutable; + +/** + * {@code StatementOptions} holds parameters that should be applied to {@link Statement}s. + */ +@Immutable +public class StatementOptions { + + public static final StatementOptions DEFAULT = new StatementOptions(null, null, null, null); + + private final Integer maxFieldSize; + private final Integer maxRows; + private final Integer queryTimeout; + private final Integer fetchSize; + + public StatementOptions(Integer maxFieldSize, Integer maxRows, Integer queryTimeout, Integer fetchSize) { + this.maxFieldSize = maxFieldSize; + this.maxRows = maxRows; + this.queryTimeout = queryTimeout; + this.fetchSize = fetchSize; + } + + public Integer getMaxFieldSize() { + return maxFieldSize; + } + + public Integer getMaxRows() { + return maxRows; + } + + public Integer getQueryTimeout() { + return queryTimeout; + } + + public Integer getFetchSize() { + return fetchSize; + } + + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link StatementOptions} + */ + public static final class Builder { + private Integer maxFieldSize; + private Integer maxRows; + private Integer queryTimeout; + private Integer fetchSize; + + private Builder() { } + + public Builder setMaxFieldSize(Integer maxFieldSize) { + this.maxFieldSize = maxFieldSize; + return this; + } + + public Builder setMaxRows(Integer maxRows) { + this.maxRows = maxRows; + return this; + } + + public Builder setQueryTimeout(Integer queryTimeout) { + this.queryTimeout = queryTimeout; + return this; + } + + public Builder setFetchSize(Integer fetchSize) { + this.fetchSize = fetchSize; + return this; + } + + public StatementOptions build() { + return new StatementOptions(maxFieldSize, maxRows, queryTimeout, fetchSize); + } + } +} \ No newline at end of file diff --git a/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java b/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java index 8ea681fa7..ea7228aec 100644 --- a/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java +++ b/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java @@ -1872,4 +1872,22 @@ public class SelectBase extends AbstractBaseTest { assertEquals(Integer.valueOf(200007), query.singleResult(employee.datefield.yearWeek())); } + @Test + public void StatementOptions() { + StatementOptions options = StatementOptions.builder().setFetchSize(15).setMaxRows(150).build(); + TestQuery query = query().from(employee).orderBy(employee.id.asc()); + query.setStatementOptions(options); + query.addListener(new TestLoggingListener() { + public void preExecute(SQLListenerContext context) { + try { + assertEquals(15, context.getPreparedStatement().getFetchSize()); + assertEquals(150, context.getPreparedStatement().getMaxRows()); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + }); + query.list(employee.id); + } + }