Search in sources :

Example 1 with Operator

use of org.pmiops.workbench.model.Operator in project workbench by all-of-us.

the class FieldSetQueryBuilder method handleComparison.

private void handleComparison(ColumnFilter columnFilter, ColumnConfig columnConfig, StringBuilder sqlBuilder, Map<String, QueryParameterValue> paramMap) {
    String paramName = "p" + paramMap.size();
    Operator operator = columnFilter.getOperator();
    if (columnFilter.getValueNumbers() != null || columnFilter.getValues() != null) {
        throw new BadRequestException("Can't use valueNumbers or values with operator " + operator);
    }
    if (!((columnFilter.getValue() != null) ^ (columnFilter.getValueDate() != null) ^ (columnFilter.getValueNumber() != null) ^ (columnFilter.getValueNull() != null && columnFilter.getValueNull()))) {
        throw new BadRequestException("Exactly one of value, valueDate, valueNumber, and valueNull " + "must be specified for filter on column " + columnConfig.name);
    }
    if (operator.equals(Operator.LIKE) && columnFilter.getValue() == null) {
        throw new BadRequestException("LIKE operator only support with value");
    }
    if (columnFilter.getValue() != null) {
        if (!columnConfig.type.equals(ColumnType.STRING)) {
            throw new BadRequestException("Can't use value with column " + columnConfig.name + " of type " + columnConfig.type);
        }
        paramMap.put(paramName, QueryParameterValue.string(columnFilter.getValue()));
    } else if (columnFilter.getValueDate() != null) {
        if (columnConfig.type.equals(ColumnType.DATE)) {
            try {
                DATE_FORMAT.parse(columnFilter.getValueDate());
                paramMap.put(paramName, QueryParameterValue.date(columnFilter.getValueDate()));
            } catch (ParseException e) {
                throw new BadRequestException("Couldn't parse date value " + columnFilter.getValueDate() + "; expected format: " + DATE_FORMAT_PATTERN);
            }
        } else if (columnConfig.type.equals(ColumnType.TIMESTAMP)) {
            try {
                long timestamp = DATE_TIME_FORMAT.parseDateTime(columnFilter.getValueDate()).getMillis() * 1000;
                paramMap.put(paramName, QueryParameterValue.timestamp(timestamp));
            } catch (IllegalArgumentException e) {
                throw new BadRequestException("Couldn't parse timestamp value " + columnFilter.getValueDate() + "; expected format: " + DATE_TIME_FORMAT_PATTERN);
            }
        } else {
            throw new BadRequestException("Can't use valueDate with column " + columnConfig.name + " of type " + columnConfig.type);
        }
    } else if (columnFilter.getValueNumber() != null) {
        if (columnConfig.type.equals(ColumnType.FLOAT)) {
            paramMap.put(paramName, QueryParameterValue.float64(columnFilter.getValueNumber().doubleValue()));
        } else if (columnConfig.type.equals(ColumnType.INTEGER)) {
            paramMap.put(paramName, QueryParameterValue.int64(columnFilter.getValueNumber().longValue()));
        } else {
            throw new BadRequestException("Can't use valueNumber with column " + columnConfig.name + " of type " + columnConfig.type);
        }
    } else if (columnFilter.getValueNull() != null && columnFilter.getValueNull()) {
        if (operator != Operator.EQUAL) {
            throw new BadRequestException("Unsupported operator for valueNull: " + operator);
        }
        sqlBuilder.append(columnFilter.getColumnName());
        sqlBuilder.append(" is null\n");
        return;
    }
    sqlBuilder.append(columnFilter.getColumnName());
    sqlBuilder.append(' ');
    sqlBuilder.append(OperatorUtils.getSqlOperator(columnFilter.getOperator()));
    sqlBuilder.append(" @");
    sqlBuilder.append(paramName);
    sqlBuilder.append("\n");
}
Also used : Operator(org.pmiops.workbench.model.Operator) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ParseException(java.text.ParseException)

Aggregations

ParseException (java.text.ParseException)1 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)1 Operator (org.pmiops.workbench.model.Operator)1