Exemplo n.º 1
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());
   }
 }
  /**
   * Executes an UPDATE statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.UPDATE
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeUpdateStatement(CompiledStatement cs) throws HsqlException {

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

    if (filter.findFirst()) {
      int[] colmap = cs.columnMap; // column map
      Expression[] colvalues = cs.columnValues;
      Expression condition = cs.condition; // update condition
      int len = colvalues.length;
      HashMappedList rowset = new HashMappedList();
      int size = table.getColumnCount();
      int[] coltypes = table.getColumnTypes();
      boolean success = false;

      do {
        if (condition == null || condition.test(session)) {
          try {
            Row row = filter.currentRow;
            Object[] ni = table.getNewRow();

            System.arraycopy(row.getData(), 0, ni, 0, size);

            for (int i = 0; i < len; i++) {
              int ci = colmap[i];

              ni[ci] = colvalues[i].getValue(session, coltypes[ci]);
            }

            rowset.add(row, ni);
          } catch (HsqlInternalException e) {
          }
        }
      } while (filter.next());

      session.beginNestedTransaction();

      try {
        count = table.update(session, rowset, colmap);
        success = true;
      } finally {

        // update failed (constraint violation) or succeeded
        session.endNestedTransaction(!success);
      }
    }

    updateResult.iUpdateCount = count;

    return updateResult;
  }
Exemplo n.º 3
0
    public void testUpdate() {
      System.out.println("update set state='YY' where state='XX'");
      int updated =
          address.update(
              new Selector() {
                public boolean approve(Cursor[] tables) {
                  return tables[0].column("state").equals("XX");
                }

                public void modify(Cursor current) {
                  current.update("state", "YY");
                }
              });
      print(address);
      System.out.println(updated + " rows affected\n");
    }
Exemplo n.º 4
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 */
      }
    }
Exemplo n.º 5
0
 public void update() {
   mainTable.update();
   footerTable.update();
 }
Exemplo n.º 6
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());
    }