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    @Override
40    public String getTargetTable() {
41      return getTypeRow().name1;
42    }
43  
44    @Override
45    public List<String> getTargetColumns() {
46      return new RowFormatter(getTargetRows()) {
47          @Override protected void format(StringBuilder builder, Row row) {
48            toOptionalQuotedExpr(builder, row.name2, true);
49          }
50        }.format();
51    }
52  
53    @Override
54    public String getRemoteDbPath() {
55      return getTypeRow().name2;
56    }
57  
58    @Override
59    public String getRemoteDbType() {
60      return getTypeRow().expression;
61    }
62  
63    @Override
64    public List<String> getValues() {
65      return new RowFormatter(getValueRows()) {
66          @Override protected void format(StringBuilder builder, Row row) {
67            builder.append(row.expression);
68          }
69        }.format();
70    }
71  
72    protected List<Row> getValueRows() {
73      return filterRowsByFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
74    }
75  
76    @Override
77    protected List<Row> getColumnRows() {
78      return filterRowsByNotFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
79    }
80  
81    protected List<Row> getTargetRows() {
82      return new RowFilter() {
83          @Override protected boolean keep(Row row) {
84            return (row.name2 != null);
85          }      
86      }.filter(super.getColumnRows());
87    }
88  
89    @Override
90    protected void toSQLString(StringBuilder builder)
91    {
92      builder.append("INSERT INTO ");
93      toOptionalQuotedExpr(builder, getTargetTable(), true);
94      List<String> columns = getTargetColumns();
95      if(!columns.isEmpty()) {
96        builder.append(" (").append(columns).append(')');
97      }
98      toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
99      builder.append(NEWLINE);
100     List<String> values = getValues();
101     if(!values.isEmpty()) {
102       builder.append("VALUES (").append(values).append(')');
103     } else {
104       toSQLSelectString(builder, true);
105     }
106   }
107   
108 }