Java Code Examples for org.jooq.impl.DSL#count()

The following examples show how to use org.jooq.impl.DSL#count() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryBuilder.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Build a count query.
 *
 * @return the count query.
 */
public ResultQuery<Record1<Integer>> buildCount() {
    gatherData();

    DSLContext dslContext = pm.getDslContext();
    AggregateFunction<Integer> count;
    if (queryState.isDistinctRequired()) {
        count = DSL.countDistinct(queryState.getSqlMainIdField());
    } else {
        count = DSL.count(queryState.getSqlMainIdField());
    }
    SelectConditionStep<Record1<Integer>> query = dslContext.select(count)
            .from(queryState.getSqlFrom())
            .where(queryState.getSqlWhere());

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(GENERATED_SQL, query.getSQL(ParamType.INDEXED));
    }
    return query;
}
 
Example 2
Source File: ChangeReporter.java    From waltz with Apache License 2.0 5 votes vote down vote up
private static Result<Record> summarizeChangesByAppSelector(
        DSLContext dsl,
        Select<Record1<Long>> appIdSelector,
        LocalDateTime startTime,
        LocalDateTime endTime) {

    Field<Integer> countField = DSL.count();

    return dsl
            .select(APPLICATION.NAME.as("Application Name"),
                    CHANGE_LOG.PARENT_ID.as("Application ID"),
                    CHANGE_LOG.USER_ID.as("User ID"),
                    CHANGE_LOG.CHILD_KIND.as("Updated Entity Type"),
                    CHANGE_LOG.OPERATION.as("Operation"))
            .select(countField.as("Count"))
            .from(CHANGE_LOG)
            .join(APPLICATION)
            .on(CHANGE_LOG.PARENT_ID.eq(APPLICATION.ID)
                    .and(CHANGE_LOG.PARENT_KIND.eq(EntityKind.APPLICATION.name())))
            .where(CHANGE_LOG.PARENT_KIND.eq(EntityKind.APPLICATION.name())
                    .and(CHANGE_LOG.PARENT_ID.in(appIdSelector))
                    .and(CHANGE_LOG.CREATED_AT.between(Timestamp.valueOf(startTime), Timestamp.valueOf(endTime))))
            .groupBy(CHANGE_LOG.PARENT_ID,
                    APPLICATION.NAME,
                    CHANGE_LOG.USER_ID,
                    CHANGE_LOG.CHILD_KIND,
                    CHANGE_LOG.OPERATION)
            .orderBy(APPLICATION.NAME,
                    CHANGE_LOG.USER_ID,
                    CHANGE_LOG.CHILD_KIND,
                    CHANGE_LOG.OPERATION)
            .fetch();
}
 
Example 3
Source File: AuthFlowsRatios.java    From waltz with Apache License 2.0 4 votes vote down vote up
private void go() {
    LOG.info("started");

    AggregateFunction<Integer> count = DSL.count();
    Result<Record4<String, String, String, Integer>> records = dsl
            .select(
                    dt.CODE,
                    dt.NAME,
                    lfd.RATING,
                    count)
            .from(dt)
            .innerJoin(lfd).on(dtToLfdJoinCond)
            .innerJoin(lf).on(lfdToLfJoinCond)
            .where(lfNotRemovedCond)
            .groupBy(
                    dt.CODE,
                    dt.NAME,
                    lfd.RATING)
            .fetch();

    String body = seq(records)
            .grouped(r -> tuple(
                    r.get(dt.CODE),
                    r.get(dt.NAME)))
            .map(g -> {
                Map<String, Integer> countsByRating = g.v2.toMap(
                        r -> r.get(lfd.RATING),
                        r -> r.get(count));
                int primary = countsByRating.getOrDefault("PRIMARY", 0);
                int secondary = countsByRating.getOrDefault("SECONDARY", 0);
                int discouraged = countsByRating.getOrDefault("DISCOURAGED", 0);
                int noOpinion = countsByRating.getOrDefault("NO_OPINION", 0);
                long total = sumInts(countsByRating.values());

                return tuple(
                        g.v1.v2,    // dtName
                        g.v1.v1,    // dtCode
                        total,
                        primary,
                        secondary,
                        discouraged,
                        noOpinion,
                        primary > 0
                                ? (float) total / primary
                                : 0,
                        primary + secondary > 0
                                ? (float) total / (primary + secondary)
                                : 0);
            })
            .map(t -> join(
                    t.toList(),
                    "\t"))
            .collect(joining("\n"));

    System.out.printf(
            "%s\n%s\n",
            mkHeader(),
            body);

    LOG.info("ended");
}