[#7171] [#7288] Add DSL.count(SelectFieldOrAsterisk) and countDistinct(SelectFieldOrAsterisk)

This commit is contained in:
lukaseder 2018-03-13 10:33:29 +01:00
parent ec42a92708
commit de8efe0430
2 changed files with 34 additions and 9 deletions

View File

@ -16622,6 +16622,14 @@ public class DSL {
return new org.jooq.impl.Function<Integer>("count", SQLDataType.INTEGER, nullSafe(field));
}
/**
* Get the count(field) function.
*/
@Support
public static AggregateFunction<Integer> count(SelectFieldOrAsterisk field) {
return new org.jooq.impl.Function<Integer>("count", SQLDataType.INTEGER, field("{0}", field));
}
/**
* Get the count(table) function.
* <p>
@ -16642,6 +16650,14 @@ public class DSL {
return new org.jooq.impl.Function<Integer>("count", true, SQLDataType.INTEGER, nullSafe(field));
}
/**
* Get the count(distinct field) function.
*/
@Support({ CUBRID, DERBY, H2, HSQLDB, FIREBIRD, MARIADB, MYSQL, POSTGRES, SQLITE })
public static AggregateFunction<Integer> countDistinct(SelectFieldOrAsterisk field) {
return new org.jooq.impl.Function<Integer>("count", true, SQLDataType.INTEGER, field("{0}", field));
}
/**
* Get the count(distinct table) function.
* <p>

View File

@ -5999,22 +5999,31 @@ final class ParserImpl implements Parser {
private static final AggregateFunction<?> parseCountIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "COUNT")) {
parse(ctx, '(');
if (parseIf(ctx, '*')) {
parse(ctx, ')');
return count();
}
boolean distinct = parseSetQuantifier(ctx);
List<Field<?>> fields = distinct
? parseFields(ctx)
: Collections.<Field<?>>singletonList(parseField(ctx));
if (parseIf(ctx, '*') && parse(ctx, ')'))
if (distinct)
return countDistinct();
else
return count();
QualifiedAsterisk asterisk = parseQualifiedAsteriskIf(ctx);
List<Field<?>> fields = (asterisk == null)
? distinct
? parseFields(ctx)
: Collections.<Field<?>>singletonList(parseField(ctx))
: null;
parse(ctx, ')');
if (distinct)
if (fields.size() > 0)
if (fields == null)
return countDistinct(asterisk);
else if (fields.size() > 0)
return countDistinct(fields.toArray(EMPTY_FIELD));
else
return countDistinct(fields.get(0));
else if (fields == null)
return count(asterisk);
else
return count(fields.get(0));
}