Java Code Examples for io.vertx.sqlclient.SqlClient

The following examples show how to use io.vertx.sqlclient.SqlClient. 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 enumType01Example(SqlClient client) {
  client
    .preparedQuery("INSERT INTO colors VALUES ($1)")
    .execute(Tuple.of(Color.red))
    .flatMap(res ->
      client
        .preparedQuery("SELECT color FROM colors")
        .execute()
    ).onComplete(res -> {
      if (res.succeeded()) {
        RowSet<Row> rows = res.result();
        for (Row row : rows) {
          System.out.println(row.get(Color.class, "color"));
        }
      }
  });
}
 
Example 2
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 3
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 4
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries08(SqlClient client) {

    // Add commands to the batch
    List<Tuple> batch = new ArrayList<>();
    batch.add(Tuple.of("julien", "Julient Viet"));
    batch.add(Tuple.of("emad", "Emad Alblueshi"));
    batch.add(Tuple.of("andy", "Andy Guibert"));

    // Execute the prepared batch
    client
      .preparedQuery("INSERT INTO USERS (id, name) VALUES ($1, $2)")
      .executeBatch(batch, res -> {
      if (res.succeeded()) {

        // Process rows
        RowSet<Row> rows = res.result();
      } else {
        System.out.println("Batch failed " + res.cause());
      }
    });
  }
 
Example 5
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 6
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 7
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries08(SqlClient client) {

    // Add commands to the batch
    List<Tuple> batch = new ArrayList<>();
    batch.add(Tuple.of("julien", "Julien Viet"));
    batch.add(Tuple.of("emad", "Emad Alblueshi"));

    // Execute the prepared batch
    client
      .preparedQuery("INSERT INTO USERS (id, name) VALUES ($1, $2)")
      .executeBatch(batch, res -> {
      if (res.succeeded()) {

        // Process rows
        RowSet<Row> rows = res.result();
      } else {
        System.out.println("Batch failed " + res.cause());
      }
    });
  }
 
Example 8
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries08(SqlClient client) {

    // Add commands to the batch
    List<Tuple> batch = new ArrayList<>();
    batch.add(Tuple.of("julien", "Julien Viet"));
    batch.add(Tuple.of("emad", "Emad Alblueshi"));

    // Execute the prepared batch
    client
      .preparedQuery("INSERT INTO USERS (id, name) VALUES (?, ?)")
      .executeBatch(batch, res -> {
      if (res.succeeded()) {

        // Process rows
        RowSet<Row> rows = res.result();
      } else {
        System.out.println("Batch failed " + res.cause());
      }
    });
  }
 
Example 9
Source Project: vertx-sql-client   Source File: MySQLClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void booleanExample01(SqlClient client) {
  client
    .query("SELECT graduated FROM students WHERE id = 0")
    .execute(ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rowSet = ar.result();
      for (Row row : rowSet) {
        int pos = row.getColumnIndex("graduated");
        Byte value = row.get(Byte.class, pos);
        Boolean graduated = row.getBoolean("graduated");
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 10
Source Project: vertx-sql-client   Source File: MySQLClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void geometryExample03(SqlClient client) {
  client
    .query("SELECT g FROM geom;")
    .execute(ar -> {
    if (ar.succeeded()) {
      // Fetch the spatial data as a Vert.x Data Object
      RowSet<Row> result = ar.result();
      for (Row row : result) {
        Point point = row.get(Point.class, 0);
        System.out.println("Point x: " + point.getX());
        System.out.println("Point y: " + point.getY());
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 11
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 12
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 13
Source Project: vertx-auth   Source File: AuthSqlExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example9(SqlAuthentication jdbcAuth, SqlClient sqlClient) {

    String hash = jdbcAuth.hash(
      "pkdbf2", // hashing algorithm (OWASP recommended)
      VertxContextPRNG.current().nextString(32), // secure random salt
      "sausages" // password
    );

    // save to the database
    sqlClient
      .preparedQuery("INSERT INTO user (username, password) VALUES (?, ?)")
      .execute(Tuple.of("tim", hash))
      .onSuccess(rowset -> {
        // password updated
      });
  }
 
Example 14
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries01(SqlClient client) {
  client
    .query("SELECT * FROM users WHERE id='julien'")
    .execute(ar -> {
    if (ar.succeeded()) {
      RowSet<Row> result = ar.result();
      System.out.println("Got " + result.size() + " rows ");
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 15
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries02(SqlClient client) {
  client
    .preparedQuery("SELECT * FROM users WHERE id=$1")
    .execute(Tuple.of("julien"), ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println("Got " + rows.size() + " rows ");
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 16
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries03(SqlClient client) {
  client
    .preparedQuery("SELECT first_name, last_name FROM users")
    .execute(ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      for (Row row : rows) {
        System.out.println("User " + row.getString(0) + " " + row.getString(1));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 17
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries04(SqlClient client) {
  client
    .preparedQuery("INSERT INTO users (first_name, last_name) VALUES ($1, $2)")
    .execute(Tuple.of("Julien", "Viet"),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println(rows.rowCount());
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 18
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries08(SqlClient client) {

    // Add commands to the batch
    List<Tuple> batch = new ArrayList<>();
    batch.add(Tuple.of("julien", "Julien Viet"));
    batch.add(Tuple.of("emad", "Emad Alblueshi"));

    // Execute the prepared batch
    client
      .preparedQuery("INSERT INTO USERS (id, name) VALUES ($1, $2)")
      .executeBatch(batch, res -> {
      if (res.succeeded()) {

        // Process rows
        RowSet<Row> rows = res.result();
      } else {
        System.out.println("Batch failed " + res.cause());
      }
    });
  }
 
Example 19
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries09(SqlClient client, SqlConnectOptions connectOptions) {

    // Enable prepare statements caching
    connectOptions.setCachePreparedStatements(true);
    client
      .preparedQuery("SELECT * FROM users WHERE id = $1")
      .execute(Tuple.of("julien"), ar -> {
        if (ar.succeeded()) {
          RowSet<Row> rows = ar.result();
          System.out.println("Got " + rows.size() + " rows ");
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });
  }
 
Example 20
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void customType01Example(SqlClient client) {
  client
    .preparedQuery("SELECT address, (address).city FROM address_book WHERE id=$1")
    .execute(Tuple.of(3),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      for (Row row : rows) {
        System.out.println("Full Address " + row.getString(0) + ", City " + row.getString(1));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 21
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void customType02Example(SqlClient client) {
  client
    .preparedQuery("INSERT INTO address_book (id, address) VALUES ($1, $2)")
    .execute(Tuple.of(3, "('Anytown', 'Second Ave', false)"),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println(rows.rowCount());
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 22
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void tsQuery01Example(SqlClient client) {
  client
    .preparedQuery("SELECT to_tsvector( $1 ) @@ to_tsquery( $2 )")
    .execute(Tuple.of("fat cats ate fat rats", "fat & rat"),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      for (Row row : rows) {
        System.out.println("Match : " + row.getBoolean(0));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 23
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void tsQuery02Example(SqlClient client) {
  client
    .preparedQuery("SELECT to_tsvector( $1 ), to_tsquery( $2 )")
    .execute(Tuple.of("fat cats ate fat rats", "fat & rat"),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      for (Row row : rows) {
        System.out.println("Vector : " + row.getString(0) + ", query : "+row.getString(1));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 24
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void enumeratedType01Example(SqlClient client) {
  client
    .preparedQuery("INSERT INTO colors VALUES ($2)")
    .execute(Tuple.of("red"),  res -> {
      // ...
    });
}
 
Example 25
Source Project: vertx-sql-client   Source File: PgClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void returning(SqlClient client) {
  client
    .preparedQuery("INSERT INTO color (color_name) VALUES ($1), ($2), ($3) RETURNING color_id")
    .execute(Tuple.of("white", "red", "blue"), ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println(rows.rowCount());
      for (Row row : rows) {
        System.out.println("generated key: " + row.getInteger("color_id"));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 26
Source Project: vertx-sql-client   Source File: PgTestBase.java    License: Apache License 2.0 5 votes vote down vote up
static void insertIntoTestTable(TestContext ctx, SqlClient client, int amount, Runnable completionHandler) {
  AtomicInteger count = new AtomicInteger();
  for (int i = 0;i < 10;i++) {
    client
      .query("INSERT INTO Test (id, val) VALUES (" + i + ", 'Whatever-" + i + "')")
      .execute(ctx.asyncAssertSuccess(r1 -> {
      ctx.assertEquals(1, r1.rowCount());
      if (count.incrementAndGet() == amount) {
        completionHandler.run();
      }
    }));
  }
}
 
Example 27
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries01(SqlClient client) {
  client
    .query("SELECT * FROM users WHERE id='andy'")
    .execute(ar -> {
    if (ar.succeeded()) {
      RowSet<Row> result = ar.result();
      System.out.println("Got " + result.size() + " rows ");
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 28
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries02(SqlClient client) {
  client
    .preparedQuery("SELECT * FROM users WHERE id=$1")
    .execute(Tuple.of("andy"), ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println("Got " + rows.size() + " rows ");
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 29
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries03(SqlClient client) {
  client
    .preparedQuery("SELECT first_name, last_name FROM users")
    .execute(ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      for (Row row : rows) {
        System.out.println("User " + row.getString(0) + " " + row.getString(1));
      }
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}
 
Example 30
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void queries04(SqlClient client) {
  client
    .preparedQuery("INSERT INTO users (first_name, last_name) VALUES ($1, $2)")
    .execute(Tuple.of("Andy", "Guibert"),  ar -> {
    if (ar.succeeded()) {
      RowSet<Row> rows = ar.result();
      System.out.println(rows.rowCount());
    } else {
      System.out.println("Failure: " + ar.cause().getMessage());
    }
  });
}