Skip to content

Improve flexibility of named params#908

Closed
thomcc wants to merge 1 commit intorusqlite:masterfrom
thomcc:more-flexible-named-params
Closed

Improve flexibility of named params#908
thomcc wants to merge 1 commit intorusqlite:masterfrom
thomcc:more-flexible-named-params

Conversation

@thomcc
Copy link
Member

@thomcc thomcc commented Mar 3, 2021

Fixes #907... but I guess maybe they don't need it.

My main concern with this change is that it might make type inference work less well for users not using named_params!. It also might make the docs less clear...

What do you think @gwenn. The use case for this is pretty niche, is it worth supporting?

@thomcc thomcc requested a review from gwenn March 3, 2021 02:21
@codecov
Copy link

codecov bot commented Mar 3, 2021

Codecov Report

Merging #908 (cfd351c) into master (2c64275) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #908   +/-   ##
=======================================
  Coverage   77.35%   77.35%           
=======================================
  Files          48       48           
  Lines        5600     5600           
=======================================
  Hits         4332     4332           
  Misses       1268     1268           
Impacted Files Coverage Δ
src/params.rs 100.00% <ø> (ø)
src/statement.rs 91.29% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2c64275...acac5a1. Read the comment docs.

@gwenn
Copy link
Collaborator

gwenn commented Mar 3, 2021

Is it the same issue as described here:
#312 (comment)
?

@thomcc
Copy link
Member Author

thomcc commented Mar 3, 2021

Is it the same issue as described here:
#312 (comment)
?

It's similar, but with named params only.

Anyway, I'm not sure it's worth it, although there's no real alternative if you did have a Vec<(&str, Box<dyn ToSql>)>.

That said, this fix doesn't fix it if you had a Vec<(String, Box<dyn ToSql>)>. Maybe it could, but I'm worried about accidentally adding another case where something that seems like it should typecheck fails (similar to NO_PARAMS being necessary since &[] is ambiguous)

Maybe handling it the way iterables are handled would be better...

@gwenn
Copy link
Collaborator

gwenn commented Mar 4, 2021

Maybe handling it the way iterables are handled would be better...

You mean like ParamsFromIter ?

pub struct NamedParamsFromIter<I>(I);

pub fn named_params_from_iter<I>(iter: I) -> ParamsFromIter<I>
where
    I: IntoIterator,
    I::Item: (&str, ToSql), // or T: ToSql
{
    NamedParamsFromIter(iter)
}

@thomcc
Copy link
Member Author

thomcc commented Mar 4, 2021

Basically. I think we could solve the &str vs String case too like:

pub struct NamedParamsFromIter<I>(I);

pub fn named_params_from_iter<I, S, T>(iter: I) -> NamedParamsFromIter<I>
where
    I: IntoIterator<Item = (S, T)>,
    S: AsRef<str>,
    T: ToSql,
{
    NamedParamsFromIter(iter)
}

(Haven't actually tried this code)

@thomcc
Copy link
Member Author

thomcc commented Mar 4, 2021

The use case here seems pretty niche still though, but it's not like there's a good way to do it otherwise, and this shouldn't negatively impact normal usage. Hrm.

@gwenn
Copy link
Collaborator

gwenn commented Mar 4, 2021

Do you know if something like BorrowToSql would help here ?

A trait used by clients to abstract over &dyn ToSql and T: ToSql

@hutchisr
Copy link

hutchisr commented Mar 4, 2021

@thomcc Less that I don't "need" it as much as it is possible to work around by using numbered parameters. The ability to do this would still be very much appreciated if it doesn't end up breaking other functionality.

@mzr
Copy link

mzr commented Feb 22, 2022

@thomcc are you thinking of finishing this PR? This looks exactly like something I was looking for.

@thomcc
Copy link
Member Author

thomcc commented Feb 22, 2022

I'll look into finishing it this weekend.

gwenn added a commit to gwenn/rusqlite that referenced this pull request Feb 16, 2025
@gwenn
Copy link
Collaborator

gwenn commented Feb 16, 2025

See #1652 with BindIndex

@gwenn gwenn closed this Feb 16, 2025
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

Successfully merging this pull request may close these issues.

How do I make a (variable length) set of named parameters at runtime?

4 participants