Skip to content

Commit c81c207

Browse files
committed
Add methods to look at params and results
1 parent f8ce356 commit c81c207

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ impl<'self> PostgresTransaction<'self> {
421421
}
422422
423423
pub trait PostgresStatement {
424-
fn num_params(&self) -> uint;
424+
fn param_types<'a>(&'a self) -> &'a [PostgresType];
425+
fn result_descriptions<'a>(&'a self) -> &'a [ResultDescription];
425426
fn update(&self, params: &[&ToSql]) -> uint;
426427
fn try_update(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError>;
427428
fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<'a>;
@@ -438,6 +439,7 @@ pub struct NormalPostgresStatement<'self> {
438439
priv next_portal_id: Cell<uint>
439440
}
440441
442+
#[deriving(Eq)]
441443
pub struct ResultDescription {
442444
name: ~str,
443445
ty: PostgresType
@@ -548,8 +550,12 @@ impl<'self> NormalPostgresStatement<'self> {
548550
}
549551
550552
impl<'self> PostgresStatement for NormalPostgresStatement<'self> {
551-
fn num_params(&self) -> uint {
552-
self.param_types.len()
553+
fn param_types<'a>(&'a self) -> &'a [PostgresType] {
554+
self.param_types.as_slice()
555+
}
556+
557+
fn result_descriptions<'a>(&'a self) -> &'a [ResultDescription] {
558+
self.result_desc.as_slice()
553559
}
554560
555561
fn update(&self, params: &[&ToSql]) -> uint {
@@ -618,8 +624,12 @@ pub struct TransactionalPostgresStatement<'self> {
618624
}
619625
620626
impl<'self> PostgresStatement for TransactionalPostgresStatement<'self> {
621-
fn num_params(&self) -> uint {
622-
self.stmt.num_params()
627+
fn param_types<'a>(&'a self) -> &'a [PostgresType] {
628+
self.stmt.param_types()
629+
}
630+
631+
fn result_descriptions<'a>(&'a self) -> &'a [ResultDescription] {
632+
self.stmt.result_descriptions()
623633
}
624634
625635
fn update(&self, params: &[&ToSql]) -> uint {

src/test.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::f32;
77
use std::f64;
88

99
use postgres::*;
10-
use postgres::types::{ToSql, FromSql};
10+
use postgres::types::{ToSql, FromSql, PgInt4, PgVarchar};
1111

1212
#[test]
1313
fn test_prepare_err() {
@@ -88,6 +88,22 @@ fn test_lazy_query() {
8888
}
8989
}
9090

91+
#[test]
92+
fn test_param_types() {
93+
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
94+
let stmt = conn.prepare("SELECT $1::INT, $2::VARCHAR");
95+
assert_eq!(stmt.param_types(), [PgInt4, PgVarchar]);
96+
}
97+
98+
#[test]
99+
fn test_result_descriptions() {
100+
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
101+
let stmt = conn.prepare("SELECT 1::INT as a, 'hi'::VARCHAR as b");
102+
assert_eq!(stmt.result_descriptions(),
103+
[ResultDescription { name: ~"a", ty: PgInt4},
104+
ResultDescription { name: ~"b", ty: PgVarchar}]);
105+
}
106+
91107
fn test_type<T: Eq+ToSql+FromSql>(sql_type: &str, values: &[T]) {
92108
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
93109
conn.update("CREATE TEMPORARY TABLE foo (

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static BPCHAROID: Oid = 1042;
2525
static VARCHAROID: Oid = 1043;
2626
static UUIDOID: Oid = 2950;
2727

28+
#[deriving(Eq)]
2829
pub enum PostgresType {
2930
PgBool,
3031
PgByteA,

0 commit comments

Comments
 (0)