Skip to content

Commit

Permalink
Merge pull request #4227 from valkrypton/fix/array_index
Browse files Browse the repository at this point in the history
fix index method
  • Loading branch information
weiznich authored Sep 6, 2024
2 parents bc0e030 + dabb7bd commit 79399de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
31 changes: 29 additions & 2 deletions diesel/src/pg/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,50 @@ 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("]");
Ok(())
}
}

// we cannot use the additional
// parenthesis for updates
#[derive(Debug)]
pub struct UpdateArrayIndex<L, R>(ArrayIndex<L, R>);

impl<L, R> QueryFragment<Pg> for UpdateArrayIndex<L, R>
where
L: QueryFragment<Pg>,
R: QueryFragment<Pg>,
{
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<L, R> AssignmentTarget for ArrayIndex<L, R>
where
L: Column,
{
type Table = <L as Column>::Table;
type QueryAstNode = ArrayIndex<UncorrelatedColumn<L>, R>;
type QueryAstNode = UpdateArrayIndex<UncorrelatedColumn<L>, 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,
))
}
}

Expand Down
13 changes: 13 additions & 0 deletions diesel_tests/tests/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(feature = "postgres")]
#[test]
fn test_array_index() {
use diesel::dsl::array_append;
use diesel::sql_types::{Array, Integer};
use diesel::{PgArrayExpressionMethods, RunQueryDsl};
let connection = &mut crate::schema::connection();
let result = diesel::select(array_append::<Array<_>, Integer, _, _>(vec![1, 2, 3], 4).index(4))
.get_result::<i32>(connection)
.unwrap();

assert_eq!(4, result);
}
1 change: 1 addition & 0 deletions diesel_tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod filter_operators;
mod find;
mod group_by;
mod having;
mod index;
mod insert;
mod insert_from_select;
mod instrumentation;
Expand Down

0 comments on commit 79399de

Please sign in to comment.