View Javadoc
1   package com.randomnoun.common.db.to;
2   
3   import org.apache.log4j.Logger;
4   
5   /** Holds type information for a table column. 
6    */
7   public class TableColumnTO {
8   	
9   	Logger logger = Logger.getLogger(TableColumnTO.class);
10  	
11  	private TableTO table;
12  	private String name;
13  	private long columnId;
14  	private boolean isPrimaryKey;
15  	private String dataType;
16  	private long dataTypeLength;
17  	private long dataTypePrecision;
18  	private long dataScale;
19  	private boolean nullable;
20  	private String defaultValue;
21  	private String comments;
22  	
23  	public TableColumnTO(TableTO table, String name, long columnId, boolean isPrimaryKey, String dataType,
24  		long dataTypeLength, long dataTypePrecision, long dataScale, 
25  		boolean nullable, String defaultValue, String comments) 
26  	{
27  		logger.debug("Datatype for '" + table.getName() + "." + name + "' is " + (isPrimaryKey ? "PK " : "") + dataType + " (" + dataTypeLength + ", " + dataTypePrecision + ", " + dataScale + ")");  
28  		this.table = table;
29  		this.name = name;
30  		this.columnId = columnId;
31  		this.isPrimaryKey = isPrimaryKey;
32  		this.dataType = dataType;
33  		this.dataTypeLength = dataTypeLength;
34  		this.dataTypePrecision = dataTypePrecision;
35  		this.dataScale = dataScale;
36  		this.nullable = nullable;
37  		this.defaultValue = defaultValue;
38  		this.comments = comments;
39  	}
40  	public TableTO getTable() { return table; }
41  	public String getName() { return name; }
42  	public long   getColumnId() { return columnId; }
43  	public boolean isPrimaryKey() { return isPrimaryKey; }
44  	public void setPrimaryKey(boolean b) {
45  		this.isPrimaryKey = b;
46  	}
47  	public String getDataType() { return dataType; }
48  	public long   getDataTypeLength() { return dataTypeLength; }
49  	public long   getDataTypePrecision() { return dataTypePrecision; }
50  	public long   getDataScale() { return dataScale; }
51  	public boolean getNullable() { return nullable; }
52  	public String getDefaultValue() { return defaultValue; }
53  	public String getComments() { return comments; }
54  	
55  	
56  	public String getTypeString() {
57  		if (dataScale!=-1 && dataTypePrecision==-1) {
58  			throw new IllegalStateException("datatype scale set without having precision set");
59  		}
60  		if (dataTypeLength!=-1 && dataTypePrecision!=-1) {
61  			throw new IllegalStateException("both datatype length and precision set");
62  		}
63  		if (dataTypeLength!=-1) {
64  			return dataType + "(" + dataTypeLength + ")";
65  		} else if (dataTypePrecision!=-1) {
66  			if (dataScale!=-1) {
67  				// @TODO is this the wrong way round ?
68  				return dataType + "(" + dataTypePrecision + "," + dataScale + ")";
69  			} else {
70  				return dataType + "(" + dataTypePrecision + ")";
71  			}
72  		} else {
73  			return dataType;
74  		}
75  	}
76  	
77  	
78  	
79  	
80  }