/**
   * Executes a DELETE statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.DELETE
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeDeleteStatement(CompiledStatement cs) throws HsqlException {

    Table table = cs.targetTable;
    TableFilter filter = cs.tf;
    int count = 0;

    if (filter.findFirst()) {
      Expression c = cs.condition;
      HsqlArrayList del;

      del = new HsqlArrayList();

      if (c == null) {
        do {
          del.add(filter.currentRow);
        } while (filter.next());

        count = table.delete(session, del);
      } else {
        do {
          if (c.test(session)) {
            del.add(filter.currentRow);
          }
        } while (filter.next());

        count = table.delete(session, del);
      }
    }

    updateResult.iUpdateCount = count;

    return updateResult;
  }
Esempio n. 2
0
  public void testRowDeleteTypes() throws SQLException, NamingException {
    DatabaseConnValueContext dbvc = new BasicDatabaseConnValueContext();
    dbvc.setConnectionProvider(TestUtils.getConnProvider(this.getClass().getPackage().getName()));
    dbvc.setDefaultDataSource(this.getClass().getPackage().getName());

    final Table testTable = populatedSchema.getTables().getByName("Test_Retire");
    assertEquals(
        RowDeleteType.LOGICAL_CASCADE_CHILDREN, testTable.getRowDeleteType().getValueIndex());

    final Row testTableRow = testTable.createRow();
    final ColumnValues testTableRowValues = testTableRow.getColumnValues();
    testTableRowValues.getByName("text").setTextValue("Test 001");

    ConnectionContext cc = dbvc.getConnection(this.getClass().getPackage().getName(), true);
    testTable.insert(cc, testTableRow);

    cc.commitAndClose();

    cc = dbvc.getConnection(this.getClass().getPackage().getName(), true);
    testTable.delete(cc, testTableRow);
    cc.commitAndClose();

    cc = dbvc.getConnection(this.getClass().getPackage().getName(), true);
    assertEquals(
        1, testTable.getCount(cc)); // make sure the record is there (it's status should be updated)
    cc.commitAndClose();

    // TODO: add child cascade tests
  }
Esempio n. 3
0
 @Override
 protected void receive(Jedis jedis, Event event) {
   if (event instanceof AbstractMysqlWriteRow) {
     Benchmark benchmark = new Benchmark();
     AbstractMysqlWriteRow e = (AbstractMysqlWriteRow) event;
     String tableName = e.table().toLowerCase();
     Table table = tableMap.get(tableName);
     if (null == table) {
       log.warn("Has not information about " + tableName);
       return;
     }
     List<Column> primaryKeyColumns = e.primaryKeys();
     List<Column> row = e.row();
     String command = null;
     if (e instanceof MysqlUpdateRow) {
       command = table.update(jedis, primaryKeyColumns, row);
     } else if (e instanceof MysqlInsertRow) {
       command = table.insert(jedis, primaryKeyColumns, row);
     } else if (e instanceof MysqlDeleteRow) {
       command = table.delete(jedis, primaryKeyColumns, row);
     } else {
       log.info(event.toString());
     }
     if (null != command) {
       log.info(benchmark.elapsedMsec(3) + "ms execute : " + command);
     }
   } else {
     log.warn(event.getClass().getName() + " is not AbstractMysqlWriteRow : " + event.toString());
   }
 }
Esempio n. 4
0
 /** Delegates to {@link Table#delete(Row)}. */
 public void delete() {
   Object id = get(table.getPrimaryKeyColumn().get());
   if (id == null) {
     throw new IllegalStateException(
         "Can not delete this LinkedRow because the primary key value is not present.");
   }
   table.delete(this);
 }
Esempio n. 5
0
 public void testDelete() {
   System.out.println("delete where street='Bogus'");
   int deleted =
       address.delete(
           new Selector.Adapter() {
             @Override
             public boolean approve(Cursor[] tables) {
               return tables[0].column("street").equals("Bogus");
             }
           });
   print(address);
   System.out.println(deleted + " rows affected\n");
 }
Esempio n. 6
0
    public void testSelect() {
      Selector flintstoneSelector =
          new Selector.Adapter() {
            @Override
            public boolean approve(Cursor[] tables) {
              return tables[0].column("last").equals("Flintstone");
            }
          };

      // SELECT first, last FROM people WHERE last = "Flintstone"
      // The collection version chains to the string version, so the
      // following call tests both versions

      List columns = new ArrayList();
      columns.add("first");
      columns.add("last");

      Table result = people.select(flintstoneSelector, columns);
      print(result);

      // SELECT * FROM people WHERE last = "Flintstone"
      result = people.select(flintstoneSelector);
      print(result);

      // Check that the result is indeed unmodifiable

      try {
        result.insert(new String[] {"x", "y", "z"});
        throw new AssertionError("Insert to Immutable Table test failed");
      } catch (Exception e) {
        /* it failed correctly */
      }

      try {
        result.update(flintstoneSelector);
        throw new AssertionError("Update of Immutable Table test failed");
      } catch (Exception e) {
        /* it failed correctly */
      }

      try {
        result.delete(flintstoneSelector);
        throw new AssertionError("Delete of Immutable Table test failed");
      } catch (Exception e) {
        /* it failed correctly */
      }
    }
Esempio n. 7
0
 /** Delete but do not delete any indexes (for rebuilding). */
 public void deleteRowOnly() throws SQLException {
   setDirty();
   _table.delete(_xa, _block, _buffer, _rowOffset, false);
 }
Esempio n. 8
0
 public boolean delete() throws SQLException {
   setDirty();
   return _table.delete(_xa, _block, _buffer, _rowOffset, true);
 }
Esempio n. 9
0
    public void testUndo() {
      // Verify that commit works properly
      people.begin();
      System.out.println("begin/insert into people (Solo, Han, 5)");

      people.insert(new String[] {"Solo", "Han", "5"});
      System.out.println(people.toString());

      people.begin();
      System.out.println("begin/insert into people (Lea, Princess, 6)");

      people.insert(new String[] {"Lea", "Princess", "6"});
      System.out.println(people.toString());

      System.out.println("commit(THIS_LEVEL)\n" + "rollback(Table.THIS_LEVEL)\n");
      people.commit(Table.THIS_LEVEL);
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());

      // Now test that nested transactions work correctly.

      System.out.println(people.toString());

      System.out.println("begin/insert into people (Vader,Darth, 4)");
      people.begin();
      people.insert(new String[] {"Vader", "Darth", "4"});
      System.out.println(people.toString());

      System.out.println("begin/update people set last=Skywalker where last=Vader");

      people.begin();
      people.update(
          new Selector() {
            public boolean approve(Cursor[] tables) {
              return tables[0].column("last").equals("Vader");
            }

            public void modify(Cursor current) {
              current.update("last", "Skywalker");
            }
          });
      System.out.println(people.toString());

      System.out.println("delete from people where last=Skywalker");
      people.delete(
          new Selector.Adapter() {
            @Override
            public boolean approve(Cursor[] tables) {
              return tables[0].column("last").equals("Skywalker");
            }
          });
      System.out.println(people.toString());

      System.out.println("rollback(Table.THIS_LEVEL) the delete and update");
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());

      System.out.println("rollback(Table.THIS_LEVEL) insert");
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());
    }