KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > table > TableFactory


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.table;
30
31 import com.caucho.db.Database;
32 import com.caucho.db.sql.Expr;
33 import com.caucho.util.L10N;
34
35 import java.io.IOException JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 public class TableFactory {
40   private static final L10N L = new L10N(TableFactory.class);
41   
42   private Database _database;
43   
44   private String JavaDoc _name;
45   private Row _row;
46
47   private ArrayList JavaDoc<Constraint> _constraints = new ArrayList JavaDoc<Constraint>();
48
49   public TableFactory(Database database)
50   {
51     _database = database;
52   }
53
54   /**
55    * Returns the table name.
56    */

57   public String JavaDoc getName()
58   {
59     return _name;
60   }
61
62   /**
63    * Returns the row.
64    */

65   public Row getRow()
66   {
67     return _row;
68   }
69   
70   /**
71    * Creates a table.
72    */

73   public void startTable(String JavaDoc name)
74   {
75     _name = name;
76     _row = new Row();
77   }
78
79   /**
80    * Adds a varchar
81    */

82   public Column addVarchar(String JavaDoc name, int size)
83   {
84     _row.allocateColumn();
85     
86     if (size <= 128)
87       return _row.addColumn(new StringColumn(_row, name, size));
88     else
89       return _row.addColumn(new BigStringColumn(_row, name, size));
90   }
91
92   /**
93    * Adds a blob
94    */

95   public Column addBlob(String JavaDoc name)
96   {
97     _row.allocateColumn();
98     
99     return _row.addColumn(new BlobColumn(_row, name));
100   }
101
102   /**
103    * Adds an integer
104    */

105   public Column addInteger(String JavaDoc name)
106   {
107     _row.allocateColumn();
108     
109     return _row.addColumn(new IntColumn(_row, name));
110   }
111
112   /**
113    * Adds a long
114    */

115   public Column addLong(String JavaDoc name)
116   {
117     _row.allocateColumn();
118     
119     return _row.addColumn(new LongColumn(_row, name));
120   }
121
122   /**
123    * Adds a double
124    */

125   public Column addDouble(String JavaDoc name)
126   {
127     _row.allocateColumn();
128     
129     return _row.addColumn(new DoubleColumn(_row, name));
130   }
131
132   /**
133    * Adds a datetime column
134    */

135   public Column addDateTime(String JavaDoc name)
136   {
137     _row.allocateColumn();
138     
139     return _row.addColumn(new DateColumn(_row, name));
140   }
141
142   /**
143    * Adds a numeric
144    */

145   public Column addNumeric(String JavaDoc name, int precision, int scale)
146   {
147     _row.allocateColumn();
148     
149     return _row.addColumn(new NumericColumn(_row, name, precision, scale));
150   }
151
152   /**
153    * Sets the named column as a primary key constraint.
154    */

155   public void setPrimaryKey(String JavaDoc name)
156     throws SQLException JavaDoc
157   {
158     Column column = _row.getColumn(name);
159
160     if (column == null)
161       throw new SQLException JavaDoc(L.l("`{0}' is not a valid column for primary key",
162                  name));
163
164     column.setUnique();
165     column.setNotNull();
166
167     //addConstraint(new PrimaryKeySingleColumnConstraint(column));
168
}
169
170   /**
171    * Sets the named column as not null
172    */

173   public void setNotNull(String JavaDoc name)
174     throws SQLException JavaDoc
175   {
176     Column column = _row.getColumn(name);
177
178     if (column == null)
179       throw new SQLException JavaDoc(L.l("`{0}' is not a valid column for NOT NULL",
180                  name));
181
182     column.setNotNull();
183   }
184
185   /**
186    * Sets the column default
187    */

188   public void setDefault(String JavaDoc name, Expr expr)
189     throws SQLException JavaDoc
190   {
191     Column column = _row.getColumn(name);
192
193     if (column == null)
194       throw new SQLException JavaDoc(L.l("`{0}' is not a valid column for DEFAULT",
195                  name));
196
197     column.setDefault(expr);
198   }
199
200   /**
201    * Sets the named column as unique
202    */

203   public void setUnique(String JavaDoc name)
204     throws SQLException JavaDoc
205   {
206     Column column = _row.getColumn(name);
207
208     if (column == null)
209       throw new SQLException JavaDoc(L.l("'{0}' is not a valid column for NOT NULL",
210                  name));
211
212     column.setUnique();
213
214     // already checked by unique
215
//addConstraint(new UniqueSingleColumnConstraint(column));
216
}
217   
218   /**
219    * Sets the named column as auto-increment
220    */

221   public void setAutoIncrement(String JavaDoc name, int min)
222     throws SQLException JavaDoc
223   {
224     Column column = _row.getColumn(name);
225
226     if (column == null)
227       throw new SQLException JavaDoc(L.l("'{0}' is not a valid column for auto_increment",
228                  name));
229
230     column.setAutoIncrement(min);
231   }
232
233
234   /**
235    * Sets the array of columns as unique
236    */

237   public void addUnique(ArrayList JavaDoc<String JavaDoc> names)
238     throws SQLException JavaDoc
239   {
240     if (names.size() == 1) {
241       setUnique(names.get(0));
242       return;
243     }
244     
245     ArrayList JavaDoc<Column> columns = new ArrayList JavaDoc<Column>();
246
247     for (int i = 0; i < names.size(); i++) {
248       String JavaDoc name = names.get(i);
249       
250       Column column = _row.getColumn(name);
251
252       if (column == null)
253     throw new SQLException JavaDoc(L.l("`{0}' is not a valid column for UNIQUE",
254                    name));
255     }
256
257     Column []columnArray = new Column[columns.size()];
258     columns.toArray(columnArray);
259
260     if (columnArray.length == 1) {
261       columnArray[0].setUnique();
262       
263       //addConstraint(new UniqueSingleColumnConstraint(columnArray[0]));
264
}
265     else
266       addConstraint(new UniqueConstraint(columnArray));
267   }
268
269   /**
270    * Sets the array of columns as the primary key
271    */

272   public void addPrimaryKey(ArrayList JavaDoc<String JavaDoc> names)
273     throws SQLException JavaDoc
274   {
275     if (names.size() == 1) {
276       setPrimaryKey(names.get(0));
277       return;
278     }
279     
280     ArrayList JavaDoc<Column> columns = new ArrayList JavaDoc<Column>();
281
282     for (int i = 0; i < names.size(); i++) {
283       String JavaDoc name = names.get(i);
284       
285       Column column = _row.getColumn(name);
286
287       if (column == null)
288     throw new SQLException JavaDoc(L.l("`{0}' is not a valid column for PRIMARY KEY",
289                    name));
290     }
291
292     Column []columnArray = new Column[columns.size()];
293     columns.toArray(columnArray);
294     
295     if (columnArray.length == 1) {
296       columnArray[0].setPrimaryKey(true);
297       //addConstraint(new PrimaryKeySingleColumnConstraint(columnArray[0]));
298
}
299     else
300       addConstraint(new PrimaryKeyConstraint(columnArray));
301   }
302
303   /**
304    * Adds a constraint.
305    */

306   public void addConstraint(Constraint constraint)
307   {
308     _constraints.add(constraint);
309   }
310
311   /**
312    * Returns the constraints.
313    */

314   public Constraint []getConstraints()
315   {
316     Constraint []constraints = new Constraint[_constraints.size()];
317     _constraints.toArray(constraints);
318
319     return constraints;
320   }
321
322   /**
323    * Creates the table.
324    */

325   public void create()
326     throws java.sql.SQLException JavaDoc, IOException JavaDoc
327   {
328     Table table = new Table(_database, _name, _row, getConstraints());
329     
330     table.create();
331   }
332 }
333
Popular Tags