Skip to content

Commit

Permalink
Skip ordering by view id if only one field was selected
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Nov 15, 2023
1 parent ad88458 commit b15f65c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions aquadoggo/src/db/stores/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ fn typecast_field_sql(
}

/// Values to bind to SQL query.
#[derive(Debug)]

Check warning on line 252 in aquadoggo/src/db/stores/query.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/db/stores/query.rs#L252

Added line #L252 was not covered by tests
enum BindArgument {
String(String),
Integer(i64),
Expand Down Expand Up @@ -864,7 +865,12 @@ async fn where_pagination_sql(
}
}

fn order_sql(order: &Order, schema: &Schema, list: Option<&RelationList>) -> String {
fn order_sql(
order: &Order,
schema: &Schema,
list: Option<&RelationList>,
fields: &ApplicationFields,
) -> String {
// Create custom ordering if query set one
let custom = order
.field
Expand Down Expand Up @@ -911,11 +917,19 @@ fn order_sql(order: &Order, schema: &Schema, list: Option<&RelationList>) -> Str
_ => None,
};

// Always order by document view id, except of if it was selected manually. We need this to
// assemble the rows to documents correctly at the end
let id_sql = match order.field {
Some(Field::Meta(MetaField::DocumentViewId)) => None,
_ => Some("documents.document_view_id ASC".to_string()),
// Order by document view id, except of if it was selected manually. We need this to assemble
// the rows to documents correctly at the end
let id_sql = {
// Skip this step when only one field was selected for this query to not mess with the
// order as soon as there are duplicate ordered fields
if fields.len() == 1 {
None
} else {
match order.field {
Some(Field::Meta(MetaField::DocumentViewId)) => None,
_ => Some("documents.document_view_id ASC".to_string()),
}
}
};

// On top we sort always by the unique operation cursor in case the previous order value is
Expand Down Expand Up @@ -1169,7 +1183,7 @@ impl SqlStore {
)
.await?;

let order = order_sql(&args.order, schema, list);
let order = order_sql(&args.order, schema, list, &application_fields);
let (page_size, limit) = limit_sql(&args.pagination, &application_fields);

let sea_quel = format!(
Expand Down

0 comments on commit b15f65c

Please sign in to comment.