001: // Copyright (c) 2008 Health Market Science, Inc.
002:
003: package com.healthmarketscience.jackcess;
004:
005: import java.sql.SQLException;
006:
007: /**
008: * Builder style class for constructing a Column.
009: *
010: * @author James Ahlborn
011: */
012: public class ColumnBuilder {
013:
014: /** name of the new column */
015: private String _name;
016: /** the type of the new column */
017: private DataType _type;
018: /** optional length for the new column */
019: private Integer _length;
020: /** optional precision for the new column */
021: private Integer _precision;
022: /** optional scale for the new column */
023: private Integer _scale;
024: /** whether or not the column is auto-number */
025: private boolean _autoNumber;
026:
027: public ColumnBuilder(String name) {
028: this (name, null);
029: }
030:
031: public ColumnBuilder(String name, DataType type) {
032: _name = name;
033: _type = type;
034: }
035:
036: /**
037: * Sets the type for the new column.
038: */
039: public ColumnBuilder setType(DataType type) {
040: _type = type;
041: return this ;
042: }
043:
044: /**
045: * Sets the type for the new column based on the given SQL type.
046: */
047: public ColumnBuilder setSQLType(int type) throws SQLException {
048: return setSQLType(type, 0);
049: }
050:
051: /**
052: * Sets the type for the new column based on the given SQL type and target
053: * data length (in type specific units).
054: */
055: public ColumnBuilder setSQLType(int type, int lengthInUnits)
056: throws SQLException {
057: return setType(DataType.fromSQLType(type, lengthInUnits));
058: }
059:
060: /**
061: * Sets the precision for the new column.
062: */
063: public ColumnBuilder setPrecision(int newPrecision) {
064: _precision = newPrecision;
065: return this ;
066: }
067:
068: /**
069: * Sets the scale for the new column.
070: */
071: public ColumnBuilder setScale(int newScale) {
072: _scale = newScale;
073: return this ;
074: }
075:
076: /**
077: * Sets the length (in bytes) for the new column.
078: */
079: public ColumnBuilder setLength(int length) {
080: _length = length;
081: return this ;
082: }
083:
084: /**
085: * Sets the length (in type specific units) for the new column.
086: */
087: public ColumnBuilder setLengthInUnits(int unitLength) {
088: return setLength(_type.getUnitSize() * unitLength);
089: }
090:
091: /**
092: * Sets whether of not the new column is an auto-number column.
093: */
094: public ColumnBuilder setAutoNumber(boolean autoNumber) {
095: _autoNumber = autoNumber;
096: return this ;
097: }
098:
099: /**
100: * Sets all attributes except name from the given Column template.
101: */
102: public ColumnBuilder setFromColumn(Column template) {
103: DataType type = template.getType();
104: setType(type);
105: setLength(template.getLength());
106: setAutoNumber(template.isAutoNumber());
107: if (type.getHasScalePrecision()) {
108: setScale(template.getScale());
109: setPrecision(template.getPrecision());
110: }
111:
112: return this ;
113: }
114:
115: /**
116: * Creates a new Column with the currently configured attributes.
117: */
118: public Column toColumn() {
119: Column col = new Column();
120: col.setName(_name);
121: col.setType(_type);
122: if (_length != null) {
123: col.setLength(_length.shortValue());
124: }
125: if (_precision != null) {
126: col.setPrecision(_precision.byteValue());
127: }
128: if (_scale != null) {
129: col.setScale(_scale.byteValue());
130: }
131: if (_autoNumber) {
132: col.setAutoNumber(true);
133: }
134: return col;
135: }
136:
137: }
|