Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7a5af3

Browse files
committed
Implement FromSql for tuples up to length 4
This makes it very ergonomic to decode the results of a query like SELECT (1, 'a')where (1, 'a') is returned as an anonymous record type.The big downside to this approach is that only built-in OIDs aresupported, as there is no way to know ahead of time what OIDs will bereturned, and so we'll only have metadata for the built-in OIDs lyingaround.
1 parent598fc0f commitc7a5af3

File tree

2 files changed

+109
-0
lines changed
  • postgres-types/src
  • tokio-postgres/tests/test/types

2 files changed

+109
-0
lines changed

‎postgres-types/src/lib.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,62 @@ impl<'a> FromSql<'a> for IpAddr {
617617
accepts!(INET);
618618
}
619619

620+
macro_rules! impl_from_sql_tuple{
621+
($n:expr; $($ty_ident:ident),*; $($var_ident:ident),*) =>{
622+
impl<'a, $($ty_ident),*>FromSql<'a>for($($ty_ident,)*)
623+
where
624+
$($ty_ident:FromSql<'a>),*
625+
{
626+
fn from_sql(
627+
_:&Type,
628+
mut raw:&'a[u8],
629+
) ->Result<($($ty_ident,)*),Box<dynError +Sync +Send>>{
630+
let num_fields = private::read_be_i32(&mut raw)?;
631+
if num_fieldsasusize != $n{
632+
returnErr(format!(
633+
"Postgres record field count does not match Rust tuple length: {} vs {}",
634+
num_fields,
635+
$n,
636+
).into());
637+
}
638+
639+
$(
640+
let oid = private::read_be_i32(&mut raw)?asu32;
641+
let ty =matchType::from_oid(oid){
642+
None =>{
643+
returnErr(format!(
644+
"cannot decode OID {} inside of anonymous record",
645+
oid,
646+
).into());
647+
}
648+
Some(ty)if !$ty_ident::accepts(&ty) =>{
649+
returnErr(Box::new(WrongType::new::<$ty_ident>(ty.clone())));
650+
}
651+
Some(ty) => ty,
652+
};
653+
let $var_ident = private::read_value(&ty,&mut raw)?;
654+
)*
655+
656+
Ok(($($var_ident,)*))
657+
}
658+
659+
fn accepts(ty:&Type) ->bool{
660+
match ty.kind(){
661+
Kind::Pseudo =>*ty ==Type::RECORD,
662+
Kind::Composite(fields) => fields.len() == $n,
663+
_ =>false,
664+
}
665+
}
666+
}
667+
};
668+
}
669+
670+
impl_from_sql_tuple!(0;;);
671+
impl_from_sql_tuple!(1;T0; v0);
672+
impl_from_sql_tuple!(2;T0,T1; v0, v1);
673+
impl_from_sql_tuple!(3;T0,T1,T2; v0, v1, v2);
674+
impl_from_sql_tuple!(4;T0,T1,T2,T3; v0, v1, v2, v3);
675+
620676
/// An enum representing the nullability of a Postgres value.
621677
pubenumIsNull{
622678
/// The value is NULL.

‎tokio-postgres/tests/test/types/mod.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,59 @@ async fn composite() {
547547
}
548548
}
549549

550+
#[tokio::test]
551+
asyncfntuples(){
552+
let client =connect("user=postgres").await;
553+
554+
let row = client.query_one("SELECT ROW()",&[]).await.unwrap();
555+
let val:() = row.get(0);
556+
assert_eq!(val,());
557+
558+
let row = client.query_one("SELECT ROW(1)",&[]).await.unwrap();
559+
let val:(i32,) = row.get(0);
560+
assert_eq!(val,(1,));
561+
562+
let row = client.query_one("SELECT (1, 'a')",&[]).await.unwrap();
563+
let val:(i32,String) = row.get(0);
564+
assert_eq!(val,(1,"a".into()));
565+
566+
let row = client.query_one("SELECT (1, (2, 3))",&[]).await.unwrap();
567+
let val:(i32,(i32,i32)) = row.get(0);
568+
assert_eq!(val,(1,(2,3)));
569+
570+
let row = client.query_one("SELECT (1, 2)",&[]).await.unwrap();
571+
let err = row.try_get::<_,(i32,String)>(0).unwrap_err();
572+
match err.source(){
573+
Some(e)if e.is::<WrongType>() =>{}
574+
_ =>panic!("Unexpected error {:?}", err),
575+
};
576+
577+
let row = client.query_one("SELECT (1, 2, 3)",&[]).await.unwrap();
578+
let err = row.try_get::<_,(i32,i32)>(0).unwrap_err();
579+
assert_eq!(
580+
err.to_string(),
581+
"error deserializing column 0:\
582+
Postgres record field count does not match Rust tuple length: 3 vs 2"
583+
);
584+
585+
client
586+
.batch_execute(
587+
"CREATE TYPE pg_temp.simple AS (
588+
a int,
589+
b text
590+
)",
591+
)
592+
.await
593+
.unwrap();
594+
595+
let row = client
596+
.query_one("SELECT (1, 'a')::simple",&[])
597+
.await
598+
.unwrap();
599+
let val:(i32,String) = row.get(0);
600+
assert_eq!(val,(1,"a".into()));
601+
}
602+
550603
#[tokio::test]
551604
asyncfnenum_(){
552605
let client =connect("user=postgres").await;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp