1
1
# Rust-Postgres
2
2
A native PostgreSQL driver for Rust.
3
3
4
- [ Documentation] ( https://sfackler.github.io/rust-postgres/doc/v0.10.2 /postgres )
4
+ [ Documentation] ( https://sfackler.github.io/rust-postgres/doc/v0.11.0 /postgres )
5
5
6
6
[ ![ Build Status] ( https://travis-ci.org/sfackler/rust-postgres.png?branch=master )] ( https://travis-ci.org/sfackler/rust-postgres ) [ ![ Latest Version] ( https://img.shields.io/crates/v/postgres.svg )] ( https://crates.io/crates/postgres )
7
7
8
8
You can integrate Rust-Postgres into your project through the [ releases on crates.io] ( https://crates.io/crates/postgres ) :
9
9
``` toml
10
10
# Cargo.toml
11
11
[dependencies ]
12
- postgres = " 0.10 "
12
+ postgres = " 0.11 "
13
13
```
14
14
15
15
## Overview
16
- Rust-Postgres is a pure-Rust frontend for the popular PostgreSQL database. It
17
- exposes a high level interface in the vein of JDBC or Go's ` database/sql `
18
- package.
16
+ Rust-Postgres is a pure-Rust frontend for the popular PostgreSQL database.
19
17
``` rust
20
18
extern crate postgres;
21
19
@@ -28,7 +26,7 @@ struct Person {
28
26
}
29
27
30
28
fn main () {
31
- let conn = Connection :: connect (" postgres://postgres@localhost" , & SslMode :: None )
29
+ let conn = Connection :: connect (" postgres://postgres@localhost" , SslMode :: None )
32
30
. unwrap ();
33
31
34
32
conn . execute (" CREATE TABLE person (
@@ -44,8 +42,7 @@ fn main() {
44
42
conn . execute (" INSERT INTO person (name, data) VALUES ($1, $2)" ,
45
43
& [& me . name, & me . data]). unwrap ();
46
44
47
- let stmt = conn . prepare (" SELECT id, name, data FROM person" ). unwrap ();
48
- for row in stmt . query (& []). unwrap () {
45
+ for row in & conn . query (" SELECT id, name, data FROM person" , & []). unwrap () {
49
46
let person = Person {
50
47
id : row . get (0 ),
51
48
name : row . get (1 ),
@@ -57,7 +54,7 @@ fn main() {
57
54
```
58
55
59
56
## Requirements
60
- * ** Rust** - Rust-Postgres is developed against the 1.3 release of Rust
57
+ * ** Rust** - Rust-Postgres is developed against the 1.4 release of Rust
61
58
available on http://www.rust-lang.org . It should also compile against more
62
59
recent releases.
63
60
@@ -71,7 +68,7 @@ fn main() {
71
68
Connect to a Postgres server using the standard URI format:
72
69
``` rust
73
70
let conn = try ! (Connection :: connect (" postgres://user:pass@host:port/database?arg1=val1&arg2=val2" ,
74
- & SslMode :: None ));
71
+ SslMode :: None ));
75
72
```
76
73
` pass ` may be omitted if not needed. ` port ` defaults to ` 5432 ` and ` database `
77
74
defaults to the value of ` user ` if not specified. The driver supports ` trust ` ,
@@ -80,48 +77,48 @@ defaults to the value of `user` if not specified. The driver supports `trust`,
80
77
Unix domain sockets can be used as well by activating the ` unix_socket ` feature.
81
78
The ` host ` portion of the URI should be set to the absolute path to the
82
79
directory containing the socket file. Since ` / ` is a reserved character in
83
- URLs, the path should be URL encoded.
80
+ URLs, the path should be URL encoded. If Postgres stored its socket files in
81
+ ` /run/postgres ` , the connection would then look like:
84
82
``` rust
85
- let conn = try ! (Connection :: connect (" postgres://postgres@%2Frun%2Fpostgres" , & SslMode :: None ));
83
+ let conn = try ! (Connection :: connect (" postgres://postgres@%2Frun%2Fpostgres" , SslMode :: None ));
86
84
```
87
85
Paths which contain non-UTF8 characters can be handled in a different manner;
88
86
see the documentation for details.
89
87
90
- ### Statement Preparation
91
- Prepared statements can have parameters, represented as ` $n ` where ` n ` is an
92
- index into the parameter array starting from 1:
93
- ``` rust
94
- let stmt = try ! (conn . prepare (" SELECT * FROM foo WHERE bar = $1 AND baz = $2" ));
95
- ```
96
-
97
88
### Querying
98
- A prepared statement can be executed with the ` query ` and ` execute ` methods.
99
- Both methods take an array of parameters to bind to the query represented as
100
- ` &ToSql ` trait objects. ` execute ` returns the number of rows affected by the
101
- query (or 0 if not applicable):
89
+ SQL statements can be executed with the ` query ` and ` execute ` methods. Both
90
+ methods take a query string as well as a slice of parameters to bind to the
91
+ query. The ` i ` th query parameter is specified in the query string by ` $i ` . Note
92
+ that query parameters are 1-indexed rather than the more common 0-indexing.
93
+
94
+ ` execute ` returns the number of rows affected by the query (or 0 if not
95
+ applicable):
102
96
``` rust
103
- let stmt = try ! (conn . prepare (" UPDATE foo SET bar = $1 WHERE baz = $2" ));
104
- let updates = try ! (stmt . execute (& [& 1i32 , & " biz" ]));
97
+ let updates = try ! (conn . execute (" UPDATE foo SET bar = $1 WHERE baz = $2" , & [& 1i32 , & " biz" ]));
105
98
println! (" {} rows were updated" , updates );
106
99
```
107
- ` query ` returns an iterator over the rows returned from the database. The
108
- fields in a row can be accessed either by their indices or their column names,
109
- though access by index is more efficient. Unlike statement parameters, result
110
- columns are zero-indexed.
100
+
101
+ ` query ` returns an iterable object holding the rows returned from the database.
102
+ The fields in a row can be accessed either by their indices or their column
103
+ names, though access by index is more efficient. Unlike statement parameters,
104
+ result columns are zero-indexed.
111
105
``` rust
112
- let stmt = try ! (conn . prepare (" SELECT bar, baz FROM foo" ));
113
- for row in try ! (stmt . query (& [])) {
106
+ for row in & try ! (conn . query (" SELECT bar, baz FROM foo WHERE buz = $1" , & [& 1i32 ])) {
114
107
let bar : i32 = row . get (0 );
115
108
let baz : String = row . get (" baz" );
116
109
println! (" bar: {}, baz: {}" , bar , baz );
117
110
}
118
111
```
119
- In addition, ` Connection ` has a utility ` execute ` method which is useful if a
120
- statement is only going to be executed once:
112
+
113
+ ### Statement Preparation
114
+ If the same statement will be executed repeatedly (possibly with different
115
+ parameters), explicitly preparing it can improve performance:
116
+
121
117
``` rust
122
- let updates = try ! (conn . execute (" UPDATE foo SET bar = $1 WHERE baz = $2" ,
123
- & [& 1i32 , & " biz" ]));
124
- println! (" {} rows were updated" , updates );
118
+ let stmt = try ! (conn . prepare (" UPDATE foo SET bar = $1 WHERE baz = $2" ));
119
+ for (bar , baz ) in updates {
120
+ try ! (stmt . execute (& [bar , baz ]));
121
+ }
125
122
```
126
123
127
124
### Transactions
@@ -131,14 +128,12 @@ The `transaction` method will start a new transaction. It returns a
131
128
transaction:
132
129
``` rust
133
130
let trans = try ! (conn . transaction ());
131
+
134
132
try ! (trans . execute (... ));
135
133
let stmt = try ! (trans . prepare (... ));
134
+ // ...
136
135
137
- if the_coast_is_clear {
138
- trans . set_commit ();
139
- }
140
-
141
- try ! (trans . finish ());
136
+ try ! (trans . commit ());
142
137
```
143
138
The transaction will be active until the ` Transaction ` object falls out of
144
139
scope. A transaction will roll back by default. Nested transactions are
@@ -246,6 +241,13 @@ types. The driver currently supports the following conversions:
246
241
</td>
247
242
<td>UUID</td>
248
243
</tr>
244
+ <tr>
245
+ <td>
246
+ <a href="https://github.com/contain-rs/bit-vec">bit_vec::BitVec</a>
247
+ (<a href="#optional-features">optional</a>)
248
+ </td>
249
+ <td>BIT, VARBIT</td>
250
+ </tr>
249
251
<tr>
250
252
<td>HashMap<String, Option<String>></td>
251
253
<td>HSTORE</td>
@@ -285,8 +287,7 @@ implementations for `uuid`'s `Uuid` type.
285
287
support is provided optionally by the ` rustc-serialize ` feature, which adds
286
288
` ToSql ` and ` FromSql ` implementations for ` rustc-serialize ` 's ` Json ` type, and
287
289
the ` serde_json ` feature, which adds implementations for ` serde_json ` 's ` Value `
288
- type. The ` serde ` feature provides implementations for the older
289
- ` serde::json::Value ` type.
290
+ type.
290
291
291
292
### TIMESTAMP/TIMESTAMPTZ/DATE/TIME types
292
293
@@ -295,3 +296,9 @@ support is provided optionally by the `time` feature, which adds `ToSql` and
295
296
` FromSql ` implementations for ` time ` 's ` Timespec ` type, or the ` chrono `
296
297
feature, which adds ` ToSql ` and ` FromSql ` implementations for ` chrono ` 's
297
298
` DateTime ` , ` NaiveDateTime ` , ` NaiveDate ` and ` NaiveTime ` types.
299
+
300
+ ### BIT/VARBIT types
301
+
302
+ [ BIT and VARBIT] ( http://www.postgresql.org/docs/9.4/static/datatype-bit.html )
303
+ support is provided optionally by the ` bit-vec ` feature, which adds ` ToSql ` and
304
+ ` FromSql ` implementations for ` bit-vec ` 's ` BitVec ` type.
0 commit comments