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.

BaseSelectQueryImpl.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.BaseSelectQuery;
  17. /**
  18. * Base class for queries which represent some form of SELECT statement.
  19. *
  20. * @author James Ahlborn
  21. */
  22. public abstract class BaseSelectQueryImpl extends QueryImpl
  23. implements BaseSelectQuery
  24. {
  25. protected BaseSelectQueryImpl(String name, List<Row> rows, int objectId,
  26. int objectFlag, Type type) {
  27. super(name, rows, objectId, objectFlag, type);
  28. }
  29. protected void toSQLSelectString(StringBuilder builder,
  30. boolean useSelectPrefix)
  31. {
  32. if(useSelectPrefix) {
  33. builder.append("SELECT ");
  34. String selectType = getSelectType();
  35. if(!DEFAULT_TYPE.equals(selectType)) {
  36. builder.append(selectType).append(' ');
  37. }
  38. }
  39. builder.append(getSelectColumns());
  40. toSelectInto(builder);
  41. List<String> fromTables = getFromTables();
  42. if(!fromTables.isEmpty()) {
  43. builder.append(NEWLINE).append("FROM ").append(fromTables);
  44. toRemoteDb(builder, getFromRemoteDbPath(), getFromRemoteDbType());
  45. }
  46. String whereExpr = getWhereExpression();
  47. if(whereExpr != null) {
  48. builder.append(NEWLINE).append("WHERE ").append(whereExpr);
  49. }
  50. List<String> groupings = getGroupings();
  51. if(!groupings.isEmpty()) {
  52. builder.append(NEWLINE).append("GROUP BY ").append(groupings);
  53. }
  54. String havingExpr = getHavingExpression();
  55. if(havingExpr != null) {
  56. builder.append(NEWLINE).append("HAVING ").append(havingExpr);
  57. }
  58. List<String> orderings = getOrderings();
  59. if(!orderings.isEmpty()) {
  60. builder.append(NEWLINE).append("ORDER BY ").append(orderings);
  61. }
  62. }
  63. public String getSelectType()
  64. {
  65. if(hasFlag(DISTINCT_SELECT_TYPE)) {
  66. return "DISTINCT";
  67. }
  68. if(hasFlag(DISTINCT_ROW_SELECT_TYPE)) {
  69. return "DISTINCTROW";
  70. }
  71. if(hasFlag(TOP_SELECT_TYPE)) {
  72. StringBuilder builder = new StringBuilder();
  73. builder.append("TOP ").append(getFlagRow().name1);
  74. if(hasFlag(PERCENT_SELECT_TYPE)) {
  75. builder.append(" PERCENT");
  76. }
  77. return builder.toString();
  78. }
  79. return DEFAULT_TYPE;
  80. }
  81. public List<String> getSelectColumns()
  82. {
  83. List<String> result = (new RowFormatter(getColumnRows()) {
  84. @Override protected void format(StringBuilder builder, Row row) {
  85. // note column expression are always quoted appropriately
  86. builder.append(row.expression);
  87. toAlias(builder, row.name1);
  88. }
  89. }).format();
  90. if(hasFlag(SELECT_STAR_SELECT_TYPE)) {
  91. result.add("*");
  92. }
  93. return result;
  94. }
  95. protected void toSelectInto(StringBuilder builder)
  96. {
  97. // base does nothing
  98. }
  99. @Override
  100. public List<String> getFromTables()
  101. {
  102. return super.getFromTables();
  103. }
  104. @Override
  105. public String getFromRemoteDbPath()
  106. {
  107. return super.getFromRemoteDbPath();
  108. }
  109. @Override
  110. public String getFromRemoteDbType()
  111. {
  112. return super.getFromRemoteDbType();
  113. }
  114. @Override
  115. public String getWhereExpression()
  116. {
  117. return super.getWhereExpression();
  118. }
  119. public List<String> getGroupings()
  120. {
  121. return (new RowFormatter(getGroupByRows()) {
  122. @Override protected void format(StringBuilder builder, Row row) {
  123. builder.append(row.expression);
  124. }
  125. }).format();
  126. }
  127. public String getHavingExpression()
  128. {
  129. return getHavingRow().expression;
  130. }
  131. @Override
  132. public List<String> getOrderings()
  133. {
  134. return super.getOrderings();
  135. }
  136. }