use of org.jumpmind.db.model.Reference in project symmetric-ds by JumpMind.
the class DatabaseXmlAsciiDocBuilder method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Usage: <input_xml_file> <output_asciidoc_file>");
System.exit(-1);
}
Database db = DatabaseXmlUtil.read(new File(args[0]));
PrintWriter out = new PrintWriter(new FileWriter(args[1]));
Table[] tables = db.getTables();
for (Table table : tables) {
out.println("=== " + table.getName().toUpperCase());
out.println();
if (isNotBlank(table.getDescription())) {
out.println(table.getDescription());
}
out.println();
out.print(".");
out.println(table.getName().toUpperCase());
out.println("[cols=\"3,^1,^1,^1,^1,^1,5\"]");
out.println("|===");
out.println();
out.println("|Name|Type|Size|Default|Keys|Not Null|Description");
for (Column column : table.getColumns()) {
out.print("|");
out.print(column.getName().toUpperCase());
out.print("|");
out.print(column.getMappedType());
out.print("|");
out.print(isNotBlank(column.getSize()) ? column.getSize() : " ");
out.print("|");
out.print(isNotBlank(column.getDefaultValue()) ? column.getDefaultValue() : " ");
out.print("|");
if (column.isPrimaryKey()) {
out.print("PK ");
}
ForeignKey[] keys = table.getForeignKeys();
boolean fk = false;
for (ForeignKey foreignKey : keys) {
Reference[] references = foreignKey.getReferences();
for (Reference reference : references) {
if (reference.getLocalColumn().getName().equals(column.getName()) && !fk) {
out.print("FK");
fk = true;
}
}
}
out.print("|");
if (column.isRequired()) {
out.print("X");
}
out.print("|");
out.println(column.getDescription());
}
out.println("|===");
}
out.close();
}
use of org.jumpmind.db.model.Reference in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method findCorrespondingForeignKey.
/**
* Searches in the given table for a corresponding foreign key. If the given
* key has no name, then a foreign key to the same table with the same
* columns in the same order is searched. If the given key has a name, then
* the a corresponding key also needs to have the same name, or no name at
* all, but not a different one.
*
* @param table
* The table to search in
* @param fk
* The original foreign key
* @return The corresponding foreign key if found
*/
protected ForeignKey findCorrespondingForeignKey(Table table, ForeignKey fk) {
boolean caseMatters = delimitedIdentifierModeOn;
boolean checkFkName = (fk.getName() != null) && (fk.getName().length() > 0);
Reference[] refs = fk.getReferences();
ArrayList<Reference> curRefs = new ArrayList<Reference>();
for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) {
ForeignKey curFk = table.getForeignKey(fkIdx);
boolean checkCurFkName = checkFkName && (curFk.getName() != null) && (curFk.getName().length() > 0);
if ((!checkCurFkName || areEqual(fk.getName(), curFk.getName(), caseMatters)) && areEqual(fk.getForeignTableName(), curFk.getForeignTableName(), caseMatters)) {
curRefs.clear();
CollectionUtils.addAll(curRefs, curFk.getReferences());
// the order is not fixed, so we have to take this long way
if (curRefs.size() == refs.length) {
for (int refIdx = 0; refIdx < refs.length; refIdx++) {
boolean found = false;
for (int curRefIdx = 0; !found && (curRefIdx < curRefs.size()); curRefIdx++) {
Reference curRef = curRefs.get(curRefIdx);
if ((caseMatters && refs[refIdx].equals(curRef)) || (!caseMatters && refs[refIdx].equalsIgnoreCase(curRef))) {
curRefs.remove(curRefIdx);
found = true;
}
}
}
if (curRefs.isEmpty()) {
return curFk;
}
}
}
}
return null;
}
use of org.jumpmind.db.model.Reference in project symmetric-ds by JumpMind.
the class DatabaseXmlUtil method write.
public static void write(Table table, Writer output) {
try {
output.write("\t<table name=\"" + StringEscapeUtils.escapeXml(table.getName()) + "\">\n");
for (Column column : table.getColumns()) {
output.write("\t\t<column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
if (column.isPrimaryKey()) {
output.write(" primaryKey=\"" + column.isPrimaryKey() + "\"");
}
if (column.isRequired()) {
output.write(" required=\"" + column.isRequired() + "\"");
}
if (column.getMappedType() != null) {
output.write(" type=\"" + column.getMappedType() + "\"");
}
if (column.getSize() != null) {
output.write(" size=\"" + column.getSize() + "\"");
}
if (column.getDefaultValue() != null) {
output.write(" default=\"" + StringEscapeUtils.escapeXml(column.getDefaultValue()) + "\"");
}
if (column.isAutoIncrement()) {
output.write(" autoIncrement=\"" + column.isAutoIncrement() + "\"");
}
if (column.getJavaName() != null) {
output.write(" javaName=\"" + column.getJavaName() + "\"");
}
if (column.getPlatformColumns() != null && column.getPlatformColumns().size() > 0) {
Collection<PlatformColumn> platformColumns = column.getPlatformColumns().values();
output.write(">\n");
for (PlatformColumn platformColumn : platformColumns) {
output.write("\t\t\t<platform-column name=\"" + platformColumn.getName() + "\"");
output.write(" type=\"" + platformColumn.getType() + "\"");
if (platformColumn.getSize() > 0) {
output.write(" size=\"" + platformColumn.getSize() + "\"");
}
if (platformColumn.getDecimalDigits() > 0) {
output.write(" decimalDigits=\"" + platformColumn.getDecimalDigits() + "\"");
}
if (platformColumn.getDefaultValue() != null) {
output.write(" default=\"" + StringEscapeUtils.escapeXml(platformColumn.getDefaultValue()) + "\"");
}
output.write("/>\n");
}
output.write("\t\t</column>\n");
} else {
output.write("/>\n");
}
}
for (ForeignKey fk : table.getForeignKeys()) {
output.write("\t\t<foreign-key name=\"" + StringEscapeUtils.escapeXml(fk.getName()) + "\" foreignTable=\"" + StringEscapeUtils.escapeXml(fk.getForeignTableName()) + "\">\n");
for (Reference ref : fk.getReferences()) {
output.write("\t\t\t<reference local=\"" + StringEscapeUtils.escapeXml(ref.getLocalColumnName()) + "\" foreign=\"" + StringEscapeUtils.escapeXml(ref.getForeignColumnName()) + "\"/>\n");
}
output.write("\t\t</foreign-key>\n");
}
for (IIndex index : table.getIndices()) {
if (index.isUnique()) {
output.write("\t\t<unique name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
for (IndexColumn column : index.getColumns()) {
output.write("\t\t\t<unique-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"/>\n");
}
output.write("\t\t</unique>\n");
} else {
output.write("\t\t<index name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
for (IndexColumn column : index.getColumns()) {
output.write("\t\t\t<index-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
if (column.getSize() != null) {
output.write(" size=\"" + column.getSize() + "\"");
}
output.write("/>\n");
}
output.write("\t\t</index>\n");
}
}
output.write("\t</table>\n");
} catch (IOException e) {
throw new IoException(e);
}
}
Aggregations