📄 dumpmetadatatask.java
字号:
{
addArrayProperty(element, name, (Object[])value);
}
else if (value.getClass().isPrimitive() || (value instanceof String))
{
element.addAttribute(name, value.toString());
}
else if (value instanceof ResultSet)
{
addResultSetProperty(element, name, (ResultSet)value);
}
}
}
/**
* Adds a property to the given XML element that is represented as an array.
*
* @param element The XML element
* @param name The name of the property
* @param values The values of the property
*/
private void addArrayProperty(Element element, String name, Object[] values)
{
String propName = name;
if (propName.endsWith("s"))
{
propName = propName.substring(0, propName.length() - 1);
}
Element arrayElem = element.addElement(propName + "s");
for (int idx = 0; idx < values.length; idx++)
{
addProperty(arrayElem, "value", values[idx]);
}
}
/**
* Adds a property to the given XML element that is represented as a result set.
*
* @param element The XML element
* @param name The name of the property
* @param result The values of the property as a result set
*/
private void addResultSetProperty(Element element, String name, ResultSet result)
{
try
{
String propName = name;
if (propName.endsWith("s"))
{
propName = propName.substring(0, propName.length() - 1);
}
Element resultSetElem = element.addElement(propName + "s");
ResultSetMetaData metaData = result.getMetaData();
while (result.next())
{
Element curRow = resultSetElem.addElement(propName);
for (int idx = 1; idx <= metaData.getColumnCount(); idx++)
{
Object value = result.getObject(idx);
addProperty(curRow, metaData.getColumnLabel(idx), value);
}
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
/**
* Derives the property name from the given method name.
*
* @param methodName The method name
* @return The property name
*/
private String getPropertyName(String methodName)
{
if (methodName.startsWith("get"))
{
if (Character.isLowerCase(methodName.charAt(4)))
{
return Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
}
else
{
return methodName.substring(3);
}
}
else if (methodName.startsWith("is"))
{
if (Character.isLowerCase(methodName.charAt(3)))
{
return Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3);
}
else
{
return methodName.substring(2);
}
}
else
{
return methodName;
}
}
/**
* Dumps the catalogs and schemas of the database.
*
* @param parent The parent element
* @param metaData The database meta data
*/
private void dumpCatalogsAndSchemas(Element parent, DatabaseMetaData metaData) throws SQLException
{
// Next we determine and dump the catalogs
Element catalogsElem = parent.addElement("catalogs");
ResultSet result = metaData.getCatalogs();
try
{
while (result.next())
{
String catalogName = getString(result, "TABLE_CAT");
if ((catalogName != null) && (catalogName.length() > 0))
{
Element catalogElem = catalogsElem.addElement("catalog");
catalogElem.addAttribute("name", catalogName);
}
}
}
finally
{
if (result != null)
{
result.close();
}
}
Element schemasElem = parent.addElement("schemas");
// We also dump the schemas (some dbs only support one of the two)
result = metaData.getSchemas();
try
{
while (result.next())
{
String schemaName = getString(result, "TABLE_SCHEM");
if ((schemaName != null) && (schemaName.length() > 0))
{
Element schemaElem = schemasElem.addElement("schema");
schemaElem.addAttribute("name", schemaName);
}
}
}
finally
{
if (result != null)
{
result.close();
}
}
}
/**
* Dumps all tables.
*
* @param parent The parent element
* @param metaData The database metadata
*/
private void dumpTables(Element parent, DatabaseMetaData metaData) throws SQLException
{
String[] tableTypes = _tableTypes;
ResultSet result = null;
if ((tableTypes == null) || (tableTypes.length == 0))
{
// First we need the list of supported table types
ArrayList tableTypeList = new ArrayList();
result = metaData.getTableTypes();
try
{
while (result.next())
{
tableTypeList.add(getString(result, "TABLE_TYPE"));
}
}
finally
{
if (result != null)
{
result.close();
}
}
tableTypes = (String[])tableTypeList.toArray(new String[tableTypeList.size()]);
}
try
{
result = metaData.getTables(_catalogPattern, _schemaPattern, _tablePattern, tableTypes);
}
catch (SQLException ex)
{
log("Could not determine the tables: "+ex.getMessage(), Project.MSG_ERR);
return;
}
Element tablesElem = parent.addElement("tables");
Set columns = getColumnsInResultSet(result);
try
{
while (result.next())
{
String tableName = getString(result, "TABLE_NAME");
if ((tableName == null) || (tableName.length() == 0))
{
continue;
}
Element tableElem = tablesElem.addElement("table");
String catalog = getString(result, "TABLE_CAT");
String schema = getString(result, "TABLE_SCHEM");
log("Reading table " + ((schema != null) && (schema.length() > 0) ? schema + "." : "") + tableName, Project.MSG_INFO);
tableElem.addAttribute("name", tableName);
if (catalog != null)
{
tableElem.addAttribute("catalog", catalog);
}
if (schema != null)
{
tableElem.addAttribute("schema", schema);
}
addStringAttribute(result, columns, "TABLE_TYPE", tableElem, "type");
addStringAttribute(result, columns, "REMARKS", tableElem, "remarks");
addStringAttribute(result, columns, "TYPE_NAME", tableElem, "typeName");
addStringAttribute(result, columns, "TYPE_CAT", tableElem, "typeCatalog");
addStringAttribute(result, columns, "TYPE_SCHEM", tableElem, "typeSchema");
addStringAttribute(result, columns, "SELF_REFERENCING_COL_NAME", tableElem, "identifierColumn");
addStringAttribute(result, columns, "REF_GENERATION", tableElem, "identifierGeneration");
dumpColumns(tableElem, metaData, catalog, schema, tableName);
dumpPKs(tableElem, metaData, catalog, schema, tableName);
dumpVersionColumns(tableElem, metaData, catalog, schema, tableName);
dumpFKs(tableElem, metaData, catalog, schema, tableName);
dumpIndices(tableElem, metaData, catalog, schema, tableName);
}
}
finally
{
if (result != null)
{
result.close();
}
}
}
/**
* Dumps the columns of the indicated table.
*
* @param tableElem The XML element for the table
* @param metaData The database metadata
* @param catalogName The catalog name
* @param schemaName The schema name
* @param tableName The table name
*/
private void dumpColumns(Element tableElem, DatabaseMetaData metaData, String catalogName, String schemaName, String tableName) throws SQLException
{
ResultSet result = null;
try
{
result = metaData.getColumns(catalogName, schemaName, tableName, _columnPattern);
}
catch (SQLException ex)
{
log("Could not determine the columns for table '"+tableName+"': "+ex.getMessage(), Project.MSG_ERR);
return;
}
Set columns = getColumnsInResultSet(result);
try
{
while (result.next())
{
String columnName = getString(result, "COLUMN_NAME");
if ((columnName == null) || (columnName.length() == 0))
{
continue;
}
Element columnElem = tableElem.addElement("column");
columnElem.addAttribute("name", columnName);
addIntAttribute(result, columns, "DATA_TYPE", columnElem, "typeCode");
addStringAttribute(result, columns, "TYPE_NAME", columnElem, "type");
addIntAttribute(result, columns, "COLUMN_SIZE", columnElem, "size");
addIntAttribute(result, columns, "DECIMAL_DIGITS", columnElem, "digits");
addIntAttribute(result, columns, "NUM_PREC_RADIX", columnElem, "precision");
if (columns.contains("NULLABLE"))
{
switch (result.getInt("NULLABLE"))
{
case DatabaseMetaData.columnNoNulls:
columnElem.addAttribute("nullable", "false");
break;
case DatabaseMetaData.columnNullable:
columnElem.addAttribute("nullable", "true");
break;
default:
columnElem.addAttribute("nullable", "unknown");
break;
}
}
addStringAttribute(result, columns, "REMARKS", columnElem, "remarks");
addStringAttribute(result, columns, "COLUMN_DEF", columnElem, "defaultValue");
addIntAttribute(result, columns, "CHAR_OCTET_LENGTH", columnElem, "maxByteLength");
addIntAttribute(result, columns, "ORDINAL_POSITION", columnElem, "index");
if (columns.contains("IS_NULLABLE"))
{
String value = getString(result, "IS_NULLABLE");
if ("no".equalsIgnoreCase(value))
{
columnElem.addAttribute("isNullable", "false");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -