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

Is it possible to join statements? #96

Closed
sundaycrafts opened this issue Jul 4, 2024 · 2 comments
Closed

Is it possible to join statements? #96

sundaycrafts opened this issue Jul 4, 2024 · 2 comments

Comments

@sundaycrafts
Copy link

sundaycrafts commented Jul 4, 2024

Hello, I want join the statements to programmatically build queries with transaction such as like below.

// ...

let nested_foos = [ Foo { bars: [Bar { ... }, Bar { ... }, Bar { ... } ] }, , , ];

let mut stmt = a_connection.prepare(["begin transaction;"]);

for foo in nested_foos {
    let bars = foo.bars.clone().iter().map( ... );

    for bar in bars {
        stmt.join("insert into bars values <map iterative values here>");
    }
}

stmt.join("commit;");

// ...

It's really useful when I persist denormalized objects into the DB.
Or perhaps it also works to provide a .transaction() method like other libraries and supporting array query #38.
But I think the above approach is slightly better because it's more like generic (and also, currently we can't await the asynchronous process in the lambda).

Is there any solution? It's technically possible by using format!() macro or some template engines, for example. But format!() macro is unrealistic when normalizing complex objects. And I feel using template engines is too much for this purpose.

@IvanUkhov
Copy link
Member

Hello, there is no any abstraction to do what your pseudo-code suggests. I would indeed just have a loop but with one prepared statements to which I would keep on binding different values taken from the objects iterated over. Please see the examples in the readme. If you are looking for transactions, please take a look at this closed PR: #35.

@sundaycrafts
Copy link
Author

Understood. I'm ashamed I was silly. Thank you for adding an understandable comment to the related PR.

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

No branches or pull requests

2 participants