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.

SelectTable.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql;
  18. import java.util.ArrayList;
  19. import com.iciql.util.Utils;
  20. /**
  21. * This class represents a table in a query.
  22. *
  23. * @param <T>
  24. * the table class
  25. */
  26. class SelectTable<T> {
  27. private static int asCounter;
  28. private Query<T> query;
  29. private Class<T> clazz;
  30. private T current;
  31. private String as;
  32. private TableDefinition<T> aliasDef;
  33. private boolean outerJoin;
  34. private ArrayList<Token> joinConditions = Utils.newArrayList();
  35. private T alias;
  36. @SuppressWarnings("unchecked")
  37. SelectTable(Db db, Query<T> query, T alias, boolean outerJoin) {
  38. this.alias = alias;
  39. this.query = query;
  40. this.outerJoin = outerJoin;
  41. aliasDef = (TableDefinition<T>) db.getTableDefinition(alias.getClass());
  42. clazz = Utils.getClass(alias);
  43. as = "T" + asCounter++;
  44. }
  45. T getAlias() {
  46. return alias;
  47. }
  48. T newObject() {
  49. return Utils.newObject(clazz);
  50. }
  51. TableDefinition<T> getAliasDefinition() {
  52. return aliasDef;
  53. }
  54. void appendSQL(SQLStatement stat) {
  55. if (query.isJoin()) {
  56. stat.appendTable(aliasDef.schemaName, aliasDef.tableName).appendSQL(" AS " + as);
  57. } else {
  58. stat.appendTable(aliasDef.schemaName, aliasDef.tableName);
  59. }
  60. }
  61. void appendSQLAsJoin(SQLStatement stat, Query<T> q) {
  62. if (outerJoin) {
  63. stat.appendSQL(" LEFT OUTER JOIN ");
  64. } else {
  65. stat.appendSQL(" INNER JOIN ");
  66. }
  67. appendSQL(stat);
  68. if (!joinConditions.isEmpty()) {
  69. stat.appendSQL(" ON ");
  70. for (Token token : joinConditions) {
  71. token.appendSQL(stat, q);
  72. stat.appendSQL(" ");
  73. }
  74. }
  75. }
  76. boolean getOuterJoin() {
  77. return outerJoin;
  78. }
  79. Query<T> getQuery() {
  80. return query;
  81. }
  82. String getAs() {
  83. return as;
  84. }
  85. void addConditionToken(Token condition) {
  86. joinConditions.add(condition);
  87. }
  88. T getCurrent() {
  89. return current;
  90. }
  91. void setCurrent(T current) {
  92. this.current = current;
  93. }
  94. }