View Javadoc

1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17  USA
18  
19  You can contact Health Market Science at info@healthmarketscience.com
20  or at the following address:
21  
22  Health Market Science
23  2700 Horizon Drive
24  Suite 200
25  King of Prussia, PA 19406
26  */
27  
28  package com.healthmarketscience.jackcess;
29  
30  import java.sql.SQLException;
31  
32  /**
33   * Builder style class for constructing a Column.
34   *
35   * @author James Ahlborn
36   */
37  public class ColumnBuilder {
38  
39    /** name of the new column */
40    private String _name;
41    /** the type of the new column */
42    private DataType _type;
43    /** optional length for the new column */
44    private Integer _length;
45    /** optional precision for the new column */
46    private Integer _precision;
47    /** optional scale for the new column */
48    private Integer _scale;
49    /** whether or not the column is auto-number */
50    private boolean _autoNumber;
51    /** whether or not the column allows compressed unicode */
52    private Boolean _compressedUnicode;
53  
54    public ColumnBuilder(String name) {
55      this(name, null);
56    }
57    
58    public ColumnBuilder(String name, DataType type) {
59      _name = name;
60      _type = type;
61    }
62  
63    /**
64     * Sets the type for the new column.
65     */
66    public ColumnBuilder setType(DataType type) {
67      _type = type;
68      return this;
69    }
70  
71    /**
72     * Sets the type for the new column based on the given SQL type.
73     */
74    public ColumnBuilder setSQLType(int type) throws SQLException {
75      return setSQLType(type, 0);
76    }
77    
78    /**
79     * Sets the type for the new column based on the given SQL type and target
80     * data length (in type specific units).
81     */
82    public ColumnBuilder setSQLType(int type, int lengthInUnits)
83      throws SQLException
84    {
85      return setType(DataType.fromSQLType(type, lengthInUnits));
86    }
87  
88    /**
89     * Sets the precision for the new column.
90     */
91    public ColumnBuilder setPrecision(int newPrecision) {
92      _precision = newPrecision;
93      return this;
94    }
95  
96    /**
97     * Sets the scale for the new column.
98     */
99    public ColumnBuilder setScale(int newScale) {
100     _scale = newScale;
101     return this;
102   }
103 
104   /**
105    * Sets the length (in bytes) for the new column.
106    */
107   public ColumnBuilder setLength(int length) {
108     _length = length;
109     return this;
110   }
111 
112   /**
113    * Sets the length (in type specific units) for the new column.
114    */
115   public ColumnBuilder setLengthInUnits(int unitLength) {
116     return setLength(_type.getUnitSize() * unitLength);
117   }
118   
119   /**
120    * Sets the length for the new column to the max length for the type.
121    */
122   public ColumnBuilder setMaxLength() {
123     return setLength(_type.getMaxSize());
124   }
125   
126   /**
127    * Sets whether of not the new column is an auto-number column.
128    */
129   public ColumnBuilder setAutoNumber(boolean autoNumber) {
130     _autoNumber = autoNumber;
131     return this;
132   }
133 
134   /**
135    * Sets whether of not the new column allows unicode compression.
136    */
137   public ColumnBuilder setCompressedUnicode(boolean compressedUnicode) {
138     _compressedUnicode = compressedUnicode;
139     return this;
140   }
141 
142   /**
143    * Sets all attributes except name from the given Column template.
144    */
145   public ColumnBuilder setFromColumn(Column template) {
146     DataType type = template.getType();
147     setType(type);
148     setLength(template.getLength());
149     setAutoNumber(template.isAutoNumber());
150     if(type.getHasScalePrecision()) {
151       setScale(template.getScale());
152       setPrecision(template.getPrecision());
153     }
154     
155     return this;
156   }
157 
158   /**
159    * Creates a new Column with the currently configured attributes.
160    */
161   public Column toColumn() {
162     Column col = new Column();
163     col.setName(_name);
164     col.setType(_type);
165     if(_length != null) {
166       col.setLength(_length.shortValue());
167     }
168     if(_precision != null) {
169       col.setPrecision(_precision.byteValue());
170     }
171     if(_scale != null) {
172       col.setScale(_scale.byteValue());
173     }
174     if(_autoNumber) {
175       col.setAutoNumber(true);
176     }
177     if(_compressedUnicode != null) {
178       col.setCompressedUnicode(_compressedUnicode);
179     }
180     return col;
181   }
182   
183 }