@Override public String toString() { // Generate "select *" if no dimension or metric is precised final SelectSelectStep<? extends Record> initialSelect = dimensions.size() == 1 && metrics.isEmpty() ? context.select() : context.select(dimensions) .select(metrics); SelectConditionStep<? extends Record> statement = initialSelect.from(tableName) .where(); if (filters != null) { statement = statement.and(Filters.of(filters)); } if (condition != null) { statement = statement.and(condition); } statement.and(DSL.fieldByName("tenant_record_id").eq(tenantRecordId)); if (shouldGroupBy) { return statement.groupBy(dimensions) .getSQL(); } else { return statement.getSQL(); } }
private SelectFinalStep<?> buildSelect() { Select<?> select; SelectFromStep<?> selectFrom; Settings settings = new Settings(); settings.setRenderNameStyle(renderNameStyle); DSLContext ctx = DSL.using(dialect, settings); Table<?> table = getTable(); if (asCount) { if (includedFieldNames.size() > 0) { List<Field<?>> distinctFields = fieldNamesToFields(); selectFrom = ctx.select(DSL.countDistinct(distinctFields.toArray(new Field[distinctFields.size()]))); } else { selectFrom = ctx.selectCount(); } } else { SelectSelectStep<?> selectStep; if (includedFieldNames.size() > 0) { selectStep = ctx.select(fieldNamesToFields()); } else { String tableName = sourceQuery.getBoundQuery().tableName(); if (joinConditions.size() > 0) { selectStep = ctx.selectDistinct(DSL.field(tableName + ".*")); } else { selectStep = ctx.select(DSL.field("*")); } } if (additionalFields.size() > 0) { selectFrom = selectStep.select(additionalFields); } else { selectFrom = selectStep; } } select = selectFrom.from(table); for (JoinCondition joinCondition : joinConditions) { if (joinCondition.isLeftJoin()) { ((SelectJoinStep<?>) select).leftOuterJoin(joinCondition.getTable()).on(joinCondition.getCondition()); } else { ((SelectJoinStep<?>) select).join(joinCondition.getTable()).on(joinCondition.getCondition()); } } select = ((SelectJoinStep<?>) select).where(getConditions()); if (groupByFields.size() > 0) { select = ((SelectJoinStep<?>) select).groupBy(groupByFields); } if (includeOrderings) { select = ((SelectOrderByStep<?>) select).orderBy(orderingsToSortFields()); } if (includeConstraints) { if (sourceQuery.getLimit().isPresent()) { select = ((SelectLimitStep<?>) select).limit(sourceQuery.getLimit().get()); } if (sourceQuery.getOffset().isPresent()) { select = ((SelectOffsetStep<?>) select).offset(sourceQuery.getOffset().get()); } } return (SelectFinalStep<?>) select; }
private SelectSelectStep<Record> select() { return create.select().select(PICTURES.ID.as("picture_Id"), LOCATION.ID, LOCATION.OFFICIAL_NAME, LOCATION.CITY, USERS.DISPLAYNAME, PICTURES.FILENAME, PICTURES.CAPTION, USERS_PICTURES_VOTES.CREATED_ON.as("vote_Created_On"), PICTURES.UP_VOTES); }
private SelectOnConditionStep<Record> from(SelectSelectStep<Record> select, int userId) { return select.from(LOCATION).join(PICTURES, JoinType.JOIN).on(LOCATION.ID.equal(PICTURES.FK_LOCATION)) .join(USERS, JoinType.JOIN).on(PICTURES.FK_USER.equal(USERS.ID)).leftOuterJoin(USERS_PICTURES_VOTES) .on(USERS_PICTURES_VOTES.FK_USER.equal(userId).and(USERS_PICTURES_VOTES.FK_PICTURE.equal(PICTURES.ID))); }
protected SelectSelectStep<Record> select(Field<?>... fields) { return create().select(); }