Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: build empty postgres-array with auto type infer #854

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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