Skip to content

Commit 6a0510c

Browse files
committed
Documentation and examples
1 parent 0ffcece commit 6a0510c

39 files changed

+450
-51
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ target/
22
.settings/
33
.classpath
44
.project
5+
.idea
6+
*.iml
7+

README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ Pg-async-driver is available on [Maven Central](http://search.maven.org/#search|
2020

2121
```java
2222
ConnectionPool pool = ...;
23-
pool.query("SELECT 'Hello world!' AS message", new ResultHandler() {
23+
pool.query("select 'Hello world!' as message",
24+
result -> System.out.println(result.get(0).getString("message") ),
25+
error -> error.printStackTrace() );
26+
```
27+
Or with Java <= 7:
28+
29+
```java
30+
ConnectionPool pool = ...;
31+
pool.query("select 'Hello world!' as message", new ResultHandler() {
2432
@Override
2533
public void onResult(ResultSet result) {
2634
System.out.println(result.get(0).getString("message"));
@@ -33,15 +41,6 @@ pool.query("SELECT 'Hello world!' AS message", new ResultHandler() {
3341
});
3442
```
3543

36-
Or with Java 8:
37-
38-
```java
39-
ConnectionPool pool = ...;
40-
pool.query("SELECT 'Hello world!' AS message",
41-
(result) -> System.out.println(result.get(0).getString("message")),
42-
(error) -> error.printStackTrace() );
43-
```
44-
4544
### Connection pools
4645

4746
Connection pools are created with [`com.github.pgasync.ConnectionPoolBuilder`](https://github.com/alaisi/pg-async-driver/blob/master/src/main/java/com/github/pgasync/ConnectionPoolBuilder.java)
@@ -55,15 +54,36 @@ ConnectionPool pool = return new ConnectionPoolBuilder()
5554
.build();
5655
```
5756

58-
Each connection pool will start one IO thread used in communicating with PostgreSQL backend.
57+
Each connection pool will start one IO thread used in communicating with PostgreSQL backend and executing callbacks.
5958

6059
### Prepared statements
6160

62-
TODO
61+
Prepared statements use native PostgreSQL syntax $<index>. Supported parameter types are all primitive types, `String`, temporal types in `java.sql` package and `byte[]`.
62+
63+
```java
64+
pool.query("insert into message(id, body) values($1, $2)", Arrays.asList(123, "hello"),
65+
result -> System.out.printf("Inserted %d rows", result.updatedRows() ),
66+
error -> error.printStackTrace() );
67+
```
6368

6469
### Transactions
6570

66-
TODO
71+
A transactional unit of work is started with `begin()`. Queries issued against connection passed to callback are executed in the same transaction. The transaction must always be committed or rolled back.
72+
73+
```java
74+
ErrorHandler err = error -> error.printStackTrace();
75+
pool.begin((connection, transaction) -> {
76+
connection.query("select 1 as id",
77+
result -> {
78+
System.out.println("Result is %d", result.get(0).getLong("id"));
79+
transaction.commit(() -> System.out.println("Transaction committed"), err);
80+
},
81+
error -> {
82+
System.out.println("Query failed");
83+
transaction.rollback(() -> System.out.println("Transaction rolled back"), err);
84+
})
85+
}, err)
86+
```
6787

6888
## References
6989
* [Scala postgresql-async](https://raw.github.com/mauricio/postgresql-async)

src/main/java/com/github/pgasync/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public interface Connection {
4444
*
4545
* @param sql SQL to execute
4646
* @param params List of parameters
47-
* @param onResult
48-
* @param onError
47+
* @param onResult Called when query is completed
48+
* @param onError Called on exception thrown
4949
*/
5050
void query(String sql, List/*<Object>*/ params, ResultHandler onResult, ErrorHandler onError);
5151

src/main/java/com/github/pgasync/ResultSet.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,25 @@
2424
*/
2525
public interface ResultSet extends Iterable<Row> {
2626

27+
/**
28+
* @return Row iterator
29+
*/
2730
Iterator<Row> iterator();
2831

32+
/**
33+
* @param index Row index starting from 0
34+
* @return Row, never null
35+
*/
2936
Row get(int index);
3037

38+
/**
39+
* @return Amount of result rows.
40+
*/
3141
int size();
3242

43+
/**
44+
* @return Amount of modified rows.
45+
*/
3346
int updatedRows();
3447

3548
}

src/main/java/com/github/pgasync/Transaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.github.pgasync.callback.TransactionCompletedHandler;
1919

2020
/**
21-
* A unit of work. Transactions must be commited or rolled back, otherwise a
21+
* A unit of work. Transactions must be committed or rolled back, otherwise a
2222
* connection left is a stale state.
2323
*
2424
* @author Antti Laisi

src/main/java/com/github/pgasync/callback/ChainedErrorHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
package com.github.pgasync.callback;
1616

17+
/**
18+
* Delegating error handler
19+
*
20+
* @author Antti Laisi
21+
*/
1722
public class ChainedErrorHandler implements ErrorHandler {
1823

1924
final ErrorHandler onError;

src/main/java/com/github/pgasync/callback/ConnectionHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
import com.github.pgasync.Connection;
1818

19+
/**
20+
* Callback for fetching a connection from pool.
21+
*
22+
* @author Antti Laisi
23+
*/
1924
public interface ConnectionHandler {
2025

2126
void onConnection(Connection connection);

src/main/java/com/github/pgasync/callback/ErrorHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414

1515
package com.github.pgasync.callback;
1616

17+
/**
18+
* Callback for errors.
19+
*
20+
* @author Antti Laisi
21+
*/
1722
public interface ErrorHandler {
1823

24+
/**
25+
* Executed on exception.
26+
* @param t Error
27+
*/
1928
void onError(Throwable t);
2029

2130
}

src/main/java/com/github/pgasync/callback/ResultHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
import com.github.pgasync.ResultSet;
1818

19+
/**
20+
* Callback for processing query results.
21+
*
22+
* @author Antti Laisi
23+
*/
1924
public interface ResultHandler {
2025

2126
void onResult(ResultSet result);

src/main/java/com/github/pgasync/callback/TransactionHandler.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@
1717
import com.github.pgasync.Connection;
1818
import com.github.pgasync.Transaction;
1919

20+
/**
21+
* Callback for a started transaction.
22+
*
23+
* @author Antti Laisi
24+
*/
2025
public interface TransactionHandler {
2126

27+
/**
28+
* Called when the transaction is started.
29+
*
30+
* @param txconn Transactional connection, queries are executed in the same transaction
31+
* @param transaction Transaction, must be committed or rolled back
32+
*/
2233
void onBegin(Connection txconn, Transaction transaction);
2334

2435
}

src/main/java/com/github/pgasync/impl/PgConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import com.github.pgasync.impl.message.Query;
4242
import com.github.pgasync.impl.message.ReadyForQuery;
4343
import com.github.pgasync.impl.message.RowDescription;
44-
import com.github.pgasync.impl.message.Startup;
44+
import com.github.pgasync.impl.message.StartupMessage;
4545

4646
/**
4747
* A connection to PostgreSQL backed. The postmaster forks a backend process for
@@ -75,7 +75,7 @@ public void connect(String username, String password, String database,
7575
this.password = password;
7676
this.connectedHandler = onConnected;
7777
this.errorHandler = onError;
78-
stream.connect(new Startup(username, database), this);
78+
stream.connect(new StartupMessage(username, database), this);
7979
}
8080

8181
public boolean isConnected() {

src/main/java/com/github/pgasync/impl/PgProtocolStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.github.pgasync.impl;
1616

1717
import com.github.pgasync.impl.message.Message;
18-
import com.github.pgasync.impl.message.Startup;
18+
import com.github.pgasync.impl.message.StartupMessage;
1919

2020
/**
2121
* Stream of messages from/to backend server.
@@ -24,7 +24,7 @@
2424
*/
2525
public interface PgProtocolStream {
2626

27-
void connect(Startup startup, PgProtocolCallbacks callbacks);
27+
void connect(StartupMessage startup, PgProtocolCallbacks callbacks);
2828

2929
void send(Message... messages);
3030

src/main/java/com/github/pgasync/impl/io/AuthenticationDecoder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@
1818

1919
import com.github.pgasync.impl.message.Authentication;
2020

21+
/**
22+
* See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">PostgreSQL message formats</a>
23+
*
24+
* <pre>
25+
* AuthenticationOk (B)
26+
* Byte1('R')
27+
* Identifies the message as an authentication request.
28+
* Int32(8)
29+
* Length of message contents in bytes, including self.
30+
* Int32(0)
31+
* Specifies that the authentication was successful.
32+
*
33+
* AuthenticationMD5Password (B)
34+
* Byte1('R')
35+
* Identifies the message as an authentication request.
36+
* Int32(12)
37+
* Length of message contents in bytes, including self.
38+
* Int32(5)
39+
* Specifies that an MD5-encrypted password is required.
40+
* Byte4
41+
* The salt to use when encrypting the password.
42+
* </pre>
43+
*
44+
* @author Antti Laisi
45+
*/
2146
public class AuthenticationDecoder implements Decoder<Authentication> {
2247

2348
static final int OK = 0;

src/main/java/com/github/pgasync/impl/io/BindEncoder.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@
1818

1919
import com.github.pgasync.impl.message.Bind;
2020

21+
/**
22+
* See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">PostgreSQL message formats</a>
23+
*
24+
* <pre>
25+
* Bind (F)
26+
* Byte1('B')
27+
* Identifies the message as a Bind command.
28+
* Int32
29+
* Length of message contents in bytes, including self.
30+
* String
31+
* The name of the destination portal (an empty string selects the unnamed portal).
32+
* String
33+
* The name of the source prepared statement (an empty string selects the unnamed prepared statement).
34+
* Int16
35+
* The number of parameter format codes that follow (denoted C below). This can be zero to indicate that there are no parameters or that the parameters all use the default format (text); or one, in which case the specified format code is applied to all parameters; or it can equal the actual number of parameters.
36+
* Int16[C]
37+
* The parameter format codes. Each must presently be zero (text) or one (binary).
38+
* Int16
39+
* The number of parameter values that follow (possibly zero). This must match the number of parameters needed by the query.
40+
* Next, the following pair of fields appear for each parameter:
41+
* Int32
42+
* The length of the parameter value, in bytes (this count does not include itself). Can be zero. As a special case, -1 indicates a NULL parameter value. No value bytes follow in the NULL case.
43+
* Byten
44+
* The value of the parameter, in the format indicated by the associated format code. n is the above length.
45+
* After the last parameter, the following fields appear:
46+
* Int16
47+
* The number of result-column format codes that follow (denoted R below). This can be zero to indicate that there are no result columns or that the result columns should all use the default format (text); or one, in which case the specified format code is applied to all result columns (if any); or it can equal the actual number of result columns of the query.
48+
* Int16[R]
49+
* The result-column format codes. Each must presently be zero (text) or one (binary).
50+
*</pre>
51+
*
52+
* @author Antti Laisi
53+
*/
2154
public class BindEncoder implements Encoder<Bind> {
2255

2356
@Override

src/main/java/com/github/pgasync/impl/io/CommandCompleteDecoder.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020

2121
import com.github.pgasync.impl.message.CommandComplete;
2222

23+
/**
24+
* See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">PostgreSQL message formats</a>
25+
*
26+
* <pre>
27+
* CommandComplete (B)
28+
* Byte1('C')
29+
* Identifies the message as a command-completed response.
30+
* Int32
31+
* Length of message contents in bytes, including self.
32+
* String
33+
* The command tag. This is usually a single word that identifies which SQL command was completed.
34+
* For an INSERT command, the tag is INSERT oid rows, where rows is the number of rows inserted. oid is the object ID of the inserted row if rows is 1 and the target table has OIDs; otherwise oid is 0.
35+
* For a DELETE command, the tag is DELETE rows where rows is the number of rows deleted.
36+
* For an UPDATE command, the tag is UPDATE rows where rows is the number of rows updated.
37+
* For a SELECT or CREATE TABLE AS command, the tag is SELECT rows where rows is the number of rows retrieved.
38+
* For a MOVE command, the tag is MOVE rows where rows is the number of rows the cursor's position has been changed by.
39+
* For a FETCH command, the tag is FETCH rows where rows is the number of rows that have been retrieved from the cursor.
40+
* For a COPY command, the tag is COPY rows where rows is the number of rows copied. (Note: the row count appears only in PostgreSQL 8.2 and later.)
41+
* </pre>
42+
*
43+
* @author Antti Laisi
44+
*/
2345
public class CommandCompleteDecoder implements Decoder<CommandComplete> {
2446

2547
@Override

src/main/java/com/github/pgasync/impl/io/DataRowDecoder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818

1919
import com.github.pgasync.impl.message.DataRow;
2020

21+
/**
22+
* See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">PostgreSQL message formats</a>
23+
*
24+
* <pre>
25+
* DataRow (B)
26+
* Byte1('D')
27+
* Identifies the message as a data row.
28+
* Int32
29+
* Length of message contents in bytes, including self.
30+
* Int16
31+
* The number of column values that follow (possibly zero).
32+
* Next, the following pair of fields appear for each column:
33+
* Int32
34+
* The length of the column value, in bytes (this count does not include itself). Can be zero. As a special case, -1 indicates a NULL column value. No value bytes follow in the NULL case.
35+
* Byten
36+
* The value of the column, in the format indicated by the associated format code. n is the above length.
37+
* </pre>
38+
*
39+
* @author Antti Laisi
40+
*/
2141
public class DataRowDecoder implements Decoder<DataRow> {
2242

2343
@Override

src/main/java/com/github/pgasync/impl/io/Decoder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@
1818

1919
import com.github.pgasync.impl.message.Message;
2020

21+
/**
22+
* Decoder reads messages from byte buffer.
23+
*
24+
* @author Antti Laisi
25+
*/
2126
public interface Decoder<T extends Message> {
2227

28+
/**
29+
* @return Protocol message id
30+
*/
2331
byte getMessageId();
2432

33+
/**
34+
* @return Decoded message
35+
*/
2536
T read(ByteBuffer buffer);
2637

2738
}

0 commit comments

Comments
 (0)