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.UpdateQuery;
23  
24  
25  /**
26   * Concrete Query subclass which represents a row update query, e.g.:
27   * {@code UPDATE <table> SET <newValues>}
28   * 
29   * @author James Ahlborn
30   */
31  public class UpdateQueryImpl extends QueryImpl implements UpdateQuery
32  {
33  
34    public UpdateQueryImpl(String name, List<Row> rows, int objectId, 
35                           int objectFlag) {
36      super(name, rows, objectId, objectFlag, Type.UPDATE);
37    }
38  
39    @Override
40    public List<String> getTargetTables() 
41    {
42      return super.getFromTables();
43    }
44  
45    @Override
46    public String getRemoteDbPath() 
47    {
48      return super.getFromRemoteDbPath();
49    }
50  
51    @Override
52    public String getRemoteDbType() 
53    {
54      return super.getFromRemoteDbType();
55    }
56  
57    @Override
58    public List<String> getNewValues()
59    {
60      return (new RowFormatter(getColumnRows()) {
61          @Override protected void format(StringBuilder builder, Row row) {
62            toOptionalQuotedExpr(builder, row.name2, true)
63              .append(" = ").append(row.expression);
64          }
65        }).format();
66    }
67  
68    @Override
69    public String getWhereExpression()
70    {
71      return super.getWhereExpression();
72    }
73  
74    @Override
75    protected void toSQLString(StringBuilder builder)
76    {
77      builder.append("UPDATE ").append(getTargetTables());
78      toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
79  
80      builder.append(NEWLINE).append("SET ").append(getNewValues());
81  
82      String whereExpr = getWhereExpression();
83      if(whereExpr != null) {
84        builder.append(NEWLINE).append("WHERE ").append(whereExpr);
85      }
86    }  
87  
88  }