Skip to content

Commit 79848f8

Browse files
committed
Support postgres enums and try fix example
1 parent ea28377 commit 79848f8

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

examples/diesel_postgres/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ struct CharacterStructChrono {
329329
created: NaiveDateTime,
330330
inet: IpNetwork,
331331
mac_address: MacAddress,
332-
array_bool: Vec<bool>,
332+
array_bool: Vec<Option<bool>>,
333333
}
334334

335335
#[derive(QueryableByName, Debug)]
@@ -346,7 +346,7 @@ struct CharacterStructTime {
346346
created: PrimitiveDateTime,
347347
inet: IpNetwork,
348348
mac_address: MacAddress,
349-
array_bool: Vec<bool>,
349+
array_bool: Vec<Option<bool>>,
350350
}
351351

352352
#[derive(QueryableByName, Debug)]

sea-query-postgres/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ impl ToSql for PostgresValue {
7979
Value::String(v) => v.as_deref().to_sql(ty, out),
8080
Value::Char(v) => v.map(|v| v.to_string()).to_sql(ty, out),
8181
Value::Bytes(v) => v.as_deref().to_sql(ty, out),
82-
Value::Enum(_) => Err(PostgresBindError::new(
83-
"Binding Enum is not supported by sea-query-postgres binder",
84-
)
85-
.into()),
82+
Value::Enum(v) => v.map(|v| v.as_str().to_sql(ty, out)),
8683
#[cfg(feature = "with-json")]
8784
Value::Json(v) => v.to_sql(ty, out),
8885
#[cfg(feature = "with-chrono")]
@@ -120,12 +117,14 @@ impl ToSql for PostgresValue {
120117
#[cfg(feature = "with-bigdecimal")]
121118
Value::BigDecimal(v) => {
122119
use bigdecimal::ToPrimitive;
123-
v.as_ref().map(|x| x.to_f64().ok_or(
124-
PostgresBindError::new(
125-
"Fail to convert bigdecimal as f64 for sea-query-postgres binder",
126-
)
127-
)).transpose()?.to_sql(ty, out)
128-
120+
v.as_ref()
121+
.map(|x| {
122+
x.to_f64().ok_or(PostgresBindError::new(
123+
"Fail to convert bigdecimal as f64 for sea-query-postgres binder",
124+
))
125+
})
126+
.transpose()?
127+
.to_sql(ty, out)
129128
}
130129
#[cfg(feature = "with-uuid")]
131130
Value::Uuid(v) => v.to_sql(ty, out),
@@ -150,7 +149,7 @@ impl ToSql for PostgresValue {
150149
Array::BigUnsigned(inner) => inner
151150
.into_iter()
152151
.map(|v| v.map(i64::try_from).transpose())
153-
.collect::<Result<Vec<Option<_>>,_>>()?
152+
.collect::<Result<Vec<Option<_>>, _>>()?
154153
.to_sql(ty, out),
155154
Array::Float(inner) => inner.to_sql(ty, out),
156155
Array::Double(inner) => inner.to_sql(ty, out),
@@ -235,10 +234,13 @@ impl ToSql for PostgresValue {
235234
"Nested arrays (Array::Array) are not supported by sea-query-postgres binder",
236235
)
237236
.into()),
238-
Array::Enum(_) => Err(PostgresBindError::new(
239-
"Array of Enum is not supported by sea-query-postgres binder; consider casting in SQL",
240-
)
241-
.into()),
237+
Array::Enum(v) => v
238+
.as_ref()
239+
.1
240+
.iter()
241+
.map(|v| v.as_ref())
242+
.collect::<Vec<_>>()
243+
.to_sql(ty, out),
242244
_ => Err(PostgresBindError::new(
243245
"Unsupported array variant for sea-query-postgres binder",
244246
)

0 commit comments

Comments
 (0)