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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. private static final int ONE_TO_ONE_FLAG = 0x00000001;
  29. /** flag indicating no referential integrity */
  30. private static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
  31. /** flag indicating cascading updates (requires referential integrity) */
  32. private static final int CASCADE_UPDATES_FLAG = 0x00000100;
  33. /** flag indicating cascading deletes (requires referential integrity) */
  34. private static final int CASCADE_DELETES_FLAG = 0x00001000;
  35. /** flag indicating left outer join */
  36. private static final int LEFT_OUTER_JOIN_FLAG = 0x01000000;
  37. /** flag indicating right outer join */
  38. private static final int RIGHT_OUTER_JOIN_FLAG = 0x02000000;
  39. /** the name of this relationship */
  40. private final String _name;
  41. /** the "from" table in this relationship */
  42. private final Table _fromTable;
  43. /** the "to" table in this relationship */
  44. private final Table _toTable;
  45. /** the columns in the "from" table in this relationship (aligned w/
  46. toColumns list) */
  47. private final List<Column> _toColumns;
  48. /** the columns in the "to" table in this relationship (aligned w/
  49. toColumns list) */
  50. private final List<Column> _fromColumns;
  51. /** the various flags describing this relationship */
  52. private final int _flags;
  53. public RelationshipImpl(String name, Table fromTable, Table toTable, int flags,
  54. int numCols)
  55. {
  56. _name = name;
  57. _fromTable = fromTable;
  58. _fromColumns = new ArrayList<Column>(
  59. Collections.nCopies(numCols, (Column)null));
  60. _toTable = toTable;
  61. _toColumns = new ArrayList<Column>(
  62. Collections.nCopies(numCols, (Column)null));
  63. _flags = flags;
  64. }
  65. public String getName() {
  66. return _name;
  67. }
  68. public Table getFromTable() {
  69. return _fromTable;
  70. }
  71. public List<Column> getFromColumns() {
  72. return _fromColumns;
  73. }
  74. public Table getToTable() {
  75. return _toTable;
  76. }
  77. public List<Column> getToColumns() {
  78. return _toColumns;
  79. }
  80. public int getFlags() {
  81. return _flags;
  82. }
  83. public boolean isOneToOne() {
  84. return hasFlag(ONE_TO_ONE_FLAG);
  85. }
  86. public boolean hasReferentialIntegrity() {
  87. return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
  88. }
  89. public boolean cascadeUpdates() {
  90. return hasFlag(CASCADE_UPDATES_FLAG);
  91. }
  92. public boolean cascadeDeletes() {
  93. return hasFlag(CASCADE_DELETES_FLAG);
  94. }
  95. public boolean isLeftOuterJoin() {
  96. return hasFlag(LEFT_OUTER_JOIN_FLAG);
  97. }
  98. public boolean isRightOuterJoin() {
  99. return hasFlag(RIGHT_OUTER_JOIN_FLAG);
  100. }
  101. private boolean hasFlag(int flagMask) {
  102. return((getFlags() & flagMask) != 0);
  103. }
  104. @Override
  105. public String toString() {
  106. return CustomToStringStyle.builder(this)
  107. .append("name", _name)
  108. .append("fromTable", _fromTable.getName())
  109. .append("fromColumns", _fromColumns)
  110. .append("toTable", _toTable.getName())
  111. .append("toColumns", _toColumns)
  112. .append("flags", Integer.toHexString(_flags))
  113. .toString();
  114. }
  115. }