View Javadoc
1   /*
2   Copyright (c) 2016 James Ahlborn
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;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import com.healthmarketscience.jackcess.impl.DatabaseImpl;
24  import com.healthmarketscience.jackcess.impl.RelationshipCreator;
25  import com.healthmarketscience.jackcess.impl.RelationshipImpl;
26  
27  /**
28   * Builder style class for constructing a {@link Relationship}, and,
29   * optionally, the associated backing foreign key (if referential integrity
30   * enforcement is enabled).  A Relationship can only be constructed for
31   * {@link Table}s which already exist in the {@link Database}.  Additionally,
32   * if integrity enforcement is enabled, there must already be a unique index
33   * on the "from" Table for the relevant columns (same requirement as MS
34   * Access).
35   * <p>
36   * Example:
37   * <pre>
38   *   Relationship rel = new RelationshipBuilder("FromTable", "ToTable")
39   *     .addColumns("ID", "FK_ID")
40   *     .setReferentialIntegrity()
41   *     .setCascadeDeletes()
42   *     .toRelationship(db);
43   * </pre>
44   *
45   * @author James Ahlborn
46   * @see TableBuilder
47   * @usage _general_class_
48   */
49  public class RelationshipBuilder 
50  {
51    private static final int JOIN_FLAGS =
52      RelationshipImpl.LEFT_OUTER_JOIN_FLAG |
53      RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
54  
55    /** relationship flags (default to "don't enforce") */
56    private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG;
57    private String _fromTable;
58    private String _toTable;
59    private List<String> _fromCols = new ArrayList<String>();
60    private List<String> _toCols = new ArrayList<String>();
61    private String _name = null;
62  
63    public RelationshipBuilder(Table../../../com/healthmarketscience/jackcess/Table.html#Table">Table fromTable, Table toTable) {
64      this(fromTable.getName(), toTable.getName());
65    }
66  
67    public RelationshipBuilder(String fromTable, String toTable) {
68      _fromTable = fromTable;
69      _toTable = toTable;
70    }
71  
72    /**
73     * Adds a pair of columns to the relationship.
74     */
75    public RelationshipBuilder addColumns(String fromCol, String toCol) {
76      _fromCols.add(fromCol);
77      _toCols.add(toCol);
78      return this;
79    }
80  
81    /**
82     * Adds a pair of columns to the relationship.
83     */
84    public RelationshipBuilder addColumns(Column"../../../com/healthmarketscience/jackcess/Column.html#Column">Column fromCol, Column toCol) {
85      return addColumns(fromCol.getName(), toCol.getName());
86    }
87  
88    /**
89     * Enables referential integrity enforcement for this relationship.
90     *
91     * Note, this requires the "from" table to have an existing unique index on
92     * the relevant columns.
93     */
94    public RelationshipBuilder setReferentialIntegrity() {
95      return clearFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
96    }
97  
98    /**
99     * Enables deletes to be cascaded from the "from" table to the "to" table.
100    *
101    * Note, this requires referential integrity to be enforced.
102    */
103   public RelationshipBuilder setCascadeDeletes() {
104     return setFlag(RelationshipImpl.CASCADE_DELETES_FLAG);
105   }
106   
107   /**
108    * Enables updates to be cascaded from the "from" table to the "to" table.
109    *
110    * Note, this requires referential integrity to be enforced.
111    */
112   public RelationshipBuilder setCascadeUpdates() {
113     return setFlag(RelationshipImpl.CASCADE_UPDATES_FLAG);
114   }
115 
116   /**
117    * Enables deletes in the "from" table to be cascaded as "null" to the "to"
118    * table.
119    *
120    * Note, this requires referential integrity to be enforced.
121    */
122   public RelationshipBuilder setCascadeNullOnDelete() {
123     return setFlag(RelationshipImpl.CASCADE_NULL_FLAG);
124   }  
125 
126   /**
127    * Sets the preferred join type for this relationship.
128    */
129   public RelationshipBuilder setJoinType(Relationship.JoinType joinType) {
130     clearFlag(JOIN_FLAGS);
131     switch(joinType) {
132     case INNER:
133       // nothing to do
134       break;
135     case LEFT_OUTER:
136       _flags |= RelationshipImpl.LEFT_OUTER_JOIN_FLAG;
137       break;
138     case RIGHT_OUTER:
139       _flags |= RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
140       break;
141     default:
142       throw new RuntimeException("unexpected join type " + joinType);
143     }
144     return this;
145   }
146   
147   /**
148    * Sets a specific name for this relationship.
149    * 
150    * Default = null, meaning that the standard Access naming convention will be used. 
151    */
152   public RelationshipBuilder setName(String relationshipName) {
153     _name = relationshipName;
154     return this;
155   }
156 
157   public boolean hasReferentialIntegrity() {
158     return !hasFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
159   }
160 
161   public int getFlags() {
162     return _flags;
163   }
164 
165   public String getFromTable() {
166     return _fromTable;
167   }
168 
169   public String getToTable() {
170     return _toTable;
171   }
172   
173   public List<String> getFromColumns() {
174     return _fromCols;
175   }
176 
177   public List<String> getToColumns() {
178     return _toCols;
179   }
180   
181   public String getName() {
182     return _name;
183   }
184   
185   /**
186    * Creates a new Relationship in the given Database with the currently
187    * configured attributes.
188    */
189   public Relationship toRelationship(Database db)
190     throws IOException
191   {
192     return new RelationshipCreator((DatabaseImpl)db).createRelationship(this);
193   }
194 
195   private RelationshipBuilder setFlag(int flagMask) {
196     _flags |= flagMask;
197     return this;
198   }
199 
200   private RelationshipBuilder clearFlag(int flagMask) {
201     _flags &= ~flagMask;
202     return this;
203   }
204 
205   private boolean hasFlag(int flagMask) {
206     return((_flags & flagMask) != 0);
207   }
208 
209 }