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.

RelationshipBuilder.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2016 James Ahlborn
  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;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  18. import com.healthmarketscience.jackcess.impl.RelationshipCreator;
  19. import com.healthmarketscience.jackcess.impl.RelationshipImpl;
  20. /**
  21. * Builder style class for constructing a {@link Relationship}, and,
  22. * optionally, the associated backing foreign key (if referential integrity
  23. * enforcement is enabled). A Relationship can only be constructed for
  24. * {@link Table}s which already exist in the {@link Database}. Additionally,
  25. * if integrity enforcement is enabled, there must already be a unique index
  26. * on the "from" Table for the relevant columns (same requirement as MS
  27. * Access).
  28. * <p/>
  29. * Example:
  30. * <pre>
  31. * Relationship rel = new RelationshipBuilder("FromTable", "ToTable")
  32. * .addColumns("ID", "FK_ID")
  33. * .setReferentialIntegrity()
  34. * .setCascadeDeletes()
  35. * .toRelationship(db);
  36. * </pre>
  37. *
  38. * @author James Ahlborn
  39. * @see TableBuilder
  40. * @usage _general_class_
  41. */
  42. public class RelationshipBuilder
  43. {
  44. private static final int JOIN_FLAGS =
  45. RelationshipImpl.LEFT_OUTER_JOIN_FLAG |
  46. RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
  47. /** relationship flags (default to "don't enforce") */
  48. private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG;
  49. private String _fromTable;
  50. private String _toTable;
  51. private List<String> _fromCols = new ArrayList<String>();
  52. private List<String> _toCols = new ArrayList<String>();
  53. public RelationshipBuilder(String fromTable, String toTable)
  54. {
  55. _fromTable = fromTable;
  56. _toTable = toTable;
  57. }
  58. /**
  59. * Adds a pair of columns to the relationship.
  60. */
  61. public RelationshipBuilder addColumns(String fromCol, String toCol) {
  62. _fromCols.add(fromCol);
  63. _toCols.add(toCol);
  64. return this;
  65. }
  66. /**
  67. * Enables referential integrity enforcement for this relationship.
  68. *
  69. * Note, this requires the "from" table to have an existing unique index on
  70. * the relevant columns.
  71. */
  72. public RelationshipBuilder setReferentialIntegrity() {
  73. return clearFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
  74. }
  75. /**
  76. * Enables deletes to be cascaded from the "from" table to the "to" table.
  77. *
  78. * Note, this requires referential integrity to be enforced.
  79. */
  80. public RelationshipBuilder setCascadeDeletes() {
  81. return setFlag(RelationshipImpl.CASCADE_DELETES_FLAG);
  82. }
  83. /**
  84. * Enables updates to be cascaded from the "from" table to the "to" table.
  85. *
  86. * Note, this requires referential integrity to be enforced.
  87. */
  88. public RelationshipBuilder setCascadeUpdates() {
  89. return setFlag(RelationshipImpl.CASCADE_UPDATES_FLAG);
  90. }
  91. /**
  92. * Enables deletes in the "from" table to be cascaded as "null" to the "to"
  93. * table.
  94. *
  95. * Note, this requires referential integrity to be enforced.
  96. */
  97. public RelationshipBuilder setCascadeNullOnDelete() {
  98. return setFlag(RelationshipImpl.CASCADE_NULL_FLAG);
  99. }
  100. /**
  101. * Sets the preferred join type for this relationship.
  102. */
  103. public RelationshipBuilder setJoinType(Relationship.JoinType joinType) {
  104. clearFlag(JOIN_FLAGS);
  105. switch(joinType) {
  106. case INNER:
  107. // nothing to do
  108. break;
  109. case LEFT_OUTER:
  110. _flags |= RelationshipImpl.LEFT_OUTER_JOIN_FLAG;
  111. break;
  112. case RIGHT_OUTER:
  113. _flags |= RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
  114. break;
  115. default:
  116. throw new RuntimeException("unexpected join type " + joinType);
  117. }
  118. return this;
  119. }
  120. public boolean hasReferentialIntegrity() {
  121. return !hasFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
  122. }
  123. public int getFlags() {
  124. return _flags;
  125. }
  126. public String getFromTable() {
  127. return _fromTable;
  128. }
  129. public String getToTable() {
  130. return _toTable;
  131. }
  132. public List<String> getFromColumns() {
  133. return _fromCols;
  134. }
  135. public List<String> getToColumns() {
  136. return _toCols;
  137. }
  138. /**
  139. * Creates a new Relationship in the given Database with the currently
  140. * configured attributes.
  141. */
  142. public Relationship toRelationship(Database db)
  143. throws IOException
  144. {
  145. return new RelationshipCreator((DatabaseImpl)db).createRelationship(this);
  146. }
  147. private RelationshipBuilder setFlag(int flagMask) {
  148. _flags |= flagMask;
  149. return this;
  150. }
  151. private RelationshipBuilder clearFlag(int flagMask) {
  152. _flags &= ~flagMask;
  153. return this;
  154. }
  155. private boolean hasFlag(int flagMask) {
  156. return((_flags & flagMask) != 0);
  157. }
  158. }