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

net.sf.jsqlparser.statement.update.Update 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 - 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 net.sf.jsqlparser.statement.update;

import java.util.List;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;

/**
 * The update statement.
 */
public class Update implements Statement {

	private List tables;
	private Expression where;
	private List columns;
	private List expressions;
	private FromItem fromItem;
	private List joins;
	private Select select;
	private boolean useColumnsBrackets = true;
	private boolean useSelect = false;
	private List orderByElements;
	private Limit limit;
	private boolean returningAllColumns = false;
	private List returningExpressionList = null;

	@Override
	public void accept(StatementVisitor statementVisitor) {
		statementVisitor.visit(this);
	}

	public List
getTables() { return tables; } public Expression getWhere() { return where; } public void setTables(List
list) { tables = list; } public void setWhere(Expression expression) { where = expression; } /** * The {@link net.sf.jsqlparser.schema.Column}s in this update (as col1 and * col2 in UPDATE col1='a', col2='b') * * @return a list of {@link net.sf.jsqlparser.schema.Column}s */ public List getColumns() { return columns; } /** * The {@link Expression}s in this update (as 'a' and 'b' in UPDATE * col1='a', col2='b') * * @return a list of {@link Expression}s */ public List getExpressions() { return expressions; } public void setColumns(List list) { columns = list; } public void setExpressions(List list) { expressions = list; } public FromItem getFromItem() { return fromItem; } public void setFromItem(FromItem fromItem) { this.fromItem = fromItem; } public List getJoins() { return joins; } public void setJoins(List joins) { this.joins = joins; } public Select getSelect() { return select; } public void setSelect(Select select) { this.select = select; } public boolean isUseColumnsBrackets() { return useColumnsBrackets; } public void setUseColumnsBrackets(boolean useColumnsBrackets) { this.useColumnsBrackets = useColumnsBrackets; } public boolean isUseSelect() { return useSelect; } public void setUseSelect(boolean useSelect) { this.useSelect = useSelect; } public void setOrderByElements(List orderByElements) { this.orderByElements = orderByElements; } public void setLimit(Limit limit) { this.limit = limit; } public List getOrderByElements() { return orderByElements; } public Limit getLimit() { return limit; } 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; } @Override public String toString() { StringBuilder b = new StringBuilder("UPDATE "); b.append(PlainSelect.getStringList(getTables(), true, false)).append(" SET "); if (!useSelect) { for (int i = 0; i < getColumns().size(); i++) { if (i != 0) { b.append(", "); } b.append(columns.get(i)).append(" = "); b.append(expressions.get(i)); } } else { if (useColumnsBrackets) { b.append("("); } for (int i = 0; i < getColumns().size(); i++) { if (i != 0) { b.append(", "); } b.append(columns.get(i)); } if (useColumnsBrackets) { b.append(")"); } b.append(" = "); b.append("(").append(select).append(")"); } if (fromItem != null) { b.append(" FROM ").append(fromItem); if (joins != null) { for (Join join : joins) { if (join.isSimple()) { b.append(", ").append(join); } else { b.append(" ").append(join); } } } } if (where != null) { b.append(" WHERE "); b.append(where); } if (orderByElements!=null) { b.append(PlainSelect.orderByToString(orderByElements)); } if (limit != null) { b.append(limit); } if (isReturningAllColumns()) { b.append(" RETURNING *"); } else if (getReturningExpressionList() != null) { b.append(" RETURNING ").append(PlainSelect.getStringList(getReturningExpressionList(), true, false)); } return b.toString(); } }