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

net.sf.jsqlparser.statement.select.Pivot Maven / Gradle / Ivy

Go to download

JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern.

There is a newer version: 5.1
Show newest version
/*-
 * #%L
 * JSQLParser library
 * %%
 * Copyright (C) 2004 - 2019 JSQLParser
 * %%
 * Dual licensed under GNU LGPL 2.1 or Apache License 2.0
 * #L%
 */
package net.sf.jsqlparser.statement.select;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.schema.Column;

public class Pivot {

    private List functionItems;
    private List forColumns;
    private List singleInItems;
    private List multiInItems;
    private Alias alias;

    public void accept(PivotVisitor pivotVisitor) {
        pivotVisitor.visit(this);
    }

    public List getSingleInItems() {
        return singleInItems;
    }

    public void setSingleInItems(List singleInItems) {
        this.singleInItems = singleInItems;
    }

    public List getMultiInItems() {
        return multiInItems;
    }

    public void setMultiInItems(List multiInItems) {
        this.multiInItems = multiInItems;
    }

    public List getFunctionItems() {
        return functionItems;
    }

    public void setFunctionItems(List functionItems) {
        this.functionItems = functionItems;
    }

    public List getForColumns() {
        return forColumns;
    }

    public void setForColumns(List forColumns) {
        this.forColumns = forColumns;
    }

    public List getInItems() {
        return singleInItems == null ? multiInItems : singleInItems;
    }

    public Alias getAlias() {
        return alias;
    }

    public void setAlias(Alias alias) {
        this.alias = alias;
    }

    @Override
    public String toString() {
        return "PIVOT ("
                + PlainSelect.getStringList(functionItems)
                + " FOR " + PlainSelect.
                        getStringList(forColumns, true, forColumns != null && forColumns.size() > 1)
                + " IN " + PlainSelect.getStringList(getInItems(), true, true) + ")"
                + (alias!=null?alias.toString():"");
    }

    public Pivot withFunctionItems(List functionItems) {
        this.setFunctionItems(functionItems);
        return this;
    }

    public Pivot withForColumns(List forColumns) {
        this.setForColumns(forColumns);
        return this;
    }

    public Pivot withSingleInItems(List singleInItems) {
        this.setSingleInItems(singleInItems);
        return this;
    }

    public Pivot withMultiInItems(List multiInItems) {
        this.setMultiInItems(multiInItems);
        return this;
    }

    public Pivot withAlias(Alias alias) {
        this.setAlias(alias);
        return this;
    }

    public Pivot addFunctionItems(FunctionItem... functionItems) {
        List collection = Optional.ofNullable(getFunctionItems()).orElseGet(ArrayList::new);
        Collections.addAll(collection, functionItems);
        return this.withFunctionItems(collection);
    }

    public Pivot addFunctionItems(Collection functionItems) {
        List collection = Optional.ofNullable(getFunctionItems()).orElseGet(ArrayList::new);
        collection.addAll(functionItems);
        return this.withFunctionItems(collection);
    }

    public Pivot addForColumns(Column... forColumns) {
        List collection = Optional.ofNullable(getForColumns()).orElseGet(ArrayList::new);
        Collections.addAll(collection, forColumns);
        return this.withForColumns(collection);
    }

    public Pivot addForColumns(Collection forColumns) {
        List collection = Optional.ofNullable(getForColumns()).orElseGet(ArrayList::new);
        collection.addAll(forColumns);
        return this.withForColumns(collection);
    }

    public Pivot addSingleInItems(SelectExpressionItem... singleInItems) {
        List collection = Optional.ofNullable(getSingleInItems()).orElseGet(ArrayList::new);
        Collections.addAll(collection, singleInItems);
        return this.withSingleInItems(collection);
    }

    public Pivot addSingleInItems(Collection singleInItems) {
        List collection = Optional.ofNullable(getSingleInItems()).orElseGet(ArrayList::new);
        collection.addAll(singleInItems);
        return this.withSingleInItems(collection);
    }

    public Pivot addMultiInItems(ExpressionListItem... multiInItems) {
        List collection = Optional.ofNullable(getMultiInItems()).orElseGet(ArrayList::new);
        Collections.addAll(collection, multiInItems);
        return this.withMultiInItems(collection);
    }

    public Pivot addMultiInItems(Collection multiInItems) {
        List collection = Optional.ofNullable(getMultiInItems()).orElseGet(ArrayList::new);
        collection.addAll(multiInItems);
        return this.withMultiInItems(collection);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy