Coverage Report - com.healthmarketscience.jackcess.ColumnBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ColumnBuilder
73%
32/44
75%
9/12
1.429
 
 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  3
     this(name, null);
 56  3
   }
 57  
   
 58  297
   public ColumnBuilder(String name, DataType type) {
 59  297
     _name = name;
 60  297
     _type = type;
 61  297
   }
 62  
 
 63  
   /**
 64  
    * Sets the type for the new column.
 65  
    */
 66  
   public ColumnBuilder setType(DataType type) {
 67  3
     _type = type;
 68  3
     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  3
     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  3
     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  2
     _precision = newPrecision;
 93  2
     return this;
 94  
   }
 95  
 
 96  
   /**
 97  
    * Sets the scale for the new column.
 98  
    */
 99  
   public ColumnBuilder setScale(int newScale) {
 100  2
     _scale = newScale;
 101  2
     return this;
 102  
   }
 103  
 
 104  
   /**
 105  
    * Sets the length (in bytes) for the new column.
 106  
    */
 107  
   public ColumnBuilder setLength(int length) {
 108  14
     _length = length;
 109  14
     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  1
     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  0
     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  1
     _autoNumber = autoNumber;
 131  1
     return this;
 132  
   }
 133  
 
 134  
   /**
 135  
    * Sets whether of not the new column allows unicode compression.
 136  
    */
 137  
   public ColumnBuilder setCompressedUnicode(boolean compressedUnicode) {
 138  0
     _compressedUnicode = compressedUnicode;
 139  0
     return this;
 140  
   }
 141  
 
 142  
   /**
 143  
    * Sets all attributes except name from the given Column template.
 144  
    */
 145  
   public ColumnBuilder setFromColumn(Column template) {
 146  0
     DataType type = template.getType();
 147  0
     setType(type);
 148  0
     setLength(template.getLength());
 149  0
     setAutoNumber(template.isAutoNumber());
 150  0
     if(type.getHasScalePrecision()) {
 151  0
       setScale(template.getScale());
 152  0
       setPrecision(template.getPrecision());
 153  
     }
 154  
     
 155  0
     return this;
 156  
   }
 157  
 
 158  
   /**
 159  
    * Creates a new Column with the currently configured attributes.
 160  
    */
 161  
   public Column toColumn() {
 162  297
     Column col = new Column();
 163  297
     col.setName(_name);
 164  297
     col.setType(_type);
 165  297
     if(_length != null) {
 166  14
       col.setLength(_length.shortValue());
 167  
     }
 168  297
     if(_precision != null) {
 169  2
       col.setPrecision(_precision.byteValue());
 170  
     }
 171  297
     if(_scale != null) {
 172  2
       col.setScale(_scale.byteValue());
 173  
     }
 174  297
     if(_autoNumber) {
 175  1
       col.setAutoNumber(true);
 176  
     }
 177  297
     if(_compressedUnicode != null) {
 178  0
       col.setCompressedUnicode(_compressedUnicode);
 179  
     }
 180  297
     return col;
 181  
   }
 182  
   
 183  
 }