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../../../../com/healthmarketscience/jackcess/Table.html#Table">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../../../../com/healthmarketscience/jackcess/Table.html#Table">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    @Override
87    public String getName() {
88      return _name;
89    }
90    
91    @Override
92    public Table getFromTable() {
93      return _fromTable;
94    }
95  
96    @Override
97    public List<Column> getFromColumns() {
98      return _fromColumns;
99    }
100 
101   @Override
102   public Table getToTable() {
103     return _toTable;
104   }
105 
106   @Override
107   public List<Column> getToColumns() {
108     return _toColumns;
109   }
110 
111   public int getFlags() {
112     return _flags;
113   }
114 
115   @Override
116   public boolean isOneToOne() {
117     return hasFlag(ONE_TO_ONE_FLAG);
118   }
119 
120   @Override
121   public boolean hasReferentialIntegrity() {
122     return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
123   }
124 
125   @Override
126   public boolean cascadeUpdates() {
127     return hasFlag(CASCADE_UPDATES_FLAG);
128   }
129   
130   @Override
131   public boolean cascadeDeletes() {
132     return hasFlag(CASCADE_DELETES_FLAG);
133   }
134 
135   @Override
136   public boolean cascadeNullOnDelete() {
137     return hasFlag(CASCADE_NULL_FLAG);
138   }
139 
140   @Override
141   public boolean isLeftOuterJoin() {
142     return hasFlag(LEFT_OUTER_JOIN_FLAG);
143   }
144 
145   @Override
146   public boolean isRightOuterJoin() {
147     return hasFlag(RIGHT_OUTER_JOIN_FLAG);
148   }
149 
150   @Override
151   public JoinType getJoinType() {
152     if(isLeftOuterJoin()) {
153       return JoinType.LEFT_OUTER;
154     } else if(isRightOuterJoin()) {
155       return JoinType.RIGHT_OUTER;
156     }
157     return JoinType.INNER;
158   }
159   
160   private boolean hasFlag(int flagMask) {
161     return((getFlags() & flagMask) != 0);
162   }
163 
164   @Override
165   public String toString() {
166     return CustomToStringStyle.builder(this)
167       .append("name", _name)
168       .append("fromTable", _fromTable.getName())
169       .append("fromColumns", _fromColumns)
170       .append("toTable", _toTable.getName())
171       .append("toColumns", _toColumns)
172       .append("flags", Integer.toHexString(_flags))
173       .toString();
174   }
175   
176 }