Skip to content

Commit 8340d4b

Browse files
committed
Statement peparation and execution
1 parent e52d5b5 commit 8340d4b

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

postgres-tokio/src/lib.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct InnerConnection {
4242
stream: PostgresStream,
4343
parameters: HashMap<String, String>,
4444
cancel_data: CancelData,
45+
next_stmt_id: u32,
4546
}
4647

4748
impl InnerConnection {
@@ -127,7 +128,8 @@ impl Connection {
127128
cancel_data: CancelData {
128129
process_id: 0,
129130
secret_key: 0,
130-
}
131+
},
132+
next_stmt_id: 0,
131133
})
132134
})
133135
.and_then(|s| s.startup(params))
@@ -402,8 +404,6 @@ impl Connection {
402404
|f, t| Column { name: f.0, type_: t })
403405
.map(|(r, s)| (p, r, s))
404406
})
405-
.and_then(|(p, r, s)| s.ready((p, r)))
406-
.map(|((p, r), s)| (p, r, s))
407407
.boxed()
408408
}
409409

@@ -454,8 +454,8 @@ impl Connection {
454454
let mut bind = vec![];
455455
let mut execute = vec![];
456456
let mut sync = vec![];
457-
let r = frontend::bind(stmt,
458-
portal,
457+
let r = frontend::bind(portal,
458+
stmt,
459459
Some(1),
460460
params.iter().zip(param_types),
461461
|(param, ty), buf| {
@@ -533,6 +533,22 @@ impl Connection {
533533
.boxed()
534534
}
535535

536+
pub fn prepare(mut self, query: &str) -> BoxFuture<(Statement, Connection), Error> {
537+
let id = self.0.next_stmt_id;
538+
self.0.next_stmt_id += 1;
539+
let name = format!("s{}", id);
540+
self.raw_prepare(&name, query)
541+
.map(|(params, columns, conn)| {
542+
let stmt = Statement {
543+
name: name,
544+
params: params,
545+
columns: columns,
546+
};
547+
(stmt, conn)
548+
})
549+
.boxed()
550+
}
551+
536552
pub fn cancel_data(&self) -> CancelData {
537553
self.0.cancel_data
538554
}
@@ -543,6 +559,24 @@ struct Column {
543559
type_: Type,
544560
}
545561

562+
pub struct Statement {
563+
name: String,
564+
params: Vec<Type>,
565+
columns: Vec<Column>,
566+
}
567+
568+
impl Statement {
569+
pub fn execute(self,
570+
params: &[&ToSql],
571+
conn: Connection)
572+
-> BoxFuture<(u64, Statement, Connection), Error> {
573+
conn.raw_execute(&self.name, "", &self.params, params)
574+
.and_then(|conn| conn.finish_execute())
575+
.map(|(n, conn)| (n, self, conn))
576+
.boxed()
577+
}
578+
}
579+
546580
fn connect_err(fields: &mut ErrorFields) -> ConnectError {
547581
match DbError::new(fields) {
548582
Ok(err) => ConnectError::Db(Box::new(err)),

postgres-tokio/src/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,20 @@ fn batch_execute_err() {
104104
});
105105
l.run(done).unwrap();
106106
}
107+
108+
#[test]
109+
fn prepare_execute() {
110+
let mut l = Core::new().unwrap();
111+
let done = Connection::connect("postgres://postgres@localhost", &l.handle())
112+
.then(|c| {
113+
c.unwrap().prepare("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, name VARCHAR)")
114+
})
115+
.and_then(|(s, c)| s.execute(&[], c))
116+
.and_then(|(n, _, c)| {
117+
assert_eq!(0, n);
118+
c.prepare("INSERT INTO foo (name) VALUES ($1), ($2)")
119+
})
120+
.and_then(|(s, c)| s.execute(&[&"steven", &"bob"], c))
121+
.map(|(n, _, _)| assert_eq!(n, 2));
122+
l.run(done).unwrap();
123+
}

0 commit comments

Comments
 (0)