in adb2client/src/main/java/com/alibaba/cloud/analyticdb/adbclient/AdbClient.java [668:750]
private void getTableInfo(String schema, List<String> tables, Connection connection) {
if (tables == null) {
throw new RuntimeException("tables is not exist");
}
StringBuilder tablesSb = new StringBuilder();
int i = 1;
for (String table : tables) {
tablesSb.append("'").append(table).append("'");
if (i < tables.size()) {
tablesSb.append(",");
}
i++;
}
Statement statement = null;
ResultSet rs = null;
try {
statement = connection.createStatement();
String columnMetaSql = String.format("select ordinal_position,column_name,data_type,type_name,column_comment, is_nullable, column_default, table_name from information_schema.columns where table_schema = '%s' and table_name in ( %s ) order by ordinal_position",
schema.toLowerCase(),
tablesSb.toString());
rs = statement.executeQuery(columnMetaSql);
Map<String, List<ColumnInfo>> columnInfoListMap = new HashMap<String, List<ColumnInfo>>();
while (rs.next()) {
if (columnInfoListMap.get(rs.getString(8)) == null) {
columnInfoListMap.put(rs.getString(8), new ArrayList<ColumnInfo>());
}
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setOrdinal(rs.getInt(1));
columnInfo.setName(rs.getString(2).toLowerCase());
//for ads version 0.8 & 0.7
columnInfo.setDataType(ColumnDataType.getTypeByName(rs.getString(4).toUpperCase()));
columnInfo.setComment(rs.getString(5));
columnInfo.setNullable(("YES").equals(rs.getString(6)));
columnInfo.setDefaultValue(rs.getString(7));
columnInfoListMap.get(rs.getString(8)).add(columnInfo);
}
for (String table : tables) {
if (columnInfoListMap.get(table) == null || columnInfoListMap.get(table).isEmpty()) {
logger("error", "Table" + table + " is not existed or do not has any column");
}
}
closeDBResources(rs, statement, null);
String tableMetaSql = String.format("select update_type, partition_type, partition_column, partition_count, primary_key_columns, sub_partition_column, table_name from information_schema.tables where table_schema = '%s' and table_name in ( %s )",
schema.toLowerCase(),
tablesSb.toString());
statement = connection.createStatement();
rs = statement.executeQuery(tableMetaSql);
while (rs.next()) {
String t = rs.getString(7);
TableInfo tableInfoTmp = new TableInfo();
tableInfoTmp.setColumns(columnInfoListMap.get(t));
tableInfoTmp.setTableSchema(schema);
tableInfoTmp.setTableName(t);
tableInfoTmp.setUpdateType(rs.getString(1));
tableInfoTmp.setPartitionType(rs.getString(2));
tableInfoTmp.setPartitionColumn(StringUtils.isNotBlank(rs.getString(3)) ? rs.getString(3).toLowerCase() : null);
tableInfoTmp.setPartitionCount(rs.getInt(4));
//primary_key_columns ads pk split by ','
String primaryKeyColumns = rs.getString(5);
if (StringUtils.isNotBlank(primaryKeyColumns)) {
tableInfoTmp.setPrimaryKeyColumns(Arrays.asList(StringUtils.split(primaryKeyColumns.toLowerCase(), ",")));
} else {
tableInfoTmp.setPrimaryKeyColumns(null);
}
tableInfoTmp.setSubPartitionColumn(StringUtils.isNotBlank(rs.getString(6)) ? rs.getString(6).toLowerCase() : null);
this.tableInfo.put(t, tableInfoTmp);
}
closeDBResources(rs, statement, null);
for (String table : tables) {
if (this.tableInfo.get(table) == null) {
logger("error", "Table" + table + " is not existed or do not has any column");
// throw new AdbClientException(AdbClientException.CONFIG_ERROR, "Table" + table + " is not existed or do not has any column", null);
}
}
} catch (Exception e) {
throw new AdbClientException(AdbClientException.CONFIG_ERROR, "GetTableInfo exception: " + e.getMessage(), e);
} finally {
closeDBResources(rs, statement, connection);
}
}