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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 com.iciql.util.Utils;
  19. import java.util.ArrayList;
  20. /**
  21. * This class represents a table in a query.
  22. *
  23. * @param <T> the table class
  24. */
  25. class SelectTable<T> {
  26. private Query<T> query;
  27. private Class<T> clazz;
  28. private T current;
  29. private String as;
  30. private TableDefinition<T> aliasDef;
  31. private boolean outerJoin;
  32. private ArrayList<Token> joinConditions = Utils.newArrayList();
  33. private T alias;
  34. @SuppressWarnings("unchecked")
  35. SelectTable(Db db, Query<T> query, T alias, boolean outerJoin) {
  36. this.alias = alias;
  37. this.query = query;
  38. this.outerJoin = outerJoin;
  39. aliasDef = (TableDefinition<T>) db.getTableDefinition(alias.getClass());
  40. clazz = Utils.getClass(alias);
  41. as = "T" + Utils.nextAsCount();
  42. }
  43. T getAlias() {
  44. return alias;
  45. }
  46. T newObject() {
  47. return Utils.newObject(clazz);
  48. }
  49. TableDefinition<T> getAliasDefinition() {
  50. return aliasDef;
  51. }
  52. void appendSQL(SQLStatement stat) {
  53. if (query.isJoin()) {
  54. stat.appendTable(aliasDef.schemaName, aliasDef.tableName).appendSQL(" AS " + as);
  55. } else {
  56. stat.appendTable(aliasDef.schemaName, aliasDef.tableName);
  57. }
  58. }
  59. void appendSQLAsJoin(SQLStatement stat, Query<T> q) {
  60. if (outerJoin) {
  61. stat.appendSQL(" LEFT OUTER JOIN ");
  62. } else {
  63. stat.appendSQL(" INNER JOIN ");
  64. }
  65. appendSQL(stat);
  66. if (!joinConditions.isEmpty()) {
  67. stat.appendSQL(" ON ");
  68. for (Token token : joinConditions) {
  69. token.appendSQL(stat, q);
  70. stat.appendSQL(" ");
  71. }
  72. }
  73. }
  74. boolean getOuterJoin() {
  75. return outerJoin;
  76. }
  77. Query<T> getQuery() {
  78. return query;
  79. }
  80. String getAs() {
  81. return as;
  82. }
  83. void addConditionToken(Token condition) {
  84. joinConditions.add(condition);
  85. }
  86. T getCurrent() {
  87. return current;
  88. }
  89. void setCurrent(T current) {
  90. this.current = current;
  91. }
  92. }