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.common.utils.StringUtils;
026import org.nuxeo.ecm.core.storage.sql.jdbc.dialect.Dialect;
027
028/**
029 * An {@code INSERT} statement.
030 *
031 * @author Florent Guillaume
032 */
033public class Insert implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    protected final Dialect dialect;
038
039    private final Table table;
040
041    private final List<Column> columns;
042
043    private Column idColumn;
044
045    private String insertValues;
046
047    public Insert(Table table) {
048        this.table = table;
049        dialect = table.getDialect();
050        columns = new LinkedList<>();
051    }
052
053    public void addColumn(Column column) {
054        columns.add(column);
055    }
056
057    public void addIdentityColumn(Column idColumn) {
058        this.idColumn = idColumn;
059    }
060
061    public void setValues(String insertValues) {
062        this.insertValues = insertValues;
063    }
064
065    /**
066     * Gets the statement to insert a row, or copy it if {@link #setValues} has been called.
067     * <p>
068     * Example: {@code INSERT INTO foo (a, b, c) SELECT ?, b, c FROM foo WHERE
069     * id = ?}
070     *
071     * @return the SQL insert or copy statement
072     */
073    public String getStatement() {
074        StringBuilder buf = new StringBuilder(128);
075        buf.append("INSERT INTO ");
076        buf.append(table.getQuotedName());
077        buf.append(' ');
078
079        List<String> columnNames = new LinkedList<>();
080        List<String> values = new LinkedList<>();
081        for (Column column : columns) {
082            columnNames.add(column.getQuotedName());
083            values.add(column.getFreeVariableSetter());
084        }
085
086        if (columnNames.isEmpty()) {
087            buf.append(dialect.getNoColumnsInsertString(idColumn));
088        } else {
089            buf.append('(');
090            buf.append(String.join(", ", columnNames));
091            buf.append(") ");
092            if (insertValues == null) {
093                buf.append("VALUES (");
094                buf.append(String.join(", ", values));
095                buf.append(')');
096            } else {
097                buf.append(insertValues);
098            }
099        }
100        return buf.toString();
101    }
102}