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.

RelationshipImpl.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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;
  14. import java.util.Collections;
  15. import java.util.List;
  16. import java.util.ArrayList;
  17. import com.healthmarketscience.jackcess.Column;
  18. import com.healthmarketscience.jackcess.Relationship;
  19. import com.healthmarketscience.jackcess.Table;
  20. import com.healthmarketscience.jackcess.util.ToStringBuilder;
  21. /**
  22. * Information about a relationship between two tables in the database.
  23. *
  24. * @author James Ahlborn
  25. */
  26. public class RelationshipImpl implements Relationship
  27. {
  28. /** flag indicating one-to-one relationship */
  29. public static final int ONE_TO_ONE_FLAG = 0x00000001;
  30. /** flag indicating no referential integrity */
  31. public static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
  32. /** flag indicating cascading updates (requires referential integrity) */
  33. public static final int CASCADE_UPDATES_FLAG = 0x00000100;
  34. /** flag indicating cascading deletes (requires referential integrity) */
  35. public static final int CASCADE_DELETES_FLAG = 0x00001000;
  36. /** flag indicating cascading null on delete (requires referential
  37. integrity) */
  38. public static final int CASCADE_NULL_FLAG = 0x00002000;
  39. /** flag indicating left outer join */
  40. public static final int LEFT_OUTER_JOIN_FLAG = 0x01000000;
  41. /** flag indicating right outer join */
  42. public static final int RIGHT_OUTER_JOIN_FLAG = 0x02000000;
  43. /** the name of this relationship */
  44. private final String _name;
  45. /** the "from" table in this relationship */
  46. private final Table _fromTable;
  47. /** the "to" table in this relationship */
  48. private final Table _toTable;
  49. /** the columns in the "from" table in this relationship (aligned w/
  50. toColumns list) */
  51. private final List<Column> _toColumns;
  52. /** the columns in the "to" table in this relationship (aligned w/
  53. toColumns list) */
  54. private final List<Column> _fromColumns;
  55. /** the various flags describing this relationship */
  56. private final int _flags;
  57. public RelationshipImpl(String name, Table fromTable, Table toTable, int flags,
  58. int numCols)
  59. {
  60. this(name, fromTable, toTable, flags,
  61. Collections.nCopies(numCols, (Column)null),
  62. Collections.nCopies(numCols, (Column)null));
  63. }
  64. public RelationshipImpl(String name, Table fromTable, Table toTable, int flags,
  65. List<? extends Column> fromCols,
  66. List<? extends Column> toCols)
  67. {
  68. _name = name;
  69. _fromTable = fromTable;
  70. _fromColumns = new ArrayList<Column>(fromCols);
  71. _toTable = toTable;
  72. _toColumns = new ArrayList<Column>(toCols);
  73. _flags = flags;
  74. }
  75. @Override
  76. public String getName() {
  77. return _name;
  78. }
  79. @Override
  80. public Table getFromTable() {
  81. return _fromTable;
  82. }
  83. @Override
  84. public List<Column> getFromColumns() {
  85. return _fromColumns;
  86. }
  87. @Override
  88. public Table getToTable() {
  89. return _toTable;
  90. }
  91. @Override
  92. public List<Column> getToColumns() {
  93. return _toColumns;
  94. }
  95. public int getFlags() {
  96. return _flags;
  97. }
  98. @Override
  99. public boolean isOneToOne() {
  100. return hasFlag(ONE_TO_ONE_FLAG);
  101. }
  102. @Override
  103. public boolean hasReferentialIntegrity() {
  104. return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
  105. }
  106. @Override
  107. public boolean cascadeUpdates() {
  108. return hasFlag(CASCADE_UPDATES_FLAG);
  109. }
  110. @Override
  111. public boolean cascadeDeletes() {
  112. return hasFlag(CASCADE_DELETES_FLAG);
  113. }
  114. @Override
  115. public boolean cascadeNullOnDelete() {
  116. return hasFlag(CASCADE_NULL_FLAG);
  117. }
  118. @Override
  119. public boolean isLeftOuterJoin() {
  120. return hasFlag(LEFT_OUTER_JOIN_FLAG);
  121. }
  122. @Override
  123. public boolean isRightOuterJoin() {
  124. return hasFlag(RIGHT_OUTER_JOIN_FLAG);
  125. }
  126. @Override
  127. public JoinType getJoinType() {
  128. if(isLeftOuterJoin()) {
  129. return JoinType.LEFT_OUTER;
  130. } else if(isRightOuterJoin()) {
  131. return JoinType.RIGHT_OUTER;
  132. }
  133. return JoinType.INNER;
  134. }
  135. private boolean hasFlag(int flagMask) {
  136. return((getFlags() & flagMask) != 0);
  137. }
  138. @Override
  139. public String toString() {
  140. return ToStringBuilder.builder(this)
  141. .append("name", _name)
  142. .append("fromTable", _fromTable.getName())
  143. .append("fromColumns", _fromColumns)
  144. .append("toTable", _toTable.getName())
  145. .append("toColumns", _toColumns)
  146. .append("flags", Integer.toHexString(_flags))
  147. .toString();
  148. }
  149. }