1 22 23 28 29 package org.xquark.mapper.dbms; 30 31 import java.sql.Types ; 32 33 import org.xquark.jdbc.typing.ColumnMetaDataImpl; 34 35 40 public class ColumnInfo 41 { 42 private static final String RCSRevision = "$Revision: 1.2 $"; 43 private static final String RCSName = "$Name: $"; 44 45 private ColumnSpec template = null; 46 protected long size = -1; 47 protected int prec = -1; 48 protected int scale = -1; 49 int realisticJDBCType; private StringBuffer columnClause = null; 51 private StringBuffer constraintColumnClause = null; 52 private StringBuffer compactColumnClause = null; 53 54 59 public ColumnInfo(ColumnSpec template, long[] sizeParameters, AbstractConnection dbmsInfo) 60 { 61 this.template = template; 62 size = template.getSize(sizeParameters); 63 prec = template.getPrecision(sizeParameters); 64 scale = template.getScale(sizeParameters); 65 calculateIntegerPrecision(); 66 realisticJDBCType = ColumnMetaDataImpl.findRealisticType(getDataType(), size, dbmsInfo.getTypeMap()); 67 initializeSatements(dbmsInfo); 68 } 69 70 74 public long getSize() 75 { 76 return size; 77 } 78 79 public int getPrecision() 80 { 81 return prec; 82 } 83 84 public int getScale() 85 { 86 return scale; 87 } 88 89 public String getColumnClause() 90 { 91 return columnClause.toString(); 92 } 93 public String getConstraintColumnClause() 94 { 95 return constraintColumnClause.toString(); 96 } 97 public String getCompactColumnClause() 98 { 99 return compactColumnClause.toString(); 100 } 101 102 public String getName() 103 { 104 return template.getName(); 105 } 106 107 public int getDataType() 108 { 109 return template.getDataType(); 110 } 111 112 113 public boolean isPrimaryKey() 114 { 115 return template.isPrimaryKey(); 116 } 117 118 public boolean isNullable() 119 { 120 return template.isNullable(); 121 } 122 123 private void initializeSatements(AbstractConnection dbmsInfo) 127 { 128 columnClause = new StringBuffer (); 129 constraintColumnClause = new StringBuffer (); 130 columnClause = generateCreationString(columnClause, dbmsInfo); 131 compactColumnClause = new StringBuffer (columnClause.toString()); 132 133 if (isPrimaryKey()) 134 { 135 constraintColumnClause.append(getName()); 136 constraintColumnClause.append(","); 137 columnClause.append(" NOT NULL"); 138 compactColumnClause.append(" NOT NULL PRIMARY KEY"); 139 } 140 else if (isNullable()) 141 { 142 columnClause.append(" NULL"); 143 compactColumnClause.append(" NULL"); 144 } 145 else 146 { 147 columnClause.append(" NOT NULL"); 148 compactColumnClause.append(" NOT NULL"); 149 } 150 } 151 public boolean isIndexable(int type, long size, AbstractConnection dbmsInfo) 152 { 153 return realisticJDBCType != Types.LONGVARBINARY 154 && realisticJDBCType != Types.LONGVARCHAR 155 && realisticJDBCType != Types.BLOB 156 && realisticJDBCType != Types.CLOB; 157 } 158 159 163 private StringBuffer generateCreationString(StringBuffer stmt, AbstractConnection dbmsInfo) 164 { 165 166 stmt.append(getName()); 167 168 169 170 stmt.append(" "); 172 stmt.append(ColumnMetaDataImpl.getTypeCreationString( 173 realisticJDBCType, 174 dbmsInfo.getTypeMap(), 175 size, 176 prec, 177 scale 178 )); 179 180 return stmt; 181 } 182 183 184 private void calculateIntegerPrecision() 185 { 186 switch (getDataType()) 187 { 188 case Types.DECIMAL: case Types.NUMERIC: 189 if (scale <= 0) prec = (int)Math.ceil(Math.log(Math.pow(2,prec))/Math.log(10)); 191 break; 192 case Types.BIGINT: 193 prec = 19; 194 break; 195 case Types.INTEGER: 196 prec = 10; 197 break; 198 case Types.SMALLINT: 199 prec = 5; 200 break; 201 case Types.TINYINT: 202 prec = 3; 203 break; 204 case Types.BIT: 205 prec = 1; 206 break; 207 default: 208 } 209 } 210 211 } 212 | Popular Tags |