📄 database.java
字号:
package org.apache.ddlutils.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.Serializable;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.ddlutils.dynabean.DynaClassCache;
import org.apache.ddlutils.dynabean.SqlDynaClass;
import org.apache.ddlutils.dynabean.SqlDynaException;
/**
* Represents the database model, ie. the tables in the database. It also
* contains the corresponding dyna classes for creating dyna beans for the
* objects stored in the tables.
*
* @version $Revision: 504014 $
*/
public class Database implements Serializable, Cloneable
{
/** Unique ID for serialization purposes. */
private static final long serialVersionUID = -3160443396757573868L;
/** The name of the database model. */
private String _name;
/** The method for generating primary keys (currently ignored). */
private String _idMethod;
/** The version of the model. */
private String _version;
/** The tables. */
private ArrayList _tables = new ArrayList();
/** The dyna class cache for this model. */
private transient DynaClassCache _dynaClassCache = null;
/**
* Adds all tables from the other database to this database.
* Note that the other database is not changed.
*
* @param otherDb The other database model
*/
public void mergeWith(Database otherDb) throws ModelException
{
for (Iterator it = otherDb._tables.iterator(); it.hasNext();)
{
Table table = (Table)it.next();
if (findTable(table.getName()) != null)
{
// TODO: It might make more sense to log a warning and overwrite the table (or merge them) ?
throw new ModelException("Cannot merge the models because table "+table.getName()+" already defined in this model");
}
try
{
addTable((Table)table.clone());
}
catch (CloneNotSupportedException ex)
{
// won't happen
}
}
}
/**
* Returns the name of this database model.
*
* @return The name
*/
public String getName()
{
return _name;
}
/**
* Sets the name of this database model.
*
* @param name The name
*/
public void setName(String name)
{
_name = name;
}
/**
* Returns the version of this database model.
*
* @return The version
*/
public String getVersion()
{
return _version;
}
/**
* Sets the version of this database model.
*
* @param version The version
*/
public void setVersion(String version)
{
_version = version;
}
/**
* Returns the method for generating primary key values.
*
* @return The method
*/
public String getIdMethod()
{
return _idMethod;
}
/**
* Sets the method for generating primary key values. Note that this
* value is ignored by DdlUtils and only for compatibility with Torque.
*
* @param idMethod The method
*/
public void setIdMethod(String idMethod)
{
_idMethod = idMethod;
}
/**
* Returns the number of tables in this model.
*
* @return The number of tables
*/
public int getTableCount()
{
return _tables.size();
}
/**
* Returns the tables in this model.
*
* @return The tables
*/
public Table[] getTables()
{
return (Table[])_tables.toArray(new Table[_tables.size()]);
}
/**
* Returns the table at the specified position.
*
* @param idx The index of the table
* @return The table
*/
public Table getTable(int idx)
{
return (Table)_tables.get(idx);
}
/**
* Adds a table.
*
* @param table The table to add
*/
public void addTable(Table table)
{
if (table != null)
{
_tables.add(table);
}
}
/**
* Adds a table at the specified position.
*
* @param idx The index where to insert the table
* @param table The table to add
*/
public void addTable(int idx, Table table)
{
if (table != null)
{
_tables.add(idx, table);
}
}
/**
* Adds the given tables.
*
* @param tables The tables to add
*/
public void addTables(Collection tables)
{
for (Iterator it = tables.iterator(); it.hasNext();)
{
addTable((Table)it.next());
}
}
/**
* Removes the given table.
*
* @param table The table to remove
*/
public void removeTable(Table table)
{
if (table != null)
{
_tables.remove(table);
}
}
/**
* Removes the indicated table.
*
* @param idx The index of the table to remove
*/
public void removeTable(int idx)
{
_tables.remove(idx);
}
// Helper methods
/**
* Initializes the model by establishing the relationships between elements in this model encoded
* eg. in foreign keys etc. Also checks that the model elements are valid (table and columns have
* a name, foreign keys rference existing tables etc.)
*/
public void initialize() throws ModelException
{
// we have to setup
// * target tables in foreign keys
// * columns in foreign key references
// * columns in indices
// * columns in uniques
HashSet namesOfProcessedTables = new HashSet();
HashSet namesOfProcessedColumns = new HashSet();
HashSet namesOfProcessedFks = new HashSet();
HashSet namesOfProcessedIndices = new HashSet();
int tableIdx = 0;
if ((getName() == null) || (getName().length() == 0))
{
throw new ModelException("The database model has no name");
}
for (Iterator tableIt = _tables.iterator(); tableIt.hasNext(); tableIdx++)
{
Table curTable = (Table)tableIt.next();
if ((curTable.getName() == null) || (curTable.getName().length() == 0))
{
throw new ModelException("The table nr. "+tableIdx+" has no name");
}
if (namesOfProcessedTables.contains(curTable.getName()))
{
throw new ModelException("There are multiple tables with the name "+curTable.getName());
}
namesOfProcessedTables.add(curTable.getName());
namesOfProcessedColumns.clear();
namesOfProcessedFks.clear();
namesOfProcessedIndices.clear();
for (int idx = 0; idx < curTable.getColumnCount(); idx++)
{
Column column = curTable.getColumn(idx);
if ((column.getName() == null) || (column.getName().length() == 0))
{
throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has no name");
}
if (namesOfProcessedColumns.contains(column.getName()))
{
throw new ModelException("There are multiple column with the name "+column.getName()+" in the table "+curTable.getName());
}
namesOfProcessedColumns.add(column.getName());
if ((column.getType() == null) || (column.getType().length() == 0))
{
throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has no type");
}
if ((column.getTypeCode() == Types.OTHER) && !"OTHER".equalsIgnoreCase(column.getType()))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -