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

jaskell.sql.Select Maven / Gradle / Ivy

Go to download

This is a utils library for java 8 project. It include parsec combinators and sql generators library.

There is a newer version: 2.9.2
Show newest version
package jaskell.sql;

import jaskell.script.Directive;

import javax.naming.SizeLimitExceededException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Select extends Query implements CouldFrom, CouldAlias {
    private final List _fields = new ArrayList<>();
    private Boolean distinct = false;

    Select(){
    }

    public Select distinct() {
        this.distinct = true;
        return this;
    }

    Select(String names){
        _fields.addAll(Arrays.stream(names.split(","))
                .map(String::trim)
                .map(Name::new)
                .collect(Collectors.toList()));
    }

    Select(String... names){
        _fields.addAll(Arrays.stream(names).map(Name::new).collect(Collectors.toList()));
    }

    Select(List names){
        _fields.addAll(names);
    }


    Select(Directive... directives){
        _fields.addAll(Arrays.asList(directives));
    }

    public Select select(String names){
        _fields.addAll(Arrays.stream(names.split(","))
                .map(String::trim)
                .map(Name::new)
                .collect(Collectors.toList()));
        return this;
    }

    public Select select(List names){
        _fields.addAll(names);
        return this;
    }

    public Select select(String ... names){
        _fields.addAll(Arrays.stream(names).map(Name::new).collect(Collectors.toList()));
        return this;
    }

    public Select select(Directive ... directives){
        _fields.addAll(Arrays.asList(directives));
        return this;
    }

    public Select all(){
        _fields.add(new Literal("*"));
        return this;
    }

    public From from(String name){
        From re = new From();
        re._from = new Name(name);
        re._select = this;
        return re;
    }

    public From from(Directive f) {
        From re = new From();
        re._select = this;
        re._from = f;
        return re;
    }

    public Where where(Predicate predicate){
        Where re = new Where(predicate);
        re._prefix = this;
        re._predicate = predicate;
        return re;
    }

    @Override
    public String script() {
        if (distinct) {
            return String.format("SELECT DISTINCT %s",
                _fields.stream().map(Directive::script).collect(Collectors.joining(", ")));
        } else {
            return String.format("SELECT %s",
                _fields.stream().map(Directive::script).collect(Collectors.joining(", ")));
        }
    }

    @Override
    public List> parameters() {
        List> re = new ArrayList<>();
        for (Directive field : _fields) {
            re.addAll(field.parameters());
        }
        setOrder(re);
        return re;
    }

    public static class From extends Query implements jaskell.sql.From, CouldWhere, CouldGroup, CouldOrder, CouldAlias {
        Select _select;
        Directive _from;

        @Override
        public String script() {
            return String.format("%s FROM %s", _select.script(), _from.script());
        }

        @Override
        public List> parameters() {
            ArrayList> re = new ArrayList<>();
            re.addAll(_select.parameters());
            re.addAll(_from.parameters());
            for (int i = 0; i < re.size(); i++) {
                re.get(i).order(i);
            }
            return re;
        }

        public Join join(Directive other){
            Join re = new Join();
            re._prefix = this;
            re._join = other;
            return re;
        }

        public Left left(){
            Left re = new Left();
            re._prefix = this;
            return re;
        }

        public Right right(){
            Right re = new Right();
            re._prefix = this;
            return re;
        }

        public Full full(){
            Full re = new Full();
            re._prefix = this;
            return re;
        }

        public Inner inner(){
            Inner re = new Inner();
            re._prefix = this;
            return re;
        }

        public Cross cross(){
            Cross re = new Cross();
            re._prefix = this;
            return re;
        }

        public Where where(Predicate predicate){
            Where re = new Where(predicate);
            re._prefix = this;
            re._predicate = predicate;
            return re;
        }

        public Group group() {
            Group re = new Group();
            re._prefix = this;
            return re;
        }

        public Order order() {
            Order re = new Order();
            re._prefix = this;
            return re;
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy