Skip to content

feat: add order-by logic to database queries - #491

Open
xelab04 wants to merge 21 commits into
cot-rs:masterfrom
xelab04:db-sort-by
Open

feat: add order-by logic to database queries#491
xelab04 wants to merge 21 commits into
cot-rs:masterfrom
xelab04:db-sort-by

Conversation

@xelab04

@xelab04 xelab04 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Refer to #490

@github-actions github-actions Bot added the C-lib Crate: cot (main library crate) label Feb 24, 2026
@xelab04

xelab04 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

https://docs.rs/sea-query/latest/sea_query/query/trait.OrderedStatement.html
The sea query docs make a reference to order_by_expr which takes expr: SimpleExpr, order: Order

I'm not exactly sure how to plug sea query into this?

@xelab04
xelab04 marked this pull request as draft February 24, 2026 17:00
@xelab04

xelab04 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

Right, updated it to use seaquery's own order_by method. I'm not getting the types down for the time being, will work on it more later.

@xelab04

xelab04 commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

I have changed it so that instead of ordering on a string of the field name, it uses the fieldref identifier.
So, it would look something like this:

use cot::db::model;
use cot::db::query::Query;
use sea_query::Order; (?)

#[model]
struct User {
    #[model(primary_key)]
    id: i32,
    age: i32,
}

let query = Query::<User>::new().order_by(User::age, Order::Asc);

Last bit is the ownership because it's not happy with the .clone()
And of course, tests

@xelab04
xelab04 marked this pull request as ready for review March 11, 2026 09:18
@seqre seqre linked an issue Mar 23, 2026 that may be closed by this pull request
@seqre

seqre commented May 3, 2026

Copy link
Copy Markdown
Member

Hey @xelab04, how it's going with this PR? Do you need any help from us?

@ElijahAhianyo

Copy link
Copy Markdown
Contributor

Hey @xelab04, How's it going with this PR? We're thinking of getting this PR in for the next release planned for this week. I see this is mostly done, and if you don't mind, I would love to finish this if you don't have the time.

@xelab04

xelab04 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ElijahAhianyo I had forgotten about this PR till recently because of a series of illnesses. My apologies.

Yes, I remember having mostly completed the PR, but was particularly stumped with creating the proper tests for it. I'd be really happy if you could complete it; I'll also use the opportunity to better understand how I should have done it.

I'm sorry it's been in draft for so long.

@ElijahAhianyo

ElijahAhianyo commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Hi @ElijahAhianyo I had forgotten about this PR till recently because of a series of illnesses. My apologies.

Yes, I remember having mostly completed the PR, but was particularly stumped with creating the proper tests for it. I'd be really happy if you could complete it; I'll also use the opportunity to better understand how I should have done it.

I'm sorry it's been in draft for so long.

Ah, no worries. I'll take this up from here and also wish you a speedy recovery. Thanks a lot for your contribution!

@seqre seqre changed the title Add order-by logic to database queries feat: add order-by logic to database queries Jul 6, 2026
@ElijahAhianyo
ElijahAhianyo marked this pull request as draft August 26, 2026 00:05
@github-actions github-actions Bot added the A-docs Area: Documentation label Sep 5, 2026
fn desc(&self) -> OrderByExpr;

/// Sorts rows by the position of this field's value
fn field_value<I>(&self, values: I) -> OrderByExpr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even sure if field_value is the right name here. I felt using field would be confusing since its technially not the field name. However mysql uses the term (Field). In fact, I started off with custom but that felt way too generic.

@ElijahAhianyo
ElijahAhianyo marked this pull request as ready for review September 8, 2026 04:02
@ElijahAhianyo
ElijahAhianyo requested review from a team and m4tx September 8, 2026 04:17
@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.26087% with 40 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cot/src/db/query/expr.rs 67.34% 13 Missing and 3 partials ⚠️
cot/src/db/query/expr/order_by.rs 87.15% 13 Missing and 1 partial ⚠️
cot/src/common_types.rs 0.00% 6 Missing ⚠️
cot/src/db/query.rs 84.21% 2 Missing and 1 partial ⚠️
cot/src/db.rs 0.00% 0 Missing and 1 partial ⚠️
Flag Coverage Δ
rust 90.09% <78.26%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
cot/src/db/fields.rs 87.71% <ø> (ø)
cot/src/db.rs 83.98% <0.00%> (-0.09%) ⬇️
cot/src/db/query.rs 86.47% <84.21%> (-0.29%) ⬇️
cot/src/common_types.rs 82.85% <0.00%> (-1.82%) ⬇️
cot/src/db/query/expr/order_by.rs 87.15% <87.15%> (ø)
cot/src/db/query/expr.rs 77.02% <67.34%> (+3.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Area: Documentation C-lib Crate: cot (main library crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DB sort by method

4 participants