diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2008-03-15 02:41:54 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2008-03-15 02:41:54 +0000 |
commit | fbb55b5a66dae1f962e885f5eecf988f11505e4f (patch) | |
tree | 17f0a6f84b370567551f1b6b0c349bcb3a8c48c4 /src | |
parent | 9dd0f931e34aaab52f22347e44128c0defe46ea0 (diff) | |
download | jackcess-fbb55b5a66dae1f962e885f5eecf988f11505e4f.tar.gz jackcess-fbb55b5a66dae1f962e885f5eecf988f11505e4f.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/ColumnBuilder.java | 138 | ||||
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/Database.java | 8 |
2 files changed, 141 insertions, 5 deletions
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 { |