All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protempa.backend.dsb.relationaldb.AbstractSelectStatement Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * Protempa Commons Backend Provider
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.protempa.backend.dsb.relationaldb;

import java.util.ArrayList;
import org.protempa.backend.dsb.filter.Filter;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class AbstractSelectStatement implements SelectStatement {

    private final EntitySpec entitySpec;
    private final ReferenceSpec referenceSpec;
    private final List entitySpecs;
    private final Map inboundReferenceSpecs;
    private final Set filters;
    private final Set propIds;
    private final Set keyIds;
    private final SQLOrderBy order;
    private final SQLGenResultProcessor resultProcessor;
    private final StagingSpec[] stagedTables;
    private final boolean streamingMode;
    private final boolean wrapKeyId;

    protected AbstractSelectStatement(EntitySpec entitySpec,
            ReferenceSpec referenceSpec, List entitySpecs,
            Map inboundReferenceSpecs,
            Set filters, Set propIds, Set keyIds,
            SQLOrderBy order, SQLGenResultProcessor resultProcessor,
            StagingSpec[] stagedTables, boolean streamingMode,
            boolean wrapKeyId) {
        this.entitySpec = entitySpec;
        this.referenceSpec = referenceSpec;
        this.entitySpecs = Collections.unmodifiableList(entitySpecs);
        this.inboundReferenceSpecs = Collections.unmodifiableMap(inboundReferenceSpecs);
        this.filters = Collections.unmodifiableSet(filters);
        this.propIds = Collections.unmodifiableSet(propIds);
        this.keyIds = Collections.unmodifiableSet(keyIds);
        this.order = order;
        this.resultProcessor = resultProcessor;
        this.stagedTables = stagedTables;
        this.streamingMode = streamingMode;
        this.wrapKeyId = wrapKeyId;
    }

    protected EntitySpec getEntitySpec() {
        return entitySpec;
    }

    protected ReferenceSpec getReferenceSpec() {
        return referenceSpec;
    }

    protected List getEntitySpecs() {
        return entitySpecs;
    }

    protected Map getInboundReferenceSpecs() {
        return inboundReferenceSpecs;
    }

    protected Set getFilters() {
        return filters;
    }

    protected Set getPropIds() {
        return propIds;
    }

    protected Set getKeyIds() {
        return keyIds;
    }

    protected SQLOrderBy getOrder() {
        return order;
    }

    protected SQLGenResultProcessor getResultProcessor() {
        return resultProcessor;
    }

    protected StagingSpec[] getStagedTables() {
        return stagedTables;
    }

    protected abstract SelectClause getSelectClause(ColumnSpecInfo info,
            TableAliaser referenceIndices, EntitySpec entitySpec, boolean wrapKeyId);

    protected abstract FromClause getFromClause(List columnSpecs,
            TableAliaser referenceIndices, StagingSpec[] stagedTables);

    protected abstract WhereClause getWhereClause(Set propIds,
            ColumnSpecInfo info, List entitySpecs,
            Set filters, TableAliaser referenceIndices,
            Set keyIds, SQLOrderBy order,
            SQLGenResultProcessor resultProcessor, SelectClause selectClause,
            StagingSpec[] stagedTables);

    @Override
    public String generateStatement() {
        ColumnSpecInfo info = new ColumnSpecInfoFactory().newInstance(propIds,
                entitySpec, entitySpecs, inboundReferenceSpecs, filters, referenceSpec,
                this.streamingMode);
        TableAliaser referenceIndices = new TableAliaser(info.getColumnSpecs(),
                "a");

        SelectClause select = getSelectClause(info, referenceIndices,
                this.entitySpec, wrapKeyId);
        FromClause from = getFromClause(toColumnSpecs(info.getColumnSpecs()),
                referenceIndices, this.stagedTables);
        WhereClause where = getWhereClause(propIds, info, this.entitySpecs,
                this.filters, referenceIndices, this.keyIds, this.order,
                this.resultProcessor, select, this.stagedTables);

        StringBuilder result = new StringBuilder(select.generateClause())
                .append(" ").append(from.generateClause()).append(" ")
                .append(where.generateClause());

        return result.toString();
    }
    
    protected final List toColumnSpecs(List columnSpecWrappers) {
        List result = new ArrayList();
        for (IntColumnSpecWrapper icsw : columnSpecWrappers) {
            result.add(icsw.getColumnSpec());
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy