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

com.heliorm.impl.SelectPart Maven / Gradle / Ivy

The newest version!
package com.heliorm.impl;

import com.heliorm.OrmException;
import com.heliorm.Table;
import com.heliorm.def.Complete;
import com.heliorm.def.Executable;
import com.heliorm.def.FieldOrder;
import com.heliorm.def.Select;
import com.heliorm.def.Where;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static java.lang.String.format;

/**
 * @author gideon
 */
public final class SelectPart extends ExecutablePart implements Select {

    private final Selector selector;
    private final Table table;
    private final Where where;
    private final List> joins;
    private List> order;
    private LimitPart limit;

    public SelectPart(Selector orm, Table table) {
        this(orm, table, null,  Collections.emptyList());
    }

    public SelectPart(Selector orm, Table table, Where where, List> joins, List> order, LimitPart limit) {
        super(orm);
        this.table = table;
        this.where = where;
        this.selector = orm;
        this.joins = joins;
        this.order = order;
        this.limit = limit;
    }

    public SelectPart(Selector orm, Table table, Where where, List> joins) {
        this(orm, table, where, joins, Collections.emptyList(), new LimitPart(-1, -1));
    }


    public Selector getSelector() {
        return selector;
    }


    public Table getTable() {
        return table;
    }

    @Override
    public > Complete orderBy(F order, F... orders) {
        List> list = new ArrayList<>();
        list.add(makePart(order));
        for (F o : orders) {
            list.add(makePart(o));
        }
        this.order = list;
        return new OrderedPart(getSelector(), this, list, limit);
    }

    @Override
    public SelectPart getSelect() {
        return this;
    }

    @Override
    public List> getOrder() {
        return order == null ? Collections.EMPTY_LIST : order;
    }

    @Override
    public LimitPart getLimit() {
        return limit;
    }

    void setLimit(LimitPart limit) {
        this.limit = limit;
    }

    @Override
    public List list() throws OrmException {
        return getSelector().list(this);
    }

    @Override
    public Stream stream() throws OrmException {
        return getSelector().stream(this);
    }

    @Override
    public DO one() throws OrmException {
        return getSelector().one(this);
    }

    @Override
    public Optional optional() throws OrmException {
        return getSelector().optional(this);
    }

    public Optional> getWhere() {
        return Optional.ofNullable(where);
    }

    public List> getJoins() {
        return joins;
    }

    @Override
    public String toString() {
        return format("SELECT %s", table.getSqlTable());
    }

    private > OrderPart makePart(F order) {
        return new OrderPart(order.getDirection() == FieldOrder.Direction.ASC ? OrderPart.Direction.ASCENDING : OrderPart.Direction.DESCENDING,
                (FieldPart) (order.getField()));
    }

    @Override
    public Executable limit(int from, int number) {
        limit = new LimitPart(from, number);
        return this;
    }

    @Override
    public Executable limit(int number) {
        limit = new LimitPart(number);
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy