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.
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.sqlbuilder;
29  
30  import java.io.IOException;
31  
32  import com.healthmarketscience.common.util.AppendableExt;
33  import com.healthmarketscience.sqlbuilder.dbspec.Column;
34  import com.healthmarketscience.sqlbuilder.dbspec.Constraint;
35  import com.healthmarketscience.sqlbuilder.dbspec.Table;
36  
37  /**
38   * Query which generates an {@code ALTER TABLE} statement.
39   *
40   * @author James Ahlborn
41   */
42  public class AlterTableQuery extends Query<AlterTableQuery>
43  {
44    private SqlObject _table;
45    private SqlObject _action;
46    
47    public AlterTableQuery(Table table) {
48      this((Object)table);
49    }
50  
51    /**
52     * {@code Object} -&gt; {@code SqlObject} conversions handled by
53     * {@link Converter#toCustomTableSqlObject(Object)}.
54     */
55    public AlterTableQuery(Object tableStr) {
56      _table = Converter.toCustomTableSqlObject(tableStr);
57    }
58  
59    /**
60     * Sets the alter table action.
61     *
62     * {@code Object} -&gt; {@code SqlObject} conversions handled by
63     * {@link Converter#toCustomSqlObject(Object)}.
64     */
65    public AlterTableQuery setAction(Object action) {
66      _action = Converter.toCustomSqlObject(action);
67      return this;
68    }
69  
70    /**
71     * Sets the alter table action to add the given constraint.
72     */
73    public AlterTableQuery setAddConstraint(Constraint constraint) {
74      return setAddCustomConstraint(constraint);
75    }
76  
77    /**
78     * Sets the alter table action to add the given constraint.
79       * <p>
80       * {@code Object} -&gt; {@code SqlObject} conversions handled by
81       * {@link Converter#toCustomConstraintClause}.
82     */
83    public AlterTableQuery setAddCustomConstraint(Object constraint) {
84      return setAction(new AddConstraintAction(constraint));
85    }
86  
87    /**
88     * Sets the alter table action to add the given column.
89     */
90    public AlterTableQuery setAddColumn(Column column) {
91      return setAddCustomColumn(column);
92    }
93  
94    /**
95     * Sets the alter table action to add the given column.
96       * <p>
97       * {@code Object} -&gt; {@code SqlObject} conversions handled by
98       * {@link Converter#toCustomTypedColumnSqlObject}.
99     */
100   public AlterTableQuery setAddCustomColumn(Object column) {
101     return setAction(new AddColumnAction(column));
102   }
103 
104   @Override
105   protected void collectSchemaObjects(ValidationContext vContext) {
106     super.collectSchemaObjects(vContext);
107     _table.collectSchemaObjects(vContext);
108     _action.collectSchemaObjects(vContext);
109   }
110   
111   @Override
112   protected void appendTo(AppendableExt app, SqlContext newContext)
113     throws IOException
114   {
115     newContext.setUseTableAliases(false);
116     
117     app.append("ALTER TABLE ").append(_table).append(_action);
118   }
119   
120   /**
121    * "Action" for adding a unique constraint to a table.,
122    * e.g. {@code "... ADD <constraint_clause>}.
123    */
124   public static class AddConstraintAction extends SqlObject
125   {
126     protected SqlObject _constraint;
127 
128     public AddConstraintAction(Object constraint) {
129       _constraint = Converter.toCustomConstraintClause(constraint);
130     }
131 
132     protected ConstraintClause getConstraint() {
133       return (ConstraintClause)_constraint;
134     }
135 
136     @Override
137     protected void collectSchemaObjects(ValidationContext vContext) {
138       _constraint.collectSchemaObjects(vContext);
139     }
140 
141     @Override
142     public void appendTo(AppendableExt app) throws IOException {
143       app.append(" ADD ").append(_constraint);
144     }
145   }
146 
147 
148   /**
149    * "Action" for adding a column to a table.,
150    * e.g. {@code "... ADD <column name> <column type> [constraints]}.
151    *
152    * @author Eric Fennell
153    */
154   public static class AddColumnAction extends SqlObject
155   {
156     private SqlObject _column;
157 
158     public AddColumnAction(Object column) {
159       _column = Converter.toCustomTypedColumnSqlObject(column);
160     }
161 
162     @Override
163     protected void collectSchemaObjects(ValidationContext vContext) {
164       _column.collectSchemaObjects(vContext);
165     }
166 
167     @Override
168     public void appendTo(AppendableExt app) throws IOException {
169       app.append(" ADD ").append(_column);
170     }
171 
172     /**
173      * Adds a constraint to the column specified in this action
174      * <p>
175      * {@code Object} -&gt; {@code SqlObject} constraint conversions handled by
176      * {@link Converter#toCustomConstraintClause}.
177      */
178     public AddColumnAction addConstraint(Object constraint) {
179       if(_column instanceof TypedColumnObject) {
180         ((TypedColumnObject)_column).addConstraint(constraint);
181       }
182       return this;
183     }
184 
185     /**
186      * Sets the given value as the column default value for this action.
187      * <p>
188      * {@code Object} -&gt; {@code SqlObject} value conversions handled by
189      * {@link Converter#toValueSqlObject}.
190      */
191     public AddColumnAction setDefaultValue(Object defaultValue) {
192       if(_column instanceof TypedColumnObject) {
193         ((TypedColumnObject)_column).setDefaultValue(defaultValue);
194       }
195       return this;
196     }
197   }
198 
199 
200   /**
201    * "Action" for adding a unique constraint to a table.,
202    * e.g. {@code "... ADD UNIQUE (<col1> ... [<coln>])}.
203    * @deprecated use AddConstraintAction instead
204    */
205   @Deprecated
206   public static class AddUniqueConstraintAction extends AddConstraintAction
207   {
208     public AddUniqueConstraintAction() {
209       super(ConstraintClause.unique());
210     }
211 
212     /**
213      * Adds a column to the unique constraint definition.
214      */
215     public AddUniqueConstraintAction addColumns(Column... columns) {
216       return addCustomColumns((Object[])columns);
217     }
218 
219     /**
220      * Adds a custom column to the unique constraint definition.
221      * <p>
222      * {@code Object} -&gt; {@code SqlObject} conversions handled by
223      * {@link Converter#CUSTOM_COLUMN_TO_OBJ}.
224      */
225     public AddUniqueConstraintAction addCustomColumns(Object... columnStrs) {
226       getConstraint().addCustomColumns(columnStrs);
227       return this;
228     }
229   }
230   
231   /**
232    * "Action" for adding a primary key constraint to a table,
233    * e.g. {@code "... ADD PRIMARY KEY (<col1> ... [<coln>])}.
234    * @deprecated use AddConstraintAction instead
235    */
236   @Deprecated
237   public static class AddPrimaryConstraintAction extends AddConstraintAction
238   {
239     public AddPrimaryConstraintAction() {
240       super(ConstraintClause.primaryKey());
241     }
242 
243     /**
244      * Adds a column to the primary key constraint definition.
245      */
246     public AddPrimaryConstraintAction addColumns(Column... columns) {
247       return addCustomColumns((Object[])columns);
248     }
249 
250     /**
251      * Adds a custom column to the primary key constraint definition.
252      * <p>
253      * {@code Object} -&gt; {@code SqlObject} conversions handled by
254      * {@link Converter#CUSTOM_COLUMN_TO_OBJ}.
255      */
256     public AddPrimaryConstraintAction addCustomColumns(Object... columnStrs) {
257       getConstraint().addCustomColumns(columnStrs);
258       return this;
259     }
260   }
261 
262 
263   /**
264    * "Action" for adding a foreign key constraint to a table,
265    * e.g. 
266    * {@code "... ADD FOREIGN KEY (<c1>...[<cn>]) REFERENCES t2 [(<c1>...<cn>)]}.
267    * @deprecated use AddConstraintAction instead
268    */
269   @Deprecated
270   public static class AddForeignConstraintAction extends AddConstraintAction
271   {
272     /** 
273      * Creates a new {@link AddForeignConstraintAction} which references
274      * the given {@link Table}.
275      */
276     public AddForeignConstraintAction(Table table) {
277       this((Object)table);
278     }
279 
280     /** 
281      * Creates a new {@link AddForeignConstraintAction} which references
282      * the given {@link Table}.
283      *
284      * {@code Object} -&gt; {@code SqlObject} conversions handled by
285      * {@link Converter#toCustomTableSqlObject(Object)}.
286      */
287     public AddForeignConstraintAction(Object table) {
288       super(ConstraintClause.foreignKey(table));
289     }
290 
291     @Override
292     protected ForeignKeyConstraintClause getConstraint() {
293       return (ForeignKeyConstraintClause)super.getConstraint();
294     }
295 
296     /** 
297      * Adds {@code col} as a reference to the primary key in the referenced
298      * table.
299      */
300     public AddForeignConstraintAction addPrimaryKeyReference(Column col) {
301       return addReference(col, null);
302     }
303 
304     /** 
305      * Adds {@code col} as a reference to the primary key in the referenced
306      * table.
307      * <p>
308      * {@code Object} -&gt; {@code SqlObject} conversions handled by
309      * {@link Converter#CUSTOM_COLUMN_TO_OBJ}.
310      */
311     public AddForeignConstraintAction addCustomPrimaryKeyReference(Object col){
312       return addCustomReference(col, null);
313     }
314 
315     /**
316      * Adds {@code col} as a reference to {@code referencedCol} in the
317      * referenced table.
318      */
319     public AddForeignConstraintAction addReference(
320         Column col, Column referencedCol) {
321       return addCustomReference(col, referencedCol);
322     }
323 
324     /**
325      * Adds {@code col} as a reference to {@code referencedCol} in the
326      * referenced table.  If referencedCol is {@code null}, it is ignored
327      * (useful for referencing the primary key in the referenced table).
328      * <p>
329      * {@code Object} -&gt; {@code SqlObject} conversions handled by
330      * {@link Converter#CUSTOM_COLUMN_TO_OBJ}.
331      */
332     public AddForeignConstraintAction addCustomReference(
333         Object col, Object referencedCol) {
334       getConstraint().addCustomColumns(col);
335       if (referencedCol != null) {
336         getConstraint().addCustomRefColumns(referencedCol);
337       }
338       return this;
339     }
340   }
341   
342 }