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

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