View Javadoc
1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl.query;
18  
19  import java.util.List;
20  
21  import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*;
22  import com.healthmarketscience.jackcess.query.AppendQuery;
23  
24  
25  /**
26   * Concrete Query subclass which represents an append query, e.g.:
27   * {@code INSERT INTO <table> VALUES (<values>)}
28   * 
29   * @author James Ahlborn
30   */
31  public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
32  {
33  
34    public AppendQueryImpl(String name, List<Row> rows, int objectId, 
35                           int objectFlag) {
36      super(name, rows, objectId, objectFlag, Type.APPEND);
37    }
38  
39    public String getTargetTable() {
40      return getTypeRow().name1;
41    }
42  
43    public List<String> getTargetColumns() {
44      return new RowFormatter(getTargetRows()) {
45          @Override protected void format(StringBuilder builder, Row row) {
46            toOptionalQuotedExpr(builder, row.name2, true);
47          }
48        }.format();
49    }
50  
51    public String getRemoteDbPath() {
52      return getTypeRow().name2;
53    }
54  
55    public String getRemoteDbType() {
56      return getTypeRow().expression;
57    }
58  
59    public List<String> getValues() {
60      return new RowFormatter(getValueRows()) {
61          @Override protected void format(StringBuilder builder, Row row) {
62            builder.append(row.expression);
63          }
64        }.format();
65    }
66  
67    protected List<Row> getValueRows() {
68      return filterRowsByFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
69    }
70  
71    @Override
72    protected List<Row> getColumnRows() {
73      return filterRowsByNotFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
74    }
75  
76    protected List<Row> getTargetRows() {
77      return new RowFilter() {
78          @Override protected boolean keep(Row row) {
79            return (row.name2 != null);
80          }      
81      }.filter(super.getColumnRows());
82    }
83  
84    @Override
85    protected void toSQLString(StringBuilder builder)
86    {
87      builder.append("INSERT INTO ");
88      toOptionalQuotedExpr(builder, getTargetTable(), true);
89      List<String> columns = getTargetColumns();
90      if(!columns.isEmpty()) {
91        builder.append(" (").append(columns).append(')');
92      }
93      toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
94      builder.append(NEWLINE);
95      List<String> values = getValues();
96      if(!values.isEmpty()) {
97        builder.append("VALUES (").append(values).append(')');
98      } else {
99        toSQLSelectString(builder, true);
100     }
101   }
102   
103 }