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.

AppendQueryImpl.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.AppendQuery;
  17. /**
  18. * Concrete Query subclass which represents an append query, e.g.:
  19. * {@code INSERT INTO <table> VALUES (<values>)}
  20. *
  21. * @author James Ahlborn
  22. */
  23. public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
  24. {
  25. public AppendQueryImpl(String name, List<Row> rows, int objectId,
  26. int objectFlag) {
  27. super(name, rows, objectId, objectFlag, Type.APPEND);
  28. }
  29. public String getTargetTable() {
  30. return getTypeRow().name1;
  31. }
  32. public String getRemoteDbPath() {
  33. return getTypeRow().name2;
  34. }
  35. public String getRemoteDbType() {
  36. return getTypeRow().expression;
  37. }
  38. protected List<Row> getValueRows() {
  39. return filterRowsByFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
  40. }
  41. @Override
  42. protected List<Row> getColumnRows() {
  43. return filterRowsByNotFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
  44. }
  45. public List<String> getValues() {
  46. return new RowFormatter(getValueRows()) {
  47. @Override protected void format(StringBuilder builder, Row row) {
  48. builder.append(row.expression);
  49. }
  50. }.format();
  51. }
  52. @Override
  53. protected void toSQLString(StringBuilder builder)
  54. {
  55. builder.append("INSERT INTO ");
  56. toOptionalQuotedExpr(builder, getTargetTable(), true);
  57. toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
  58. builder.append(NEWLINE);
  59. List<String> values = getValues();
  60. if(!values.isEmpty()) {
  61. builder.append("VALUES (").append(values).append(')');
  62. } else {
  63. toSQLSelectString(builder, true);
  64. }
  65. }
  66. }