From: James Ahlborn Date: Sat, 15 Mar 2008 02:41:54 +0000 (+0000) Subject: add ColumnBuilder utility for simplifying table construction X-Git-Tag: rel_1_1_13~16 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fbb55b5a66dae1f962e885f5eecf988f11505e4f;p=jackcess.git add ColumnBuilder utility for simplifying table construction git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@276 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java b/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java new file mode 100644 index 0000000..860e438 --- /dev/null +++ b/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java @@ -0,0 +1,138 @@ +// 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; + } + +} diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index c6b5a0e..123445f 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -833,11 +833,9 @@ public class Database String[] columnNames = line.split(delim); for (int i = 0; i < columnNames.length; i++) { - Column column = new Column(); - column.setName(escape(columnNames[i])); - column.setType(DataType.TEXT); - column.setLength((short)DataType.TEXT.getMaxSize()); - columns.add(column); + columns.add(new ColumnBuilder(escape(columnNames[i]), DataType.TEXT) + .setLength((short)DataType.TEXT.getMaxSize()) + .toColumn()); } try { diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java index 531805a..f001a79 100644 --- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java @@ -74,14 +74,8 @@ public class CursorTest extends TestCase { Database db = create(); List columns = new ArrayList(); - Column col = new Column(); - col.setName("id"); - col.setType(DataType.LONG); - columns.add(col); - col = new Column(); - col.setName("value"); - col.setType(DataType.TEXT); - columns.add(col); + columns.add(new ColumnBuilder("id", DataType.LONG).toColumn()); + columns.add(new ColumnBuilder("value", DataType.TEXT).toColumn()); db.createTable("test", columns); Table table = db.getTable("test"); diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index d77f54a..814e162 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -100,14 +100,8 @@ public class DatabaseTest extends TestCase { } List columns = new ArrayList(); - 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); @@ -117,11 +111,8 @@ public class DatabaseTest extends TestCase { } columns = new ArrayList(); - 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); @@ -131,10 +122,7 @@ public class DatabaseTest extends TestCase { } columns = new ArrayList(); - 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 { @@ -354,18 +342,9 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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"; @@ -442,10 +421,7 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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"); @@ -480,10 +456,7 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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"); @@ -522,20 +495,13 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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"); @@ -658,15 +624,9 @@ public class DatabaseTest extends TestCase { { 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"); @@ -712,10 +672,7 @@ public class DatabaseTest extends TestCase { 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); @@ -742,15 +699,9 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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); @@ -793,14 +744,8 @@ public class DatabaseTest extends TestCase { Database db = create(); List columns = new ArrayList(); - 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"); @@ -865,42 +810,15 @@ public class DatabaseTest extends TestCase { static void createTestTable(Database db) throws Exception { List columns = new ArrayList(); - 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); } diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java index f54109e..907d0f4 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java @@ -143,14 +143,8 @@ public class IndexCodesTest extends TestCase { Database db = create(true); List columns = new ArrayList(); - Column col = new Column(); - col.setName("row"); - col.setType(DataType.TEXT); - columns.add(col); - col = new Column(); - col.setName("data"); - col.setType(DataType.TEXT); - columns.add(col); + columns.add(new ColumnBuilder("row", DataType.TEXT).toColumn()); + columns.add(new ColumnBuilder("data", DataType.TEXT).toColumn()); db.createTable("test", columns);