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 String insertValues;
045
046    public Insert(Table table) {
047        this.table = table;
048        dialect = table.getDialect();
049        columns = new LinkedList<Column>();
050    }
051
052    public void addColumn(Column column) {
053        columns.add(column);
054    }
055
056    public void setValues(String insertValues) {
057        this.insertValues = insertValues;
058    }
059
060    /**
061     * Gets the statement to insert a row, or copy it if {@link #setValues} has been called.
062     * <p>
063     * Example: {@code INSERT INTO foo (a, b, c) SELECT ?, b, c FROM foo WHERE
064     * id = ?}
065     *
066     * @return the SQL insert or copy statement
067     */
068    public String getStatement() {
069        StringBuilder buf = new StringBuilder(128);
070        buf.append("INSERT INTO ");
071        buf.append(table.getQuotedName());
072        buf.append(' ');
073
074        List<String> columnNames = new LinkedList<String>();
075        List<String> values = new LinkedList<String>();
076        for (Column column : columns) {
077            columnNames.add(column.getQuotedName());
078            values.add(column.getFreeVariableSetter());
079        }
080
081        if (columnNames.isEmpty()) {
082            buf.append(dialect.getNoColumnsInsertString());
083        } else {
084            buf.append('(');
085            buf.append(StringUtils.join(columnNames, ", "));
086            buf.append(") ");
087            if (insertValues == null) {
088                buf.append("VALUES (");
089                buf.append(StringUtils.join(values, ", "));
090                buf.append(')');
091            } else {
092                buf.append(insertValues);
093            }
094        }
095        return buf.toString();
096    }
097}