Пример #1
0
  /** ************ Helper stuff ***************** */
  private void createAndFillUserTable() {

    int rowCount = 10000;
    sql2o
        .createQuery(
            "create table User(\n"
                + "id int identity primary key,\n"
                + "name varchar(20),\n"
                + "email varchar(255),\n"
                + "text varchar(100))")
        .executeUpdate();

    Query insQuery =
        sql2o.createQuery("insert into User(name, email, text) values (:name, :email, :text)");
    Date before = new Date();
    for (int idx = 0; idx < rowCount; idx++) {
      insQuery
          .addParameter("name", "a name " + idx)
          .addParameter("email", String.format("*****@*****.**", idx))
          .addParameter("text", "some text")
          .addToBatch();
    }
    insQuery.executeBatch();
    Date after = new Date();
    Long span = after.getTime() - before.getTime();

    System.out.println(
        String.format("inserted %d rows into User table. Time used: %s ms", rowCount, span));

    insertIntoUsers += rowCount;
  }
Пример #2
0
  @Test
  public void testExecuteAndFetch() {
    createAndFillUserTable();

    Date before = new Date();
    List<User> allUsers = sql2o.createQuery("select * from User").executeAndFetch(User.class);
    Date after = new Date();
    long span = after.getTime() - before.getTime();
    System.out.println(String.format("Fetched %s user: %s ms", insertIntoUsers, span));

    // repeat this
    before = new Date();
    allUsers = sql2o.createQuery("select * from User").executeAndFetch(User.class);
    after = new Date();
    span = after.getTime() - before.getTime();
    System.out.println(String.format("Again Fetched %s user: %s ms", insertIntoUsers, span));

    assertTrue(allUsers.size() == insertIntoUsers);
    deleteUserTable();
  }