Skip to content

Commit

Permalink
sql client
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 20, 2024
1 parent 7bc6c0d commit 62a1988
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.sqlclient.query_params;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.Pool;
Expand All @@ -13,7 +14,7 @@
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -39,18 +40,19 @@ public static void main(String[] args) {
}

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
public Future<?> start() {

Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

// create a test table
pool.query("create table test(id int primary key, name varchar(255))")
return pool.query("create table test(id int primary key, name varchar(255))")
.execute()
.compose(r ->
// insert some test data
Expand All @@ -66,6 +68,6 @@ public void start() {
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
}).onFailure(Throwable::printStackTrace);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.example.sqlclient.simple;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.Pool;
Expand All @@ -12,7 +14,7 @@
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -37,18 +39,19 @@ public static void main(String[] args) {
vertx.deployVerticle(new SqlClientExample(options)); }

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
public Future<?> start() {

Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

// create a test table
pool.query("create table test(id int primary key, name varchar(255))")
return pool.query("create table test(id int primary key, name varchar(255))")
.execute()
.compose(r ->
// insert some test data
Expand All @@ -64,6 +67,6 @@ public void start() {
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
}).onFailure(Throwable::printStackTrace);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package io.vertx.example.sqlclient.streaming;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.*;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.*;
import org.testcontainers.containers.PostgreSQLContainer;

/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -35,53 +33,43 @@ public static void main(String[] args) {
vertx.deployVerticle(new SqlClientExample(options)); }

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
public Future<?> start() {

Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

pool.getConnection().compose(connection -> {
Promise<Void> promise = Promise.promise();
return pool.withConnection(connection -> {
// create a test table
connection.query("create table test(id int primary key, name varchar(255))").execute()
return connection
.query("create table test(id int primary key, name varchar(255))")
.execute()
.compose(v -> {
// insert some test data
return connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute();
return connection
.query("insert into test values (1, 'Hello'), (2, 'World')")
.execute();
})
.compose(v -> {
// prepare the query
return connection.prepare("select * from test");
})
.map(preparedStatement -> {
// create a stream
return preparedStatement.createStream(50, Tuple.tuple());
})
.onComplete(ar -> {
if (ar.succeeded()) {
RowStream<Row> stream = ar.result();
.compose(v -> connection
.prepare("select * from test")
.compose(ps -> {
RowStream<Row> stream = ps.createStream(50);
Promise<Void> promise = Promise.promise();
stream
.exceptionHandler(promise::fail)
.endHandler(promise::complete)
.handler(row -> System.out.println("row = " + row.toJson()));
} else {
promise.fail(ar.cause());
}
});
return promise.future().onComplete(v -> {
// close the connection
connection.close();
});
}).onComplete(ar -> {
if (ar.succeeded()) {
System.out.println("done");
} else {
ar.cause().printStackTrace();
}
return promise
.future()
.eventually(ps::close);
}));
}).onSuccess(ar -> {
System.out.println("done");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.example.sqlclient.template;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.Pool;
Expand All @@ -17,7 +19,7 @@
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -43,22 +45,23 @@ public static void main(String[] args) {
}

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
public Future<?> start() {

Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

// create a SQL template for use
SqlTemplate<Map<String, Object>, RowSet<Row>> template = SqlTemplate
.forQuery(pool, "select * from test where id = #{id}");

// create a test table
pool.query("create table test(id int primary key, name varchar(255))")
return pool.query("create table test(id int primary key, name varchar(255))")
.execute()
.compose(r ->
// insert some test data
Expand All @@ -72,6 +75,6 @@ public void start() {
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
}).onFailure(Throwable::printStackTrace);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.example.sqlclient.template_mapping;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.Pool;
Expand All @@ -18,7 +20,7 @@
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -44,15 +46,16 @@ public static void main(String[] args) {
}

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
public Future<?> start() {

Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

// create a SQL template for inserting users
SqlTemplate<User, SqlResult<Void>> insertTemplate = SqlTemplate
Expand All @@ -63,7 +66,7 @@ public void start() {
.forQuery(pool, "select * from users where id = #{id}").mapTo(UserRowMapper.INSTANCE);

// create a test table
pool.query("create table users(id int primary key, first_name varchar(255), last_name varchar(255))")
return pool.query("create table users(id int primary key, first_name varchar(255), last_name varchar(255))")
.execute()
.compose(r ->
// insert some test data
Expand All @@ -78,6 +81,6 @@ public void start() {
for (User user : users) {
System.out.println("user = " + user);
}
}).onFailure(Throwable::printStackTrace);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.example.sqlclient.transaction;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.Pool;
Expand All @@ -12,7 +14,7 @@
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
public class SqlClientExample extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Expand All @@ -38,16 +40,17 @@ public static void main(String[] args) {
}

private final SqlConnectOptions options;
private Pool pool;

public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}

@Override
public void start() {
Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
public Future<?> start() {
pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));

pool.withTransaction(sqlClient -> {
return pool.withTransaction(sqlClient -> {
// create a test table
return sqlClient.query("create table test(id int primary key, name varchar(255))").execute()
.compose(v -> {
Expand All @@ -62,6 +65,6 @@ public void start() {
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
}).onFailure(Throwable::printStackTrace);
});
}
}
Loading

0 comments on commit 62a1988

Please sign in to comment.