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

net.sf.jsqlparser.util.deparser.InsertDeParser Maven / Gradle / Ivy

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.util.deparser;

import java.util.Iterator;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ItemsListVisitor;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.statement.select.WithItem;

/**
 * A class to de-parse (that is, tranform from JSqlParser hierarchy into a string) an
 * {@link net.sf.jsqlparser.statement.insert.Insert}
 */
public class InsertDeParser implements ItemsListVisitor {

    protected StringBuilder buffer;
    private ExpressionVisitor expressionVisitor;
    private SelectVisitor selectVisitor;

    public InsertDeParser() {
    }

    /**
     * @param expressionVisitor a {@link ExpressionVisitor} to de-parse
     * {@link net.sf.jsqlparser.expression.Expression}s. It has to share the same
* StringBuilder (buffer parameter) as this object in order to work * @param selectVisitor a {@link SelectVisitor} to de-parse {@link net.sf.jsqlparser.statement.select.Select}s. It * has to share the same
* StringBuilder (buffer parameter) as this object in order to work * @param buffer the buffer that will be filled with the insert */ public InsertDeParser(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuilder buffer) { this.buffer = buffer; this.expressionVisitor = expressionVisitor; this.selectVisitor = selectVisitor; } public StringBuilder getBuffer() { return buffer; } public void setBuffer(StringBuilder buffer) { this.buffer = buffer; } public void deParse(Insert insert) { buffer.append("INSERT "); if (insert.getModifierPriority() != null) { buffer.append(insert.getModifierPriority()).append(" "); } if (insert.isModifierIgnore()) { buffer.append("IGNORE "); } buffer.append("INTO "); buffer.append(insert.getTable().toString()); if (insert.getColumns() != null) { buffer.append(" ("); for (Iterator iter = insert.getColumns().iterator(); iter.hasNext();) { Column column = iter.next(); buffer.append(column.getColumnName()); if (iter.hasNext()) { buffer.append(", "); } } buffer.append(")"); } if (insert.getItemsList() != null) { insert.getItemsList().accept(this); } if (insert.getSelect() != null) { buffer.append(" "); if (insert.isUseSelectBrackets()) { buffer.append("("); } if (insert.getSelect().getWithItemsList() != null) { buffer.append("WITH "); for (WithItem with : insert.getSelect().getWithItemsList()) { with.accept(selectVisitor); } buffer.append(" "); } insert.getSelect().getSelectBody().accept(selectVisitor); if (insert.isUseSelectBrackets()) { buffer.append(")"); } } if (insert.isUseSet()) { buffer.append(" SET "); for (int i = 0; i < insert.getSetColumns().size(); i++) { Column column = insert.getSetColumns().get(i); column.accept(expressionVisitor); buffer.append(" = "); Expression expression = insert.getSetExpressionList().get(i); expression.accept(expressionVisitor); if (i < insert.getSetColumns().size() - 1) { buffer.append(", "); } } } if (insert.isUseDuplicate()) { buffer.append(" ON DUPLICATE KEY UPDATE "); for (int i = 0; i < insert.getDuplicateUpdateColumns().size(); i++) { Column column = insert.getDuplicateUpdateColumns().get(i); buffer.append(column.getFullyQualifiedName()).append(" = "); Expression expression = insert.getDuplicateUpdateExpressionList().get(i); expression.accept(expressionVisitor); if (i < insert.getDuplicateUpdateColumns().size() - 1) { buffer.append(", "); } } } if (insert.isReturningAllColumns()) { buffer.append(" RETURNING *"); } else if (insert.getReturningExpressionList() != null) { buffer.append(" RETURNING "); for (Iterator iter = insert.getReturningExpressionList(). iterator(); iter.hasNext();) { buffer.append(iter.next().toString()); if (iter.hasNext()) { buffer.append(", "); } } } } @Override public void visit(ExpressionList expressionList) { buffer.append(" VALUES ("); for (Iterator iter = expressionList.getExpressions().iterator(); iter.hasNext();) { Expression expression = iter.next(); expression.accept(expressionVisitor); if (iter.hasNext()) { buffer.append(", "); } } buffer.append(")"); } // not used in a top-level insert statement @Override public void visit(NamedExpressionList NamedExpressionList) { } @Override public void visit(MultiExpressionList multiExprList) { buffer.append(" VALUES "); for (Iterator it = multiExprList.getExprList().iterator(); it.hasNext();) { buffer.append("("); for (Iterator iter = it.next().getExpressions().iterator(); iter.hasNext();) { Expression expression = iter.next(); expression.accept(expressionVisitor); if (iter.hasNext()) { buffer.append(", "); } } buffer.append(")"); if (it.hasNext()) { buffer.append(", "); } } } @Override public void visit(SubSelect subSelect) { subSelect.getSelectBody().accept(selectVisitor); } public ExpressionVisitor getExpressionVisitor() { return expressionVisitor; } public SelectVisitor getSelectVisitor() { return selectVisitor; } public void setExpressionVisitor(ExpressionVisitor visitor) { expressionVisitor = visitor; } public void setSelectVisitor(SelectVisitor visitor) { selectVisitor = visitor; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy