nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.insert.Insert Maven / Gradle / Ivy
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2013 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.insert;
import java.util.List;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.Expression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.ItemsList;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Column;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Table;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.Statement;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.StatementVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.PlainSelect;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.Select;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectExpressionItem;
/**
* The insert statement. Every column name in columnNames
matches an item in
* itemsList
*/
public class Insert implements Statement {
private Table table;
private List columns;
private ItemsList itemsList;
private boolean useValues = true;
private Select select;
private boolean useSelectBrackets = true;
private boolean useDuplicate = false;
private List duplicateUpdateColumns;
private List duplicateUpdateExpressionList;
private InsertModifierPriority modifierPriority = null;
private boolean modifierIgnore = false;
private boolean returningAllColumns = false;
private List returningExpressionList = null;
@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}
public Table getTable() {
return table;
}
public void setTable(Table name) {
table = name;
}
/**
* Get the columns (found in "INSERT INTO (col1,col2..) [...]" )
*
* @return a list of {@link nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Column}
*/
public List getColumns() {
return columns;
}
public void setColumns(List list) {
columns = list;
}
/**
* Get the values (as VALUES (...) or SELECT)
*
* @return the values of the insert
*/
public ItemsList getItemsList() {
return itemsList;
}
public void setItemsList(ItemsList list) {
itemsList = list;
}
public boolean isUseValues() {
return useValues;
}
public void setUseValues(boolean useValues) {
this.useValues = useValues;
}
public boolean isReturningAllColumns() {
return returningAllColumns;
}
public void setReturningAllColumns(boolean returningAllColumns) {
this.returningAllColumns = returningAllColumns;
}
public List getReturningExpressionList() {
return returningExpressionList;
}
public void setReturningExpressionList(List returningExpressionList) {
this.returningExpressionList = returningExpressionList;
}
public Select getSelect() {
return select;
}
public void setSelect(Select select) {
this.select = select;
}
public boolean isUseSelectBrackets() {
return useSelectBrackets;
}
public void setUseSelectBrackets(boolean useSelectBrackets) {
this.useSelectBrackets = useSelectBrackets;
}
public boolean isUseDuplicate() {
return useDuplicate;
}
public void setUseDuplicate(boolean useDuplicate) {
this.useDuplicate = useDuplicate;
}
public List getDuplicateUpdateColumns() {
return duplicateUpdateColumns;
}
public void setDuplicateUpdateColumns(List duplicateUpdateColumns) {
this.duplicateUpdateColumns = duplicateUpdateColumns;
}
public List getDuplicateUpdateExpressionList() {
return duplicateUpdateExpressionList;
}
public void setDuplicateUpdateExpressionList(List duplicateUpdateExpressionList) {
this.duplicateUpdateExpressionList = duplicateUpdateExpressionList;
}
public InsertModifierPriority getModifierPriority() {
return modifierPriority;
}
public void setModifierPriority(InsertModifierPriority modifierPriority) {
this.modifierPriority = modifierPriority;
}
public boolean isModifierIgnore() {
return modifierIgnore;
}
public void setModifierIgnore(boolean modifierIgnore) {
this.modifierIgnore = modifierIgnore;
}
@Override
public String toString() {
StringBuilder sql = new StringBuilder();
sql.append("INSERT ");
if (modifierPriority != null) {
sql.append(modifierPriority.name()).append(" ");
}
if (modifierIgnore) {
sql.append("IGNORE ");
}
sql.append("INTO ");
sql.append(table).append(" ");
if (columns != null) {
sql.append(PlainSelect.getStringList(columns, true, true)).append(" ");
}
if (useValues) {
sql.append("VALUES ");
}
if (itemsList != null) {
sql.append(itemsList);
} else {
if (useSelectBrackets) {
sql.append("(");
}
if (select != null) {
sql.append(select);
}
if (useSelectBrackets) {
sql.append(")");
}
}
if (useDuplicate) {
sql.append(" ON DUPLICATE KEY UPDATE ");
for (int i = 0; i < getDuplicateUpdateColumns().size(); i++) {
if (i != 0) {
sql.append(", ");
}
sql.append(duplicateUpdateColumns.get(i)).append(" = ");
sql.append(duplicateUpdateExpressionList.get(i));
}
}
if (isReturningAllColumns()) {
sql.append(" RETURNING *");
} else if (getReturningExpressionList() != null) {
sql.append(" RETURNING ").append(PlainSelect.
getStringList(getReturningExpressionList(), true, false));
}
return sql.toString();
}
}