8000 Updated readme. · juliombb/postgres-async-driver@f6f0afb · GitHub
[go: up one dir, main page]

Skip to content

Commit f6f0afb

Browse files
committed
Updated readme.
1 parent 45474a7 commit f6f0afb

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,40 @@ Postgres-async-driver is available on [Maven Central](http://search.maven.org/#s
1010
<dependency>
1111
<groupId>com.github.alaisi.pgasync</groupId>
1212
<artifactId>postgres-async-driver</artifactId>
13-
<version>0.6</version>
13+
<version>0.7</version>
1414
</dependency>
1515
```
1616

1717
## Usage
1818

1919
### Hello world
2020

21-
Queries are submitted to a `Db` with success and failure callbacks.
21+
Querying for a set returns an [rx.Observable](http://reactivex.io/documentation/observable.html) that emits a single [ResultSet](https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/ResultSet.java).
2222

2323
```java
2424
Db db = ...;
25-
db.query("select 'Hello world!' as message",
26-
result -> out.println(result.row(0).getString("message") ),
27-
error -> error.printStackTrace() );
25+
db.querySet("select 'Hello world!' as message")
26+
.map(result -> result.row(0).getString("message"))
27+
.subscribe(System.out::println)
28+
29+
// => Hello world
30+
```
31+
32+
Querying for rows returns an [rx.Observable](http://reactivex.io/documentation/observable.html) that emits 0-n [Rows](https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/Row.java).
33+
34+
```java
35+
Db db = ...;
36+
db.queryRows("select unnest('{ hello, world }'::text[] as message)")
37+
.map(row -> row.getString("message"))
38+
.subscribe(System.out::println)
39+
40+
// => hello
41+
// => world
2842
```
2943

3044
### Creating a Db
3145

32-
Db is usually a connection pool that is created with [`com.github.pgasync.ConnectionPoolBuilder`](https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/ConnectionPoolBuilder.java)
46+
Db is a connection pool that is created with [`com.github.pgasync.ConnectionPoolBuilder`](https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/ConnectionPoolBuilder.java)
3347

3448
```java
3549
Db db = new ConnectionPoolBuilder()
@@ -49,25 +63,24 @@ Each connection *pool* will start only one IO thread used in communicating with
4963
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[]`.
5064

5165
```java
52-
db.query("insert into message(id, body) values($1, $2)", Arrays.asList(123, "hello"),
53-
result -> out.printf("Inserted %d rows", result.updatedRows() ),
54-
error -> error.printStackTrace() );
66+
db.querySet("insert into message(id, body) values($1, $2)", 123, "hello"))
67+
.subscribe(result -> out.printf("Inserted %d rows", result.updatedRows() ));
5568
```
5669

5770
### Transactions
5871

59-
A transactional unit of work is started with `begin()`. Queries issued to transaction passed to callback are executed in the same transaction and the tx is automatically rolled back on query failure.
72+
A transactional unit of work is started with `begin()`. Queries issued to the emitted [Transaction](https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/Transaction.java) are executed in the same transaction and the tx is automatically rolled back on query failure.
6073

6174
```java
62-
Consumer<Throwable> onError = error -> error.printStackTrace();
63-
db.begin(transaction -> {
64-
transaction.query("select 1 as id",
65-
result -> {
66-
out.printf("Result is %d", result.row(0).getLong("id"));
67-
transaction.commit(() -> out.println("Transaction committed"), onError);
68-
},
69-
error -> err.println("Query failed, tx is now rolled back"))
70-
}, onError)
75+
db.begin()
76+
.flatMap(tx -> tx.querySet("insert into products (name) values ($1) returning id", "saw")
77+
.map(productsResult -> productsResult.row(0).getLong("id"))
78+
.flatMap(id -> tx.querySet("insert into promotions (product_id) values ($1)", id))
79+
.flatMap(promotionsResult -> tx.commit())
80+
).subscribe(
81+
__ -> System.out.println("Transaction committed"),
82+
Throwable::printStackTrace);
83+
7184
```
7285

7386
### Custom data types

0 commit comments

Comments
 (0)
0