Search in sources :

Example 26 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class FieldSetQueryBuilder method handleColumnFilters.

private void handleColumnFilters(List<ColumnFilter> columnFilters, TableQueryAndConfig tableQueryAndConfig, StringBuilder sqlBuilder, Map<String, QueryParameterValue> paramMap) {
    if (columnFilters.isEmpty()) {
        throw new BadRequestException("Empty column filter list is invalid");
    }
    sqlBuilder.append("(");
    boolean first = true;
    for (ColumnFilter columnFilter : columnFilters) {
        if (first) {
            first = false;
        } else {
            sqlBuilder.append("\nand\n");
        }
        handleColumnFilter(columnFilter, tableQueryAndConfig, sqlBuilder, paramMap);
    }
    sqlBuilder.append(")");
}
Also used : BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ColumnFilter(org.pmiops.workbench.model.ColumnFilter)

Example 27 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class FieldSetQueryBuilder method buildQuery.

public QueryJobConfiguration buildQuery(ParticipantCriteria participantCriteria, TableQueryAndConfig tableQueryAndConfig, long resultSize, long offset) {
    TableQuery tableQuery = tableQueryAndConfig.getTableQuery();
    List<String> columnNames = tableQuery.getColumns();
    String tableName = tableQuery.getTableName();
    StringBuilder startSql = new StringBuilder("select ");
    // TODO: add column aliases, use below
    startSql.append(Joiner.on(", ").join(columnNames));
    startSql.append("\nfrom `${projectId}.${dataSetId}.");
    startSql.append(tableName);
    startSql.append("` ");
    startSql.append(tableName);
    startSql.append("\nwhere\n");
    Map<String, QueryParameterValue> paramMap = new HashMap<>();
    List<List<ColumnFilter>> columnFilters = tableQuery.getFilters();
    if (columnFilters != null && !columnFilters.isEmpty()) {
        startSql.append("(");
        boolean first = true;
        for (List<ColumnFilter> filterList : columnFilters) {
            if (first) {
                first = false;
            } else {
                startSql.append("\nor\n");
            }
            handleColumnFilters(filterList, tableQueryAndConfig, startSql, paramMap);
        }
        startSql.append(")\nand\n");
    }
    StringBuilder endSql = new StringBuilder("order by ");
    List<String> orderBy = tableQuery.getOrderBy();
    if (orderBy.isEmpty()) {
        throw new BadRequestException("Order by list must not be empty");
    }
    endSql.append(Joiner.on(", ").join(orderBy));
    endSql.append(" limit ");
    endSql.append(resultSize);
    if (offset > 0) {
        endSql.append(" offset ");
        endSql.append(offset);
    }
    return participantCounter.buildQuery(participantCriteria, startSql.toString(), endSql.toString(), tableName, paramMap);
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) HashMap(java.util.HashMap) ColumnFilter(org.pmiops.workbench.model.ColumnFilter) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) TableQuery(org.pmiops.workbench.model.TableQuery) List(java.util.List)

Aggregations

BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)27 Timestamp (java.sql.Timestamp)5 CohortReview (org.pmiops.workbench.db.model.CohortReview)5 Workspace (org.pmiops.workbench.db.model.Workspace)5 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)5 Test (org.junit.Test)4 CohortAnnotationDefinition (org.pmiops.workbench.db.model.CohortAnnotationDefinition)4 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)4 ConflictException (org.pmiops.workbench.exceptions.ConflictException)4 User (org.pmiops.workbench.db.model.User)3 Workspace (org.pmiops.workbench.model.Workspace)3 QueryResult (com.google.cloud.bigquery.QueryResult)2 Gson (com.google.gson.Gson)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ParticipantCriteria (org.pmiops.workbench.cohortbuilder.ParticipantCriteria)2 TableQueryAndConfig (org.pmiops.workbench.cohortbuilder.TableQueryAndConfig)2 CdrVersion (org.pmiops.workbench.db.model.CdrVersion)2