Пример #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());
   }
 }
Пример #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
  }
Пример #3
0
  public int createEntity1RowAndChildren(
      ConnectionContext cc,
      Table entity1Table,
      EntityHierarchyTable entity1HierarchyTable,
      Object parentId,
      String namePrefix,
      int createChildrenCount,
      int maxDepth,
      int atDepth)
      throws SQLException, NamingException {
    int result = 0;

    if (atDepth > maxDepth) return result;

    for (int i = 0; i < createChildrenCount; i++) {
      final Row entity1Row = entity1Table.createRow();
      final ColumnValues entity1ColumnValues = entity1Row.getColumnValues();
      final String entityName = namePrefix + i;
      entity1ColumnValues.getByName("name").setValue(entityName);
      entity1Table.insert(cc, entity1Row);

      // we've inserted an entity record so keep track
      result++;

      final Object entity1IdValue = entity1ColumnValues.getByName("entity_1_id").getValue();
      if (parentId != null) {
        final Row hierChildRow = entity1HierarchyTable.createRow();
        final ColumnValues entity1HierColumnValues = hierChildRow.getColumnValues();
        entity1HierColumnValues
            .getByName("hier_type_id")
            .setValue(EntityHierarchyTable.RELTYPEID_OBJ_HIERARCHY_PARENT);
        entity1HierColumnValues.getByName("primary_id").setValue(parentId);
        entity1HierColumnValues.getByName("related_id").setValue(entity1IdValue);
        entity1HierarchyTable.insert(cc, hierChildRow);
      }

      result =
          result
              + createEntity1RowAndChildren(
                  cc,
                  entity1Table,
                  entity1HierarchyTable,
                  entity1IdValue,
                  entityName + ".",
                  createChildrenCount,
                  maxDepth,
                  atDepth + 1);
    }

    return result;
  }
Пример #4
0
    public void testInsert() {
      people.insert(new String[] {"Holub", "Allen", "1"});
      people.insert(new String[] {"Flintstone", "Wilma", "2"});
      people.insert(
          new String[] {"addrId", "first", "last"}, new String[] {"2", "Fred", "Flintstone"});

      address.insert(new String[] {"1", "123 MyStreet", "Berkeley", "CA", "99999"});

      List l = new ArrayList();
      l.add("2");
      l.add("123 Quarry Ln.");
      l.add("Bedrock ");
      l.add("XX");
      l.add("12345");
      assert (address.insert(l) == 1);

      l.clear();
      l.add("3");
      l.add("Bogus");
      l.add("Bad");
      l.add("XX");
      l.add("12345");

      List c = new ArrayList();
      c.add("addrId");
      c.add("street");
      c.add("city");
      c.add("state");
      c.add("zip");
      assert (address.insert(c, l) == 1);

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

      try {
        people.insert(new String[] {"x"});
        throw new AssertionError("insert wrong number of fields test failed");
      } catch (Throwable t) {
        /* Failed correctly, do nothing */
      }

      try {
        people.insert(new String[] {"?"}, new String[] {"y"});
        throw new AssertionError("insert-nonexistent-field test failed");
      } catch (Exception t) {
        /* Failed correctly, do nothing */
      }
    }
Пример #5
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 */
      }
    }
  /**
   * Executes an INSERT_SELECT statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.INSERT_SELECT
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeInsertSelectStatement(CompiledStatement cs) throws HsqlException {

    Table t = cs.targetTable;
    Select s = cs.select;
    int[] ct = t.getColumnTypes(); // column types
    Result r = s.getResult(session.getMaxRows(), session);
    Record rc = r.rRoot;
    int[] cm = cs.columnMap; // column map
    boolean[] ccl = cs.checkColumns; // column check list
    int len = cm.length;
    Object[] row;
    int count;
    boolean success = false;

    session.beginNestedTransaction();

    try {
      while (rc != null) {
        row = t.getNewRowData(session, ccl);

        for (int i = 0; i < len; i++) {
          int j = cm[i];

          if (ct[j] != r.metaData.colType[i]) {
            row[j] = Column.convertObject(rc.data[i], ct[j]);
          } else {
            row[j] = rc.data[i];
          }
        }

        rc.data = row;
        rc = rc.next;
      }

      count = t.insert(session, r);
      success = true;
    } finally {
      session.endNestedTransaction(!success);
    }

    updateResult.iUpdateCount = count;

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

    Table t = cs.targetTable;
    Object[] row = t.getNewRowData(session, cs.checkColumns);
    int[] cm = cs.columnMap; // column map
    Expression[] acve = cs.columnValues;
    Expression cve;
    int[] ct = t.getColumnTypes(); // column types
    int ci; // column index
    int len = acve.length;

    for (int i = 0; i < len; i++) {
      cve = acve[i];
      ci = cm[i];
      row[ci] = cve.getValue(session, ct[ci]);
    }

    t.insert(session, row);

    updateResult.iUpdateCount = 1;

    return updateResult;
  }
Пример #8
0
 /** Delegates to {@link Table#insert(Row)}. */
 public Optional<?> insert() {
   return table.insert(this);
 }
Пример #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());
    }
Пример #10
0
    public void testJoin() {
      // First test a two-way join

      System.out.println(
          "\nSELECT first,last,street,city,state,zip"
              + " FROM people, address"
              + " WHERE people.addrId = address.addrId");

      // Collection version chains to String[] version,
      // so this code tests both:
      List columns = new ArrayList();
      columns.add("first");
      columns.add("last");
      columns.add("street");
      columns.add("city");
      columns.add("state");
      columns.add("zip");

      List tables = new ArrayList();
      tables.add(address);
      // WHERE people.addrID = address.addrID
      Table result =
          people.select(
              new Selector.Adapter() {
                @Override
                public boolean approve(Cursor[] tables) {
                  String a = tables[0].column("addrId");
                  String b = tables[1].column("addrId");
                  return StringUtil.equals(a, b);
                  // return tables[0].column("addrId").equals(tables[1].column("addrId"));
                }
              },
              columns,
              tables);

      print(result);
      System.out.println("");

      // Now test a three-way join
      //
      System.out.println(
          "\nSELECT first,last,street,city,state,zip,text"
              + " FROM people, address, third"
              + " WHERE (people.addrId = address.addrId)"
              + " AND (people.addrId = third.addrId)");

      Table third = TableFactory.create("third", new String[] {"addrId", "text"});
      third.insert(new String[] {"1", "addrId=1"});
      third.insert(new String[] {"2", "addrId=2"});

      result =
          people.select(
              new Selector.Adapter() {
                @Override
                public boolean approve(Cursor[] tables) {
                  return (StringUtil.equals(tables[0].column("addrId"), tables[1].column("addrId"))
                      && StringUtil.equals(tables[0].column("addrId"), tables[2].column("addrId")));
                }
              },
              new String[] {"last", "first", "state", "text"},
              new Table[] {address, third});

      System.out.println(result.toString() + "\n");
    }