001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.table;
030:
031: import com.caucho.db.Database;
032: import com.caucho.db.sql.Expr;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.sql.SQLException;
037: import java.util.ArrayList;
038:
039: public class TableFactory {
040: private static final L10N L = new L10N(TableFactory.class);
041:
042: private Database _database;
043:
044: private String _name;
045: private Row _row;
046:
047: private ArrayList<Constraint> _constraints = new ArrayList<Constraint>();
048:
049: public TableFactory(Database database) {
050: _database = database;
051: }
052:
053: /**
054: * Returns the table name.
055: */
056: public String getName() {
057: return _name;
058: }
059:
060: /**
061: * Returns the row.
062: */
063: public Row getRow() {
064: return _row;
065: }
066:
067: /**
068: * Creates a table.
069: */
070: public void startTable(String name) {
071: _name = name;
072: _row = new Row();
073: }
074:
075: /**
076: * Adds a varchar
077: */
078: public Column addVarchar(String name, int size) {
079: _row.allocateColumn();
080:
081: if (size <= 128)
082: return _row.addColumn(new StringColumn(_row, name, size));
083: else
084: return _row
085: .addColumn(new BigStringColumn(_row, name, size));
086: }
087:
088: /**
089: * Adds a varbinary
090: */
091: public Column addVarbinary(String name, int size) {
092: _row.allocateColumn();
093:
094: if (size <= 128)
095: return _row.addColumn(new BinaryColumn(_row, name, size));
096: else
097: return _row.addColumn(new BlobColumn(_row, name));
098: }
099:
100: /**
101: * Adds a blob
102: */
103: public Column addBlob(String name) {
104: _row.allocateColumn();
105:
106: return _row.addColumn(new BlobColumn(_row, name));
107: }
108:
109: /**
110: * Adds an integer
111: */
112: public Column addInteger(String name) {
113: _row.allocateColumn();
114:
115: return _row.addColumn(new IntColumn(_row, name));
116: }
117:
118: /**
119: * Adds a long
120: */
121: public Column addLong(String name) {
122: _row.allocateColumn();
123:
124: return _row.addColumn(new LongColumn(_row, name));
125: }
126:
127: /**
128: * Adds a double
129: */
130: public Column addDouble(String name) {
131: _row.allocateColumn();
132:
133: return _row.addColumn(new DoubleColumn(_row, name));
134: }
135:
136: /**
137: * Adds a datetime column
138: */
139: public Column addDateTime(String name) {
140: _row.allocateColumn();
141:
142: return _row.addColumn(new DateColumn(_row, name));
143: }
144:
145: /**
146: * Adds a numeric
147: */
148: public Column addNumeric(String name, int precision, int scale) {
149: _row.allocateColumn();
150:
151: return _row.addColumn(new NumericColumn(_row, name, precision,
152: scale));
153: }
154:
155: /**
156: * Sets the named column as a primary key constraint.
157: */
158: public void setPrimaryKey(String name) throws SQLException {
159: Column column = _row.getColumn(name);
160:
161: if (column == null)
162: throw new SQLException(L
163: .l("`{0}' is not a valid column for primary key",
164: name));
165:
166: column.setUnique();
167: column.setNotNull();
168:
169: //addConstraint(new PrimaryKeySingleColumnConstraint(column));
170: }
171:
172: /**
173: * Sets the named column as not null
174: */
175: public void setNotNull(String name) throws SQLException {
176: Column column = _row.getColumn(name);
177:
178: if (column == null)
179: throw new SQLException(L.l(
180: "`{0}' is not a valid column for NOT NULL", name));
181:
182: column.setNotNull();
183: }
184:
185: /**
186: * Sets the column default
187: */
188: public void setDefault(String name, Expr expr) throws SQLException {
189: Column column = _row.getColumn(name);
190:
191: if (column == null)
192: throw new SQLException(L.l(
193: "`{0}' is not a valid column for DEFAULT", name));
194:
195: column.setDefault(expr);
196: }
197:
198: /**
199: * Sets the named column as unique
200: */
201: public void setUnique(String name) throws SQLException {
202: Column column = _row.getColumn(name);
203:
204: if (column == null)
205: throw new SQLException(L.l(
206: "'{0}' is not a valid column for NOT NULL", name));
207:
208: column.setUnique();
209:
210: // already checked by unique
211: //addConstraint(new UniqueSingleColumnConstraint(column));
212: }
213:
214: /**
215: * Sets the named column as auto-increment
216: */
217: public void setAutoIncrement(String name, int min)
218: throws SQLException {
219: Column column = _row.getColumn(name);
220:
221: if (column == null)
222: throw new SQLException(L.l(
223: "'{0}' is not a valid column for auto_increment",
224: name));
225:
226: column.setAutoIncrement(min);
227: }
228:
229: /**
230: * Sets the array of columns as unique
231: */
232: public void addUnique(ArrayList<String> names) throws SQLException {
233: if (names.size() == 1) {
234: setUnique(names.get(0));
235: return;
236: }
237:
238: ArrayList<Column> columns = new ArrayList<Column>();
239:
240: for (int i = 0; i < names.size(); i++) {
241: String name = names.get(i);
242:
243: Column column = _row.getColumn(name);
244:
245: if (column == null)
246: throw new SQLException(L.l(
247: "`{0}' is not a valid column for UNIQUE", name));
248: }
249:
250: Column[] columnArray = new Column[columns.size()];
251: columns.toArray(columnArray);
252:
253: if (columnArray.length == 1) {
254: columnArray[0].setUnique();
255:
256: //addConstraint(new UniqueSingleColumnConstraint(columnArray[0]));
257: } else
258: addConstraint(new UniqueConstraint(columnArray));
259: }
260:
261: /**
262: * Sets the array of columns as the primary key
263: */
264: public void addPrimaryKey(ArrayList<String> names)
265: throws SQLException {
266: if (names.size() == 1) {
267: setPrimaryKey(names.get(0));
268: return;
269: }
270:
271: ArrayList<Column> columns = new ArrayList<Column>();
272:
273: for (int i = 0; i < names.size(); i++) {
274: String name = names.get(i);
275:
276: Column column = _row.getColumn(name);
277:
278: if (column == null)
279: throw new SQLException(L.l(
280: "`{0}' is not a valid column for PRIMARY KEY",
281: name));
282: }
283:
284: Column[] columnArray = new Column[columns.size()];
285: columns.toArray(columnArray);
286:
287: if (columnArray.length == 1) {
288: columnArray[0].setPrimaryKey(true);
289: //addConstraint(new PrimaryKeySingleColumnConstraint(columnArray[0]));
290: } else
291: addConstraint(new PrimaryKeyConstraint(columnArray));
292: }
293:
294: /**
295: * Adds a constraint.
296: */
297: public void addConstraint(Constraint constraint) {
298: _constraints.add(constraint);
299: }
300:
301: /**
302: * Returns the constraints.
303: */
304: public Constraint[] getConstraints() {
305: Constraint[] constraints = new Constraint[_constraints.size()];
306: _constraints.toArray(constraints);
307:
308: return constraints;
309: }
310:
311: /**
312: * Creates the table.
313: */
314: public void create() throws java.sql.SQLException, IOException {
315: Table table = new Table(_database, _name, _row,
316: getConstraints());
317:
318: table.create();
319: }
320: }
|