Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdbc-mysql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn rewrite(sql: &str, params: &[rdbc::Value]) -> rdbc::Result<String> {
Token::Word(Word {
value: param.to_string(),
quote_style: None,
keyword: "".to_owned(),
keyword: sqlparser::dialect::keywords::Keyword::NoKeyword,
})
}
_ => t.clone(),
Expand Down
25 changes: 24 additions & 1 deletion rdbc-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl rdbc::Connection for PConnection {
Token::Word(Word {
value: format!("${}", i),
quote_style: None,
keyword: "".to_owned(),
keyword: sqlparser::dialect::keywords::Keyword::NoKeyword,
})
}
_ => t.clone(),
Expand Down Expand Up @@ -163,6 +163,27 @@ impl rdbc::ResultSet for PResultSet {
get_string -> String,
get_bytes -> Vec<u8>
}

fn get<T>(&self, i: u64) -> rdbc::Result<Option<T>> where T: rdbc::ResultSetGet {
T::get(self, i)
}
}

macro_rules! impl_resultget {
($($ty: ty),*) => {
$(
impl rdbc::ResultSetGet for $ty {
type Set = PResultSet;
fn get(set: &Self::Set, i: u64) -> rdbc::Result<Option<Self>> {
Ok(set.rows.get(set.i - 1).get(i as usize))
}
}
)*
};
}

impl_resultget! {
i8, i16, i32, i64, f32, f64, String, Vec<u8>
}

/// Convert a Postgres error into an RDBC error
Expand Down Expand Up @@ -214,6 +235,8 @@ mod tests {
assert_eq!(Some(123), rs.get_i32(0)?);
assert!(!rs.next());

let x = rs.get::<i32>(0)??;

Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions rdbc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ pub trait ResultSet {
fn get_f64(&self, i: u64) -> Result<Option<f64>>;
fn get_string(&self, i: u64) -> Result<Option<String>>;
fn get_bytes(&self, i: u64) -> Result<Option<Vec<u8>>>;

fn get<T>(&self, i: u64) -> Result<Option<T>> where T: ResultSetGet;
}

pub trait ResultSetGet {
type Set: ResultSet;
fn get(set: &Self::Set, i: u64) -> Result<Option<Self>>;
}

/// Meta data for result set
Expand Down