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.sqlbuilder.dbspec.basic;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import com.healthmarketscience.sqlbuilder.Condition;
23  import com.healthmarketscience.sqlbuilder.dbspec.Constraint;
24  import com.healthmarketscience.sqlbuilder.dbspec.RejoinTable;
25  import com.healthmarketscience.sqlbuilder.dbspec.Table;
26  
27  /**
28   * Representation of a table in a database schema.
29   *
30   * @author James Ahlborn
31   */
32  public class DbTable extends DbObject<DbSchema> implements Table {
33  
34    /** alias to use for this table in queries (should be unique) */
35    private final String _alias;
36    /** columns currently created for this table */
37    private final List<DbColumn> _columns = new ArrayList<DbColumn>();
38    /** constraints currently defined for this table */
39    private final List<DbConstraint> _constraints = new ArrayList<DbConstraint>();
40  
41    public DbTable(DbSchema parent, String name) {
42      this(parent, name, parent.getSpec().getNextAlias());
43    }
44  
45    public DbTable(DbSchema parent, String name, String alias) {
46      super(parent, name);
47      _alias = alias;
48    }
49  
50    @Override
51    public String getAlias() {
52      return _alias;
53    }
54  
55    @Override
56    public String getTableNameSQL() {
57      return getAbsoluteName();
58    }
59  
60    @Override
61    public List<DbColumn> getColumns() {
62      return _columns;
63    }
64  
65    @Override
66    public List<DbConstraint> getConstraints() {
67      return _constraints;
68    }
69  
70    /**
71     * @param name name of the column to find
72     * @return the column previously added to this table with the given name, or
73     *         {@code null} if none.
74     */
75    public DbColumn findColumn(String name) {
76      return findObject(_columns, name);
77    }
78  
79    /**
80     * @param names name(s) of the column(s) to find
81     * @return the column(s) previously added to this table with the given
82     *         name(s), or {@code null} if none.
83     */
84    public DbColumn[] findColumns(String... names) {
85      if(names == null) {
86        return null;
87      }
88      DbColumnbuilder/dbspec/basic/DbColumn.html#DbColumn">DbColumn[] cols = new DbColumn[names.length];
89      for(int i = 0; i < names.length; ++i) {
90        cols[i] = findObject(_columns, names[i]);
91      }
92      return cols;
93    }
94  
95    /**
96     * Creates and adds an untyped column with the given name to this table.
97     * <p>
98     * Note, no effort is made to make sure the given name is unique.
99     * @param name the name of the new column
100    * @return the freshly created column
101    */
102   public DbColumn addColumn(String name) {
103     return addColumn(name, null, null, null);
104   }
105 
106   /**
107    * Creates and adds an typed column with the given parameters to this table.
108    * <p>
109    * Note, no effort is made to make sure the given name is unique.
110    * @param name the name of the new column
111    * @param typeName type name for the column
112    * @param typeLength optional length specification for the column
113    * @return the freshly created column
114    */
115   public DbColumn addColumn(String name, String typeName, Integer typeLength) {
116     return addColumn(name, typeName, typeLength, null);
117   }
118 
119   /**
120    * Creates and adds an typed column with the given parameters to this table.
121    * <p>
122    * Note, no effort is made to make sure the given name is unique.
123    * @param name the name of the new column
124    * @param typeName type name for the column
125    * @param typePrecision optional precision specification for the column
126    * @param typeScale optional scale specification for the column
127    * @return the freshly created column
128    */
129   public DbColumn addColumn(String name, String typeName,
130                             Integer typePrecision, Integer typeScale) {
131     DbColumn column = getSpec().createColumn(this, name, typeName,
132                                              typePrecision, typeScale);
133     return addColumn(column);
134   }
135 
136   /**
137    * Creates and adds an typed column with the given parameters to this table.
138    * <p>
139    * Note, no effort is made to make sure the given name is unique.
140    * @param name the name of the new column
141    * @param type type for the column (one of {@link java.sql.Types})
142    * @param typeLength optional length specification for the column
143    * @return the freshly created column
144    */
145   public DbColumn addColumn(String name, int type, Integer typeLength) {
146     return addColumn(name, type, typeLength, null);
147   }
148 
149   /**
150    * Creates and adds an typed column with the given parameters to this table.
151    * <p>
152    * Note, no effort is made to make sure the given name is unique.
153    * @param name the name of the new column
154    * @param type type for the column (one of {@link java.sql.Types})
155    * @param typePrecision optional precision specification for the column
156    * @param typeScale optional scale specification for the column
157    * @return the freshly created column
158    */
159   public DbColumn addColumn(String name, int type, Integer typePrecision,
160                             Integer typeScale) {
161     return addColumn(name, DbColumn.getTypeName(type), typePrecision,
162                      typeScale);
163   }
164 
165   /**
166    * Adds the given column to this table.
167    * <p>
168    * Note, no effort is made to make sure the column is unique.
169    * @param column the column to be added
170    * @return the given column
171    */
172   public <T extends DbColumn> T addColumn(T column) {
173     _columns.add(checkOwnership(column));
174     return column;
175   }
176 
177   /**
178    * Creates and adds unique constraint with the given parameters to this
179    * table.
180    * <p>
181    * Note, no effort is made to make sure the given name is unique.
182    * @param name the name of the new constraint
183    * @param colNames the name of the constrained columns
184    */
185   public DbConstraint unique(String name, String... colNames) {
186     DbConstraint constraint = getSpec().createTableConstraint(
187         this, name, Constraint.Type.UNIQUE, colNames);
188     return addConstraint(constraint);
189   }
190 
191   /**
192    * Creates and adds primary key constraint with the given parameters to this
193    * table.
194    * <p>
195    * Note, no effort is made to make sure the given name is unique.
196    * @param name the name of the new constraint
197    * @param colNames the name of the constrained columns
198    */
199   public DbConstraint primaryKey(String name, String... colNames) {
200     DbConstraint constraint = getSpec().createTableConstraint(
201         this, name, Constraint.Type.PRIMARY_KEY, colNames);
202     return addConstraint(constraint);
203   }
204 
205   /**
206    * Creates and adds foreign key constraint with the given parameters to this
207    * table.
208    * <p>
209    * Note, no effort is made to make sure the given name is unique.
210    * @param name the name of the new constraint
211    * @param colNames the name of the constrained columns
212    * @param referencedTableName the name of the referenced table
213    * @param referencedColNames the names of the referenced columns
214    */
215   public DbForeignKeyConstraint foreignKey(String name, String[] colNames,
216                                            String referencedTableName,
217                                            String[] referencedColNames)
218   {
219     DbTable referencedTable = getParent().findTable(referencedTableName);
220     return foreignKey(name, findColumns(colNames), referencedTable,
221                       referencedTable.findColumns(referencedColNames));
222   }
223 
224   /**
225    * Creates and adds foreign key constraint with the given parameters to this
226    * table.
227    * <p>
228    * Note, no effort is made to make sure the given name is unique.
229    * @param name the name of the new constraint
230    * @param colNames the name of the constrained columns
231    * @param referencedSchemaName the name of the referenced schema
232    * @param referencedTableName the name of the referenced table
233    * @param referencedColNames the names of the referenced columns
234    */
235   public DbForeignKeyConstraint foreignKey(String name, String[] colNames,
236                                            String referencedSchemaName,
237                                            String referencedTableName,
238                                            String[] referencedColNames)
239   {
240     DbTable referencedTable = getSpec().findSchema(referencedSchemaName)
241       .findTable(referencedTableName);
242     return foreignKey(name, findColumns(colNames), referencedTable,
243                       referencedTable.findColumns(referencedColNames));
244   }
245 
246   /**
247    * Creates and adds foreign key constraint with the given parameters to this
248    * table.
249    * <p>
250    * Note, no effort is made to make sure the given name is unique.
251    * @param name the name of the new constraint
252    * @param columns the constrained columns
253    * @param referencedTable the referenced table
254    * @param refColumns the referenced columns
255    */
256   public DbForeignKeyConstraint foreignKey(String name, DbColumn[] columns,
257                                            DbTable referencedTable,
258                                            DbColumn[] refColumns)
259   {
260     DbForeignKeyConstraint fkConstraint =
261       getSpec().createTableForeignKeyConstraint(
262         this, name, referencedTable, columns, refColumns);
263     return addConstraint(fkConstraint);
264   }
265 
266   /**
267    * Creates and adds check constraint with the given parameters to this
268    * table.
269    * <p>
270    * Note, no effort is made to make sure the given name is unique.
271    * @param condition the check condition
272    */
273   public DbCheckConstraint checkCondition(String name, Condition condition) {
274     DbCheckConstraint constraint = getSpec().createTableCheckConstraint(
275         this, name, condition);
276     return addConstraint(constraint);
277   }
278 
279   /**
280    * Adds the given constraint to this table.
281    * <p>
282    * Note, no effort is made to make sure the given constraint is unique.
283    * @param constraint the constraint to be added
284    * @return the given constraint
285    */
286   public <T extends DbConstraint> T addConstraint(T constraint) {
287     _constraints.add(checkOwnership(constraint));
288     return constraint;
289   }
290 
291   /**
292    * Convenience method for creating a new RejoinTable instance for this table
293    * with the given alias.
294    */
295   public RejoinTable rejoin(String newAlias) {
296     return new RejoinTable(this, newAlias);
297   }
298 
299   @Override
300   public String toString() {
301     return super.toString() + "(" + getAlias() + ")";
302   }
303 
304 }