View Javadoc

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