[#1038] Introduce new type GroupField for cube(), rollup() and

groupingSets() functions. Accept only Field... and GroupField... in
groupBy() clauses
This commit is contained in:
Lukas Eder 2012-10-27 22:48:55 +02:00
parent 9caa7041c2
commit cfec453d50
8 changed files with 90 additions and 37 deletions

View File

@ -62,11 +62,14 @@ import org.jooq.util.oracle.OracleFactory;
/**
* A field used in tables and conditions
* <p>
* Note that all fields qualify as {@link GroupField}, i.e. they can always be
* used in <code>GROUP BY</code> clauses
*
* @param <T> The field type
* @author Lukas Eder
*/
public interface Field<T> extends QueryPart {
public interface Field<T> extends GroupField {
// ------------------------------------------------------------------------
// API

View File

@ -0,0 +1,46 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 "jOOQ" nor the names of its contributors 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 OWNER 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.
*/
package org.jooq;
/**
* A <code>QueryPart</code> to be used exclusively in <code>GROUP BY</code>
* clauses
*
* @author Lukas Eder
*/
public interface GroupField extends QueryPart {
}

View File

@ -88,7 +88,7 @@ public interface SelectGroupByStep extends SelectHavingStep {
* <code>GROUP BY ()</code> clause being rendered.
*/
@Support
SelectHavingStep groupBy(Field<?>... fields);
SelectHavingStep groupBy(GroupField... fields);
/**
* Add a <code>GROUP BY</code> clause to the query
@ -97,5 +97,5 @@ public interface SelectGroupByStep extends SelectHavingStep {
* <code>GROUP BY ()</code> clause being rendered.
*/
@Support
SelectHavingStep groupBy(Collection<? extends Field<?>> fields);
SelectHavingStep groupBy(Collection<? extends GroupField> fields);
}

View File

@ -194,7 +194,7 @@ public interface SelectQuery extends SimpleSelectQuery<Record> {
* @param fields The grouping fields
*/
@Support
void addGroupBy(Field<?>... fields);
void addGroupBy(GroupField... fields);
/**
* Adds grouping fields
@ -205,7 +205,7 @@ public interface SelectQuery extends SimpleSelectQuery<Record> {
* @param fields The grouping fields
*/
@Support
void addGroupBy(Collection<? extends Field<?>> fields);
void addGroupBy(Collection<? extends GroupField> fields);
/**
* Adds new conditions to the having clause of the query, connecting it to

View File

@ -57,6 +57,7 @@ import org.jooq.BindContext;
import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.GroupField;
import org.jooq.Operator;
import org.jooq.Param;
import org.jooq.QueryPart;
@ -81,28 +82,28 @@ abstract class AbstractSubSelect<R extends Record> extends AbstractSelect<R> imp
/**
* Generated UID
*/
private static final long serialVersionUID = 1646393178384872967L;
private static final long serialVersionUID = 1646393178384872967L;
private final FieldList select;
private String hint;
private boolean distinct;
private boolean forUpdate;
private final FieldList forUpdateOf;
private final TableList forUpdateOfTables;
private ForUpdateMode forUpdateMode;
private int forUpdateWait;
private boolean forShare;
private final TableList from;
private final ConditionProviderImpl condition;
private final ConditionProviderImpl connectBy;
private boolean connectByNoCycle;
private final ConditionProviderImpl connectByStartWith;
private boolean grouping;
private final FieldList groupBy;
private final ConditionProviderImpl having;
private final SortFieldList orderBy;
private boolean orderBySiblings;
private final Limit limit;
private final FieldList select;
private String hint;
private boolean distinct;
private boolean forUpdate;
private final FieldList forUpdateOf;
private final TableList forUpdateOfTables;
private ForUpdateMode forUpdateMode;
private int forUpdateWait;
private boolean forShare;
private final TableList from;
private final ConditionProviderImpl condition;
private final ConditionProviderImpl connectBy;
private boolean connectByNoCycle;
private final ConditionProviderImpl connectByStartWith;
private boolean grouping;
private final QueryPartList<GroupField> groupBy;
private final ConditionProviderImpl having;
private final SortFieldList orderBy;
private boolean orderBySiblings;
private final Limit limit;
AbstractSubSelect(Configuration configuration) {
this(configuration, null);
@ -121,7 +122,7 @@ abstract class AbstractSubSelect<R extends Record> extends AbstractSelect<R> imp
this.condition = new ConditionProviderImpl();
this.connectBy = new ConditionProviderImpl();
this.connectByStartWith = new ConditionProviderImpl();
this.groupBy = new FieldList();
this.groupBy = new QueryPartList<GroupField>();
this.having = new ConditionProviderImpl();
this.orderBy = new SortFieldList();
this.limit = new Limit();
@ -748,7 +749,7 @@ abstract class AbstractSubSelect<R extends Record> extends AbstractSelect<R> imp
grouping = true;
}
final FieldList getGroupBy() {
final QueryPartList<GroupField> getGroupBy() {
return groupBy;
}

View File

@ -70,6 +70,7 @@ import org.jooq.DatePart;
import org.jooq.FactoryOperations;
import org.jooq.Field;
import org.jooq.GroupConcatOrderByStep;
import org.jooq.GroupField;
import org.jooq.Name;
import org.jooq.OrderedAggregateFunction;
import org.jooq.Param;
@ -2427,7 +2428,7 @@ public class Factory {
* @return A field to be used in a <code>GROUP BY</code> clause
*/
@Support({ CUBRID, DB2, MYSQL, ORACLE, SQLSERVER, SYBASE })
public static Field<?> rollup(Field<?>... fields) {
public static GroupField rollup(Field<?>... fields) {
return new Rollup(nullSafe(fields));
}
@ -2453,7 +2454,7 @@ public class Factory {
* @return A field to be used in a <code>GROUP BY</code> clause
*/
@Support({ DB2, ORACLE, SQLSERVER, SYBASE })
public static Field<?> cube(Field<?>... fields) {
public static GroupField cube(Field<?>... fields) {
return function("cube", Object.class, nullSafe(fields));
}
@ -2481,7 +2482,7 @@ public class Factory {
*/
@SuppressWarnings("unchecked")
@Support({ DB2, ORACLE, SQLSERVER, SYBASE })
public static Field<?> groupingSets(Field<?>... fields) {
public static GroupField groupingSets(Field<?>... fields) {
List<Field<?>>[] array = new List[fields.length];
for (int i = 0; i < fields.length; i++) {
@ -2515,7 +2516,7 @@ public class Factory {
*/
@SuppressWarnings("unchecked")
@Support({ DB2, ORACLE, SQLSERVER, SYBASE })
public static Field<?> groupingSets(Field<?>[]... fieldSets) {
public static GroupField groupingSets(Field<?>[]... fieldSets) {
List<Field<?>>[] array = new List[fieldSets.length];
for (int i = 0; i < fieldSets.length; i++) {
@ -2548,7 +2549,7 @@ public class Factory {
* @return A field to be used in a <code>GROUP BY</code> clause
*/
@Support({ DB2, ORACLE, SQLSERVER, SYBASE })
public static Field<?> groupingSets(Collection<Field<?>>... fieldSets) {
public static GroupField groupingSets(Collection<Field<?>>... fieldSets) {
WrappedList[] array = new WrappedList[fieldSets.length];
for (int i = 0; i < fieldSets.length; i++) {

View File

@ -47,6 +47,7 @@ import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.GroupField;
import org.jooq.JoinType;
import org.jooq.Operator;
import org.jooq.Param;
@ -391,13 +392,13 @@ class SelectImpl extends AbstractDelegatingSelect<Record> implements
}
@Override
public final SelectImpl groupBy(Field<?>... fields) {
public final SelectImpl groupBy(GroupField... fields) {
getQuery().addGroupBy(fields);
return this;
}
@Override
public final SelectImpl groupBy(Collection<? extends Field<?>> fields) {
public final SelectImpl groupBy(Collection<? extends GroupField> fields) {
getQuery().addGroupBy(fields);
return this;
}

View File

@ -43,6 +43,7 @@ import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.GroupField;
import org.jooq.JoinType;
import org.jooq.Operator;
import org.jooq.Record;
@ -104,13 +105,13 @@ class SelectQueryImpl extends AbstractSubSelect<Record> implements SelectQuery {
}
@Override
public final void addGroupBy(Collection<? extends Field<?>> fields) {
public final void addGroupBy(Collection<? extends GroupField> fields) {
setGrouping();
getGroupBy().addAll(fields);
}
@Override
public final void addGroupBy(Field<?>... fields) {
public final void addGroupBy(GroupField... fields) {
addGroupBy(Arrays.asList(fields));
}