Giter Club home page Giter Club logo

postgres-async-driver's Introduction

postgres-async-driver - Asynchronous PostgreSQL Java driver

Maven Central

Postgres-async-driver is a non-blocking Java driver for PostgreSQL. The driver supports connection pooling, prepared statements, transactions, all standard SQL types and custom column types.

Download

Postgres-async-driver is available on Maven Central.

<dependency>
    <groupId>com.github.alaisi.pgasync</groupId>
    <artifactId>postgres-async-driver</artifactId>
    <version>0.9</version>
</dependency>

Usage

Hello world

Querying for a set returns an rx.Observable that emits a single ResultSet.

Db db = ...;
db.querySet("select 'Hello world!' as message")
    .map(result -> result.row(0).getString("message"))
    .subscribe(System.out::println)

// => Hello world

Querying for rows returns an rx.Observable that emits 0-n Rows. The rows are emitted immediately as they are received from the server instead of waiting for the entire query to complete.

Db db = ...;
db.queryRows("select unnest('{ hello, world }'::text[] as message)")
    .map(row -> row.getString("message"))
    .subscribe(System.out::println)

// => hello
// => world

Creating a Db

Db is a connection pool that is created with com.github.pgasync.ConnectionPoolBuilder

Db db = new ConnectionPoolBuilder()
    .hostname("localhost")
    .port(5432)
    .database("db")
    .username("user")
    .password("pass")
    .poolSize(20)
    .build();

Each connection pool will start only one IO thread used in communicating with PostgreSQL backend and executing callbacks for all connections.

Prepared statements

Prepared statements use native PostgreSQL syntax $index. Supported parameter types are all primitive types, String, BigDecimal, BigInteger, UUID, temporal types in java.sql package and byte[].

db.querySet("insert into message(id, body) values($1, $2)", 123, "hello")
    .subscribe(result -> out.printf("Inserted %d rows", result.updatedRows() ));

Transactions

A transactional unit of work is started with begin(). Queries issued to the emitted Transaction are executed in the same transaction and the tx is automatically rolled back on query failure.

db.begin()
    .flatMap(tx -> tx.querySet("insert into products (name) values ($1) returning id", "saw")
        .map(productsResult -> productsResult.row(0).getLong("id"))
        .flatMap(id -> tx.querySet("insert into promotions (product_id) values ($1)", id))
        .flatMap(promotionsResult -> tx.commit())
    ).subscribe(
        __ -> System.out.println("Transaction committed"),
        Throwable::printStackTrace);

Custom data types

Support for additional data types requires registering converters to com.github.pgasync.ConnectionPoolBuilder

class JsonConverter implements Converter<example.Json> {
    @Override
    public Class<example.Json> type() {
        return example.Json.class;
    }
    @Override
    public byte[] from(example.Json json) {
        return json.toBytes();
    }
    @Override
    public example.Json to(Oid oid, byte[] value) {
        return new example.Json(new String(value, UTF_8));
    }
}

Db db = return new ConnectionPoolBuilder()
    ...
    .converters(new JsonConverter())
    .build();

Used in

References

postgres-async-driver's People

Contributors

alaisi avatar moea avatar thoughtmanifest avatar ljodal avatar cretz avatar gmokki avatar meoyawn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.