View Javadoc
1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.ArrayList;
22  
23  import com.healthmarketscience.jackcess.Column;
24  import com.healthmarketscience.jackcess.Relationship;
25  import com.healthmarketscience.jackcess.Table;
26  
27  /**
28   * Information about a relationship between two tables in the database.
29   *
30   * @author James Ahlborn
31   */
32  public class RelationshipImpl implements Relationship
33  {
34  
35    /** flag indicating one-to-one relationship */
36    public static final int ONE_TO_ONE_FLAG =               0x00000001;
37    /** flag indicating no referential integrity */
38    public static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
39    /** flag indicating cascading updates (requires referential integrity) */
40    public static final int CASCADE_UPDATES_FLAG =          0x00000100;
41    /** flag indicating cascading deletes (requires referential integrity) */
42    public static final int CASCADE_DELETES_FLAG =          0x00001000;
43    /** flag indicating cascading null on delete (requires referential
44        integrity) */
45    public static final int CASCADE_NULL_FLAG =             0x00002000;
46    /** flag indicating left outer join */
47    public static final int LEFT_OUTER_JOIN_FLAG =          0x01000000;
48    /** flag indicating right outer join */
49    public static final int RIGHT_OUTER_JOIN_FLAG =         0x02000000;
50  
51    /** the name of this relationship */
52    private final String _name;
53    /** the "from" table in this relationship */
54    private final Table _fromTable;
55    /** the "to" table in this relationship */
56    private final Table _toTable;
57    /** the columns in the "from" table in this relationship (aligned w/
58        toColumns list) */
59    private final List<Column> _toColumns;
60    /** the columns in the "to" table in this relationship (aligned w/
61        toColumns list) */
62    private final List<Column> _fromColumns;
63    /** the various flags describing this relationship */
64    private final int _flags;
65  
66    public RelationshipImpl(String name, Table fromTable, Table toTable, int flags,
67                            int numCols)
68    {
69      this(name, fromTable, toTable, flags, 
70           Collections.nCopies(numCols, (Column)null),
71           Collections.nCopies(numCols, (Column)null));
72    }
73  
74    public RelationshipImpl(String name, Table fromTable, Table toTable, int flags,
75                            List<? extends Column> fromCols,
76                            List<? extends Column> toCols)
77    {
78      _name = name;
79      _fromTable = fromTable;
80      _fromColumns = new ArrayList<Column>(fromCols);
81      _toTable = toTable;
82      _toColumns = new ArrayList<Column>(toCols);
83      _flags = flags;
84    }
85  
86    public String getName() {
87      return _name;
88    }
89    
90    public Table getFromTable() {
91      return _fromTable;
92    }
93  
94    public List<Column> getFromColumns() {
95      return _fromColumns;
96    }
97  
98    public Table getToTable() {
99      return _toTable;
100   }
101 
102   public List<Column> getToColumns() {
103     return _toColumns;
104   }
105 
106   public int getFlags() {
107     return _flags;
108   }
109 
110   public boolean isOneToOne() {
111     return hasFlag(ONE_TO_ONE_FLAG);
112   }
113 
114   public boolean hasReferentialIntegrity() {
115     return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
116   }
117 
118   public boolean cascadeUpdates() {
119     return hasFlag(CASCADE_UPDATES_FLAG);
120   }
121   
122   public boolean cascadeDeletes() {
123     return hasFlag(CASCADE_DELETES_FLAG);
124   }
125 
126   public boolean cascadeNullOnDelete() {
127     return hasFlag(CASCADE_NULL_FLAG);
128   }
129 
130   public boolean isLeftOuterJoin() {
131     return hasFlag(LEFT_OUTER_JOIN_FLAG);
132   }
133 
134   public boolean isRightOuterJoin() {
135     return hasFlag(RIGHT_OUTER_JOIN_FLAG);
136   }
137 
138   public JoinType getJoinType() {
139     if(isLeftOuterJoin()) {
140       return JoinType.LEFT_OUTER;
141     } else if(isRightOuterJoin()) {
142       return JoinType.RIGHT_OUTER;
143     }
144     return JoinType.INNER;
145   }
146   
147   private boolean hasFlag(int flagMask) {
148     return((getFlags() & flagMask) != 0);
149   }
150 
151   @Override
152   public String toString() {
153     return CustomToStringStyle.builder(this)
154       .append("name", _name)
155       .append("fromTable", _fromTable.getName())
156       .append("fromColumns", _fromColumns)
157       .append("toTable", _toTable.getName())
158       .append("toColumns", _toColumns)
159       .append("flags", Integer.toHexString(_flags))
160       .toString();
161   }
162   
163 }