Search in sources :

Example 1 with VoltXMLDiff

use of org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff in project voltdb by VoltDB.

the class TestVoltXMLElement method testDupeChild.

public void testDupeChild() {
    VoltXMLElement first = makeNamedElement("element", "element");
    VoltXMLElement child1 = new VoltXMLElement("child");
    child1.attributes.put("value", "3");
    first.children.add(child1);
    // Same element name, no attribute "name"
    VoltXMLElement child2 = new VoltXMLElement("child");
    child2.attributes.put("value", "4");
    first.children.add(child2);
    VoltXMLElement second = makeNamedElement("element", "element");
    VoltXMLElement child1s = new VoltXMLElement("child");
    child1s.attributes.put("value", "5");
    second.children.add(child1s);
    // Same element name, no attribute "name"
    VoltXMLElement child2s = new VoltXMLElement("child");
    child2s.attributes.put("value", "6");
    second.children.add(child2s);
    VoltXMLDiff diff = VoltXMLElement.computeDiff(first, second);
    System.out.println("diff: " + diff.toString());
    VoltXMLElement third = first.duplicate();
    third.applyDiff(diff);
    System.out.println(first.toMinString());
    System.out.println(second.toMinString());
    System.out.println(third.toMinString());
    assertEquals(second.toMinString(), third.toMinString());
}
Also used : VoltXMLDiff(org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 2 with VoltXMLDiff

use of org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff in project voltdb by VoltDB.

the class TestVoltXMLElement method testDiff.

public void testDiff() {
    VoltXMLElement first = makeNamedElement("element", "element");
    VoltXMLElement changedChild1 = makeNamedElement("child", "changedchild1");
    first.children.add(changedChild1);
    changedChild1.attributes.put("deleteme", "noreally");
    VoltXMLElement changedChild2 = makeNamedElement("child", "changedchild2");
    first.children.add(changedChild2);
    VoltXMLElement changedGrandchild = makeNamedElement("child", "changedgrandchild");
    changedChild2.children.add(changedGrandchild);
    changedGrandchild.children.add(makeNamedElement("child", "doomeddescendent"));
    first.attributes.put("deleted", "doesntmatter");
    first.attributes.put("remains", "doesntmatter");
    first.attributes.put("changes", "oldvalue");
    first.children.add(makeNamedElement("child", "deletedchild"));
    first.children.add(makeNamedElement("child", "unchangedchild"));
    VoltXMLElement second = first.duplicate();
    second.attributes.remove("deleted");
    second.attributes.put("added", "addedval");
    second.attributes.put("changes", "newvalue");
    second.children.add(makeNamedElement("child", "addedchild"));
    second.children.remove(second.findChild("child", "deletedchild"));
    second.findChild("child", "changedchild1").attributes.remove("deleteme");
    VoltXMLElement temp = second.findChild("child", "changedchild2").findChild("child", "changedgrandchild");
    temp.children.remove(temp.findChild("child", "doomeddescendent"));
    VoltXMLDiff diff = VoltXMLElement.computeDiff(first, second);
    Map<String, String> addedAtt = diff.getAddedAttributes();
    assertEquals(1, addedAtt.size());
    assertTrue(addedAtt.keySet().contains("added"));
    assertEquals("addedval", addedAtt.get("added"));
    Map<String, String> changedAtt = diff.getChangedAttributes();
    assertEquals(1, changedAtt.size());
    assertTrue(changedAtt.keySet().contains("changes"));
    assertEquals("newvalue", changedAtt.get("changes"));
    Set<String> removedAtt = diff.getRemovedAttributes();
    assertEquals(1, removedAtt.size());
    assertTrue(removedAtt.contains("deleted"));
    List<VoltXMLElement> added = diff.getAddedNodes();
    assertEquals(1, added.size());
    assertTrue(findNamedNode(added, "addedchild") != null);
    List<VoltXMLElement> removed = diff.getRemovedNodes();
    assertEquals(1, removed.size());
    assertTrue(findNamedNode(removed, "deletedchild") != null);
    Map<String, VoltXMLDiff> changed = diff.getChangedNodes();
    assertEquals(2, changed.size());
    assertTrue(changed.containsKey("childchangedchild1"));
    VoltXMLDiff child1 = changed.get("childchangedchild1");
    assertTrue(child1.getRemovedAttributes().contains("deleteme"));
    assertTrue(changed.containsKey("childchangedchild2"));
    VoltXMLDiff child2 = changed.get("childchangedchild2");
    assertTrue(child2.getChangedNodes().containsKey("childchangedgrandchild"));
    VoltXMLDiff grandchild = child2.getChangedNodes().get("childchangedgrandchild");
    assertTrue(findNamedNode(grandchild.getRemovedNodes(), "doomeddescendent") != null);
    VoltXMLElement third = first.duplicate();
    third.applyDiff(diff);
    System.out.println(first.toMinString());
    System.out.println(second.toMinString());
    System.out.println(third.toMinString());
    assertEquals(second.toMinString(), third.toMinString());
}
Also used : VoltXMLDiff(org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 3 with VoltXMLDiff

use of org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff in project voltdb by VoltDB.

the class TestVoltXMLElement method testOrderFail.

public void testOrderFail() {
    VoltXMLElement first = makeNamedElement("element", "element");
    first.children.add(makeNamedElement("first", "first"));
    first.children.add(makeNamedElement("third", "third"));
    first.children.add(makeNamedElement("fourth", "fourth"));
    VoltXMLElement second = makeNamedElement("element", "element");
    second.children.add(makeNamedElement("first", "first"));
    second.children.add(makeNamedElement("second", "second"));
    second.children.add(makeNamedElement("third", "third"));
    second.children.add(makeNamedElement("fourth", "fourth"));
    VoltXMLDiff diff = VoltXMLElement.computeDiff(first, second);
    System.out.println("diff: " + diff.toString());
    VoltXMLElement third = first.duplicate();
    third.applyDiff(diff);
    System.out.println(first.toMinString());
    System.out.println(second.toMinString());
    System.out.println(third.toMinString());
    assertEquals(second.toMinString(), third.toMinString());
}
Also used : VoltXMLDiff(org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 4 with VoltXMLDiff

use of org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff in project voltdb by VoltDB.

the class TestVoltXMLElement method testNoDiff.

public void testNoDiff() {
    VoltXMLElement first = makeNamedElement("element", "element");
    VoltXMLElement changedChild1 = makeNamedElement("child", "changedchild1");
    first.children.add(changedChild1);
    changedChild1.attributes.put("deleteme", "noreally");
    VoltXMLElement changedChild2 = makeNamedElement("child", "changedchild2");
    first.children.add(changedChild2);
    VoltXMLElement changedGrandchild = makeNamedElement("child", "changedgrandchild");
    changedChild2.children.add(changedGrandchild);
    changedGrandchild.children.add(makeNamedElement("child", "doomeddescendent"));
    first.attributes.put("deleted", "doesntmatter");
    first.attributes.put("remains", "doesntmatter");
    first.attributes.put("changes", "oldvalue");
    first.children.add(makeNamedElement("child", "deletedchild"));
    first.children.add(makeNamedElement("child", "unchangedchild"));
    VoltXMLElement second = first.duplicate();
    VoltXMLDiff diff = VoltXMLElement.computeDiff(first, second);
    System.out.println("diff: " + diff.toString());
    VoltXMLElement third = first.duplicate();
    third.applyDiff(diff);
    System.out.println(first.toMinString());
    System.out.println(second.toMinString());
    System.out.println(third.toMinString());
    assertEquals(second.toMinString(), third.toMinString());
}
Also used : VoltXMLDiff(org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 5 with VoltXMLDiff

use of org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff in project voltdb by VoltDB.

the class HSQLInterface method runDDLCommandAndDiff.

/**
     * Modify the current schema with a SQL DDL command and get the
     * diff which represents the changes.
     *
     * Note that you have to be consistent WRT case for the expected names.
     *
     * @param expectedTableAffected The name of the table affected by this DDL
     * or null if unknown
     * @param expectedIndexAffected The name of the index affected by this DDL
     * or null if table is known instead
     * @param ddl The SQL DDL statement to be run.
     * @return the "diff" of the before and after trees for the affected table
     * @throws HSQLParseException Throws exception if SQL parse error is
     * encountered.
     */
public VoltXMLDiff runDDLCommandAndDiff(HSQLDDLInfo stmtInfo, String ddl) throws HSQLParseException {
    // name of the table we're going to have to diff (if any)
    String expectedTableAffected = null;
    // If we fail to pre-process a statement, then we want to fail, but we're
    // still going to run the statement through HSQL to get its error message.
    // This variable helps us make sure we don't fail to preprocess and then
    // succeed at runnign the statement through HSQL.
    boolean expectFailure = false;
    // If cascade, we're going to need to look for any views that might have
    // gotten deleted. So get a list of all tables and views that existed before
    // we run the ddl, then we'll do a comparison later.
    Set<String> existingTableNames = null;
    if (stmtInfo != null) {
        if (stmtInfo.cascade) {
            existingTableNames = getTableNames();
        }
        // we either have an index name or a table/view name, but not both
        if (stmtInfo.noun == HSQLDDLInfo.Noun.INDEX) {
            if (stmtInfo.verb == HSQLDDLInfo.Verb.CREATE) {
                expectedTableAffected = stmtInfo.secondName;
            } else {
                expectedTableAffected = tableNameForIndexName(stmtInfo.name);
            }
        } else {
            expectedTableAffected = stmtInfo.name;
        }
        // Note that we're assuming ifexists can't happen with "create"
        expectFailure = (expectedTableAffected == null) && !stmtInfo.ifexists;
    } else {
        expectFailure = true;
    }
    runDDLCommand(ddl);
    // (Shouldn't get here ever I think)
    if (expectFailure) {
        throw new HSQLParseException("Unable to plan statement due to VoltDB DDL pre-processing error");
    }
    // sanity checks for non-failure
    assert (stmtInfo != null);
    // get old and new XML representations for the affected table
    VoltXMLElement tableXMLNew = null, tableXMLOld = null;
    if (expectedTableAffected != null) {
        tableXMLNew = getXMLForTable(expectedTableAffected);
        tableXMLOld = lastSchema.get(expectedTableAffected);
    }
    // valid reasons for tableXMLNew to be null are DROP IF EXISTS and not much else
    if (tableXMLNew == null) {
        tableXMLNew = emptySchema;
    }
    // the old table can be null for CREATE TABLE or for IF EXISTS stuff
    if (tableXMLOld == null) {
        tableXMLOld = emptySchema;
    }
    VoltXMLDiff diff = VoltXMLElement.computeDiff(tableXMLOld, tableXMLNew);
    // they're gone
    if (stmtInfo.cascade) {
        Set<String> finalTableNames = getTableNames();
        for (String tableName : existingTableNames) {
            if (!finalTableNames.contains(tableName)) {
                tableName = tableName.toLowerCase();
                tableXMLOld = lastSchema.get(tableName).children.get(0);
                lastSchema.remove(tableName);
                if (tableName.equals(expectedTableAffected)) {
                    continue;
                }
                diff.m_removedElements.add(tableXMLOld);
            }
        }
    }
    // this is a hack to allow the diff-apply-er to accept a diff that has no order
    diff.m_elementOrder.clear();
    // remember the current schema
    if (expectedTableAffected != null) {
        lastSchema.put(expectedTableAffected, tableXMLNew.duplicate());
    }
    return diff;
}
Also used : VoltXMLDiff(org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff)

Aggregations

VoltXMLDiff (org.hsqldb_voltpatches.VoltXMLElement.VoltXMLDiff)7 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)5 HashSet (java.util.HashSet)1 HSQLDDLInfo (org.hsqldb_voltpatches.HSQLDDLInfo)1 HSQLParseException (org.hsqldb_voltpatches.HSQLInterface.HSQLParseException)1 VoltCompilerException (org.voltdb.compiler.VoltCompiler.VoltCompilerException)1