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

PaginatorTrait for SelectorRaw #500

Closed
cemoktra opened this issue Feb 2, 2022 · 5 comments · Fixed by #617
Closed

PaginatorTrait for SelectorRaw #500

cemoktra opened this issue Feb 2, 2022 · 5 comments · Fixed by #617
Labels
good first issue Good for newcomers

Comments

@cemoktra
Copy link
Contributor

cemoktra commented Feb 2, 2022

Hi,

it seems this is not implemented at the moment, any plans on doing it?

@billy1624
Copy link
Member

Hey @cemoktra, good suggestions! Currently, we have no plans on doing it. I would open this for the community, any contributions are welcomed! Ping us if you need any help along the way :P

@billy1624 billy1624 added the good first issue Good for newcomers label Feb 13, 2022
@shinbunbun
Copy link
Contributor

@billy1624

Hi, I would like to contribute to solving this issue.
I have thought of the following implementation, but could you please let me know if there is another good way to implement it?

impl<'db, C, S> PaginatorTrait<'db, C> for SelectorRaw<S>
where
    C: ConnectionTrait,
    S: SelectorTrait + Send + Sync + 'db,
{
    type Selector = S;
    fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, S> {
        let mut sql = self.stmt.sql.trim();
        if sql.starts_with("SELECT") || sql.starts_with("select") {
            sql = &sql[6..];
        }

        let mut query = SelectStatement::new();
        query.expr(Expr::cust(sql));

        Paginator {
            query,
            page: 0,
            page_size,
            db,
            selector: PhantomData,
        }
    }
}

@billy1624
Copy link
Member

Hey @shinbunbun, thank you so much for reaching out!!

I think we can assume the statement is a select statement. We can explicitly mention this assumption on the method docs.

So, this part and be ignored.

if sql.starts_with("SELECT") || sql.starts_with("select") {
    sql = &sql[6..];
}

Statement might have sea_query::Value with it. So, we also need to pass it in (if any).

if have_values {
    query.expr(Expr::cust_with_values(sql, values));
} else {
    query.expr(Expr::cust(sql));
}

All in all your implementation looks good! :P

@shinbunbun
Copy link
Contributor

@billy1624

Thank you for your reply!
When we build a SelectStatement, it is automatically prefixed with SELECT. So, if I implement it as documented, the SQL statement generated will be, for example, SELECT SELECT "cake". name" FROM "cake" GROUP BY "cake"." name". Therefore, I have implemented a process to eliminate the SELECT. Please point out if I am wrong as I may not have understood the intent of your comment.

@shinbunbun
Copy link
Contributor

@billy1624
Sorry, I misunderstood your advice.
I have submitted a Pull Request and would like you to review it.

#617

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants