001package com.randomnoun.common.db.to; 002 003import org.apache.log4j.Logger; 004 005/** Holds type information for a table column. 006 */ 007public class TableColumnTO { 008 009 Logger logger = Logger.getLogger(TableColumnTO.class); 010 011 private TableTO table; 012 private String name; 013 private long columnId; 014 private boolean isPrimaryKey; 015 private String dataType; 016 private long dataTypeLength; 017 private long dataTypePrecision; 018 private long dataScale; 019 private boolean nullable; 020 private String defaultValue; 021 private String comments; 022 023 public TableColumnTO(TableTO table, String name, long columnId, boolean isPrimaryKey, String dataType, 024 long dataTypeLength, long dataTypePrecision, long dataScale, 025 boolean nullable, String defaultValue, String comments) 026 { 027 logger.debug("Datatype for '" + table.getName() + "." + name + "' is " + (isPrimaryKey ? "PK " : "") + dataType + " (" + dataTypeLength + ", " + dataTypePrecision + ", " + dataScale + ")"); 028 this.table = table; 029 this.name = name; 030 this.columnId = columnId; 031 this.isPrimaryKey = isPrimaryKey; 032 this.dataType = dataType; 033 this.dataTypeLength = dataTypeLength; 034 this.dataTypePrecision = dataTypePrecision; 035 this.dataScale = dataScale; 036 this.nullable = nullable; 037 this.defaultValue = defaultValue; 038 this.comments = comments; 039 } 040 public TableTO getTable() { return table; } 041 public String getName() { return name; } 042 public long getColumnId() { return columnId; } 043 public boolean isPrimaryKey() { return isPrimaryKey; } 044 public void setPrimaryKey(boolean b) { 045 this.isPrimaryKey = b; 046 } 047 public String getDataType() { return dataType; } 048 public long getDataTypeLength() { return dataTypeLength; } 049 public long getDataTypePrecision() { return dataTypePrecision; } 050 public long getDataScale() { return dataScale; } 051 public boolean getNullable() { return nullable; } 052 public String getDefaultValue() { return defaultValue; } 053 public String getComments() { return comments; } 054 055 056 public String getTypeString() { 057 if (dataScale!=-1 && dataTypePrecision==-1) { 058 throw new IllegalStateException("datatype scale set without having precision set"); 059 } 060 if (dataTypeLength!=-1 && dataTypePrecision!=-1) { 061 throw new IllegalStateException("both datatype length and precision set"); 062 } 063 if (dataTypeLength!=-1) { 064 return dataType + "(" + dataTypeLength + ")"; 065 } else if (dataTypePrecision!=-1) { 066 if (dataScale!=-1) { 067 // @TODO is this the wrong way round ? 068 return dataType + "(" + dataTypePrecision + "," + dataScale + ")"; 069 } else { 070 return dataType + "(" + dataTypePrecision + ")"; 071 } 072 } else { 073 return dataType; 074 } 075 } 076 077 078 079 080}