@SuppressWarnings("unchecked")
  public void testVisitJoinWithoutLock() throws Exception {
    Select select = new Select("test");
    select.setLocked(false);
    Visitor queryVis = getVisitor();
    Table tab1 = new Table("tab1");
    Column col1 = tab1.column("col1");
    Table tab2 = new Table("tab2");
    Column col2 = tab2.column("col2");
    Join join = new Join(JoinOperator.LEFT, new Table("tab1"), col1.equal(col2));

    Class[] clazz = {Select.class};
    Object[] obj = {select};
    try {
      Method method = queryVis.getClass().getDeclaredMethod("setSelect", clazz);
      method.setAccessible(true);
      method.invoke(queryVis, obj);
    } catch (Exception e) {
      fail("Something went wrong using reflection to execute private method.");
      e.printStackTrace();
    }

    queryVis.visit(join);

    assertEquals("LEFT JOIN \"tab1\" ON \"tab1\".\"col1\"=\"tab2\".\"col2\"", queryVis.toString());
  }
  @Test
  public void testSelectWithFromWithLock() {
    Visitor queryVis = getVisitor();

    Table tab1 = new Table("FOO");
    Table tab2 = new Table("BAR");
    TableAlias tab3 = new TableAlias(new Table("ABC"), "xyz");

    Select sel = new Select(tab1);
    sel.setLocked(true);
    sel.addFrom(tab2);
    sel.addFrom(tab3);

    queryVis.visit(sel);

    assertTrue(sel.hasFrom());

    Iterator<Qualifier> iter = sel.getFrom().iterator();
    assertEquals(iter.next(), tab1);
    assertEquals(iter.next(), tab2);
    assertEquals(iter.next(), tab3);
    assertFalse(iter.hasNext());

    assertEquals(
        "SELECT * " + "FROM \"FOO\" HOLDLOCK, \"BAR\" HOLDLOCK, " + "\"ABC\" \"xyz\" HOLDLOCK",
        queryVis.toString());
  }
  @Test
  public void testSelectWithJoinsDepthWithLock() {
    Visitor queryVis = getVisitor();

    Table tab1 = new Table("FOO");
    Column col1 = new Column(tab1, "ID");

    Table tab2 = new Table("BAR");
    Column col21 = new Column(tab2, "ID");
    Column col22 = new Column(tab2, "ABC_ID");

    TableAlias tab3 = new TableAlias(new Table("ABC"), "xyz");
    Column col3 = new Column(tab3, "ID");

    Select sel = new Select(tab1);
    sel.setLocked(true);
    queryVis.visit(sel);
    assertEquals("SELECT * FROM \"FOO\" HOLDLOCK", queryVis.toString());

    queryVis = getVisitor();
    tab1.addInnerJoin(tab2, col1.equal(col21));
    queryVis.visit(sel);
    assertEquals(
        "SELECT * "
            + "FROM (\"FOO\" HOLDLOCK "
            + "INNER JOIN \"BAR\" HOLDLOCK "
            + "ON \"FOO\".\"ID\"=\"BAR\".\"ID\")",
        queryVis.toString());

    queryVis = getVisitor();
    tab2.addLeftJoin(tab3, col22.equal(col3));
    queryVis.visit(sel);
    assertEquals(
        "SELECT * "
            + "FROM (\"FOO\" HOLDLOCK "
            + "INNER JOIN (\"BAR\" HOLDLOCK "
            + "LEFT JOIN \"ABC\" \"xyz\" HOLDLOCK "
            + "ON \"BAR\".\"ABC_ID\"=\"xyz\".\"ID\") "
            + "ON \"FOO\".\"ID\"=\"BAR\".\"ID\")",
        queryVis.toString());
  }
Beispiel #4
0
  @Test
  public void testSelectNoConditionNoExpressionWithLock() throws Exception {
    Select select = new Select("TestTable");
    select.setLocked(true);

    Visitor queryVis = getVisitor();
    queryVis.visit(select);

    String expected = "SELECT * FROM TestTable FOR UPDATE";

    assertEquals(expected, queryVis.toString());
  }
  @Test
  public void testSelectWithJoinsDepthAndBreadthWithLock() {
    Visitor queryVis = getVisitor();

    Table tab1 = new Table("table1");
    Column col1 = new Column(tab1, "col1");

    Table tab2 = new Table("table2");
    Column col2 = new Column(tab2, "col2");

    Table tab3 = new Table("table3");
    Column col3 = new Column(tab3, "col3");

    Table tab4 = new Table("table4");
    Column col4 = new Column(tab4, "col4");

    Table tab5 = new Table("table5");
    Column col5 = new Column(tab5, "col5");

    Table tab6 = new Table("table6");
    Column col6 = new Column(tab6, "col6");

    Table tab7 = new Table("table7");
    Column col7 = new Column(tab7, "col7");

    Select select = new Select(tab1);
    select.setLocked(true);

    tab1.addInnerJoin(tab2, col1.equal(col2));
    tab2.addFullJoin(tab3, col2.equal(col3));
    tab2.addLeftJoin(tab7, col2.equal(col7));

    tab1.addInnerJoin(tab4, col1.equal(col4));
    tab4.addRightJoin(tab5, col4.equal(col5));
    tab5.addLeftJoin(tab6, col5.equal(col6));

    queryVis.visit(select);

    assertEquals(
        "SELECT * FROM "
            + "((\"table1\" HOLDLOCK INNER JOIN "
            + "((\"table2\" HOLDLOCK FULL JOIN \"table3\" HOLDLOCK ON "
            + "\"table2\".\"col2\"=\"table3\".\"col3\") "
            + "LEFT JOIN \"table7\" HOLDLOCK ON \"table2\".\"col2\"=\"table7\".\"col7\") "
            + "ON \"table1\".\"col1\"=\"table2\".\"col2\") "
            + "INNER JOIN (\"table4\" HOLDLOCK "
            + "RIGHT JOIN (\"table5\" HOLDLOCK LEFT JOIN \"table6\" HOLDLOCK "
            + "ON \"table5\".\"col5\"=\"table6\".\"col6\") "
            + "ON \"table4\".\"col4\"=\"table5\".\"col5\") "
            + "ON \"table1\".\"col1\"=\"table4\".\"col4\")",
        queryVis.toString());
  }
  @Test
  public void testSelectWithJoinsBreadthWithLock() {
    Visitor queryVis = getVisitor();

    Table tab1 = new Table("FOO");
    Column col1 = new Column(tab1, "ID");

    Table tab2 = new Table("BAR");
    Column col21 = new Column(tab2, "ID");

    TableAlias tab3 = new TableAlias(new Table("ABC"), "xyz");
    Column col3 = new Column(tab3, "ID");

    Table tab4 = new Table("tab4");

    Table tab5 = new Table("tab5");

    Select sel = new Select(tab1);
    sel.setLocked(true);
    queryVis.visit(sel);
    assertEquals("SELECT * FROM \"FOO\" HOLDLOCK", queryVis.toString());

    queryVis = getVisitor();
    tab1.addInnerJoin(tab2, col1.equal(col21));
    tab1.addInnerJoin(tab3, col1.equal(col3));
    tab1.addInnerJoin(tab4);
    tab1.addInnerJoin(tab5);
    queryVis.visit(sel);
    assertEquals(
        "SELECT * FROM "
            + "((((\"FOO\" HOLDLOCK INNER JOIN \"BAR\" HOLDLOCK ON "
            + "\"FOO\".\"ID\"=\"BAR\".\"ID\") "
            + "INNER JOIN \"ABC\" \"xyz\" HOLDLOCK ON \"FOO\".\"ID\"=\"xyz\".\"ID\") "
            + "INNER JOIN \"tab4\" HOLDLOCK) "
            + "INNER JOIN \"tab5\" HOLDLOCK)",
        queryVis.toString());
  }
  @Test
  public void testSelectWithFromAndJoinsWithLock() {
    Visitor queryVis = getVisitor();

    Table tab1 = new Table("FOO");
    Column col1 = new Column(tab1, "ID");

    Table tab2 = new Table("BAR");
    Column col21 = new Column(tab2, "ID");
    Column col22 = new Column(tab2, "ABC_ID");

    TableAlias tab3 = new TableAlias(new Table("ABC"), "xyz");
    Column col3 = new Column(tab3, "ID");

    Table tab4 = new Table("FN");
    Table tab5 = new Table("ORD");
    TableAlias tab6 = new TableAlias(new Table("FN"), "abc");

    Select sel = new Select(tab1);
    sel.setLocked(true);
    tab1.addInnerJoin(tab2, col1.equal(col21));
    tab2.addLeftJoin(tab3, col22.equal(col3));

    sel.addFrom(tab4);
    sel.addFrom(tab5);
    tab5.addRightJoin(tab6);

    queryVis.visit(sel);

    assertTrue(sel.hasFrom());

    Iterator<Qualifier> iter = sel.getFrom().iterator();
    assertEquals(iter.next(), tab1);
    assertEquals(iter.next(), tab4);
    assertEquals(iter.next(), tab5);
    assertFalse(iter.hasNext());

    assertEquals(
        "SELECT * "
            + "FROM (\"FOO\" HOLDLOCK "
            + "INNER JOIN (\"BAR\" HOLDLOCK "
            + "LEFT JOIN \"ABC\" \"xyz\" HOLDLOCK "
            + "ON \"BAR\".\"ABC_ID\"=\"xyz\".\"ID\") "
            + "ON \"FOO\".\"ID\"=\"BAR\".\"ID\")"
            + ", \"FN\" HOLDLOCK, (\"ORD\" HOLDLOCK RIGHT JOIN \"FN\" \"abc\" "
            + "HOLDLOCK)",
        queryVis.toString());
  }
  @Test
  public void testSelectNoConditionNoExpressionWithLock() throws Exception {
    Select select = new Select("TestTable");
    select.setLocked(true);

    Visitor queryVis = getVisitor();
    queryVis.visit(select);

    String expected = "SELECT * FROM \"TestTable\" HOLDLOCK";

    assertEquals(expected, queryVis.toString());

    Object result = null;
    try {
      Method method = queryVis.getClass().getDeclaredMethod("getSelect", (Class[]) null);
      method.setAccessible(true);
      result = method.invoke(queryVis, (Object[]) null);
    } catch (Exception e) {
      fail("Something went wrong using reflection to execute private method.");
      e.printStackTrace();
    }

    assertEquals(select, result);
  }