use of org.apache.ddlutils.model.Column in project siena by mandubian.
the class DdlGenerator method createColumn.
private Column createColumn(Class<?> clazz, Field field, String col) {
Class<?> type = field.getType();
Column column = new Column();
column.setName(col);
int columnType;
if (type == Byte.class || type == Byte.TYPE)
columnType = Types.TINYINT;
else if (type == Short.class || type == Short.TYPE)
columnType = Types.SMALLINT;
else if (type == Integer.class || type == Integer.TYPE)
columnType = Types.INTEGER;
else if (type == Long.class || type == Long.TYPE)
columnType = Types.BIGINT;
else if (// TODO verify
type == Float.class || type == Float.TYPE)
// TODO verify
columnType = Types.FLOAT;
else if (// TODO verify
type == Double.class || type == Double.TYPE)
// TODO verify
columnType = Types.DOUBLE;
else if (type == String.class) {
if (field.getAnnotation(Text.class) != null) {
columnType = Types.LONGVARCHAR;
} else {
columnType = Types.VARCHAR;
Max max = field.getAnnotation(Max.class);
if (max == null) {
// throw new SienaRestrictedApiException(DB, "createColumn", "Field "+field.getName()+" in class "
// +clazz.getName()+" doesn't have a @Max annotation");
// default is 255 chars as in hibernate
column.setSize("255");
} else
column.setSize("" + max.value());
}
} else if (type == Boolean.class || type == Boolean.TYPE)
columnType = Types.BOOLEAN;
else if (type == Date.class) {
if (field.getAnnotation(DateTime.class) != null)
columnType = Types.TIMESTAMP;
else if (field.getAnnotation(Time.class) != null)
columnType = Types.TIME;
else if (field.getAnnotation(SimpleDate.class) != null)
columnType = Types.DATE;
else
columnType = Types.TIMESTAMP;
} else if (type == Json.class) {
columnType = Types.LONGVARCHAR;
} else if (type == byte[].class) {
columnType = Types.BLOB;
} else if (Enum.class.isAssignableFrom(type)) {
// enums are stored as string
columnType = Types.VARCHAR;
Max max = field.getAnnotation(Max.class);
if (max == null)
// fixes by default to this value in order to prevent alter tables every time
column.setSize("" + 255);
else
column.setSize("" + max.value());
} else if (type == BigDecimal.class) {
DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
if (an == null) {
columnType = Types.DECIMAL;
column.setSizeAndScale(19, 2);
} else {
if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
columnType = Types.DECIMAL;
column.setSizeAndScale(an.size(), an.scale());
} else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
columnType = Types.VARCHAR;
// should be an.size+"."+sign
column.setSize((an.size() + 2) + "");
} else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
columnType = Types.DOUBLE;
} else {
columnType = Types.DECIMAL;
column.setSizeAndScale(19, 2);
}
}
} else {
Embedded embedded = field.getAnnotation(Embedded.class);
if (embedded != null) {
if ("h2".equals(DB)) {
columnType = Types.CLOB;
} else {
columnType = Types.LONGVARCHAR;
}
} else if (field.isAnnotationPresent(Polymorphic.class)) {
columnType = Types.BLOB;
} else {
throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + clazz.getName() + "." + field.getName());
}
}
column.setTypeCode(columnType);
return column;
}
use of org.apache.ddlutils.model.Column in project siena by mandubian.
the class DdlGenerator method addTable.
public Table addTable(Class<?> clazz) {
if (Modifier.isAbstract(clazz.getModifiers())) {
return null;
}
Table table = new Table();
ClassInfo info = ClassInfo.getClassInfo(clazz);
table.setName(info.tableName);
table.setType("MyISAM");
database.addTable(table);
Map<String, UniqueIndex> uniques = new HashMap<String, UniqueIndex>();
Map<String, NonUniqueIndex> indexes = new HashMap<String, NonUniqueIndex>();
/* columns */
for (Field field : info.allFields) {
String[] columns = ClassInfo.getColumnNames(field);
boolean notNull = field.getAnnotation(NotNull.class) != null;
Class<?> type = field.getType();
if (!ClassInfo.isModel(type) || (ClassInfo.isModel(type) && ClassInfo.isEmbedded(field))) {
Column column = createColumn(clazz, field, columns[0]);
if (notNull || type.isPrimitive()) {
column.setRequired(true);
if (type.isPrimitive() && !ClassInfo.isId(field)) {
// TODO: add also Boolean, Long, Double,... ?
if (type == Boolean.TYPE) {
column.setDefaultValue("false");
} else {
column.setDefaultValue("0");
}
}
}
Id id = field.getAnnotation(Id.class);
if (id != null) {
column.setPrimaryKey(true);
column.setRequired(true);
// auto_increments managed ONLY for long
if (id.value() == Generator.AUTO_INCREMENT && (Long.TYPE == type || Long.class.isAssignableFrom(type)))
column.setAutoIncrement(true);
// adds index on primary key
/*UniqueIndex i = uniques.get(columns[0]);
if(i == null) {
i = new UniqueIndex();
i.setName(columns[0]);
uniques.put(columns[0], i);
table.addIndex(i);
}
fillIndex(i, field);*/
}
table.addColumn(column);
} else {
List<Field> keys = ClassInfo.getClassInfo(type).keys;
for (int i = 0; i < columns.length; i++) {
Field f = keys.get(i);
Column column = createColumn(clazz, f, columns[i]);
if (notNull)
column.setRequired(true);
table.addColumn(column);
}
}
}
/* indexes */
for (Field field : info.updateFields) {
Index index = field.getAnnotation(Index.class);
if (index != null) {
String[] names = index.value();
for (String name : names) {
NonUniqueIndex i = indexes.get(name);
if (i == null) {
i = new NonUniqueIndex();
i.setName(name);
indexes.put(name, i);
table.addIndex(i);
}
fillIndex(i, field);
}
}
Unique unique = field.getAnnotation(Unique.class);
if (unique != null) {
String[] names = unique.value();
for (String name : names) {
UniqueIndex i = uniques.get(name);
if (i == null) {
i = new UniqueIndex();
i.setName(name);
uniques.put(name, i);
table.addIndex(i);
}
fillIndex(i, field);
}
}
}
tables.put(table.getName(), table);
return table;
}
use of org.apache.ddlutils.model.Column in project otter by alibaba.
the class DbDialectTableTest method testOracleTable.
@Test
public void testOracleTable() {
DbDataMedia oracleMedia = getOracleMedia();
DbDialect dbDialect = dbDialectFactory.getDbDialect(1L, oracleMedia.getSource());
Table table = dbDialect.findTable(oracleMedia.getNamespace(), oracleMedia.getName());
want.object(table).notNull();
System.out.println("tableName = " + table.getName());
Column[] columns = table.getColumns();
for (Column column : columns) {
System.out.println("columnName = " + column.getName() + ",columnType = " + column.getTypeCode() + ",isPrimary = " + column.isPrimaryKey() + ",nullable = " + column.isRequired());
}
}
use of org.apache.ddlutils.model.Column in project otter by alibaba.
the class DataMediaServiceImpl method queryColumnByMedia.
@Override
public List<String> queryColumnByMedia(DataMedia dataMedia) {
List<String> columnResult = new ArrayList<String>();
if (dataMedia.getSource().getType().isNapoli()) {
return columnResult;
}
DataSource dataSource = dataSourceCreator.createDataSource(dataMedia.getSource());
// 针对multi表,直接获取第一个匹配的表结构
String schemaName = dataMedia.getNamespaceMode().getSingleValue();
String tableName = dataMedia.getNameMode().getSingleValue();
try {
Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), schemaName, schemaName, tableName);
for (Column column : table.getColumns()) {
columnResult.add(column.getName());
}
} catch (Exception e) {
logger.error("ERROR ## DdlUtils find table happen error!", e);
}
return columnResult;
}
use of org.apache.ddlutils.model.Column in project otter by alibaba.
the class DdlUtils method readTable.
private static Table readTable(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
String tableName = (String) values.get("TABLE_NAME");
Table table = null;
if ((tableName != null) && (tableName.length() > 0)) {
table = new Table();
table.setName(tableName);
table.setType((String) values.get("TABLE_TYPE"));
table.setCatalog((String) values.get("TABLE_CAT"));
table.setSchema((String) values.get("TABLE_SCHEM"));
table.setDescription((String) values.get("REMARKS"));
table.addColumns(readColumns(metaData, tableName));
Collection<String> primaryKeys = readPrimaryKeyNames(metaData, tableName);
for (Object key : primaryKeys) {
Column col = table.findColumn((String) key, true);
if (col != null) {
col.setPrimaryKey(true);
} else {
throw new NullPointerException(String.format("%s pk %s is null - %s %s", tableName, key, ToStringBuilder.reflectionToString(metaData, ToStringStyle.SIMPLE_STYLE), ToStringBuilder.reflectionToString(values, ToStringStyle.SIMPLE_STYLE)));
}
}
}
return table;
}
Aggregations