io.vertx.ext.sql.ResultSet.getRows()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中io.vertx.ext.sql.ResultSet.getRows方法的一些代码示例,展示了ResultSet.getRows的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResultSet.getRows方法的具体详情如下:
包路径:io.vertx.ext.sql.ResultSet
类名称:ResultSet
方法名:getRows

ResultSet.getRows介绍

[英]Get the rows - each row represented as a JsonObject where the keys are the column names and the values are the column values. Beware that it's legal for a query result in SQL to contain duplicate column names, in which case one will overwrite the other if using this method. If that's the case use #getResults instead. Be aware that column names are defined as returned by the database, this means that even if your SQL statement is for example:

SELECT a, b FROM table

the column names are not required to be:

a

and

b

and could be in fact

A

and

B

. For cases when there is the need for case insentivitity you should see #getRows(boolean)
[中]获取行——每行表示为一个JsonObject,其中键是列名,值是列值。请注意,SQL中的查询结果包含重复的列名是合法的,在这种情况下,如果使用此方法,一个列名将覆盖另一个列名。如果是这种情况,请使用#getResults。请注意,列名是由数据库返回的,这意味着即使您的SQL语句是:

SELECT a, b FROM table

列名也不需要是:

a

b

,实际上可以是

A

B

。对于需要区分大小写的情况,您应该看到#getRows(布尔值)

代码示例

代码示例来源:origin: vert-x3/vertx-examples

private void handleListProducts(RoutingContext routingContext) {
 HttpServerResponse response = routingContext.response();
 SQLConnection conn = routingContext.get("conn");
 conn.query("SELECT id, name, price, weight FROM products", query -> {
  if (query.failed()) {
   sendError(500, response);
  } else {
   JsonArray arr = new JsonArray();
   query.result().getRows().forEach(arr::add);
   routingContext.response().putHeader("content-type", "application/json").end(arr.encode());
  }
 });
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true")
   .put("driver_class", "org.hsqldb.jdbcDriver");

  JDBCClient jdbc = JDBCClient.createShared(vertx, config);

  // Connect to the database
  jdbc.rxGetConnection().flatMap(conn -> {

   // Now chain some statements using flatmap composition
   Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))")
    .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')"))
    .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')"))
    .flatMap(result -> conn.rxQuery("SELECT * FROM test"));

   return resa.doAfterTerminate(conn::close);

  }).subscribe(resultSet -> {
   // Subscribe to the final result
   System.out.println("Results : " + resultSet.getRows());
  }, err -> {
   System.out.println("Database problem");
   err.printStackTrace();
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

private void handleGetProduct(RoutingContext routingContext) {
 String productID = routingContext.request().getParam("productID");
 HttpServerResponse response = routingContext.response();
 if (productID == null) {
  sendError(400, response);
 } else {
  SQLConnection conn = routingContext.get("conn");
  conn.queryWithParams("SELECT id, name, price, weight FROM products where id = ?", new JsonArray().add(Integer.parseInt(productID)), query -> {
   if (query.failed()) {
    sendError(500, response);
   } else {
    if (query.result().getNumRows() == 0) {
     sendError(404, response);
    } else {
     response.putHeader("content-type", "application/json").end(query.result().getRows().get(0).encode());
    }
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true")
   .put("driver_class", "org.hsqldb.jdbcDriver");

  JDBCClient jdbc = JDBCClient.createShared(vertx, config);

  // Connect to the database
  jdbc.rxGetConnection().flatMap(conn -> {

   // Now chain some statements using flatmap composition
   Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))")
    .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')"))
    .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')"))
    .flatMap(result -> conn.rxQuery("SELECT * FROM test"));

   return resa.doAfterTerminate(conn::close);

  }).subscribe(resultSet -> {
   // Subscribe to the final result
   System.out.println("Results : " + resultSet.getRows());
  }, err -> {
   System.out.println("Database problem");
   err.printStackTrace();
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

).subscribe(resultSet -> {
 System.out.println("Results : " + resultSet.getRows());
}, Throwable::printStackTrace);

代码示例来源:origin: vert-x3/vertx-examples

).subscribe(resultSet -> {
 System.out.println("Results : " + resultSet.getRows());
}, Throwable::printStackTrace);

代码示例来源:origin: redhat-developer-demos/istio-tutorial

private List<String> findRecommendation(final ResultSet resultSet) {
  final List<String> recommendations = new ArrayList<>();
  for (JsonObject row : resultSet.getRows()) {
    recommendations.add(row.getString("name"));            
  }
  return recommendations;
}

代码示例来源:origin: redhat-developer-demos/istio-tutorial

private List<String> findRecommendationNew(final ResultSet resultSet) {
  final List<String> recommendations = new ArrayList<>();
  for (JsonObject row : resultSet.getRows()) {
    recommendations.add(row.getString("movie_name"));            
  }
  return recommendations;
}

代码示例来源:origin: sczyh30/vertx-blueprint-todo-backend

@Override
public Single<List<Todo>> getAll() {
 return client.rxQuery(SQL_QUERY_ALL)
  .map(ar -> ar.getRows().stream()
   .map(Todo::new)
   .collect(Collectors.toList())
  );
}

代码示例来源:origin: redhat-developer/introduction-to-eclipse-vertx

private Single<List<Article>> query(SQLConnection connection) {
  return connection.rxQuery("SELECT * FROM articles")
    .map(rs -> rs.getRows().stream().map(Article::new).collect(Collectors.toList()))
    .doFinally(connection::close);
}

代码示例来源:origin: com.jporm/jpo-rx-vertx3

@Override
public boolean getBoolean(final String columnLabel) {
  return orDefault(resultSet.getRows().get(position).getBoolean(columnLabel), false);
}

代码示例来源:origin: com.jporm/jpo-rx-vertx3

@Override
public double getDouble(final String columnLabel) {
  return orDefault(resultSet.getRows().get(position).getDouble(columnLabel), 0d);
}

代码示例来源:origin: sczyh30/vertx-blueprint-microservice

protected Future<List<JsonObject>> retrieveMany(JsonArray param, String sql) {
 return getConnection().compose(connection -> {
  Future<List<JsonObject>> future = Future.future();
  connection.queryWithParams(sql, param, r -> {
   if (r.succeeded()) {
    future.complete(r.result().getRows());
   } else {
    future.fail(r.cause());
   }
   connection.close();
  });
  return future;
 });
}

代码示例来源:origin: sczyh30/vertx-blueprint-microservice

protected Future<List<JsonObject>> retrieveAll(String sql) {
 return getConnection().compose(connection -> {
  Future<List<JsonObject>> future = Future.future();
  connection.query(sql, r -> {
   if (r.succeeded()) {
    future.complete(r.result().getRows());
   } else {
    future.fail(r.cause());
   }
   connection.close();
  });
  return future;
 });
}

代码示例来源:origin: io.github.jklingsporn/vertx-jooq-async-future

@Override
public <P> CompletableFuture<P> fetchOne(Query query, Function<JsonObject, P> mapper){
  return getConnection().thenCompose(sqlConnection -> {
    CompletableFuture<P> cf = new VertxCompletableFuture<P>(vertx);
    sqlConnection.queryWithParams(query.getSQL(), getBindValues(query), executeAndClose(rs -> {
      Optional<P> optional = rs.getRows().stream().findFirst().map(mapper);
      return optional.orElseGet(() -> null);
    }, sqlConnection, cf));
    return cf;
  });
}

代码示例来源:origin: io.github.jklingsporn/vertx-jooq-async-rx

@Override
public <P> Single<List<P>> fetch(Query query, java.util.function.Function<JsonObject, P> mapper){
  return getConnection().flatMap(executeAndClose(sqlConnection ->
      sqlConnection.rxQueryWithParams(query.getSQL(), getBindValues(query)).map(rs ->
              rs.getRows().stream().map(mapper).collect(Collectors.toList())
      )));
}

代码示例来源:origin: io.github.jklingsporn/vertx-jooq-async-rx

@Override
public <P> Single<P> fetchOne(Query query, Function<JsonObject, P> mapper){
  return getConnection().flatMap(executeAndClose(sqlConnection ->
    sqlConnection.rxQueryWithParams(query.getSQL(), getBindValues(query)).map(rs -> {
      Optional<P> optional = rs.getRows().stream().findFirst().map(mapper);
      return optional.orElseGet(() -> null);
    })));
}

代码示例来源:origin: jklingsporn/vertx-jooq-async

@Override
public <P> Single<P> fetchOne(Query query, Function<JsonObject, P> mapper){
  return getConnection().flatMap(executeAndClose(sqlConnection ->
    sqlConnection.rxQueryWithParams(query.getSQL(), getBindValues(query)).map(rs -> {
      Optional<P> optional = rs.getRows().stream().findFirst().map(mapper);
      return optional.orElseGet(() -> null);
    })));
}

代码示例来源:origin: jklingsporn/vertx-jooq-async

@Override
public <P> Single<List<P>> fetch(Query query, java.util.function.Function<JsonObject, P> mapper){
  return getConnection().flatMap(executeAndClose(sqlConnection ->
      sqlConnection.rxQueryWithParams(query.getSQL(), getBindValues(query)).map(rs ->
              rs.getRows().stream().map(mapper).collect(Collectors.toList())
      )));
}

代码示例来源:origin: io.vertx/vertx-sql-common

@Test
public void testCaseInsensitiveRows() {
 JsonObject row = rs.getRows(true).get(0);
 assertEquals("res0", row.getString("foo"));
 assertEquals("res0", row.getString("FOO"));
 assertEquals("res0", row.getString("fOo"));
}

相关文章