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

Way to convert QueryAs to string #1771

Closed
rex-remind101 opened this issue Mar 31, 2022 · 6 comments
Closed

Way to convert QueryAs to string #1771

rex-remind101 opened this issue Mar 31, 2022 · 6 comments

Comments

@rex-remind101
Copy link

I'd like a way to convert QueryAs to string in order to assert against the raw bound sql string, however it looks like the function that produces a string is not pub

fn sql(&self) -> &'q str {
. Could this be made public or is there an alternative way to accomplish this?

@abonander
Copy link
Collaborator

The bindings are not formatted into the string, the string you give it is the one that's sent to the database and the bindings are sent alongside in a binary encoding.

@rex-remind101
Copy link
Author

That makes sense, thank you. My thought is it would be best in a test assertion to assert against a string which includes bound variables interpolated, so it's easy to visually inspect where they were bound, given human error in writing queries with many variables. How would you suggest going about this?

@abonander
Copy link
Collaborator

#875 is intended to make it harder to mess up with bindings.

@rex-remind101
Copy link
Author

That looks great! Approximately how far away is that, because I'm hoping to get this testing in a nice place asap?

If it's on the order of months, which in the open source world could mean next year, possibly there's a quicker short term solution?

@abonander
Copy link
Collaborator

I don't have an exact timeline on it, nor do I have a good solution here right now.

What I typically do for queries long enough that I'm worried about mixing up bind parameters, is leave a comment at the top of the query noting what is what so it's easier to keep track. This really only works for Postgres and SQLite though, which have numbered/named bind parameters:

sqlx::query!(
    // $1: foo_id
    // $2: bar_id
    // $3: baz_id
    r#"
        select * from foo 
        inner join bar on foo.bar_id = bar.id 
        inner join baz on bar.baz_id = baz.id
        where foo.id = $1 and bar.id = $2 and baz.id = $3
    "#,
    foo_id,
    bar_id,
    baz_id
)

@abonander abonander closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2022
@rex-remind101
Copy link
Author

@abonander
I'd also like to have the query string available for tracing, we have the QueryAs type available to us. What we have to do instead now is wrap QueryAs and the original string in a tuple which is awkward and wouldn't be necessary if fn sql was public.

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

No branches or pull requests

2 participants