You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateQueryImpl.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.query;
  14. import java.util.List;
  15. import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*;
  16. import com.healthmarketscience.jackcess.query.UpdateQuery;
  17. /**
  18. * Concrete Query subclass which represents a row update query, e.g.:
  19. * {@code UPDATE <table> SET <newValues>}
  20. *
  21. * @author James Ahlborn
  22. */
  23. public class UpdateQueryImpl extends QueryImpl implements UpdateQuery
  24. {
  25. public UpdateQueryImpl(String name, List<Row> rows, int objectId,
  26. int objectFlag) {
  27. super(name, rows, objectId, objectFlag, Type.UPDATE);
  28. }
  29. public List<String> getTargetTables()
  30. {
  31. return super.getFromTables();
  32. }
  33. public String getRemoteDbPath()
  34. {
  35. return super.getFromRemoteDbPath();
  36. }
  37. public String getRemoteDbType()
  38. {
  39. return super.getFromRemoteDbType();
  40. }
  41. public List<String> getNewValues()
  42. {
  43. return (new RowFormatter(getColumnRows()) {
  44. @Override protected void format(StringBuilder builder, Row row) {
  45. toOptionalQuotedExpr(builder, row.name2, true)
  46. .append(" = ").append(row.expression);
  47. }
  48. }).format();
  49. }
  50. @Override
  51. public String getWhereExpression()
  52. {
  53. return super.getWhereExpression();
  54. }
  55. @Override
  56. protected void toSQLString(StringBuilder builder)
  57. {
  58. builder.append("UPDATE ").append(getTargetTables());
  59. toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
  60. builder.append(NEWLINE).append("SET ").append(getNewValues());
  61. String whereExpr = getWhereExpression();
  62. if(whereExpr != null) {
  63. builder.append(NEWLINE).append("WHERE ").append(whereExpr);
  64. }
  65. }
  66. }