Skip to content

Commit ba752f8

Browse files
committed
Remove the need to set the oid correctly everywhere for deserializing
We don't look at the oid's internally and won't do that any time soon. So document that behaviour and simplify the code.
1 parent d2db2eb commit ba752f8

10 files changed

Lines changed: 42 additions & 74 deletions

File tree

diesel/src/pg/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use self::metadata_lookup::PgMetadataLookup;
2222
pub use self::query_builder::DistinctOnClause;
2323
pub use self::query_builder::PgQueryBuilder;
2424
pub use self::transaction::TransactionBuilder;
25-
pub use self::value::{PgValue, StaticSqlType};
25+
pub use self::value::PgValue;
2626

2727
/// Data structures for PG types which have no corresponding Rust type
2828
///

diesel/src/pg/types/date_and_time/chrono.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::Write;
99

1010
use super::{PgDate, PgTime, PgTimestamp};
1111
use deserialize::{self, FromSql};
12-
use pg::{Pg, PgValue, StaticSqlType};
12+
use pg::{Pg, PgValue};
1313
use serialize::{self, Output, ToSql};
1414
use sql_types::{Date, Time, Timestamp, Timestamptz};
1515

@@ -47,7 +47,7 @@ impl ToSql<Timestamp, Pg> for NaiveDateTime {
4747

4848
impl FromSql<Timestamptz, Pg> for NaiveDateTime {
4949
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
50-
FromSql::<Timestamp, Pg>::from_sql(bytes.map(|b| b.with_new_oid(Timestamp::OID)))
50+
FromSql::<Timestamp, Pg>::from_sql(bytes)
5151
}
5252
}
5353

diesel/src/pg/types/date_and_time/deprecated_time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Write;
55
use self::time::{Duration, Timespec};
66

77
use deserialize::{self, FromSql};
8-
use pg::{Pg, PgValue, StaticSqlType};
8+
use pg::{Pg, PgValue};
99
use serialize::{self, Output, ToSql};
1010
use sql_types;
1111

@@ -29,7 +29,7 @@ impl ToSql<sql_types::Timestamp, Pg> for Timespec {
2929
impl FromSql<sql_types::Timestamp, Pg> for Timespec {
3030
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
3131
let t = <i64 as FromSql<sql_types::BigInt, Pg>>::from_sql(
32-
bytes.map(|b| b.with_new_oid(sql_types::BigInt::OID)),
32+
bytes,
3333
)?;
3434
let pg_epoch = Timespec::new(TIME_SEC_CONV, 0);
3535
let duration = Duration::microseconds(t);

diesel/src/pg/types/date_and_time/mod.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::Write;
22
use std::ops::Add;
33

44
use deserialize::{self, FromSql};
5-
use pg::{Pg, PgValue, StaticSqlType};
5+
use pg::{Pg, PgValue};
66
use serialize::{self, IsNull, Output, ToSql};
77
use sql_types::{self, Date, Interval, Time, Timestamp, Timestamptz};
88

@@ -90,10 +90,7 @@ impl ToSql<sql_types::Timestamp, Pg> for PgTimestamp {
9090

9191
impl FromSql<sql_types::Timestamp, Pg> for PgTimestamp {
9292
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
93-
FromSql::<sql_types::BigInt, Pg>::from_sql(
94-
bytes.map(|b| b.with_new_oid(sql_types::BigInt::OID)),
95-
)
96-
.map(PgTimestamp)
93+
FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTimestamp)
9794
}
9895
}
9996

@@ -105,9 +102,7 @@ impl ToSql<sql_types::Timestamptz, Pg> for PgTimestamp {
105102

106103
impl FromSql<sql_types::Timestamptz, Pg> for PgTimestamp {
107104
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
108-
FromSql::<sql_types::Timestamp, Pg>::from_sql(
109-
bytes.map(|b| b.with_new_oid(sql_types::Timestamp::OID)),
110-
)
105+
FromSql::<sql_types::Timestamp, Pg>::from_sql(bytes)
111106
}
112107
}
113108

@@ -119,10 +114,7 @@ impl ToSql<sql_types::Date, Pg> for PgDate {
119114

120115
impl FromSql<sql_types::Date, Pg> for PgDate {
121116
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
122-
FromSql::<sql_types::Integer, Pg>::from_sql(
123-
bytes.map(|b| b.with_new_oid(sql_types::Integer::OID)),
124-
)
125-
.map(PgDate)
117+
FromSql::<sql_types::Integer, Pg>::from_sql(bytes).map(PgDate)
126118
}
127119
}
128120

@@ -134,10 +126,7 @@ impl ToSql<sql_types::Time, Pg> for PgTime {
134126

135127
impl FromSql<sql_types::Time, Pg> for PgTime {
136128
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
137-
FromSql::<sql_types::BigInt, Pg>::from_sql(
138-
bytes.map(|b| b.with_new_oid(sql_types::BigInt::OID)),
139-
)
140-
.map(PgTime)
129+
FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTime)
141130
}
142131
}
143132

@@ -153,20 +142,10 @@ impl ToSql<sql_types::Interval, Pg> for PgInterval {
153142
impl FromSql<sql_types::Interval, Pg> for PgInterval {
154143
fn from_sql(value: Option<PgValue>) -> deserialize::Result<Self> {
155144
let value = not_none!(value);
156-
let bytes = value.as_bytes();
157145
Ok(PgInterval {
158-
microseconds: FromSql::<sql_types::BigInt, Pg>::from_sql(Some(PgValue::new(
159-
&bytes[..8],
160-
sql_types::BigInt::OID,
161-
)))?,
162-
days: FromSql::<sql_types::Integer, Pg>::from_sql(Some(PgValue::new(
163-
&bytes[8..12],
164-
sql_types::Integer::OID,
165-
)))?,
166-
months: FromSql::<sql_types::Integer, Pg>::from_sql(Some(PgValue::new(
167-
&bytes[12..16],
168-
sql_types::Integer::OID,
169-
)))?,
146+
microseconds: FromSql::<sql_types::BigInt, Pg>::from_sql(Some(value.subslice(0..8)))?,
147+
days: FromSql::<sql_types::Integer, Pg>::from_sql(Some(value.subslice(8..12)))?,
148+
months: FromSql::<sql_types::Integer, Pg>::from_sql(Some(value.subslice(12..16)))?,
170149
})
171150
}
172151
}

diesel/src/pg/types/date_and_time/std_time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::Write;
22
use std::time::{Duration, SystemTime, UNIX_EPOCH};
33

44
use deserialize::{self, FromSql};
5-
use pg::{Pg, PgValue, StaticSqlType};
5+
use pg::{Pg, PgValue};
66
use serialize::{self, Output, ToSql};
77
use sql_types;
88

@@ -29,7 +29,7 @@ impl ToSql<sql_types::Timestamp, Pg> for SystemTime {
2929
impl FromSql<sql_types::Timestamp, Pg> for SystemTime {
3030
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
3131
let usecs_passed = <i64 as FromSql<sql_types::BigInt, Pg>>::from_sql(
32-
bytes.map(|b| b.with_new_oid(sql_types::BigInt::OID)),
32+
bytes,
3333
)?;
3434
let before_epoch = usecs_passed < 0;
3535
let time_passed = usecs_to_duration(usecs_passed.abs() as u64);

diesel/src/pg/types/ranges.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Write;
55
use deserialize::{self, FromSql, FromSqlRow, Queryable};
66
use expression::bound::Bound as SqlBound;
77
use expression::AsExpression;
8-
use pg::{Pg, PgMetadataLookup, PgTypeMetadata, PgValue, StaticSqlType};
8+
use pg::{Pg, PgMetadataLookup, PgTypeMetadata, PgValue};
99
use serialize::{self, IsNull, Output, ToSql};
1010
use sql_types::*;
1111

@@ -26,7 +26,6 @@ bitflags! {
2626
impl<T, ST> Queryable<Range<ST>, Pg> for (Bound<T>, Bound<T>)
2727
where
2828
T: FromSql<ST, Pg> + Queryable<ST, Pg>,
29-
ST: StaticSqlType,
3029
{
3130
type Row = Self;
3231
fn build(row: Self) -> Self {
@@ -78,11 +77,10 @@ where
7877
impl<T, ST> FromSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
7978
where
8079
T: FromSql<ST, Pg>,
81-
ST: StaticSqlType,
8280
{
8381
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
84-
let bytes = not_none!(bytes);
85-
let mut bytes = bytes.as_bytes();
82+
let value = not_none!(bytes);
83+
let mut bytes = value.as_bytes();
8684
let flags: RangeFlags = RangeFlags::from_bits_truncate(bytes.read_u8()?);
8785
let mut lower_bound = Bound::Unbounded;
8886
let mut upper_bound = Bound::Unbounded;
@@ -91,7 +89,10 @@ where
9189
let elem_size = bytes.read_i32::<NetworkEndian>()?;
9290
let (elem_bytes, new_bytes) = bytes.split_at(elem_size as usize);
9391
bytes = new_bytes;
94-
let value = T::from_sql(Some(PgValue::new(elem_bytes, ST::OID)))?;
92+
let value = T::from_sql(Some(PgValue::new(
93+
elem_bytes,
94+
value.get_oid(),
95+
)))?;
9596

9697
lower_bound = if flags.contains(RangeFlags::LB_INC) {
9798
Bound::Included(value)
@@ -102,7 +103,10 @@ where
102103

103104
if !flags.contains(RangeFlags::UB_INF) {
104105
let _size = bytes.read_i32::<NetworkEndian>()?;
105-
let value = T::from_sql(Some(PgValue::new(bytes, ST::OID)))?;
106+
let value = T::from_sql(Some(PgValue::new(
107+
bytes,
108+
value.get_oid(),
109+
)))?;
106110

107111
upper_bound = if flags.contains(RangeFlags::UB_INC) {
108112
Bound::Included(value)

diesel/src/pg/types/record.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ macro_rules! tuple_impls {
2525
// but the only other option would be to use `mem::uninitialized`
2626
// and `ptr::write`.
2727
#[allow(clippy::eval_order_dependence)]
28-
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
29-
let bytes = not_none!(bytes);
30-
let mut bytes = bytes.as_bytes();
28+
fn from_sql(value: Option<PgValue>) -> deserialize::Result<Self> {
29+
let value = not_none!(value);
30+
let mut bytes = value.as_bytes();
3131
let num_elements = bytes.read_i32::<NetworkEndian>()?;
3232

3333
if num_elements != $Tuple {

diesel/src/pg/types/uuid_v0_7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct UuidProxy(uuid::Uuid);
1515

1616
impl FromSql<Uuid, Pg> for uuid::Uuid {
1717
fn from_sql(bytes: Option<PgValue>) -> deserialize::Result<Self> {
18-
let value = not_none!(value);
18+
let value = not_none!(bytes);
1919
uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
2020
}
2121
}

diesel/src/pg/value.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
use super::Pg;
1+
use super::{Pg, PgMetadataLookup};
22
use backend::BinaryRawValue;
33
use std::num::NonZeroU32;
4-
5-
/// Marker trait for types with oid's known at compile time
6-
pub trait StaticSqlType {
7-
/// Type oid
8-
const OID: NonZeroU32;
9-
/// Array oid
10-
const ARRAY_OID: NonZeroU32;
11-
}
4+
use std::ops::Range;
125

136
/// Raw postgres value as received from the database
147
#[derive(Clone, Copy)]
@@ -42,7 +35,10 @@ impl<'a> PgValue<'a> {
4235
self.type_oid
4336
}
4437

45-
pub(crate) fn with_new_oid(self, type_oid: NonZeroU32) -> Self {
46-
Self { type_oid, ..self }
38+
pub(crate) fn subslice(&self, range: Range<usize>) -> Self {
39+
Self {
40+
raw_value: &self.raw_value[range],
41+
..*self
42+
}
4743
}
4844
}

diesel_derives/src/sql_type.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,23 @@ fn pg_tokens(item: &syn::DeriveInput) -> Option<proc_macro2::TokenStream> {
110110
let struct_name = &item.ident;
111111
let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
112112

113-
let (metadata_fn, const_metadata) = match ty {
113+
let metadata_fn = match ty {
114114
PgType::Fixed { oid, array_oid } => {
115-
let metadata_fn = quote!(
115+
quote!(
116116
fn metadata(_: &PgMetadataLookup) -> PgTypeMetadata {
117117
PgTypeMetadata {
118118
oid: #oid,
119119
array_oid: #array_oid,
120120
}
121121
}
122-
);
122+
)
123123

124-
let const_metadata = quote! {
125-
impl #impl_generics diesel::pg::StaticSqlType for #struct_name #ty_generics
126-
{
127-
const OID: std::num::NonZeroU32 = unsafe {std::num::NonZeroU32::new_unchecked(#oid) };
128-
const ARRAY_OID: std::num::NonZeroU32 = unsafe {std::num::NonZeroU32::new_unchecked(#array_oid) };
129-
}
130-
};
131-
132-
(metadata_fn, Some(const_metadata))
133124
}
134-
PgType::Lookup(type_name) => (quote!(
125+
PgType::Lookup(type_name) => quote!(
135126
fn metadata(lookup: &PgMetadataLookup) -> PgTypeMetadata {
136127
lookup.lookup_type(#type_name)
137128
}
138-
), None)
129+
)
139130
};
140131

141132
Some(quote! {
@@ -147,8 +138,6 @@ fn pg_tokens(item: &syn::DeriveInput) -> Option<proc_macro2::TokenStream> {
147138
{
148139
#metadata_fn
149140
}
150-
151-
#const_metadata
152141
})
153142
})
154143
}

0 commit comments

Comments
 (0)