Example usage for io.vertx.pgclient PgConnectOptions PgConnectOptions

List of usage examples for io.vertx.pgclient PgConnectOptions PgConnectOptions

Introduction

In this page you can find the example usage for io.vertx.pgclient PgConnectOptions PgConnectOptions.

Prototype

public PgConnectOptions() 

Source Link

Usage

From source file:examples.PgClientExamples.java

License:Apache License

public void gettingStarted() {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the client pool
    PgPool client = PgPool.pool(connectOptions, poolOptions);

    // A simple query
    client.query("SELECT * FROM users WHERE id='julien'", ar -> {
        if (ar.succeeded()) {
            RowSet<Row> result = ar.result();
            System.out.println("Got " + result.size() + " rows ");
        } else {/*from  w w w.j av a 2s . c o  m*/
            System.out.println("Failure: " + ar.cause().getMessage());
        }

        // Now close the pool
        client.close();
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void configureFromDataObject(Vertx vertx) {

    // Data object
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool Options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pool from the data object
    PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);

    pool.getConnection(ar -> {//from  w  w  w .ja va 2 s  .c o m
        // Handling your connection
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void configureDefaultSchema() {
    // Data object
    PgConnectOptions connectOptions = new PgConnectOptions();

    // Set the default schema
    connectOptions.addProperty("search_path", "myschema");
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting01() {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(connectOptions, poolOptions);
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting02(Vertx vertx) {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
    // Create the pooled client
    PgPool client = PgPool.pool(vertx, connectOptions, poolOptions);
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting04(Vertx vertx) {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {//w  w w. j a  v  a2 s.co m

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Release the connection to the pool
                        conn.close();
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting05(Vertx vertx) {

    // Pool options
    PgConnectOptions options = new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db")
            .setUser("user").setPassword("secret");

    // Connect to Postgres
    PgConnection.connect(vertx, options, res -> {
        if (res.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            PgConnection conn = res.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Close the connection
                        conn.close();/*from   w w w. j ava 2 s . co m*/
                    });
                } else {
                    // Close the connection
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + res.cause().getMessage());
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting06(Vertx vertx) {

    // Connect Options
    // Socket file name will be /var/run/postgresql/.s.PGSQL.5432
    PgConnectOptions connectOptions = new PgConnectOptions().setHost("/var/run/postgresql").setPort(5432)
            .setDatabase("the-db");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(connectOptions, poolOptions);

    // Create the pooled client with a vertx instance
    // Make sure the vertx instance has enabled native transports
    PgPool client2 = PgPool.pool(vertx, connectOptions, poolOptions);
}

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub02(Vertx vertx) {

    PgSubscriber subscriber = PgSubscriber.subscriber(vertx, new PgConnectOptions().setPort(5432)
            .setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"));

    // You can set the channel before connect
    subscriber.channel("channel1").handler(payload -> {
        System.out.println("Received " + payload);
    });//from w  w w  . j ava 2 s .  c  o  m

    subscriber.connect(ar -> {
        if (ar.succeeded()) {

            // Or you can set the channel after connect
            subscriber.channel("channel2").handler(payload -> {
                System.out.println("Received " + payload);
            });
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub03(Vertx vertx) {

    PgSubscriber subscriber = PgSubscriber.subscriber(vertx, new PgConnectOptions().setPort(5432)
            .setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"));

    subscriber.connect(ar -> {/*from  ww w  .j av  a  2  s.co  m*/
        if (ar.succeeded()) {
            // Complex channel name - name in PostgreSQL requires a quoted ID
            subscriber.channel("Complex.Channel.Name").handler(payload -> {
                System.out.println("Received " + payload);
            });
            subscriber.channel("Complex.Channel.Name").subscribeHandler(subscribed -> {
                subscriber.actualConnection().query("NOTIFY \"Complex.Channel.Name\", 'msg'", notified -> {
                    System.out.println("Notified \"Complex.Channel.Name\"");
                });
            });

            // PostgreSQL simple ID's are forced lower-case
            subscriber.channel("simple_channel").handler(payload -> {
                System.out.println("Received " + payload);
            });
            subscriber.channel("simple_channel").subscribeHandler(subscribed -> {
                // The following simple channel identifier is forced to lower case
                subscriber.actualConnection().query("NOTIFY Simple_CHANNEL, 'msg'", notified -> {
                    System.out.println("Notified simple_channel");
                });
            });

            // The following channel name is longer than the current
            // (NAMEDATALEN = 64) - 1 == 63 character limit and will be truncated
            subscriber.channel("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb")
                    .handler(payload -> {
                        System.out.println("Received " + payload);
                    });
        }
    });
}