@@ -80,8 +80,8 @@ Connecting
8080----------
8181Connect to a Postgres server using the standard URI format:
8282``` rust
83- let conn = PostgresConnection :: connect (" postgres://user:pass@host:port/database?arg1=val1&arg2=val2" ,
84- & NoSsl );
83+ let conn = try ! ( PostgresConnection :: connect (" postgres://user:pass@host:port/database?arg1=val1&arg2=val2" ,
84+ & NoSsl ) );
8585```
8686` pass ` may be omitted if not needed. ` port ` defaults to ` 5432 ` and ` database `
8787defaults to the value of ` user ` if not specified. The driver supports ` trust ` ,
@@ -92,7 +92,7 @@ Statement Preparation
9292Prepared statements can have parameters, represented as ` $n ` where ` n ` is an
9393index into the parameter array starting from 1:
9494``` rust
95- let stmt = conn . prepare (" SELECT * FROM foo WHERE bar = $1 AND baz = $2" );
95+ let stmt = try ! ( conn . prepare (" SELECT * FROM foo WHERE bar = $1 AND baz = $2" ) );
9696```
9797
9898Querying
@@ -111,8 +111,8 @@ fields in a row can be accessed either by their indices or their column names,
111111though access by index is more efficient. Like statement parameters, result
112112columns are one-indexed.
113113``` rust
114- let stmt = conn . prepare (" SELECT bar, baz FROM foo" );
115- for row in stmt . query ([]) {
114+ let stmt = try ! ( conn . prepare (" SELECT bar, baz FROM foo" ) );
115+ for row in try ! ( stmt . query ([]) ) {
116116 let bar : i32 = row [1 ];
117117 let baz : ~str = row [" baz" ];
118118 println! (" bar: {}, baz: {}" , bar , baz );
@@ -121,8 +121,8 @@ for row in stmt.query([]) {
121121In addition, ` PostgresConnection ` has a utility ` execute ` method which is useful
122122if a statement is only going to be executed once:
123123``` rust
124- let updates = conn . execute (" UPDATE foo SET bar = $1 WHERE baz = $2" ,
125- [& 1i32 as & ToSql , & & " biz" as & ToSql ]);
124+ let updates = try ! ( conn . execute (" UPDATE foo SET bar = $1 WHERE baz = $2" ,
125+ [& 1i32 as & ToSql , & & " biz" as & ToSql ]) );
126126println! (" {} rows were updated" , updates );
127127```
128128
@@ -133,9 +133,9 @@ The `transaction` method will start a new transaction. It returns a
133133` PostgresConnection ` as well as methods to control the result of the
134134transaction:
135135``` rust
136- let trans = conn . transaction ();
137- trans . execute (... );
138- let stmt = trans . prepare (... );
136+ let trans = try ! ( conn . transaction () );
137+ try ! ( trans . execute (... ) );
138+ let stmt = try ! ( trans . prepare (... ) );
139139
140140if a_bad_thing_happened {
141141 trans . set_rollback ();
@@ -151,37 +151,20 @@ The transaction will be active until the `PostgresTransaction` object falls out
151151of scope. A transaction will commit by default. Nested transactions are
152152supported via savepoints.
153153
154- Error Handling
155- --------------
156- The methods described above will fail if there is an error. For each of these
157- methods, there is a second variant prefixed with ` try_ ` which returns a
158- ` Result ` :
159- ``` rust
160- match conn . try_execute (query , params ) {
161- Ok (updates ) => println! (" {} rows were updated" , updates ),
162- Err (PgDbError (PostgresDbError { code : NotNullViolation , .. })) =>
163- println! (" Something was NULL that shouldn't be" ),
164- Err (PgDbError (PostgresDbError { code : SyntaxError , .. })) =>
165- println! (" Invalid query syntax" ),
166- _ => println! (" A bad thing happened: {}" , err ),
167- }
168- }
169- ```
170-
171154Connection Pooling
172155------------------
173156A very basic fixed-size connection pool is provided in the ` pool ` module. A
174157single pool can be shared across tasks and ` get_connection ` will block until a
175158connection is available.
176159``` rust
177- let pool = PostgresConnectionPool :: new (" postgres://postgres@localhost" ,
178- NoSsl , 5 );
160+ let pool = try ! ( PostgresConnectionPool :: new (" postgres://postgres@localhost" ,
161+ NoSsl , 5 ) );
179162
180163for _ in range (0 , 10 ) {
181164 let pool = pool . clone ();
182165 spawn (proc () {
183166 let conn = pool . get_connection ();
184- conn . query (... );
167+ conn . query (... ). unwrap () ;
185168 })
186169}
187170```
0 commit comments