Java Code Examples for io.vertx.sqlclient.SqlResult

The following examples show how to use io.vertx.sqlclient.SqlResult. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector01Example(SqlClient client) {

    // Create a collector projecting a row set to a map
    Collector<Row, ?, Map<Long, String>> collector = Collectors.toMap(
      row -> row.getLong("id"),
      row -> row.getString("last_name"));

    // Run the query with the collector
    client.query("SELECT * FROM users")
      .collecting(collector)
      .execute(ar -> {
      if (ar.succeeded()) {
        SqlResult<Map<Long, String>> result = ar.result();

        // Get the map created by the collector
        Map<Long, String> map = result.value();
        System.out.println("Got " + map);
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
  }
 
Example 2
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector02Example(SqlClient client) {

    // Create a collector projecting a row set to a (last_name_1,last_name_2,...)
    Collector<Row, ?, String> collector = Collectors.mapping(
      row -> row.getString("last_name"),
      Collectors.joining(",", "(", ")")
    );

    // Run the query with the collector
    client.query("SELECT * FROM users").collecting(collector).execute(ar -> {
        if (ar.succeeded()) {
          SqlResult<String> result = ar.result();

          // Get the string created by the collector
          String list = result.value();
          System.out.println("Got " + list);
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });
  }
 
Example 3
Source Project: vertx-sql-client   Source File: PgPoolTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPool(TestContext ctx) {
  int num = 5000;
  Async async = ctx.async(num);
  PgPool pool = createPool(options, 4);
  for (int i = 0;i < num;i++) {
    pool.getConnection(ctx.asyncAssertSuccess(conn -> {
      conn.query("SELECT id, randomnumber from WORLD").execute(ar -> {
        if (ar.succeeded()) {
          SqlResult result = ar.result();
          ctx.assertEquals(10000, result.size());
        } else {
          ctx.assertEquals("closed", ar.cause().getMessage());
        }
        conn.close();
        async.countDown();
      });
    }));
  }
}
 
Example 4
Source Project: vertx-sql-client   Source File: PgPoolTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery(TestContext ctx) {
  int num = 1000;
  Async async = ctx.async(num);
  PgPool pool = createPool(options, 4);
  for (int i = 0;i < num;i++) {
    pool.query("SELECT id, randomnumber from WORLD").execute(ar -> {
      if (ar.succeeded()) {
        SqlResult result = ar.result();
        ctx.assertEquals(10000, result.size());
      } else {
        ctx.assertEquals("closed", ar.cause().getMessage());
      }
      async.countDown();
    });
  }
}
 
Example 5
Source Project: vertx-sql-client   Source File: PgPoolTestBase.java    License: Apache License 2.0 6 votes vote down vote up
private void testQueryWithParams(TestContext ctx, PgConnectOptions options) {
  int num = 2;
  Async async = ctx.async(num);
  PgPool pool = createPool(options, 1);
  for (int i = 0;i < num;i++) {
    pool.preparedQuery("SELECT id, randomnumber from WORLD where id=$1").execute(Tuple.of(i + 1), ar -> {
      if (ar.succeeded()) {
        SqlResult result = ar.result();
        ctx.assertEquals(1, result.size());
      } else {
        ar.cause().printStackTrace();
        ctx.assertEquals("closed", ar.cause().getMessage());
      }
      async.countDown();
    });
  }
}
 
Example 6
Source Project: vertx-sql-client   Source File: PgPoolTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdate(TestContext ctx) {
  int num = 1000;
  Async async = ctx.async(num);
  PgPool pool = createPool(options, 4);
  for (int i = 0;i < num;i++) {
    pool.query("UPDATE Fortune SET message = 'Whatever' WHERE id = 9").execute(ar -> {
      if (ar.succeeded()) {
        SqlResult result = ar.result();
        ctx.assertEquals(1, result.rowCount());
      } else {
        ctx.assertEquals("closed", ar.cause().getMessage());
      }
      async.countDown();
    });
  }
}
 
Example 7
Source Project: vertx-sql-client   Source File: PgPoolTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateWithParams(TestContext ctx) {
  int num = 1000;
  Async async = ctx.async(num);
  PgPool pool = createPool(options, 4);
  for (int i = 0;i < num;i++) {
    pool.preparedQuery("UPDATE Fortune SET message = 'Whatever' WHERE id = $1").execute(Tuple.of(9), ar -> {
      if (ar.succeeded()) {
        SqlResult result = ar.result();
        ctx.assertEquals(1, result.rowCount());
      } else {
        ctx.assertEquals("closed", ar.cause().getMessage());
      }
      async.countDown();
    });
  }
}
 
Example 8
Source Project: vertx-sql-client   Source File: PgConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testQueueQueries(TestContext ctx) {
  int num = 1000;
  Async async = ctx.async(num + 1);
  connector.accept(ctx.asyncAssertSuccess(conn -> {
    for (int i = 0;i < num;i++) {
      conn
        .query("SELECT id, randomnumber from WORLD")
        .execute(ar -> {
        if (ar.succeeded()) {
          SqlResult result = ar.result();
          ctx.assertEquals(10000, result.size());
        } else {
          ctx.assertEquals("closed", ar.cause().getMessage());
        }
        async.countDown();
      });
    }
    conn.closeHandler(v -> {
      ctx.assertEquals(1, async.count());
      async.countDown();
    });
    conn.close();
  }));
}
 
Example 9
Source Project: vertx-sql-client   Source File: DB2ClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector01Example(SqlClient client) {

    // Create a collector projecting a row set to a map
    Collector<Row, ?, Map<Long, String>> collector = Collectors.toMap(
      row -> row.getLong("id"),
      row -> row.getString("last_name"));

    // Run the query with the collector
    client.query("SELECT * FROM users")
      .collecting(collector)
      .execute(ar -> {
      if (ar.succeeded()) {
        SqlResult<Map<Long, String>> result = ar.result();

        // Get the map created by the collector
        Map<Long, String> map = result.value();
        System.out.println("Got " + map);
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
  }
 
Example 10
Source Project: vertx-sql-client   Source File: DB2ClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector02Example(SqlClient client) {

    // Create a collector projecting a row set to a (last_name_1,last_name_2,...)
    Collector<Row, ?, String> collector = Collectors.mapping(
      row -> row.getString("last_name"),
      Collectors.joining(",", "(", ")")
    );

    // Run the query with the collector
    client.query("SELECT * FROM users").collecting(collector).execute(ar -> {
        if (ar.succeeded()) {
          SqlResult<String> result = ar.result();

          // Get the string created by the collector
          String list = result.value();
          System.out.println("Got " + list);
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });
  }
 
Example 11
Source Project: vertx-sql-client   Source File: PreparedStatementImpl.java    License: Apache License 2.0 6 votes vote down vote up
<R, F extends SqlResult<R>> void execute(Tuple args,
                                         int fetch,
                                         String cursorId,
                                         boolean suspended,
                                         QueryExecutor<R, ?, F> builder,
                                         Promise<F> p) {
  if (context == Vertx.currentContext()) {
    builder.executeExtendedQuery(
      conn,
        ps,
      autoCommit,
      args,
      fetch,
      cursorId,
      suspended,
      p);
  } else {
    context.runOnContext(v -> execute(args, fetch, cursorId, suspended, builder, p));
  }
}
 
Example 12
Source Project: vertx-sql-client   Source File: MySQLClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector01Example(SqlClient client) {

    // Create a collector projecting a row set to a map
    Collector<Row, ?, Map<Long, String>> collector = Collectors.toMap(
      row -> row.getLong("id"),
      row -> row.getString("last_name"));

    // Run the query with the collector
    client.query("SELECT * FROM users").collecting(collector).execute(ar -> {
        if (ar.succeeded()) {
          SqlResult<Map<Long, String>> result = ar.result();

          // Get the map created by the collector
          Map<Long, String> map = result.value();
          System.out.println("Got " + map);
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });
  }
 
Example 13
Source Project: vertx-sql-client   Source File: MySQLClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void collector02Example(SqlClient client) {

    // Create a collector projecting a row set to a (last_name_1,last_name_2,...)
    Collector<Row, ?, String> collector = Collectors.mapping(
      row -> row.getString("last_name"),
      Collectors.joining(",", "(", ")")
    );

    // Run the query with the collector
    client.query("SELECT * FROM users").collecting(collector).execute(ar -> {
        if (ar.succeeded()) {
          SqlResult<String> result = ar.result();

          // Get the string created by the collector
          String list = result.value();
          System.out.println("Got " + list);
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });
  }
 
Example 14
Source Project: vertx-sql-client   Source File: PgClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalDateTimeWithCodegenCollector(TestContext ctx) {
  SqlTemplate<Map<String, Object>, SqlResult<List<LocalDateTimeDataObject>>> template = SqlTemplate
    .forQuery(connection, "SELECT #{value} :: TIMESTAMP WITHOUT TIME ZONE \"localDateTime\"")
    .collecting(LocalDateTimeDataObjectRowMapper.COLLECTOR);
  LocalDateTime ldt = LocalDateTime.parse("2017-05-14T19:35:58.237666");
  template.execute(Collections.singletonMap("value", ldt), ctx.asyncAssertSuccess(result -> {
    ctx.assertEquals(1, result.size());
    ctx.assertEquals(ldt, result.value().get(0).getLocalDateTime());
  }));
}
 
Example 15
Source Project: vertx-sql-client   Source File: PreparedStatementImpl.java    License: Apache License 2.0 5 votes vote down vote up
<R, F extends SqlResult<R>> void executeBatch(List<Tuple> argsList,
                                              QueryExecutor<R, ?, F> builder,
                                              Promise<F> p) {
  if (context == Vertx.currentContext()) {
    builder.executeBatchQuery(conn, ps, autoCommit, argsList, p);
  } else {
    context.runOnContext(v -> executeBatch(argsList, builder, p));
  }
}
 
Example 16
Source Project: raml-module-builder   Source File: PostgresClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query<RowSet<Row>> query(String s) {

  return new Query<RowSet<Row>>() {

    @Override
    public void execute(Handler<AsyncResult<RowSet<Row>>> handler) {
      if (s.startsWith("EXPLAIN") && failExplain) {
        handler.handle(Future.failedFuture("failExplain"));
      } else if (s.startsWith("COUNT ") && asyncResult.succeeded()) {
        List<String> columnNames = new LinkedList<>();
        columnNames.add("COUNT");
        RowDesc rowDesc = new RowDesc(columnNames);
        Row row = new RowImpl(rowDesc);
        row.addInteger(asyncResult.result().size());
        List<Row> rows = new LinkedList<>();
        rows.add(row);
        RowSet rowSet = new LocalRowSet(asyncResult.result().size()).withColumns(columnNames).withRows(rows);
        handler.handle(Future.succeededFuture(rowSet));
      } else {
        handler.handle(asyncResult);
      }
    }

    @Override
    public <R> Query<SqlResult<R>> collecting(Collector<Row, ?, R> collector) {
      return null;
    }

    @Override
    public <U> Query<RowSet<U>> mapping(Function<Row, U> function) {
      return null;
    }
  };
}
 
Example 17
@Override
public CompletionStage<Integer> update(String sql) {
	return preparedQuery( sql ).thenApply(SqlResult::rowCount);
}
 
Example 18
public CompletionStage<Integer> update(String sql, Tuple parameters) {
	return preparedQuery( sql, parameters ).thenApply(SqlResult::rowCount);
}
 
Example 19
Source Project: vertx-sql-client   Source File: SqlTemplateImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <U> io.vertx.sqlclient.templates.SqlTemplate<T, SqlResult<U>> collecting(Collector<Row, ?, U> collector) {
  return new SqlTemplateImpl<>(client, sqlTemplate, query -> query.collecting(collector), paramsMapper);
}
 
Example 20
Source Project: vertx-sql-client   Source File: QueryBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <U> Query<SqlResult<U>> collecting(Collector<Row, ?, U> collector) {
  Objects.requireNonNull(collector, "Supplied collector must not be null");
  return copy(new QueryExecutor<>(builder.tracer(), builder.metrics(), SqlResultImpl::new, collector));
}
 
Example 21
Source Project: vertx-sql-client   Source File: SqlClientBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected <T2, R2 extends SqlResult<T2>> QueryBase<T2, R2> copy(QueryExecutor<T2, ?, R2> builder) {
  return new QueryImpl<>(autoCommit, singleton, sql, builder);
}
 
Example 22
Source Project: vertx-sql-client   Source File: SqlClientBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <U> PreparedQuery<SqlResult<U>> collecting(Collector<Row, ?, U> collector) {
  return (PreparedQuery<SqlResult<U>>) super.collecting(collector);
}
 
Example 23
Source Project: vertx-sql-client   Source File: SqlClientBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected <T2, R2 extends SqlResult<T2>> QueryBase<T2, R2> copy(QueryExecutor<T2, ?, R2> builder) {
  return new PreparedQueryImpl<>(autoCommit, singleton, sql, builder);
}
 
Example 24
Source Project: vertx-sql-client   Source File: PreparedStatementImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected <T2, R2 extends SqlResult<T2>> QueryBase<T2, R2> copy(QueryExecutor<T2, ?, R2> builder) {
  return new PreparedStatementQuery<>(builder);
}
 
Example 25
Source Project: vertx-sql-client   Source File: PreparedStatementImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <U> PreparedQuery<SqlResult<U>> collecting(Collector<Row, ?, U> collector) {
  return (PreparedQuery<SqlResult<U>>) super.collecting(collector);
}
 
Example 26
Source Project: vertx-sql-client   Source File: SqlResultBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SqlResult<T> next() {
  return next;
}
 
Example 27
Source Project: okapi   Source File: PostgresQueryTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <R> SqlConnection preparedQuery(String string, Collector<Row, ?, R> clctr, Handler<AsyncResult<SqlResult<R>>> hndlr) {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
 
Example 28
Source Project: okapi   Source File: PostgresQueryTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <R> SqlConnection query(String string, Collector<Row, ?, R> clctr, Handler<AsyncResult<SqlResult<R>>> hndlr) {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
 
Example 29
Source Project: okapi   Source File: PostgresQueryTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <R> SqlConnection preparedQuery(String string, Tuple tuple, Collector<Row, ?, R> clctr, Handler<AsyncResult<SqlResult<R>>> hndlr) {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
 
Example 30
Source Project: okapi   Source File: PostgresQueryTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <R> SqlConnection preparedBatch(String string, List<Tuple> list, Collector<Row, ?, R> clctr, Handler<AsyncResult<SqlResult<R>>> hndlr) {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}