001: // Copyright (c) 2008 Health Market Science, Inc.
002:
003: package com.healthmarketscience.jackcess;
004:
005: import java.util.Collections;
006: import java.util.List;
007: import java.util.ArrayList;
008:
009: /**
010: * Information about a relationship between two tables in the database.
011: *
012: * @author James Ahlborn
013: */
014: public class Relationship {
015:
016: /** flag indicating one-to-one relationship */
017: private static final int ONE_TO_ONE_FLAG = 0x00000001;
018: /** flag indicating no referential integrity */
019: private static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
020: /** flag indicating cascading updates (requires referential integrity) */
021: private static final int CASCADE_UPDATES_FLAG = 0x00000100;
022: /** flag indicating cascading deletes (requires referential integrity) */
023: private static final int CASCADE_DELETES_FLAG = 0x00001000;
024: /** flag indicating left outer join */
025: private static final int LEFT_OUTER_JOIN_FLAG = 0x01000000;
026: /** flag indicating right outer join */
027: private static final int RIGHT_OUTER_JOIN_FLAG = 0x02000000;
028:
029: /** the name of this relationship */
030: private final String _name;
031: /** the "from" table in this relationship */
032: private final Table _fromTable;
033: /** the "to" table in this relationship */
034: private final Table _toTable;
035: /** the columns in the "from" table in this relationship (aligned w/
036: toColumns list) */
037: private List<Column> _toColumns;
038: /** the columns in the "to" table in this relationship (aligned w/
039: toColumns list) */
040: private List<Column> _fromColumns;
041: /** the various flags describing this relationship */
042: private final int _flags;
043:
044: public Relationship(String name, Table fromTable, Table toTable,
045: int flags, int numCols) {
046: _name = name;
047: _fromTable = fromTable;
048: _fromColumns = new ArrayList<Column>(Collections.nCopies(
049: numCols, (Column) null));
050: _toTable = toTable;
051: _toColumns = new ArrayList<Column>(Collections.nCopies(numCols,
052: (Column) null));
053: _flags = flags;
054: }
055:
056: public String getName() {
057: return _name;
058: }
059:
060: public Table getFromTable() {
061: return _fromTable;
062: }
063:
064: public List<Column> getFromColumns() {
065: return _fromColumns;
066: }
067:
068: public Table getToTable() {
069: return _toTable;
070: }
071:
072: public List<Column> getToColumns() {
073: return _toColumns;
074: }
075:
076: public int getFlags() {
077: return _flags;
078: }
079:
080: public boolean isOneToOne() {
081: return hasFlag(ONE_TO_ONE_FLAG);
082: }
083:
084: public boolean hasReferentialIntegrity() {
085: return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
086: }
087:
088: public boolean cascadeUpdates() {
089: return hasFlag(CASCADE_UPDATES_FLAG);
090: }
091:
092: public boolean cascadeDeletes() {
093: return hasFlag(CASCADE_DELETES_FLAG);
094: }
095:
096: public boolean isLeftOuterJoin() {
097: return hasFlag(LEFT_OUTER_JOIN_FLAG);
098: }
099:
100: public boolean isRightOuterJoin() {
101: return hasFlag(RIGHT_OUTER_JOIN_FLAG);
102: }
103:
104: private boolean hasFlag(int flagMask) {
105: return ((getFlags() & flagMask) != 0);
106: }
107:
108: @Override
109: public String toString() {
110: StringBuilder rtn = new StringBuilder();
111: rtn.append("\tName: " + _name);
112: rtn.append("\n\tFromTable: " + _fromTable.getName());
113: rtn.append("\n\tFromColumns: " + _fromColumns);
114: rtn.append("\n\tToTable: " + _toTable.getName());
115: rtn.append("\n\tToColumns: " + _toColumns);
116: rtn.append("\n\tFlags: " + Integer.toHexString(_flags));
117: rtn.append("\n\n");
118: return rtn.toString();
119: }
120:
121: }
|