⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaseutil.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    } else {
                        // go through each relation to see if an FK already exists
                        boolean createdConstraints = false;
                        Iterator relations = entity.getRelationsIterator();
                        while (relations.hasNext()) {
                            ModelRelation modelRelation = (ModelRelation) relations.next();
                            if (!"one".equals(modelRelation.getType())) {
                                continue;
                            }

                            String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
                            if (tableIndexList.contains(relConstraintName)) {
                                tableIndexList.remove(relConstraintName);
                            } else {
                                // if not, create one
                                if (Debug.verboseOn()) Debug.logVerbose("No Index " + relConstraintName + " found for entity " + entityName, module);
                                String errMsg = createForeignKeyIndex(entity, modelRelation, datasourceInfo.constraintNameClipLength);
                                if (errMsg != null && errMsg.length() > 0) {
                                    String message = "Could not create foreign key index " + relConstraintName + " for entity \"" + entity.getEntityName() + "\": " + errMsg;
                                    Debug.logError(message, module);
                                    if (messages != null) messages.add(message);
                                } else {
                                    String message = "Created foreign key index " + relConstraintName + " for entity \"" + entity.getEntityName() + "\"";
                                    Debug.logVerbose(message, module);
                                    if (messages != null) messages.add(message);
                                    createdConstraints = true;
                                    numIndicesCreated++;
                                }
                            }
                        }
                        if (createdConstraints) {
                            String message = "Created foreign key index/indices for entity \"" + entity.getEntityName() + "\"";
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }

                    // show foreign key references that exist but are unknown
                    if (tableIndexList != null) {
                        Iterator tableIndexListIter = tableIndexList.iterator();
                        while (tableIndexListIter.hasNext()) {
                            String indexLeft = (String) tableIndexListIter.next();
                            String message = "Unknown Index " + indexLeft + " found in table " + entity.getTableName(datasourceInfo);
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }
                }
            }
            if (Debug.infoOn()) Debug.logInfo("Created " + numIndicesCreated + " indices", module);
        }

        timer.timerString("Finished Checking Entity Database");
    }

    /** Creates a list of ModelEntity objects based on meta data from the database */
    public List induceModelFromDb(Collection messages) {
        // get ALL tables from this database
        TreeSet tableNames = this.getTableNames(messages);

        // get ALL column info, put into hashmap by table name
        Map colInfo = this.getColumnInfo(tableNames, messages);

        // go through each table and make a ModelEntity object, add to list
        // for each entity make corresponding ModelField objects
        // then print out XML for the entities/fields
        List newEntList = new LinkedList();
        
        boolean isCaseSensitive = false;
        DatabaseMetaData dbData = this.getDatabaseMetaData(null, messages);
        if (dbData != null) {
            try {
                isCaseSensitive = dbData.supportsMixedCaseIdentifiers();
            } catch (SQLException e) {
                Debug.logError(e, "Error getting db meta data about case sensitive", module);
            }
        }
        
        // iterate over the table names is alphabetical order
        Iterator tableNamesIter = new TreeSet(colInfo.keySet()).iterator();
        while (tableNamesIter.hasNext()) {
            String tableName = (String) tableNamesIter.next();
            List colList = (ArrayList) colInfo.get(tableName);
            ModelEntity newEntity = new ModelEntity(tableName, colList, modelFieldTypeReader, isCaseSensitive);
            newEntList.add(newEntity);
        }

        return newEntList;
    }
    
    public DatabaseMetaData getDatabaseMetaData(Connection connection, Collection messages) {
        if (connection == null) {
            try {
                connection = getConnection();
            } catch (SQLException sqle) {
                String message = "Unable to esablish a connection with the database... Error was:" + sqle.toString();
                Debug.logError(message, module);
                if (messages != null) messages.add(message);
                return null;
            } catch (GenericEntityException e) {
                String message = "Unable to esablish a connection with the database... Error was:" + e.toString();
                Debug.logError(message, module);
                if (messages != null) messages.add(message);
                return null;
            }
        }

        if (connection == null) {
            String message = "Unable to esablish a connection with the database, no additional information available.";
            Debug.logError(message, module);
            if (messages != null) messages.add(message);
            return null;
        }

        DatabaseMetaData dbData = null;
        try {
            dbData = connection.getMetaData();
        } catch (SQLException sqle) {
            String message = "Unable to get database meta data... Error was:" + sqle.toString();
            Debug.logError(message, module);
            if (messages != null) {
                messages.add(message);
            }
            return null;
        }

        if (dbData == null) {
            Debug.logWarning("Unable to get database meta data; method returned null", module);
        }
        
        return dbData;
    }

    public void printDbMiscData(DatabaseMetaData dbData) {
        if (dbData == null) {
            return;
        }
        // Database Info
        if (Debug.infoOn()) {
            try {
                Debug.logInfo("Database Product Name is " + dbData.getDatabaseProductName(), module);
                Debug.logInfo("Database Product Version is " + dbData.getDatabaseProductVersion(), module);
            } catch (SQLException sqle) {
                Debug.logWarning("Unable to get Database name & version information", module);
            }
        }
        // JDBC Driver Info
        if (Debug.infoOn()) {
            try {
                Debug.logInfo("Database Driver Name is " + dbData.getDriverName(), module);
                Debug.logInfo("Database Driver Version is " + dbData.getDriverVersion(), module);
            } catch (SQLException sqle) {
                Debug.logWarning("Unable to get Driver name & version information", module);
            }
        }
        // Db/Driver support settings
        if (Debug.infoOn()) {
            try {
                Debug.logInfo("Database Setting/Support Information (those with a * should be true):", module);
                Debug.logInfo("- supports transactions    [" + dbData.supportsTransactions() + "]*", module);
                Debug.logInfo("- isolation None           [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_NONE) + "]", module);
                Debug.logInfo("- isolation ReadCommitted  [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED) + "]", module);
                Debug.logInfo("- isolation ReadUncommitted[" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED) + "]", module);
                Debug.logInfo("- isolation RepeatableRead [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ) + "]", module);
                Debug.logInfo("- isolation Serializable   [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE) + "]", module);
                Debug.logInfo("- is case sensitive        [" + dbData.supportsMixedCaseIdentifiers() + "]", module);
                Debug.logInfo("- stores LowerCase         [" + dbData.storesLowerCaseIdentifiers() + "]", module);
                Debug.logInfo("- stores MixedCase         [" + dbData.storesMixedCaseIdentifiers() + "]", module);
                Debug.logInfo("- stores UpperCase         [" + dbData.storesUpperCaseIdentifiers() + "]", module);
                Debug.logInfo("- max table name length    [" + dbData.getMaxTableNameLength() + "]", module);
                Debug.logInfo("- max column name length   [" + dbData.getMaxColumnNameLength() + "]", module);
                Debug.logInfo("- max schema name length   [" + dbData.getMaxSchemaNameLength() + "]", module);
                Debug.logInfo("- concurrent connections   [" + dbData.getMaxConnections() + "]", module);
                Debug.logInfo("- concurrent statements    [" + dbData.getMaxStatements() + "]", module);
                Debug.logInfo("- ANSI SQL92 Entry         [" + dbData.supportsANSI92EntryLevelSQL() + "]", module);
                Debug.logInfo("- ANSI SQL92 Itermediate   [" + dbData.supportsANSI92IntermediateSQL() + "]", module);
                Debug.logInfo("- ANSI SQL92 Full          [" + dbData.supportsANSI92FullSQL() + "]", module);
                Debug.logInfo("- ODBC SQL Grammar Core    [" + dbData.supportsCoreSQLGrammar() + "]", module);
                Debug.logInfo("- ODBC SQL Grammar Extended[" + dbData.supportsExtendedSQLGrammar() + "]", module);
                Debug.logInfo("- ODBC SQL Grammar Minimum [" + dbData.supportsMinimumSQLGrammar() + "]", module);
                Debug.logInfo("- outer joins              [" + dbData.supportsOuterJoins() + "]*", module);
                Debug.logInfo("- limited outer joins      [" + dbData.supportsLimitedOuterJoins() + "]", module);
                Debug.logInfo("- full outer joins         [" + dbData.supportsFullOuterJoins() + "]", module);
                Debug.logInfo("- group by                 [" + dbData.supportsGroupBy() + "]*", module);
                Debug.logInfo("- group by not in select   [" + dbData.supportsGroupByUnrelated() + "]", module);
                Debug.logInfo("- column aliasing          [" + dbData.supportsColumnAliasing() + "]", module);
                Debug.logInfo("- order by not in select   [" + dbData.supportsOrderByUnrelated() + "]", module);
                // this doesn't work in HSQLDB, other databases? Debug.logInfo("- named parameters         [" + dbData.supportsNamedParameters() + "]", module);
                Debug.logInfo("- alter table add column   [" + dbData.supportsAlterTableWithAddColumn() + "]*", module);
                Debug.logInfo("- non-nullable column      [" + dbData.supportsNonNullableColumns() + "]*", module);
            } catch (Exception e) {
                Debug.logWarning(e, "Unable to get misc. support/setting information", module);
            }
        }
    }

    public TreeSet getTableNames(Collection messages) {
        Connection connection = null;

        try {
            connection = getConnection();
        } catch (SQLException sqle) {
            String message = "Unable to esablish a connection with the database... Error was:" + sqle.toString();
            Debug.logError(message, module);
            if (messages != null) messages.add(message);
            return null;
        } catch (GenericEntityException e) {
            String message = "Unable to esablish a connection with the database... Error was:" + e.toString();
            Debug.logError(message, module);
            if (messages != null) messages.add(message);
            return null;
        }

        if (connection == null) {
            String message = "Unable to esablish a connection with the database, no additional information available.";
            Debug.logError(message, module);
            if (messages != null) messages.add(message);
            return null;
        }

        DatabaseMetaData dbData = this.getDatabaseMetaData(connection, messages);
        if (dbData == null) {
            return null;
        }

        printDbMiscData(dbData);
        if (Debug.infoOn()) Debug.logInfo("Getting Table Info From Database", module);

        // get ALL tables from this database
        TreeSet tableNames = new TreeSet();
        ResultSet tableSet = null;

        String lookupSchemaName = null;
        try {
            String[] types = {"TABLE", "VIEW", "ALIAS", "SYNONYM"};
            if (dbData.supportsSchemasInTableDefinitions()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -