001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.jdbc.db;
020
021import java.io.Serializable;
022import java.util.LinkedList;
023import java.util.List;
024
025import org.nuxeo.ecm.core.storage.sql.jdbc.dialect.Dialect;
026
027/**
028 * An {@code INSERT} statement.
029 *
030 * @author Florent Guillaume
031 */
032public class Insert implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    protected final Dialect dialect;
037
038    private final Table table;
039
040    private final List<Column> columns;
041
042    private Column idColumn;
043
044    private String insertValues;
045
046    public Insert(Table table) {
047        this.table = table;
048        dialect = table.getDialect();
049        columns = new LinkedList<>();
050    }
051
052    public void addColumn(Column column) {
053        columns.add(column);
054    }
055
056    public void addIdentityColumn(Column idColumn) {
057        this.idColumn = idColumn;
058    }
059
060    public void setValues(String insertValues) {
061        this.insertValues = insertValues;
062    }
063
064    /**
065     * Gets the statement to insert a row, or copy it if {@link #setValues} has been called.
066     * <p>
067     * Example: {@code INSERT INTO foo (a, b, c) SELECT ?, b, c FROM foo WHERE
068     * id = ?}
069     *
070     * @return the SQL insert or copy statement
071     */
072    public String getStatement() {
073        StringBuilder sb = new StringBuilder(128);
074        sb.append("INSERT INTO ");
075        sb.append(table.getQuotedName());
076        sb.append(' ');
077
078        List<String> columnNames = new LinkedList<>();
079        List<String> values = new LinkedList<>();
080        for (Column column : columns) {
081            columnNames.add(column.getQuotedName());
082            values.add(column.getFreeVariableSetter());
083        }
084
085        if (columnNames.isEmpty()) {
086            sb.append(dialect.getNoColumnsInsertString(idColumn));
087        } else {
088            sb.append('(');
089            sb.append(String.join(", ", columnNames));
090            sb.append(") ");
091            if (insertValues == null) {
092                sb.append("VALUES (");
093                sb.append(String.join(", ", values));
094                sb.append(')');
095            } else {
096                sb.append(insertValues);
097            }
098        }
099        return sb.toString();
100    }
101}