View Javadoc

1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17  USA
18  
19  You can contact Health Market Science at info@healthmarketscience.com
20  or at the following address:
21  
22  Health Market Science
23  2700 Horizon Drive
24  Suite 200
25  King of Prussia, PA 19406
26  */
27  
28  package com.healthmarketscience.sqlbuilder;
29  
30  import java.io.IOException;
31  import com.healthmarketscience.common.util.AppendableExt;
32  
33  
34  /**
35   * Base of a query which generates an INSERT statement.  Keeps track of the
36   * table and column names.
37   *
38   * @author James Ahlborn
39   */
40  abstract class BaseInsertQuery<ThisType extends BaseInsertQuery<ThisType>>
41    extends Query<ThisType>
42  {
43    private SqlObject _table;
44    protected SqlObjectList<SqlObject> _columns = SqlObjectList.create();
45      
46    /** @param tableStr name of the table into which to insert the values. */
47    public BaseInsertQuery(SqlObject tableStr) {
48      _table = tableStr;
49    }
50  
51    @Override
52    protected void collectSchemaObjects(ValidationContext vContext) {
53      super.collectSchemaObjects(vContext);
54      _table.collectSchemaObjects(vContext);
55      _columns.collectSchemaObjects(vContext);
56    }
57  
58    /**
59     * Appends the prefix "INSERT INTO (&lt;columns&gt;)" to the given
60     * AppendableExt.
61     */
62    protected void appendPrefixTo(AppendableExt app) throws IOException {
63      app.append("INSERT INTO ").append(_table)
64        .append(" (").append(_columns).append(") ");
65    }
66  }