Skip to content

Commit cc46778

Browse files
committed
Add a missing ToSql impl for postgres ranges
For some reasons the range implementation was missing the `ToSql<Nullable<ST>> for T` impl.
1 parent a18095e commit cc46778

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

diesel/src/pg/types/ranges.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ where
181181
}
182182
}
183183

184+
185+
impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>)
186+
where
187+
Pg: HasSqlType<Range<ST>>,
188+
(Bound<T>, Bound<T>): ToSql<Range<ST>, Pg>
189+
{
190+
fn to_sql<W: Write>(
191+
&self,
192+
out: &mut ToSqlOutput<W, Pg>,
193+
) -> Result<IsNull, Box<Error + Send + Sync>> {
194+
ToSql::<Range<ST>, Pg>::to_sql(self, out)
195+
}
196+
}
197+
184198
primitive_impls!(Int4range -> (pg: (3904, 3905)));
185199
primitive_impls!(Numrange -> (pg: (3906, 3907)));
186200
primitive_impls!(Tsrange -> (pg: (3908, 3909)));

diesel_tests/tests/types.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,3 +1122,38 @@ fn test_range_to_sql() {
11221122
(Bound<i32>, Bound<i32>),
11231123
>(expected_value, value));
11241124
}
1125+
1126+
#[cfg(feature = "postgres")]
1127+
#[test]
1128+
fn test_inserting_ranges() {
1129+
use std::collections::Bound;
1130+
1131+
let connection = connection();
1132+
connection
1133+
.execute(
1134+
"CREATE TABLE has_ranges (
1135+
id SERIAL PRIMARY KEY,
1136+
nul_range INT4RANGE,
1137+
range INT4RANGE NOT NULL)",
1138+
)
1139+
.unwrap();
1140+
table!(
1141+
has_ranges(id) {
1142+
id -> Int4,
1143+
nul_range -> Nullable<Range<Int4>>,
1144+
range -> Range<Int4>,
1145+
}
1146+
);
1147+
1148+
let value = (Bound::Included(1), Bound::Excluded(3));
1149+
1150+
let (_, v1, v2): (i32, Option<(_, _)>, (_, _)) = insert_into(has_ranges::table)
1151+
.values((
1152+
has_ranges::nul_range.eq(value),
1153+
has_ranges::range.eq(value),
1154+
))
1155+
.get_result(&connection)
1156+
.unwrap();
1157+
assert_eq!(v1, Some(value));
1158+
assert_eq!(v2, value);
1159+
}

0 commit comments

Comments
 (0)