Exemplo n.º 1
0
  @Test
  public void test_2_exec() throws SQLException {

    // Prepare data
    TableInfo tableInfo = TableInfo.Single("grade");

    Container constraintContainer = Container.And();
    constraintContainer.add(Parameter.Constraint("grade", CompareType.LIKE, "A"));

    // Perform action
    MySQLDeleteHandler deleteHandler = new MySQLDeleteHandler(cushyDBConnection);
    deleteHandler.Delete().From(tableInfo).Where(constraintContainer).execute();

    // Check using JDBC
    PreparedStatement preparedStatement =
        databaseConnection.prepareStatement("SELECT COUNT(*) FROM GRADE WHERE GRADE LIKE ?");
    preparedStatement.setString(1, "A");
    ResultSet resultSet = preparedStatement.executeQuery();
    if (resultSet.next()) {
      int count = resultSet.getInt(1);
      assertEquals(count, 0);
    } else {
      throw new AssertionError();
    }
  }
Exemplo n.º 2
0
  @Test
  public void test_4_exec() throws SQLException {

    // Prepare data
    TableInfo tableInfo = TableInfo.Multi();
    tableInfo.add("course", "c");
    tableInfo.add("grade", "g");

    Container constraintContainer = Container.And();
    constraintContainer.add(Parameter.Constraint("c", "name", CompareType.LIKE, "course_1"));
    constraintContainer.add(Join.Equals("c", "id", "g", "course_id"));

    // Perform action
    MySQLDeleteHandler deleteHandler = new MySQLDeleteHandler(cushyDBConnection);
    deleteHandler.Delete("g").From(tableInfo).Where(constraintContainer).execute();

    // Check using JDBC
    PreparedStatement preparedStatement =
        databaseConnection.prepareStatement(
            "SELECT COUNT(*) FROM GRADE G, COURSE C WHERE C.NAME LIKE ? AND G.COURSE_ID = C.ID");
    preparedStatement.setString(1, "course_1");
    ResultSet resultSet = preparedStatement.executeQuery();
    if (resultSet.next()) {
      int count = resultSet.getInt(1);
      assertEquals(count, 0);
    } else {
      throw new AssertionError();
    }
  }
Exemplo n.º 3
0
  @Test(expected = CushyDBException.class) // Exception due to foreign key constraint
  public void test_3_exec() throws SQLException {

    // Prepare data
    TableInfo tableInfo = TableInfo.Single("course");

    Container constraintContainer = Container.And();
    constraintContainer.add(Parameter.Constraint("name", CompareType.LIKE, "course_1"));

    // Perform action
    MySQLDeleteHandler deleteHandler = new MySQLDeleteHandler(cushyDBConnection);
    deleteHandler.Delete().From(tableInfo).Where(constraintContainer).execute();
  }
Exemplo n.º 4
0
  @Test
  public void test_1_sql() {

    // Prepare data
    TableInfo tableInfo = TableInfo.Single("table1");

    Container constraintContainer = Container.And();
    constraintContainer
        .add(Parameter.Constraint("param1", CompareType.EQ, 11))
        .add(Parameter.Constraint("param2", CompareType.LIKE, "12"))
        .add(Parameter.Constraint("param3", CompareType.NEQ, 13));

    // Perform action
    MySQLDeleteHandler deleteHandler = new MySQLDeleteHandler(cushyDBConnection);
    String sql = deleteHandler.Delete().From(tableInfo).Where(constraintContainer).sql();

    // Check
    assertEquals(
        sql, "DELETE a FROM table1 a WHERE (a.param1 = ? AND a.param2 LIKE ? AND a.param3 <> ?)");
  }