Search in sources :

Example 1 with ANALYTICS_TBL_ALIAS

use of org.hisp.dhis.analytics.util.AnalyticsSqlUtils.ANALYTICS_TBL_ALIAS in project dhis2-core by dhis2.

the class JdbcRawAnalyticsManager method getSelectStatement.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
 * Returns a SQL select statement.
 *
 * @param params the data query parameters.
 * @param dimensions the list of dimensions.
 * @return a SQL select statement.
 */
private String getSelectStatement(DataQueryParams params, List<DimensionalObject> dimensions) {
    String idScheme = ObjectUtils.firstNonNull(params.getOutputIdScheme(), IdScheme.UID).getIdentifiableString().toLowerCase();
    List<String> dimensionColumns = dimensions.stream().map(d -> asColumnSelect(d, idScheme)).collect(Collectors.toList());
    SqlHelper sqlHelper = new SqlHelper();
    String sql = "select " + StringUtils.join(dimensionColumns, ", ") + ", " + DIM_NAME_OU + ", value " + "from " + params.getTableName() + " as " + ANALYTICS_TBL_ALIAS + " " + "inner join organisationunit ou on ax.ou = ou.uid " + "inner join _orgunitstructure ous on ax.ou = ous.organisationunituid " + "inner join _periodstructure ps on ax.pe = ps.iso ";
    for (DimensionalObject dim : dimensions) {
        if (!dim.getItems().isEmpty() && !dim.isFixed()) {
            String col = quote(dim.getDimensionName());
            if (DimensionalObject.ORGUNIT_DIM_ID.equals(dim.getDimension())) {
                sql += sqlHelper.whereAnd() + " (";
                for (DimensionalItemObject item : dim.getItems()) {
                    OrganisationUnit unit = (OrganisationUnit) item;
                    sql += DIM_NAME_OU + " like '" + unit.getPath() + "%' or ";
                }
                sql = TextUtils.removeLastOr(sql) + ") ";
            } else {
                sql += sqlHelper.whereAnd() + " " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
            }
        }
    }
    sql += sqlHelper.whereAnd() + " " + "ps.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "' and " + "ps.enddate <= '" + DateUtils.getMediumDateString(params.getEndDate()) + "' ";
    return sql;
}
Also used : org.hisp.dhis.common(org.hisp.dhis.common) AnalyticsSqlUtils.quote(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) AnalyticsUtils(org.hisp.dhis.analytics.util.AnalyticsUtils) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) ANALYTICS_TBL_ALIAS(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.ANALYTICS_TBL_ALIAS) Qualifier(org.springframework.beans.factory.annotation.Qualifier) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils(org.hisp.dhis.util.DateUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) Assert(org.springframework.util.Assert) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)1 RawAnalyticsManager (org.hisp.dhis.analytics.RawAnalyticsManager)1 ANALYTICS_TBL_ALIAS (org.hisp.dhis.analytics.util.AnalyticsSqlUtils.ANALYTICS_TBL_ALIAS)1 AnalyticsSqlUtils.quote (org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote)1 AnalyticsUtils (org.hisp.dhis.analytics.util.AnalyticsUtils)1 org.hisp.dhis.common (org.hisp.dhis.common)1 IdentifiableObjectUtils.getUids (org.hisp.dhis.common.IdentifiableObjectUtils.getUids)1 SqlHelper (org.hisp.dhis.commons.util.SqlHelper)1 TextUtils (org.hisp.dhis.commons.util.TextUtils)1 TextUtils.getQuotedCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 DateUtils (org.hisp.dhis.util.DateUtils)1 ObjectUtils (org.hisp.dhis.util.ObjectUtils)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1