--- /dev/null
+// Copyright (c) 2008 Health Market Science, Inc.
+
+package com.healthmarketscience.jackcess;
+
+import java.sql.SQLException;
+
+/**
+ * Builder style class for constructing a Column.
+ *
+ * @author James Ahlborn
+ */
+public class ColumnBuilder {
+
+ /** name of the new column */
+ private String _name;
+ /** the type of the new column */
+ private DataType _type;
+ /** optional length for the new column */
+ private Integer _length;
+ /** optional precision for the new column */
+ private Integer _precision;
+ /** optional scale for the new column */
+ private Integer _scale;
+ /** whether or not the column is auto-number */
+ private boolean _autoNumber;
+
+ public ColumnBuilder(String name) {
+ this(name, null);
+ }
+
+ public ColumnBuilder(String name, DataType type) {
+ _name = name;
+ _type = type;
+ }
+
+ /**
+ * Sets the type for the new column.
+ */
+ public ColumnBuilder setType(DataType type) {
+ _type = type;
+ return this;
+ }
+
+ /**
+ * Sets the type for the new column based on the given SQL type.
+ */
+ public ColumnBuilder setSQLType(int type) throws SQLException {
+ return setSQLType(type, 0);
+ }
+
+ /**
+ * Sets the type for the new column based on the given SQL type and target
+ * data length (in type specific units).
+ */
+ public ColumnBuilder setSQLType(int type, int lengthInUnits)
+ throws SQLException
+ {
+ return setType(DataType.fromSQLType(type, lengthInUnits));
+ }
+
+ /**
+ * Sets the precision for the new column.
+ */
+ public ColumnBuilder setPrecision(int newPrecision) {
+ _precision = newPrecision;
+ return this;
+ }
+
+ /**
+ * Sets the scale for the new column.
+ */
+ public ColumnBuilder setScale(int newScale) {
+ _scale = newScale;
+ return this;
+ }
+
+ /**
+ * Sets the length (in bytes) for the new column.
+ */
+ public ColumnBuilder setLength(int length) {
+ _length = length;
+ return this;
+ }
+
+ /**
+ * Sets the length (in type specific units) for the new column.
+ */
+ public ColumnBuilder setLengthInUnits(int unitLength) {
+ return setLength(_type.getUnitSize() * unitLength);
+ }
+
+ /**
+ * Sets whether of not the new column is an auto-number column.
+ */
+ public ColumnBuilder setAutoNumber(boolean autoNumber) {
+ _autoNumber = autoNumber;
+ return this;
+ }
+
+ /**
+ * Sets all attributes except name from the given Column template.
+ */
+ public ColumnBuilder setFromColumn(Column template) {
+ DataType type = template.getType();
+ setType(type);
+ setLength(template.getLength());
+ setAutoNumber(template.isAutoNumber());
+ if(type.getHasScalePrecision()) {
+ setScale(template.getScale());
+ setPrecision(template.getPrecision());
+ }
+
+ return this;
+ }
+
+ /**
+ * Creates a new Column with the currently configured attributes.
+ */
+ public Column toColumn() {
+ Column col = new Column();
+ col.setName(_name);
+ col.setType(_type);
+ if(_length != null) {
+ col.setLength(_length.shortValue());
+ }
+ if(_precision != null) {
+ col.setPrecision(_precision.byteValue());
+ }
+ if(_scale != null) {
+ col.setScale(_scale.byteValue());
+ }
+ if(_autoNumber) {
+ col.setAutoNumber(true);
+ }
+ return col;
+ }
+
+}
}
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("a");
- col.setType(DataType.MEMO);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("a", DataType.MEMO).toColumn());
try {
db.createTable("test", columns);
}
columns = new ArrayList<Column>();
- col = new Column();
- col.setName("A");
- col.setType(DataType.TEXT);
- col.setLength((short)(352 * DataType.TEXT.getUnitSize()));
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.TEXT)
+ .setLengthInUnits(352).toColumn());
try {
db.createTable("test", columns);
}
columns = new ArrayList<Column>();
- col = new Column();
- col.setName("A");
- col.setType(DataType.TEXT);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.TEXT).toColumn());
db.createTable("test", columns);
try {
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("B");
- col.setType(DataType.MEMO);
- columns.add(col);
- col = new Column();
- col.setName("C");
- col.setType(DataType.OLE);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("B", DataType.MEMO).toColumn());
+ columns.add(new ColumnBuilder("C", DataType.OLE).toColumn());
db.createTable("test", columns);
String testStr = "This is a test";
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.MONEY);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.MONEY).toColumn());
db.createTable("test", columns);
Table table = db.getTable("Test");
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.GUID);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.GUID).toColumn());
db.createTable("test", columns);
Table table = db.getTable("Test");
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.NUMERIC);
- col.setScale((byte)4);
- col.setPrecision((byte)8);
+ Column col = new ColumnBuilder("A", DataType.NUMERIC)
+ .setScale(4).setPrecision(8).toColumn();
columns.add(col);
assertTrue(col.isVariableLength());
- col = new Column();
- col.setName("B");
- col.setType(DataType.NUMERIC);
- col.setScale((byte)8);
- col.setPrecision((byte)28);
- columns.add(col);
+ columns.add(new ColumnBuilder("B", DataType.NUMERIC)
+ .setScale(8).setPrecision(28).toColumn());
db.createTable("test", columns);
Table table = db.getTable("Test");
{
Database db = create();
- Column a = new Column();
- a.setName("a");
- a.setSQLType(Types.INTEGER);
- Column b = new Column();
- b.setName("b");
- b.setSQLType(Types.LONGVARCHAR);
- Column c = new Column();
- c.setName("c");
- c.setSQLType(Types.VARCHAR);
+ Column a = new ColumnBuilder("a").setSQLType(Types.INTEGER).toColumn();
+ Column b = new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR).toColumn();
+ Column c = new ColumnBuilder("c").setSQLType(Types.VARCHAR).toColumn();
db.createTable("NewTable", Arrays.asList(a, b, c));
Table newTable = db.getTable("NewTable");
for(int i = 0; i < numColumns; ++i) {
String colName = "MyColumnName" + i;
colNames.add(colName);
- Column col = new Column();
- col.setName(colName);
- col.setType(DataType.TEXT);
- columns.add(col);
+ columns.add(new ColumnBuilder(colName, DataType.TEXT).toColumn());
}
db.createTable("test", columns);
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("a");
- col.setType(DataType.LONG);
- col.setAutoNumber(true);
- columns.add(col);
- col = new Column();
- col.setName("b");
- col.setType(DataType.TEXT);
- columns.add(col);
+ columns.add(new ColumnBuilder("a", DataType.LONG)
+ .setAutoNumber(true).toColumn());
+ columns.add(new ColumnBuilder("b", DataType.TEXT).toColumn());
db.createTable("test", columns);
Database db = create();
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("name");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("date");
- col.setType(DataType.SHORT_DATE_TIME);
- columns.add(col);
+ columns.add(new ColumnBuilder("name", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("date", DataType.SHORT_DATE_TIME).toColumn());
db.createTable("test", columns);
Table table = db.getTable("test");
static void createTestTable(Database db) throws Exception {
List<Column> columns = new ArrayList<Column>();
- Column col = new Column();
- col.setName("A");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("B");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("C");
- col.setType(DataType.TEXT);
- columns.add(col);
- col = new Column();
- col.setName("D");
- col.setType(DataType.LONG);
- columns.add(col);
- col = new Column();
- col.setName("E");
- col.setType(DataType.BYTE);
- columns.add(col);
- col = new Column();
- col.setName("F");
- col.setType(DataType.DOUBLE);
- columns.add(col);
- col = new Column();
- col.setName("G");
- col.setType(DataType.FLOAT);
- columns.add(col);
- col = new Column();
- col.setName("H");
- col.setType(DataType.INT);
- columns.add(col);
- col = new Column();
- col.setName("I");
- col.setType(DataType.SHORT_DATE_TIME);
- columns.add(col);
+ columns.add(new ColumnBuilder("A", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("B", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("C", DataType.TEXT).toColumn());
+ columns.add(new ColumnBuilder("D", DataType.LONG).toColumn());
+ columns.add(new ColumnBuilder("E", DataType.BYTE).toColumn());
+ columns.add(new ColumnBuilder("F", DataType.DOUBLE).toColumn());
+ columns.add(new ColumnBuilder("G", DataType.FLOAT).toColumn());
+ columns.add(new ColumnBuilder("H", DataType.INT).toColumn());
+ columns.add(new ColumnBuilder("I", DataType.SHORT_DATE_TIME).toColumn());
db.createTable("test", columns);
}