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