From a4f69b9d616437aed720f8ecf58a0a8148fe6133 Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Tue, 3 Sep 2024 14:18:41 +0500 Subject: [PATCH 1/3] fix index method --- diesel/src/pg/expression/operators.rs | 2 ++ diesel_tests/tests/index.rs | 17 +++++++++++++++++ diesel_tests/tests/lib.rs | 1 + 3 files changed, 20 insertions(+) create mode 100644 diesel_tests/tests/index.rs diff --git a/diesel/src/pg/expression/operators.rs b/diesel/src/pg/expression/operators.rs index dc6100694b98..3ca95f98bf37 100644 --- a/diesel/src/pg/expression/operators.rs +++ b/diesel/src/pg/expression/operators.rs @@ -107,7 +107,9 @@ where &'b self, mut out: crate::query_builder::AstPass<'_, 'b, Pg>, ) -> crate::result::QueryResult<()> { + out.push_sql("("); self.array_expr.walk_ast(out.reborrow())?; + out.push_sql(")"); out.push_sql("["); self.index_expr.walk_ast(out.reborrow())?; out.push_sql("]"); diff --git a/diesel_tests/tests/index.rs b/diesel_tests/tests/index.rs new file mode 100644 index 000000000000..76d3d5633e7a --- /dev/null +++ b/diesel_tests/tests/index.rs @@ -0,0 +1,17 @@ +use crate::schema::connection; +use crate::schema::*; + + +#[cfg(feature = "postgres")] +#[test] +fn test_array_index() { + use diesel::{PgArrayExpressionMethods, RunQueryDsl}; + use diesel::dsl::array_append; + use diesel::sql_types::{Array,Integer}; + let connection = &mut connection(); + let result = diesel::select(array_append::, Integer, _, _>(vec![1, 2, 3], 4).index(4)) + .get_result::(connection) + .unwrap(); + + assert_eq!(4, result); +} diff --git a/diesel_tests/tests/lib.rs b/diesel_tests/tests/lib.rs index 3df6e6e4f056..6c3fd9a166a1 100644 --- a/diesel_tests/tests/lib.rs +++ b/diesel_tests/tests/lib.rs @@ -55,3 +55,4 @@ mod transactions; mod types; mod types_roundtrip; mod update; +mod index; From ce0630f7b8be6a99e7b57641cc69b93c1e01169a Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Fri, 6 Sep 2024 11:31:33 +0200 Subject: [PATCH 2/3] Fix imports + rustfmt --- diesel_tests/tests/index.rs | 10 +++------- diesel_tests/tests/lib.rs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/diesel_tests/tests/index.rs b/diesel_tests/tests/index.rs index 76d3d5633e7a..3bbfb3913490 100644 --- a/diesel_tests/tests/index.rs +++ b/diesel_tests/tests/index.rs @@ -1,14 +1,10 @@ -use crate::schema::connection; -use crate::schema::*; - - #[cfg(feature = "postgres")] #[test] fn test_array_index() { - use diesel::{PgArrayExpressionMethods, RunQueryDsl}; use diesel::dsl::array_append; - use diesel::sql_types::{Array,Integer}; - let connection = &mut connection(); + use diesel::sql_types::{Array, Integer}; + use diesel::{PgArrayExpressionMethods, RunQueryDsl}; + let connection = &mut crate::schema::connection(); let result = diesel::select(array_append::, Integer, _, _>(vec![1, 2, 3], 4).index(4)) .get_result::(connection) .unwrap(); diff --git a/diesel_tests/tests/lib.rs b/diesel_tests/tests/lib.rs index 6c3fd9a166a1..07743347201f 100644 --- a/diesel_tests/tests/lib.rs +++ b/diesel_tests/tests/lib.rs @@ -28,6 +28,7 @@ mod filter_operators; mod find; mod group_by; mod having; +mod index; mod insert; mod insert_from_select; mod instrumentation; @@ -55,4 +56,3 @@ mod transactions; mod types; mod types_roundtrip; mod update; -mod index; From dabb7bdd2316adf042acc26af8a8ede898b4ffdd Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Fri, 6 Sep 2024 11:31:46 +0200 Subject: [PATCH 3/3] Fix update statements that index arrays Parenthesis are not accepted in this location, so we need to skip them --- diesel/src/pg/expression/operators.rs | 29 +++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/diesel/src/pg/expression/operators.rs b/diesel/src/pg/expression/operators.rs index 3ca95f98bf37..f2d0934482b6 100644 --- a/diesel/src/pg/expression/operators.rs +++ b/diesel/src/pg/expression/operators.rs @@ -117,15 +117,40 @@ where } } +// we cannot use the additional +// parenthesis for updates +#[derive(Debug)] +pub struct UpdateArrayIndex(ArrayIndex); + +impl QueryFragment for UpdateArrayIndex +where + L: QueryFragment, + R: QueryFragment, +{ + fn walk_ast<'b>( + &'b self, + mut out: crate::query_builder::AstPass<'_, 'b, Pg>, + ) -> crate::result::QueryResult<()> { + self.0.array_expr.walk_ast(out.reborrow())?; + out.push_sql("["); + self.0.index_expr.walk_ast(out.reborrow())?; + out.push_sql("]"); + Ok(()) + } +} + impl AssignmentTarget for ArrayIndex where L: Column, { type Table = ::Table; - type QueryAstNode = ArrayIndex, R>; + type QueryAstNode = UpdateArrayIndex, R>; fn into_target(self) -> Self::QueryAstNode { - ArrayIndex::new(UncorrelatedColumn(self.array_expr), self.index_expr) + UpdateArrayIndex(ArrayIndex::new( + UncorrelatedColumn(self.array_expr), + self.index_expr, + )) } }