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.

CrossTabQueryImpl.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.CrossTabQuery;
  17. /**
  18. * Concrete Query subclass which represents a crosstab/pivot query, e.g.:
  19. * {@code TRANSFORM <expr> SELECT <query> PIVOT <expr>}
  20. *
  21. * @author James Ahlborn
  22. */
  23. public class CrossTabQueryImpl extends BaseSelectQueryImpl
  24. implements CrossTabQuery
  25. {
  26. public CrossTabQueryImpl(String name, List<Row> rows, int objectId,
  27. int objectFlag) {
  28. super(name, rows, objectId, objectFlag, Type.CROSS_TAB);
  29. }
  30. protected Row getTransformRow() {
  31. return getUniqueRow(
  32. filterRowsByNotFlag(super.getColumnRows(),
  33. (short)(CROSSTAB_PIVOT_FLAG |
  34. CROSSTAB_NORMAL_FLAG)));
  35. }
  36. @Override
  37. protected List<Row> getColumnRows() {
  38. return filterRowsByFlag(super.getColumnRows(), CROSSTAB_NORMAL_FLAG);
  39. }
  40. @Override
  41. protected List<Row> getGroupByRows() {
  42. return filterRowsByFlag(super.getGroupByRows(), CROSSTAB_NORMAL_FLAG);
  43. }
  44. protected Row getPivotRow() {
  45. return getUniqueRow(filterRowsByFlag(super.getColumnRows(),
  46. CROSSTAB_PIVOT_FLAG));
  47. }
  48. public String getTransformExpression() {
  49. Row row = getTransformRow();
  50. if(row.expression == null) {
  51. return null;
  52. }
  53. // note column expression are always quoted appropriately
  54. StringBuilder builder = new StringBuilder(row.expression);
  55. return toAlias(builder, row.name1).toString();
  56. }
  57. public String getPivotExpression() {
  58. return getPivotRow().expression;
  59. }
  60. @Override
  61. protected void toSQLString(StringBuilder builder)
  62. {
  63. String transformExpr = getTransformExpression();
  64. if(transformExpr != null) {
  65. builder.append("TRANSFORM ").append(transformExpr).append(NEWLINE);
  66. }
  67. toSQLSelectString(builder, true);
  68. builder.append(NEWLINE).append("PIVOT ")
  69. .append(getPivotExpression());
  70. }
  71. }