summaryrefslogtreecommitdiffstats
path: root/src/java/com/healthmarketscience/jackcess/Relationship.java
blob: 7d089c4eb0e08b25d169319f306ce9c903b32ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2008 Health Market Science, Inc.

package com.healthmarketscience.jackcess;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

/**
 * Information about a relationship between two tables in the database.
 *
 * @author James Ahlborn
 */
public class Relationship {

  /** flag indicating one-to-one relationship */
  private static final int ONE_TO_ONE_FLAG =               0x00000001;
  /** flag indicating no referential integrity */
  private static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
  /** flag indicating cascading updates (requires referential integrity) */
  private static final int CASCADE_UPDATES_FLAG =          0x00000100;
  /** flag indicating cascading deletes (requires referential integrity) */
  private static final int CASCADE_DELETES_FLAG =          0x00001000;
  /** flag indicating left outer join */
  private static final int LEFT_OUTER_JOIN_FLAG =          0x01000000;
  /** flag indicating right outer join */
  private static final int RIGHT_OUTER_JOIN_FLAG =         0x02000000;

  /** the name of this relationship */
  private final String _name;
  /** the "from" table in this relationship */
  private final Table _fromTable;
  /** the "to" table in this relationship */
  private final Table _toTable;
  /** the columns in the "from" table in this relationship (aligned w/
      toColumns list) */
  private List<Column> _toColumns;
  /** the columns in the "to" table in this relationship (aligned w/
      toColumns list) */
  private List<Column> _fromColumns;
  /** the various flags describing this relationship */
  private final int _flags;

  public Relationship(String name, Table fromTable, Table toTable, int flags,
                      int numCols)
  {
    _name = name;
    _fromTable = fromTable;
    _fromColumns = new ArrayList<Column>(
        Collections.nCopies(numCols, (Column)null));
    _toTable = toTable;
    _toColumns = new ArrayList<Column>(
        Collections.nCopies(numCols, (Column)null));
    _flags = flags;
  }

  public String getName() {
    return _name;
  }
  
  public Table getFromTable() {
    return _fromTable;
  }

  public List<Column> getFromColumns() {
    return _fromColumns;
  }

  public Table getToTable() {
    return _toTable;
  }

  public List<Column> getToColumns() {
    return _toColumns;
  }

  public int getFlags() {
    return _flags;
  }

  public boolean isOneToOne() {
    return hasFlag(ONE_TO_ONE_FLAG);
  }

  public boolean hasReferentialIntegrity() {
    return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
  }

  public boolean cascadeUpdates() {
    return hasFlag(CASCADE_UPDATES_FLAG);
  }
  
  public boolean cascadeDeletes() {
    return hasFlag(CASCADE_DELETES_FLAG);
  }

  public boolean isLeftOuterJoin() {
    return hasFlag(LEFT_OUTER_JOIN_FLAG);
  }

  public boolean isRightOuterJoin() {
    return hasFlag(RIGHT_OUTER_JOIN_FLAG);
  }
  
  private boolean hasFlag(int flagMask) {
    return((getFlags() & flagMask) != 0);
  }

  @Override
  public String toString() {
    StringBuilder rtn = new StringBuilder();
    rtn.append("\tName: " + _name);
    rtn.append("\n\tFromTable: " + _fromTable.getName());
    rtn.append("\n\tFromColumns: " + _fromColumns);
    rtn.append("\n\tToTable: " + _toTable.getName());
    rtn.append("\n\tToColumns: " + _toColumns);
    rtn.append("\n\tFlags: " + Integer.toHexString(_flags));
    rtn.append("\n\n");
    return rtn.toString();
  }
  
}