Skip to content

Commit

Permalink
fix: build empty postgres-array with auto type infer
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Jan 2, 2025
1 parent 605ec44 commit 62f6fff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,15 +1104,21 @@ pub trait QueryBuilder:
#[cfg(feature = "with-uuid")]
Value::Uuid(Some(v)) => write!(s, "'{v}'").unwrap(),
#[cfg(feature = "postgres-array")]
Value::Array(_, Some(v)) => write!(
s,
"ARRAY [{}]",
v.iter()
.map(|element| self.value_to_string(element))
.collect::<Vec<String>>()
.join(",")
)
.unwrap(),
Value::Array(_, Some(v)) => {
if v.is_empty() {
write!(s, "'{{}}'").unwrap()
} else {
write!(
s,
"ARRAY [{}]",
v.iter()
.map(|element| self.value_to_string(element))
.collect::<Vec<String>>()
.join(",")
)
.unwrap()
}
}
#[cfg(feature = "postgres-vector")]
Value::Vector(Some(v)) => {
write!(s, "'[").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ pub mod with_array {
use super::*;
use crate::RcOrArc;

// We only imlement conversion from Vec<T> to Array when T is not u8.
// We only implement conversion from Vec<T> to Array when T is not u8.
// This is because for u8's case, there is already conversion to Byte defined above.
// TODO When negative trait becomes a stable feature, following code can be much shorter.
pub trait NotU8 {}
Expand Down
14 changes: 14 additions & 0 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,20 @@ fn insert_10() {
);
}

// regression tests for https://github.com/SeaQL/sea-query/issues/853
#[test]
#[cfg(feature = "postgres-array")]
fn insert_issue_853() {
assert_eq!(
Query::insert()
.into_table(Glyph::Table)
.columns([Glyph::Aspect, Glyph::Tokens])
.values_panic([3.1415.into(), Vec::<String>::new().into()])
.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" ("aspect", "tokens") VALUES (3.1415, '{}')"#
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_1() {
Expand Down

0 comments on commit 62f6fff

Please sign in to comment.