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

net.sf.jsqlparser.statement.merge.MergeInsert 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.

The 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.merge;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

public class MergeInsert implements Serializable, MergeOperation {

    private Expression andPredicate;
    private ExpressionList columns;
    private ExpressionList values;
    private Expression whereCondition;

    public Expression getAndPredicate() {
        return andPredicate;
    }

    public void setAndPredicate(Expression andPredicate) {
        this.andPredicate = andPredicate;
    }

    public ExpressionList getColumns() {
        return columns;
    }

    public void setColumns(ExpressionList columns) {
        this.columns = columns;
    }

    public ExpressionList getValues() {
        return values;
    }

    public void setValues(ExpressionList values) {
        this.values = values;
    }

    public Expression getWhereCondition() {
        return whereCondition;
    }

    public void setWhereCondition(Expression whereCondition) {
        this.whereCondition = whereCondition;
    }

    @Override
    public  T accept(MergeOperationVisitor mergeOperationVisitor, S context) {
        return mergeOperationVisitor.visit(this, context);
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append(" WHEN NOT MATCHED");
        if (andPredicate != null) {
            b.append(" AND ").append(andPredicate.toString());
        }
        b.append(" THEN INSERT ");
        if (columns != null) {
            b.append(columns.toString());
        }
        b.append(" VALUES ").append(values.toString());
        if (whereCondition != null) {
            b.append(" WHERE ").append(whereCondition.toString());
        }
        return b.toString();
    }

    public MergeInsert withAndPredicate(Expression andPredicate) {
        this.setAndPredicate(andPredicate);
        return this;
    }

    public MergeInsert withColumns(ExpressionList columns) {
        this.setColumns(columns);
        return this;
    }

    public MergeInsert withValues(ExpressionList values) {
        this.setValues(values);
        return this;
    }

    public MergeInsert addColumns(Column... columns) {
        return this.addColumns(Arrays.asList(columns));
    }

    public MergeInsert addColumns(Collection columns) {
        ExpressionList collection =
                Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
        collection.addAll(columns);
        return this.withColumns(collection);
    }

    public MergeInsert addValues(Expression... values) {
        return this.addValues(Arrays.asList(values));
    }

    public MergeInsert addValues(Collection values) {
        ExpressionList collection =
                Optional.ofNullable(getValues()).orElseGet(ExpressionList::new);
        collection.addAll(values);
        return this.withValues(collection);
    }

    public MergeInsert withWhereCondition(Expression whereCondition) {
        this.setWhereCondition(whereCondition);
        return this;
    }

    public  E getAndPredicate(Class type) {
        return type.cast(getAndPredicate());
    }

    public  E getWhereCondition(Class type) {
        return type.cast(getWhereCondition());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy