📄 jdbcmodelreader.java
字号:
* @param tableName The name of the table from which to retrieve PK information
* @return The primary key column names
*/
protected Collection readPrimaryKeyNames(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException
{
List pks = new ArrayList();
ResultSet pkData = null;
try
{
pkData = metaData.getPrimaryKeys(tableName);
while (pkData.next())
{
Map values = readColumns(pkData, getColumnsForPK());
pks.add(readPrimaryKeyName(metaData, values));
}
}
finally
{
if (pkData != null)
{
pkData.close();
}
}
return pks;
}
/**
* Extracts a primary key name from the result set.
*
* @param metaData The database meta data
* @param values The primary key meta data values as defined by {@link #getColumnsForPK()}
* @return The primary key name
*/
protected String readPrimaryKeyName(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
return (String)values.get("COLUMN_NAME");
}
/**
* Retrieves the foreign keys of the indicated table.
*
* @param metaData The database meta data
* @param tableName The name of the table from which to retrieve FK information
* @return The foreign keys
*/
protected Collection readForeignKeys(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException
{
Map fks = new ListOrderedMap();
ResultSet fkData = null;
try
{
fkData = metaData.getForeignKeys(tableName);
while (fkData.next())
{
Map values = readColumns(fkData, getColumnsForFK());
readForeignKey(metaData, values, fks);
}
}
finally
{
if (fkData != null)
{
fkData.close();
}
}
return fks.values();
}
/**
* Reads the next foreign key spec from the result set.
*
* @param metaData The database meta data
* @param values The foreign key meta data as defined by {@link #getColumnsForFK()}
* @param knownFks The already read foreign keys for the current table
*/
protected void readForeignKey(DatabaseMetaDataWrapper metaData, Map values, Map knownFks) throws SQLException
{
String fkName = (String)values.get("FK_NAME");
ForeignKey fk = (ForeignKey)knownFks.get(fkName);
if (fk == null)
{
fk = new ForeignKey(fkName);
fk.setForeignTableName((String)values.get("PKTABLE_NAME"));
knownFks.put(fkName, fk);
}
Reference ref = new Reference();
ref.setForeignColumnName((String)values.get("PKCOLUMN_NAME"));
ref.setLocalColumnName((String)values.get("FKCOLUMN_NAME"));
if (values.containsKey("KEY_SEQ"))
{
ref.setSequenceValue(((Short)values.get("KEY_SEQ")).intValue());
}
fk.addReference(ref);
}
/**
* Determines the indices for the indicated table.
*
* @param metaData The database meta data
* @param tableName The name of the table
* @return The list of indices
*/
protected Collection readIndices(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException
{
Map indices = new ListOrderedMap();
ResultSet indexData = null;
try
{
indexData = metaData.getIndices(tableName, false, false);
while (indexData.next())
{
Map values = readColumns(indexData, getColumnsForIndex());
readIndex(metaData, values, indices);
}
}
finally
{
if (indexData != null)
{
indexData.close();
}
}
return indices.values();
}
/**
* Reads the next index spec from the result set.
*
* @param metaData The database meta data
* @param values The index meta data as defined by {@link #getColumnsForIndex()}
* @param knownIndices The already read indices for the current table
*/
protected void readIndex(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) throws SQLException
{
Short indexType = (Short)values.get("TYPE");
// we're ignoring statistic indices
if ((indexType != null) && (indexType.shortValue() == DatabaseMetaData.tableIndexStatistic))
{
return;
}
String indexName = (String)values.get("INDEX_NAME");
if (indexName != null)
{
Index index = (Index)knownIndices.get(indexName);
if (index == null)
{
if (((Boolean)values.get("NON_UNIQUE")).booleanValue())
{
index = new NonUniqueIndex();
}
else
{
index = new UniqueIndex();
}
index.setName(indexName);
knownIndices.put(indexName, index);
}
IndexColumn indexColumn = new IndexColumn();
indexColumn.setName((String)values.get("COLUMN_NAME"));
if (values.containsKey("ORDINAL_POSITION"))
{
indexColumn.setOrdinalPosition(((Short)values.get("ORDINAL_POSITION")).intValue());
}
index.addColumn(indexColumn);
}
}
/**
* Reads the indicated columns from the result set.
*
* @param resultSet The result set
* @param columnDescriptors The dscriptors of the columns to read
* @return The read values keyed by the column name
*/
protected Map readColumns(ResultSet resultSet, List columnDescriptors) throws SQLException
{
HashMap values = new HashMap();
for (Iterator it = columnDescriptors.iterator(); it.hasNext();)
{
MetaDataColumnDescriptor descriptor = (MetaDataColumnDescriptor)it.next();
values.put(descriptor.getName(), descriptor.readColumn(resultSet));
}
return values;
}
/**
* Helper method that determines the auto increment status for the given columns via the
* {@link ResultSetMetaData#isAutoIncrement(int)} method.
*
* @param table The table
* @param columnsToCheck The columns to check (e.g. the primary key columns)
*/
protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck) throws SQLException
{
if (columnsToCheck == null || columnsToCheck.length == 0)
{
return;
}
StringBuffer query = new StringBuffer();
query.append("SELECT ");
for (int idx = 0; idx < columnsToCheck.length; idx++)
{
if (idx > 0)
{
query.append(",");
}
if (getPlatform().isDelimitedIdentifierModeOn())
{
query.append(getPlatformInfo().getDelimiterToken());
}
query.append(columnsToCheck[idx].getName());
if (getPlatform().isDelimitedIdentifierModeOn())
{
query.append(getPlatformInfo().getDelimiterToken());
}
}
query.append(" FROM ");
if (getPlatform().isDelimitedIdentifierModeOn())
{
query.append(getPlatformInfo().getDelimiterToken());
}
query.append(table.getName());
if (getPlatform().isDelimitedIdentifierModeOn())
{
query.append(getPlatformInfo().getDelimiterToken());
}
query.append(" WHERE 1 = 0");
Statement stmt = null;
try
{
stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery(query.toString());
ResultSetMetaData rsMetaData = rs.getMetaData();
for (int idx = 0; idx < columnsToCheck.length; idx++)
{
if (rsMetaData.isAutoIncrement(idx + 1))
{
columnsToCheck[idx].setAutoIncrement(true);
}
}
}
finally
{
if (stmt != null)
{
stmt.close();
}
}
}
/**
* Sorts the foreign keys in the tables of the model.
*
* @param model The model
*/
protected void sortForeignKeys(Database model)
{
for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
{
model.getTable(tableIdx).sortForeignKeys(getPlatform().isDelimitedIdentifierModeOn());
}
}
/**
* Replaces a specific character sequence in the given text with the character sequence
* whose escaped version it is.
*
* @param text The text
* @param unescaped The unescaped string, e.g. "'"
* @param escaped The escaped version, e.g. "''"
* @return The resulting text
*/
protected String unescape(String text, String unescaped, String escaped)
{
String result = text;
// we need special handling if the single quote is escaped via a double single quote
if (result != null)
{
if (escaped.equals("''"))
{
if ((result.length() > 2) && result.startsWith("'") && result.endsWith("'"))
{
result = "'" + StringUtils.replace(result.substring(1, result.length() - 1), escaped, unescaped) + "'";
}
else
{
result = StringUtils.replace(result, escaped, unescaped);
}
}
else
{
result = StringUtils.replace(result, escaped, unescaped);
}
}
return result;
}
/**
* Tries to find the schema to which the given table belongs.
*
* @param connection The database connection
* @param schemaPattern The schema pattern to limit the schemas to search in
* @param table The table to search for
* @return The schema name or <code>null</code> if the schema of the table
* could not be found
* @deprecated Will be removed once full schema support is in place
*/
public String determineSchemaOf(Connection connection, String schemaPattern, Table table) throws SQLException
{
ResultSet tableData = null;
ResultSet columnData = null;
try
{
DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();
metaData.setMetaData(connection.getMetaData());
metaData.setCatalog(getDefaultCatalogPattern());
metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() : schemaPattern);
metaData.setTableTypes(getDefaultTableTypes());
String tablePattern = table.getName();
if (getPlatform().isDelimitedIdentifierModeOn())
{
tablePattern = tablePattern.toUpperCase();
}
tableData = metaData.getTables(tablePattern);
boolean found = false;
String schema = null;
while (!found && tableData.next())
{
Map values = readColumns(tableData, getColumnsForTable());
String tableName = (String)values.get("TABLE_NAME");
if ((tableName != null) && (tableName.length() > 0))
{
schema = (String)values.get("TABLE_SCHEM");
columnData = metaData.getColumns(tableName, getDefaultColumnPattern());
found = true;
while (found && columnData.next())
{
values = readColumns(columnData, getColumnsForColumn());
if (table.findColumn((String)values.get("COLUMN_NAME"),
getPlatform().isDelimitedIdentifierModeOn()) == null)
{
found = false;
}
}
columnData.close();
columnData = null;
}
}
return found ? schema : null;
}
finally
{
if (columnData != null)
{
columnData.close();
}
if (tableData != null)
{
tableData.close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -