}
@Override
protected SourceElement createRootElement() throws SourceException
{
SourceElement rootElement = new SourceElement("database");
{
try
{
Class.forName(driver);
}
catch (ClassNotFoundException e)
{
throw new SourceException(
"Could not find database driver class " + driver, e);
}
log.debug("DB driver " + driver + " loaded");
}
Connection con = null;
try
{
con = DriverManager.getConnection(url, username, password);
log.debug("DB connection to database " + url + " established");
DatabaseMetaData dbMetaData = con.getMetaData();
List<String> tableList = getTableNames(dbMetaData, schema);
for (int i = 0; i < tableList.size(); i++)
{
// Add Table.
String tableName = (String) tableList.get(i);
log.debug("Processing table: " + tableName);
SourceElement table = new SourceElement("table");
rootElement.getChildren().add(table);
table.setAttribute("name", tableName);
List<ColumnMetadata> columns
= getColumns(dbMetaData, tableName, schema);
Set<String> primaryKeys
= getPrimaryKeys(dbMetaData, tableName, schema);
for (ColumnMetadata col : columns)
{
String name = col.getName();
Integer type = col.getSqlType();
int size = col.getSize().intValue();
int scale = col.getDecimalDigits().intValue();
Integer nullType = col.getNullType();
String defValue = col.getDefValue();
SourceElement column = new SourceElement("column");
column.setAttribute("name", name);
column.setAttribute(
"type",
SchemaType.getByJdbcType(type).toString());
if (size > 0 && (type.intValue() == Types.CHAR
|| type.intValue() == Types.VARCHAR
|| type.intValue() == Types.LONGVARCHAR
|| type.intValue() == Types.DECIMAL
|| type.intValue() == Types.NUMERIC))
{
column.setAttribute("size", String.valueOf(size));
}
if (scale > 0 && (type.intValue() == Types.DECIMAL
|| type.intValue() == Types.NUMERIC))
{
column.setAttribute("scale", String.valueOf(scale));
}
if (primaryKeys.contains(name))
{
column.setAttribute("primaryKey", "true");
}
else if (nullType.intValue() == 0)
{
column.setAttribute("required", "true");
}
if (StringUtils.isNotEmpty(defValue))
{
// trim out parens & quotes out of def value.
// makes sense for MSSQL. not sure about others.
if (defValue.startsWith("(") && defValue.endsWith(")"))
{
defValue = defValue.substring(1, defValue.length() - 1);
}
if (defValue.startsWith("'") && defValue.endsWith("'"))
{
defValue = defValue.substring(1, defValue.length() - 1);
}
column.setAttribute("default", defValue);
}
table.getChildren().add(column);
}
// Foreign keys for this table.
Collection<ForeignKeyMetadata> forgnKeys
= getForeignKeys(dbMetaData, tableName, schema);
for (ForeignKeyMetadata foreignKeyMetadata : forgnKeys)
{
SourceElement fk = new SourceElement("foreign-key");
fk.setAttribute(
"foreignTable",
foreignKeyMetadata.getReferencedTable());
for (int m = 0; m < foreignKeyMetadata.getLocalColumns().size(); m++)
{
SourceElement ref = new SourceElement("reference");
ref.setAttribute("local", foreignKeyMetadata.getLocalColumns().get(m));
ref.setAttribute("foreign", foreignKeyMetadata.getForeignColumns().get(m));
fk.getChildren().add(ref);
}
table.getChildren().add(fk);
}
}