Search in sources :

Example 1 with DatabaseMetaDataWrapper

use of org.apache.ddlutils.platform.DatabaseMetaDataWrapper in project otter by alibaba.

the class DdlUtils method findTable.

public static Table findTable(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableName, final DdlUtilsFilter filter) throws Exception {
    return (Table) jdbcTemplate.execute(new ConnectionCallback() {

        public Object doInConnection(Connection con) throws SQLException, DataAccessException {
            Table table = null;
            DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();
            boolean isDRDS = false;
            try {
                if (filter != null) {
                    con = filter.filterConnection(con);
                    Assert.notNull(con);
                }
                DatabaseMetaData databaseMetaData = con.getMetaData();
                if (filter != null) {
                    databaseMetaData = filter.filterDataBaseMetaData(jdbcTemplate, con, databaseMetaData);
                    Assert.notNull(databaseMetaData);
                }
                String databaseName = databaseMetaData.getDatabaseProductName();
                String version = databaseMetaData.getDatabaseProductVersion();
                if (StringUtils.startsWithIgnoreCase(databaseName, "mysql") && StringUtils.contains(version, "-TDDL-")) {
                    isDRDS = true;
                }
                metaData.setMetaData(databaseMetaData);
                metaData.setTableTypes(TableType.toStrings(SUPPORTED_TABLE_TYPES));
                metaData.setCatalog(catalogName);
                metaData.setSchemaPattern(schemaName);
                String convertTableName = tableName;
                if (databaseMetaData.storesUpperCaseIdentifiers()) {
                    metaData.setCatalog(catalogName.toUpperCase());
                    metaData.setSchemaPattern(schemaName.toUpperCase());
                    convertTableName = tableName.toUpperCase();
                }
                if (databaseMetaData.storesLowerCaseIdentifiers()) {
                    metaData.setCatalog(catalogName.toLowerCase());
                    metaData.setSchemaPattern(schemaName.toLowerCase());
                    convertTableName = tableName.toLowerCase();
                }
                ResultSet tableData = null;
                try {
                    tableData = metaData.getTables(convertTableName);
                    while ((tableData != null) && tableData.next()) {
                        Map<String, Object> values = readColumns(tableData, initColumnsForTable());
                        table = readTable(metaData, values);
                        if (table.getName().equalsIgnoreCase(tableName)) {
                            break;
                        }
                    }
                } finally {
                    JdbcUtils.closeResultSet(tableData);
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            makeAllColumnsPrimaryKeysIfNoPrimaryKeysFound(table);
            if (isDRDS) {
                makeDRDSShardColumnsAsPrimaryKeys(table, jdbcTemplate, catalogName, schemaName, tableName);
            }
            return table;
        }
    });
}
Also used : Table(org.apache.ddlutils.model.Table) DatabaseMetaDataWrapper(org.apache.ddlutils.platform.DatabaseMetaDataWrapper) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException)

Example 2 with DatabaseMetaDataWrapper

use of org.apache.ddlutils.platform.DatabaseMetaDataWrapper in project otter by alibaba.

the class DdlUtils method findTables.

@SuppressWarnings("unchecked")
public static List<Table> findTables(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableNamePattern, final DdlUtilsFilter filter, final DdlTableNameFilter tableNameFilter) throws Exception {
    return (List<Table>) jdbcTemplate.execute(new ConnectionCallback() {

        public Object doInConnection(Connection con) throws SQLException, DataAccessException {
            List<Table> tables = new ArrayList<Table>();
            DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();
            boolean isDRDS = false;
            try {
                if (filter != null) {
                    con = filter.filterConnection(con);
                    Assert.notNull(con);
                }
                DatabaseMetaData databaseMetaData = con.getMetaData();
                if (filter != null) {
                    databaseMetaData = filter.filterDataBaseMetaData(jdbcTemplate, con, databaseMetaData);
                    Assert.notNull(databaseMetaData);
                }
                String databaseName = databaseMetaData.getDatabaseProductName();
                String version = databaseMetaData.getDatabaseProductVersion();
                if (StringUtils.startsWithIgnoreCase(databaseName, "mysql") && StringUtils.contains(version, "-TDDL-")) {
                    isDRDS = true;
                }
                metaData.setMetaData(databaseMetaData);
                metaData.setTableTypes(TableType.toStrings(SUPPORTED_TABLE_TYPES));
                metaData.setCatalog(catalogName);
                metaData.setSchemaPattern(schemaName);
                String convertTableName = tableNamePattern;
                if (databaseMetaData.storesUpperCaseIdentifiers()) {
                    metaData.setCatalog(catalogName.toUpperCase());
                    metaData.setSchemaPattern(schemaName.toUpperCase());
                    convertTableName = tableNamePattern.toUpperCase();
                }
                if (databaseMetaData.storesLowerCaseIdentifiers()) {
                    metaData.setCatalog(catalogName.toLowerCase());
                    metaData.setSchemaPattern(schemaName.toLowerCase());
                    convertTableName = tableNamePattern.toLowerCase();
                }
                ResultSet tableData = null;
                try {
                    tableData = metaData.getTables(convertTableName);
                    while ((tableData != null) && tableData.next()) {
                        Map<String, Object> values = readColumns(tableData, initColumnsForTable());
                        Table table = readTable(metaData, values);
                        if ((tableNameFilter == null) || tableNameFilter.accept(catalogName, schemaName, table.getName())) {
                            tables.add(table);
                        }
                    }
                } finally {
                    JdbcUtils.closeResultSet(tableData);
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            for (Table table : tables) {
                makeAllColumnsPrimaryKeysIfNoPrimaryKeysFound(table);
                if (isDRDS) {
                    makeDRDSShardColumnsAsPrimaryKeys(table, jdbcTemplate, catalogName, schemaName, table.getName());
                }
            }
            return tables;
        }
    });
}
Also used : Table(org.apache.ddlutils.model.Table) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) DatabaseMetaDataWrapper(org.apache.ddlutils.platform.DatabaseMetaDataWrapper) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback)

Aggregations

Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Table (org.apache.ddlutils.model.Table)2 DatabaseMetaDataWrapper (org.apache.ddlutils.platform.DatabaseMetaDataWrapper)2 DataAccessException (org.springframework.dao.DataAccessException)2 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1