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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.ArrayList;
  26. /**
  27. * Information about a relationship between two tables in the database.
  28. *
  29. * @author James Ahlborn
  30. */
  31. public class Relationship {
  32. /** flag indicating one-to-one relationship */
  33. private static final int ONE_TO_ONE_FLAG = 0x00000001;
  34. /** flag indicating no referential integrity */
  35. private static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
  36. /** flag indicating cascading updates (requires referential integrity) */
  37. private static final int CASCADE_UPDATES_FLAG = 0x00000100;
  38. /** flag indicating cascading deletes (requires referential integrity) */
  39. private static final int CASCADE_DELETES_FLAG = 0x00001000;
  40. /** flag indicating left outer join */
  41. private static final int LEFT_OUTER_JOIN_FLAG = 0x01000000;
  42. /** flag indicating right outer join */
  43. private static final int RIGHT_OUTER_JOIN_FLAG = 0x02000000;
  44. /** the name of this relationship */
  45. private final String _name;
  46. /** the "from" table in this relationship */
  47. private final Table _fromTable;
  48. /** the "to" table in this relationship */
  49. private final Table _toTable;
  50. /** the columns in the "from" table in this relationship (aligned w/
  51. toColumns list) */
  52. private List<Column> _toColumns;
  53. /** the columns in the "to" table in this relationship (aligned w/
  54. toColumns list) */
  55. private List<Column> _fromColumns;
  56. /** the various flags describing this relationship */
  57. private final int _flags;
  58. public Relationship(String name, Table fromTable, Table toTable, int flags,
  59. int numCols)
  60. {
  61. _name = name;
  62. _fromTable = fromTable;
  63. _fromColumns = new ArrayList<Column>(
  64. Collections.nCopies(numCols, (Column)null));
  65. _toTable = toTable;
  66. _toColumns = new ArrayList<Column>(
  67. Collections.nCopies(numCols, (Column)null));
  68. _flags = flags;
  69. }
  70. public String getName() {
  71. return _name;
  72. }
  73. public Table getFromTable() {
  74. return _fromTable;
  75. }
  76. public List<Column> getFromColumns() {
  77. return _fromColumns;
  78. }
  79. public Table getToTable() {
  80. return _toTable;
  81. }
  82. public List<Column> getToColumns() {
  83. return _toColumns;
  84. }
  85. public int getFlags() {
  86. return _flags;
  87. }
  88. public boolean isOneToOne() {
  89. return hasFlag(ONE_TO_ONE_FLAG);
  90. }
  91. public boolean hasReferentialIntegrity() {
  92. return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
  93. }
  94. public boolean cascadeUpdates() {
  95. return hasFlag(CASCADE_UPDATES_FLAG);
  96. }
  97. public boolean cascadeDeletes() {
  98. return hasFlag(CASCADE_DELETES_FLAG);
  99. }
  100. public boolean isLeftOuterJoin() {
  101. return hasFlag(LEFT_OUTER_JOIN_FLAG);
  102. }
  103. public boolean isRightOuterJoin() {
  104. return hasFlag(RIGHT_OUTER_JOIN_FLAG);
  105. }
  106. private boolean hasFlag(int flagMask) {
  107. return((getFlags() & flagMask) != 0);
  108. }
  109. @Override
  110. public String toString() {
  111. StringBuilder rtn = new StringBuilder();
  112. rtn.append("\tName: " + _name);
  113. rtn.append("\n\tFromTable: " + _fromTable.getName());
  114. rtn.append("\n\tFromColumns: " + _fromColumns);
  115. rtn.append("\n\tToTable: " + _toTable.getName());
  116. rtn.append("\n\tToColumns: " + _toColumns);
  117. rtn.append("\n\tFlags: " + Integer.toHexString(_flags));
  118. rtn.append("\n\n");
  119. return rtn.toString();
  120. }
  121. }