Skip to content

Commit d53905d

Browse files
committed
Return connection trait objects
Instead of returning a concrete postgres transaction, this returns a Connection trait object for req.tx(). This should hopefully make it a little easier to migrate (if possible) in the future, but primarily allows us to log all sql queries for now.
1 parent 72b54fc commit d53905d

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/db.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Transaction {
7878
Ok(&**self.slot.borrow().unwrap())
7979
}
8080

81-
fn tx<'a>(&'a self) -> CargoResult<&'a pg::PostgresTransaction<'a>> {
81+
fn tx<'a>(&'a self) -> CargoResult<&'a Connection + 'a> {
8282
// Similar to above, the transaction for this request is actually tied
8383
// to the connection in the request itself, not 'static. We transmute it
8484
// to static as its paired with the inner connection to achieve the
@@ -93,7 +93,7 @@ impl Transaction {
9393
}
9494
let tx = self.tx.borrow();
9595
let tx: &'a pg::PostgresTransaction<'static> = tx.unwrap();
96-
Ok(tx)
96+
Ok(tx as &Connection)
9797
}
9898
}
9999

@@ -134,7 +134,7 @@ pub trait RequestTransaction<'a> {
134134
///
135135
/// The transaction will live for the duration of the request, and it will
136136
/// only be set to commit() if a successful response code of 200 is seen.
137-
fn tx(self) -> CargoResult<&'a pg::PostgresTransaction<'a>>;
137+
fn tx(self) -> CargoResult<&'a Connection + 'a>;
138138
}
139139

140140
impl<'a> RequestTransaction<'a> for &'a Request + 'a {
@@ -144,7 +144,7 @@ impl<'a> RequestTransaction<'a> for &'a Request + 'a {
144144
.conn()
145145
}
146146

147-
fn tx(self) -> CargoResult<&'a pg::PostgresTransaction<'a>> {
147+
fn tx(self) -> CargoResult<&'a Connection + 'a> {
148148
self.extensions().find::<Transaction>()
149149
.expect("Transaction not present in request")
150150
.tx()
@@ -164,21 +164,14 @@ impl Connection for pg::PostgresConnection {
164164
self.execute(query, params)
165165
}
166166
}
167-
//
168-
// impl Connection for pg::pool::PooledPostgresConnection {
169-
// fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
170-
// self.prepare(query)
171-
// }
172-
// fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
173-
// self.execute(query, params)
174-
// }
175-
// }
176167

177168
impl<'a> Connection for pg::PostgresTransaction<'a> {
178169
fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
170+
log!(5, "prepare: {}", query);
179171
self.prepare(query)
180172
}
181173
fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
174+
log!(5, "execute: {}", query);
182175
self.execute(query, params)
183176
}
184177
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#![feature(macro_rules)]
1+
#![feature(macro_rules, phase)]
22

33
extern crate serialize;
44
extern crate time;
5+
#[phase(plugin, link)] extern crate log;
56

67
extern crate "postgres" as pg;
78
extern crate curl;

0 commit comments

Comments
 (0)