use of org.apache.ddlutils.model.Table 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;
}
use of org.apache.ddlutils.model.Table 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.Table in project otter by alibaba.
the class AbstractDbDialect method initTables.
// ================================ helper method ==========================
private void initTables(final JdbcTemplate jdbcTemplate) {
// soft引用设置,避免内存爆了
GenericMapMaker mapMaker = null;
mapMaker = new MapMaker().softValues().evictionListener(new MapEvictionListener<List<String>, Table>() {
public void onEviction(List<String> names, Table table) {
logger.warn("Eviction For Table:" + table);
}
});
this.tables = mapMaker.makeComputingMap(new Function<List<String>, Table>() {
public Table apply(List<String> names) {
Assert.isTrue(names.size() == 2);
try {
beforeFindTable(jdbcTemplate, names.get(0), names.get(0), names.get(1));
DdlUtilsFilter filter = getDdlUtilsFilter(jdbcTemplate, names.get(0), names.get(0), names.get(1));
Table table = DdlUtils.findTable(jdbcTemplate, names.get(0), names.get(0), names.get(1), filter);
afterFindTable(table, jdbcTemplate, names.get(0), names.get(0), names.get(1));
if (table == null) {
throw new NestableRuntimeException("no found table [" + names.get(0) + "." + names.get(1) + "] , pls check");
} else {
return table;
}
} catch (Exception e) {
throw new NestableRuntimeException("find table [" + names.get(0) + "." + names.get(1) + "] error", e);
}
}
});
}
use of org.apache.ddlutils.model.Table in project otter by alibaba.
the class DataSourceChecker method checkMap.
public String checkMap(String namespace, String name, Long dataSourceId) {
Connection conn = null;
Statement stmt = null;
DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
DataSource dataSource = null;
try {
DbMediaSource dbMediaSource = (DbMediaSource) source;
dataSource = dataSourceCreator.createDataSource(dbMediaSource);
// conn = dataSource.getConnection();
// if (null == conn) {
// return DATABASE_FAIL;
// }
ModeValue namespaceValue = ConfigHelper.parseMode(namespace);
ModeValue nameValue = ConfigHelper.parseMode(name);
String tempNamespace = namespaceValue.getSingleValue();
String tempName = nameValue.getSingleValue();
try {
Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), tempNamespace, tempNamespace, tempName);
if (table == null) {
return SELECT_FAIL;
}
} catch (SQLException se) {
logger.error("check error!", se);
return SELECT_FAIL;
} catch (Exception e) {
logger.error("check error!", e);
return SELECT_FAIL;
}
// String selectSql = "SELECT * from " + tempNamespace + "." +
// tempName + " where 1 = 0";
// String insertSql = "INSERT INTO " + tempNamespace + "." +
// tempName + " select * from ";
// insertSql += "( SELECT * from " + tempNamespace + "." + tempName
// + ") table2 where 1 = 0";
// String deleteSql = "DELETE from " + tempNamespace + "." +
// tempName + " where 1 = 0";
//
// stmt = conn.createStatement();
//
// try {
// stmt.executeQuery(selectSql);
// } catch (SQLException se) {
// return SELECT_FAIL;
// }
//
// try {
// stmt.execute(insertSql);
// } catch (SQLException se) {
// return INSERT_FAIL;
// }
//
// try {
// stmt.execute(deleteSql);
// } catch (SQLException se) {
// return DELETE_FAIL;
// }
} finally {
closeConnection(conn, stmt);
dataSourceCreator.destroyDataSource(dataSource);
}
return TABLE_SUCCESS;
}
use of org.apache.ddlutils.model.Table in project otter by alibaba.
the class DatabaseExtractor method checkNeedDbForRowMode.
private boolean checkNeedDbForRowMode(Pipeline pipeline, EventData eventData) {
// 获取数据表信息
DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, eventData.getTableId());
DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource());
Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName());
if (table.getColumnCount() == eventData.getColumns().size() + eventData.getKeys().size()) {
return false;
} else {
return true;
}
}
Aggregations