Search in sources :

Example 11 with Reference

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();
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) Reference(org.jumpmind.db.model.Reference) FileWriter(java.io.FileWriter) Database(org.jumpmind.db.model.Database) ForeignKey(org.jumpmind.db.model.ForeignKey) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 12 with Reference

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;
}
Also used : Reference(org.jumpmind.db.model.Reference) ArrayList(java.util.ArrayList) ForeignKey(org.jumpmind.db.model.ForeignKey)

Example 13 with Reference

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);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) Reference(org.jumpmind.db.model.Reference) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) ForeignKey(org.jumpmind.db.model.ForeignKey) PlatformColumn(org.jumpmind.db.model.PlatformColumn) IndexColumn(org.jumpmind.db.model.IndexColumn)

Aggregations

ForeignKey (org.jumpmind.db.model.ForeignKey)13 Reference (org.jumpmind.db.model.Reference)13 Column (org.jumpmind.db.model.Column)6 Table (org.jumpmind.db.model.Table)6 ArrayList (java.util.ArrayList)4 IIndex (org.jumpmind.db.model.IIndex)4 IndexColumn (org.jumpmind.db.model.IndexColumn)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 PlatformColumn (org.jumpmind.db.model.PlatformColumn)2 Row (org.jumpmind.db.sql.Row)2 IoException (org.jumpmind.exception.IoException)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1