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    public List<String> getTargetTables() 
40    {
41      return super.getFromTables();
42    }
43  
44    public String getRemoteDbPath() 
45    {
46      return super.getFromRemoteDbPath();
47    }
48  
49    public String getRemoteDbType() 
50    {
51      return super.getFromRemoteDbType();
52    }
53  
54    public List<String> getNewValues()
55    {
56      return (new RowFormatter(getColumnRows()) {
57          @Override protected void format(StringBuilder builder, Row row) {
58            toOptionalQuotedExpr(builder, row.name2, true)
59              .append(" = ").append(row.expression);
60          }
61        }).format();
62    }
63  
64    @Override
65    public String getWhereExpression()
66    {
67      return super.getWhereExpression();
68    }
69  
70    @Override
71    protected void toSQLString(StringBuilder builder)
72    {
73      builder.append("UPDATE ").append(getTargetTables());
74      toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
75  
76      builder.append(NEWLINE).append("SET ").append(getNewValues());
77  
78      String whereExpr = getWhereExpression();
79      if(whereExpr != null) {
80        builder.append(NEWLINE).append("WHERE ").append(whereExpr);
81      }
82    }  
83  
84  }