Search in sources :

Example 11 with DfTableMeta

use of org.dbflute.logic.jdbc.metadata.info.DfTableMeta in project dbflute-core by dbflute.

the class DfSchemaInitializerPostgreSQL method prepareSortedTableList.

// ===================================================================================
// Drop Table
// ==========
@Override
protected List<DfTableMeta> prepareSortedTableList(Connection conn, List<DfTableMeta> viewList, List<DfTableMeta> otherList) {
    // order for inherit tables
    final List<Map<String, String>> resultList = selectInheritList(conn);
    final Set<String> childSet = StringSet.createAsCaseInsensitive();
    for (Map<String, String> elementMap : resultList) {
        childSet.add(elementMap.get("child_name"));
    }
    final Set<String> parentSet = StringSet.createAsCaseInsensitive();
    for (Map<String, String> elementMap : resultList) {
        parentSet.add(elementMap.get("parent_name"));
    }
    final List<DfTableMeta> firstPriorityList = new ArrayList<DfTableMeta>();
    final List<DfTableMeta> secondPriorityList = new ArrayList<DfTableMeta>();
    final List<DfTableMeta> thirdPriorityList = new ArrayList<DfTableMeta>();
    for (DfTableMeta meta : otherList) {
        final String tableDbName = meta.getTableDbName();
        if (childSet.contains(tableDbName)) {
            if (!parentSet.contains(tableDbName)) {
                // both inherited and inherits
                secondPriorityList.add(meta);
            } else {
                // inherits any table
                firstPriorityList.add(meta);
            }
        } else {
            // no inheritance
            thirdPriorityList.add(meta);
        }
    }
    final List<DfTableMeta> sortedList = new ArrayList<DfTableMeta>();
    // should be before dropping reference table
    sortedList.addAll(viewList);
    sortedList.addAll(firstPriorityList);
    sortedList.addAll(secondPriorityList);
    sortedList.addAll(thirdPriorityList);
    return sortedList;
}
Also used : ArrayList(java.util.ArrayList) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta) Map(java.util.Map)

Example 12 with DfTableMeta

use of org.dbflute.logic.jdbc.metadata.info.DfTableMeta in project dbflute-core by dbflute.

the class DfConventionalTakeAsserter method throwTakeFinallyAssertionFailureEmptyTableException.

protected void throwTakeFinallyAssertionFailureEmptyTableException(final List<DfTableMeta> emptyTableList) {
    final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
    br.addNotice("Found the empty table (no-data) after ReplaceSchema.");
    br.addItem("Advice");
    br.addElement("The tables should have at least one record");
    br.addElement("by conventionalTakeAssertMap settings in replaceSchemaMap.dfprop:");
    br.addElement("");
    br.addElement(Srl.indent(2, _dispPropertiesProvider.get()));
    br.addElement("");
    br.addElement("So prepare the data (e.g. tsv or xls) for ReplaceSchema.");
    br.addElement("");
    br.addElement("  playsql");
    br.addElement("    |-data");
    br.addElement("    |   |-common");
    br.addElement("    |   |   |-tsv");
    br.addElement("    |   |   |-xls");
    br.addElement("    |   |-ut");
    br.addElement("    |   |   |-tsv");
    br.addElement("    |   |   |-xls");
    br.addElement("    ...");
    br.addElement("");
    br.addElement("Or adjust dfprop settings, for example,");
    br.addElement("you can except the table if it cannot be help.");
    br.addElement("(Of course, do after you ask your friends developing together)");
    br.addItem("Empty Table");
    for (DfTableMeta tableMeta : emptyTableList) {
        br.addElement(tableMeta.getTableDispName());
    }
    final String msg = br.buildExceptionMessage();
    throw new DfTakeFinallyAssertionFailureEmptyTableException(msg);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) DfTakeFinallyAssertionFailureEmptyTableException(org.dbflute.exception.DfTakeFinallyAssertionFailureEmptyTableException) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta)

Example 13 with DfTableMeta

use of org.dbflute.logic.jdbc.metadata.info.DfTableMeta in project dbflute-core by dbflute.

the class DfTableExtractor method getTableMap.

public Map<String, DfTableMeta> getTableMap(DatabaseMetaData metaData, UnifiedSchema unifiedSchema) throws SQLException {
    final List<DfTableMeta> tableList = getTableList(metaData, unifiedSchema);
    final Map<String, DfTableMeta> map = DfCollectionUtil.newLinkedHashMap();
    for (DfTableMeta tableInfo : tableList) {
        map.put(tableInfo.getTableName(), tableInfo);
    }
    return map;
}
Also used : DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta)

Example 14 with DfTableMeta

use of org.dbflute.logic.jdbc.metadata.info.DfTableMeta in project dbflute-core by dbflute.

the class DfTableExtractor method doGetTableList.

protected List<DfTableMeta> doGetTableList(DatabaseMetaData metaData, UnifiedSchema unifiedSchema) throws SQLException {
    final String[] objectTypes = getRealObjectTypeTargetArray(unifiedSchema);
    final List<DfTableMeta> tableList = new ArrayList<DfTableMeta>();
    ResultSet rs = null;
    try {
        _log.info("...Getting tables:");
        _log.info("  schema = " + unifiedSchema);
        _log.info("  types  = " + DfCollectionUtil.newArrayList(objectTypes));
        final String catalogName = unifiedSchema.getPureCatalog();
        final String schemaName = unifiedSchema.getPureSchema();
        rs = metaData.getTables(catalogName, schemaName, "%", objectTypes);
        while (rs.next()) {
            // /- - - - - - - - - - - - - - - - - - - - - - - - - - - -
            // basically uses getString() because
            // a JDBC driver might return an unexpected accident
            // (other methods are used only when an item can be trust)
            // - - - - - - - - - -/
            final String tableName = rs.getString("TABLE_NAME");
            final String tableType = rs.getString("TABLE_TYPE");
            final String tableCatalog;
            {
                final String plainCatalog = rs.getString("TABLE_CAT");
                if (Srl.is_NotNull_and_NotTrimmedEmpty(plainCatalog)) {
                    // because PostgreSQL returns null
                    tableCatalog = plainCatalog;
                } else {
                    tableCatalog = catalogName;
                }
            }
            final String tableSchema = rs.getString("TABLE_SCHEM");
            final String tableComment = rs.getString("REMARKS");
            // create new original unified schema for this table
            final UnifiedSchema tableUnifiedSchema = createAsDynamicSchema(tableCatalog, tableSchema);
            if (isTableExcept(tableUnifiedSchema, tableName)) {
                _log.info(tableName + " is excepted!");
                continue;
            }
            if (isSystemTableForDBMS(tableName)) {
                _log.info(tableName + " is excepted! {system table}");
                continue;
            }
            final DfTableMeta tableMetaInfo = new DfTableMeta();
            tableMetaInfo.setTableName(tableName);
            tableMetaInfo.setTableType(tableType);
            tableMetaInfo.setUnifiedSchema(tableUnifiedSchema);
            tableMetaInfo.setTableComment(tableComment);
            tableList.add(tableMetaInfo);
        }
    } finally {
        if (rs != null) {
            rs.close();
        }
    }
    return tableList;
}
Also used : UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta)

Example 15 with DfTableMeta

use of org.dbflute.logic.jdbc.metadata.info.DfTableMeta in project dbflute-core by dbflute.

the class DfAbsractDataWriter method prepareTableCaseTranslationIfNeeds.

protected void prepareTableCaseTranslationIfNeeds() {
    if (_columnHandler.isEnableTableCaseTranslation()) {
        return;
    }
    Connection conn = null;
    try {
        conn = _dataSource.getConnection();
        final DatabaseMetaData metaData = conn.getMetaData();
        final List<DfTableMeta> tableList = _tableHandler.getTableList(metaData, _unifiedSchema);
        final List<String> tableNameList = new ArrayList<String>();
        for (DfTableMeta meta : tableList) {
            tableNameList.add(meta.getTableDbName());
        }
        _columnHandler.enableTableCaseTranslation(tableNameList);
    } catch (SQLException e) {
        String msg = "Failed to get meta data of tables.";
        throw new IllegalStateException(msg, e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ignored) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)31 SQLException (java.sql.SQLException)12 Connection (java.sql.Connection)9 DatabaseMetaData (java.sql.DatabaseMetaData)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)7 UnifiedSchema (org.apache.torque.engine.database.model.UnifiedSchema)6 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 SQLFailureException (org.dbflute.exception.SQLFailureException)4 DfPrimaryKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfPrimaryKeyMeta)4 StringKeyMap (org.dbflute.helper.StringKeyMap)3 DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 TypeMap (org.apache.torque.engine.database.model.TypeMap)2