Search in sources :

Example 16 with SqlHelper

use of org.hisp.dhis.commons.util.SqlHelper in project dhis2-core by dhis2.

the class DefaultSqlViewService method getSqlForQuery.

private String getSqlForQuery(Grid grid, SqlView sqlView, Map<String, String> criteria, Map<String, String> variables, List<String> filters, List<String> fields) {
    boolean hasCriteria = criteria != null && !criteria.isEmpty();
    boolean hasFilter = filters != null && !filters.isEmpty();
    String sql = SqlViewUtils.substituteSqlVariables(sqlView.getSqlQuery(), variables);
    if (hasCriteria || hasFilter) {
        sql = SqlViewUtils.removeQuerySeparator(sql);
        String outerSql = "select " + QueryUtils.parseSelectFields(fields) + " from " + "(" + sql + ") as qry ";
        SqlHelper sqlHelper = new SqlHelper();
        if (hasCriteria) {
            outerSql += getCriteriaSqlClause(criteria, sqlHelper);
        }
        if (hasFilter) {
            outerSql += parseFilters(filters, sqlHelper);
        }
        sql = outerSql;
    }
    return sql;
}
Also used : SqlHelper(org.hisp.dhis.commons.util.SqlHelper)

Example 17 with SqlHelper

use of org.hisp.dhis.commons.util.SqlHelper in project dhis2-core by dhis2.

the class JdbcAnalyticsManager method getPartitionSql.

/**
     * If preAggregationMeasureCriteria is specified, generates a query which
     * provides a filtered view of the data according to the criteria .If not, 
     * returns the full view of the partition.
     */
private String getPartitionSql(DataQueryParams params, String partition) {
    if (params.isDataType(DataType.NUMERIC) && !params.getPreAggregateMeasureCriteria().isEmpty()) {
        SqlHelper sqlHelper = new SqlHelper();
        String sql = "";
        sql += "(select * from " + partition + " ";
        for (MeasureFilter filter : params.getPreAggregateMeasureCriteria().keySet()) {
            Double criterion = params.getPreAggregateMeasureCriteria().get(filter);
            sql += sqlHelper.whereAnd() + " value " + OPERATOR_SQL_MAP.get(filter) + " " + criterion + " ";
        }
        sql += ") as " + partition;
        return sql;
    } else {
        return partition;
    }
}
Also used : MeasureFilter(org.hisp.dhis.analytics.MeasureFilter) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Aggregations

SqlHelper (org.hisp.dhis.commons.util.SqlHelper)17 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)11 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 QueryItem (org.hisp.dhis.common.QueryItem)4 Query (org.hibernate.Query)3 TextUtils.getQuotedCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)3 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)3 MeasureFilter (org.hisp.dhis.analytics.MeasureFilter)2 QueryFilter (org.hisp.dhis.common.QueryFilter)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Resource (javax.annotation.Resource)1 StringUtils (org.apache.commons.lang.StringUtils)1 Log (org.apache.commons.logging.Log)1