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

📄 database.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                {
                    throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has an unknown type "+column.getType());
                }
                namesOfProcessedColumns.add(column.getName());
            }

            for (int idx = 0; idx < curTable.getForeignKeyCount(); idx++)
            {
                ForeignKey fk     = curTable.getForeignKey(idx);
                String     fkName = (fk.getName() == null ? "" : fk.getName());
                String     fkDesc = (fkName.length() == 0 ? "nr. " + idx : fkName);

                if (fkName.length() > 0)
                {
                    if (namesOfProcessedFks.contains(fkName))
                    {
                        throw new ModelException("There are multiple foreign keys in table "+curTable.getName()+" with the name "+fkName);
                    }
                    namesOfProcessedFks.add(fkName);
                }

                if (fk.getForeignTable() == null)
                {
                    Table targetTable = findTable(fk.getForeignTableName(), true);

                    if (targetTable == null)
                    {
                        throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined table "+fk.getForeignTableName());
                    }
                    else
                    {
                        fk.setForeignTable(targetTable);
                    }
                }
                for (int refIdx = 0; refIdx < fk.getReferenceCount(); refIdx++)
                {
                    Reference ref = fk.getReference(refIdx);

                    if (ref.getLocalColumn() == null)
                    {
                        Column localColumn = curTable.findColumn(ref.getLocalColumnName(), true);

                        if (localColumn == null)
                        {
                            throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined local column "+ref.getLocalColumnName());
                        }
                        else
                        {
                            ref.setLocalColumn(localColumn);
                        }
                    }
                    if (ref.getForeignColumn() == null)
                    {
                        Column foreignColumn = fk.getForeignTable().findColumn(ref.getForeignColumnName(), true);

                        if (foreignColumn == null)
                        {
                            throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined local column "+ref.getForeignColumnName()+" in table "+fk.getForeignTable().getName());
                        }
                        else
                        {
                            ref.setForeignColumn(foreignColumn);
                        }
                    }
                }
            }

            for (int idx = 0; idx < curTable.getIndexCount(); idx++)
            {
                Index  index     = curTable.getIndex(idx);
                String indexName = (index.getName() == null ? "" : index.getName());
                String indexDesc = (indexName.length() == 0 ? "nr. " + idx : indexName);

                if (indexName.length() > 0)
                {
                    if (namesOfProcessedIndices.contains(indexName))
                    {
                        throw new ModelException("There are multiple indices in table "+curTable.getName()+" with the name "+indexName);
                    }
                    namesOfProcessedIndices.add(indexName);
                }

                for (int indexColumnIdx = 0; indexColumnIdx < index.getColumnCount(); indexColumnIdx++)
                {
                    IndexColumn indexColumn = index.getColumn(indexColumnIdx);
                    Column      column      = curTable.findColumn(indexColumn.getName(), true);

                    if (column == null)
                    {
                        throw new ModelException("The index "+indexDesc+" in table "+curTable.getName()+" references the undefined column "+indexColumn.getName());
                    }
                    else
                    {
                        indexColumn.setColumn(column);
                    }
                }
            }
        }
    }
    
    /**
     * Finds the table with the specified name, using case insensitive matching.
     * Note that this method is not called getTable to avoid introspection
     * problems.
     * 
     * @param name The name of the table to find
     * @return The table or <code>null</code> if there is no such table
     */
    public Table findTable(String name)
    {
        return findTable(name, false);
    }

    /**
     * Finds the table with the specified name, using case insensitive matching.
     * Note that this method is not called getTable) to avoid introspection
     * problems.
     * 
     * @param name          The name of the table to find
     * @param caseSensitive Whether case matters for the names
     * @return The table or <code>null</code> if there is no such table
     */
    public Table findTable(String name, boolean caseSensitive)
    {
        for (Iterator iter = _tables.iterator(); iter.hasNext();)
        {
            Table table = (Table) iter.next();

            if (caseSensitive)
            {
                if (table.getName().equals(name))
                {
                    return table;
                }
            }
            else
            {
                if (table.getName().equalsIgnoreCase(name))
                {
                    return table;
                }
            }
        }
        return null;
    }

    /**
     * Returns the dyna class cache. If none is available yet, a new one will be created.
     * 
     * @return The dyna class cache
     */
    private DynaClassCache getDynaClassCache()
    {
        if (_dynaClassCache == null)
        {
            _dynaClassCache = new DynaClassCache();
        }
        return _dynaClassCache;
    }

    /**
     * Resets the dyna class cache. This should be done for application when a column
     * has been added or removed to a table.
     */
    public void resetDynaClassCache()
    {
        _dynaClassCache = null;
    }
    
    /**
     * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the given table name. If the it does not
     * exist yet, a new one will be created based on the Table definition.
     * 
     * @param tableName The name of the table to create the bean for
     * @return The <code>SqlDynaClass</code> for the indicated table or <code>null</code>
     *         if the model contains no such table
     */
    public SqlDynaClass getDynaClassFor(String tableName)
    {
        Table table = findTable(tableName);

        return table != null ? getDynaClassCache().getDynaClass(table) : null;
    }

    /**
     * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the given dyna bean.
     * 
     * @param bean The dyna bean
     * @return The <code>SqlDynaClass</code> for the given bean
     */
    public SqlDynaClass getDynaClassFor(DynaBean bean)
    {
        return getDynaClassCache().getDynaClass(bean);
    }

    /**
     * Creates a new dyna bean for the given table.
     * 
     * @param table The table to create the bean for
     * @return The new dyna bean
     */
    public DynaBean createDynaBeanFor(Table table) throws SqlDynaException
    {
        return getDynaClassCache().createNewInstance(table);
    }

    /**
     * Convenience method that combines {@link #createDynaBeanFor(Table)} and
     * {@link #findTable(String, boolean)}.
     * 
     * @param tableName     The name of the table to create the bean for
     * @param caseSensitive Whether case matters for the names
     * @return The new dyna bean
     */
    public DynaBean createDynaBeanFor(String tableName, boolean caseSensitive) throws SqlDynaException
    {
        return getDynaClassCache().createNewInstance(findTable(tableName, caseSensitive));
    }

    /**
     * {@inheritDoc}
     */
    public Object clone() throws CloneNotSupportedException
    {
        Database result = (Database)super.clone();

        result._name     = _name;
        result._idMethod = _idMethod;
        result._version  = _version;
        result._tables   = (ArrayList)_tables.clone();

        return result;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object obj)
    {
        if (obj instanceof Database)
        {
            Database other = (Database)obj;

            // Note that this compares case sensitive
            return new EqualsBuilder().append(_name,   other._name)
                                      .append(_tables, other._tables)
                                      .isEquals();
        }
        else
        {
            return false;
        }
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode()
    {
        return new HashCodeBuilder(17, 37).append(_name)
                                          .append(_tables)
                                          .toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    public String toString()
    {
        StringBuffer result = new StringBuffer();

        result.append("Database [name=");
        result.append(getName());
        result.append("; ");
        result.append(getTableCount());
        result.append(" tables]");

        return result.toString();
    }

    /**
     * Returns a verbose string representation of this database.
     * 
     * @return The string representation
     */
    public String toVerboseString()
    {
        StringBuffer result = new StringBuffer();

        result.append("Database [");
        result.append(getName());
        result.append("] tables:");
        for (int idx = 0; idx < getTableCount(); idx++)
        {
            result.append(" ");
            result.append(getTable(idx).toVerboseString());
        }

        return result.toString();
    }
}

⌨️ 快捷键说明

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