Skip to content

Commit ac2b849

Browse files
committed
Split out transaction to a module
1 parent 4266c79 commit ac2b849

File tree

2 files changed

+79
-61
lines changed

2 files changed

+79
-61
lines changed

postgres-tokio/src/lib.rs

+6-61
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ pub use postgres_shared::{params, Column, RowIndex};
3535
use error::{ConnectError, Error, DbError, SqlState};
3636
use params::{ConnectParams, IntoConnectParams};
3737
use stream::PostgresStream;
38-
use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, WrongType, Other, Kind, Field};
3938
use tls::Handshake;
39+
use transaction::Transaction;
40+
use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, WrongType, Other, Kind, Field};
4041

4142
pub mod error;
4243
mod stream;
4344
pub mod tls;
45+
pub mod transaction;
4446
#[macro_use]
4547
pub mod types;
4648

@@ -905,7 +907,7 @@ impl Connection {
905907

906908
pub fn transaction(self) -> BoxFuture<Transaction, Error> {
907909
self.simple_query("BEGIN")
908-
.map(|(_, c)| Transaction(c))
910+
.map(|(_, c)| Transaction::new(c))
909911
.boxed()
910912
}
911913

@@ -991,59 +993,6 @@ impl Row {
991993
}
992994
}
993995

994-
#[derive(Debug)]
995-
pub struct Transaction(Connection);
996-
997-
impl Transaction {
998-
pub fn batch_execute(self, query: &str) -> BoxFuture<Transaction, Error<Transaction>> {
999-
self.0.batch_execute(query)
1000-
.map(Transaction)
1001-
.map_err(transaction_err)
1002-
.boxed()
1003-
}
1004-
1005-
pub fn prepare(self, query: &str) -> BoxFuture<(Statement, Transaction), Error<Transaction>> {
1006-
self.0.prepare(query)
1007-
.map(|(s, c)| (s, Transaction(c)))
1008-
.map_err(transaction_err)
1009-
.boxed()
1010-
}
1011-
1012-
pub fn execute(self,
1013-
statement: &Statement,
1014-
params: &[&ToSql])
1015-
-> BoxFuture<(u64, Transaction), Error<Transaction>> {
1016-
self.0.execute(statement, params)
1017-
.map(|(n, c)| (n, Transaction(c)))
1018-
.map_err(transaction_err)
1019-
.boxed()
1020-
}
1021-
1022-
pub fn query(self,
1023-
statement: &Statement,
1024-
params: &[&ToSql])
1025-
-> BoxStateStream<Row, Transaction, Error<Transaction>> {
1026-
self.0.query(statement, params)
1027-
.map_state(Transaction)
1028-
.map_err(transaction_err)
1029-
.boxed()
1030-
}
1031-
1032-
pub fn commit(self) -> BoxFuture<Connection, Error> {
1033-
self.finish("COMMIT")
1034-
}
1035-
1036-
pub fn rollback(self) -> BoxFuture<Connection, Error> {
1037-
self.finish("ROLLBACK")
1038-
}
1039-
1040-
fn finish(self, query: &str) -> BoxFuture<Connection, Error> {
1041-
self.0.simple_query(query)
1042-
.map(|(_, c)| c)
1043-
.boxed()
1044-
}
1045-
}
1046-
1047996
fn connect_err(fields: &mut ErrorFields) -> ConnectError {
1048997
match DbError::new(fields) {
1049998
Ok(err) => ConnectError::Db(Box::new(err)),
@@ -1057,10 +1006,6 @@ fn bad_message<T>() -> T
10571006
io::Error::new(io::ErrorKind::InvalidInput, "unexpected message").into()
10581007
}
10591008

1060-
fn transaction_err(e: Error) -> Error<Transaction> {
1061-
match e {
1062-
Error::Io(e) => Error::Io(e),
1063-
Error::Db(e, c) => Error::Db(e, Transaction(c)),
1064-
Error::Conversion(e, c) => Error::Conversion(e, Transaction(c))
1065-
}
1009+
trait TransactionNew {
1010+
fn new(c: Connection) -> Transaction;
10661011
}

postgres-tokio/src/transaction.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use futures::{Future, BoxFuture};
2+
use futures_state_stream::{StateStream, BoxStateStream};
3+
4+
use {Connection, Statement, Row, TransactionNew};
5+
use error::Error;
6+
use types::ToSql;
7+
8+
#[derive(Debug)]
9+
pub struct Transaction(Connection);
10+
11+
impl TransactionNew for Transaction {
12+
fn new(c: Connection) -> Transaction {
13+
Transaction(c)
14+
}
15+
}
16+
17+
impl Transaction {
18+
pub fn batch_execute(self, query: &str) -> BoxFuture<Transaction, Error<Transaction>> {
19+
self.0.batch_execute(query)
20+
.map(Transaction)
21+
.map_err(transaction_err)
22+
.boxed()
23+
}
24+
25+
pub fn prepare(self, query: &str) -> BoxFuture<(Statement, Transaction), Error<Transaction>> {
26+
self.0.prepare(query)
27+
.map(|(s, c)| (s, Transaction(c)))
28+
.map_err(transaction_err)
29+
.boxed()
30+
}
31+
32+
pub fn execute(self,
33+
statement: &Statement,
34+
params: &[&ToSql])
35+
-> BoxFuture<(u64, Transaction), Error<Transaction>> {
36+
self.0.execute(statement, params)
37+
.map(|(n, c)| (n, Transaction(c)))
38+
.map_err(transaction_err)
39+
.boxed()
40+
}
41+
42+
pub fn query(self,
43+
statement: &Statement,
44+
params: &[&ToSql])
45+
-> BoxStateStream<Row, Transaction, Error<Transaction>> {
46+
self.0.query(statement, params)
47+
.map_state(Transaction)
48+
.map_err(transaction_err)
49+
.boxed()
50+
}
51+
52+
pub fn commit(self) -> BoxFuture<Connection, Error> {
53+
self.finish("COMMIT")
54+
}
55+
56+
pub fn rollback(self) -> BoxFuture<Connection, Error> {
57+
self.finish("ROLLBACK")
58+
}
59+
60+
fn finish(self, query: &str) -> BoxFuture<Connection, Error> {
61+
self.0.simple_query(query)
62+
.map(|(_, c)| c)
63+
.boxed()
64+
}
65+
}
66+
67+
fn transaction_err(e: Error) -> Error<Transaction> {
68+
match e {
69+
Error::Io(e) => Error::Io(e),
70+
Error::Db(e, c) => Error::Db(e, Transaction(c)),
71+
Error::Conversion(e, c) => Error::Conversion(e, Transaction(c))
72+
}
73+
}

0 commit comments

Comments
 (0)