You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You may ask, then how do we insert multiple elements? Which brings us to the next feature:
172
172
173
-
## Repeating Group
173
+
## Insert Many
174
+
175
+
When working with Rust and SQL, a common pain point is figuring out how to insert multiple rows efficiently. Writing raw SQL INSERT statements for each row is verbose, and while Postgres supports `UNNEST` for bulk inserts, its columnar API feels awkward and error-prone.
176
+
177
+
SeaQuery solves this by letting you build multi-row insert statements easily. With the `raw_query!` macro, you can pass in a vector of structs or tuples and generate a single `INSERT INTO .. VALUES (..) , (..) , (..)` query. This keeps your code clean, while still allowing you to do bulk insertions for efficiency. This solution is database agnostic so you can use it for MySQL and SQLite as well.
178
+
179
+
### With Tuples
174
180
175
181
```rust
176
182
letvalues=vec![(1, "2", 3), (4, "5", 6)];
@@ -187,11 +193,11 @@ assert_eq!(
187
193
);
188
194
```
189
195
190
-
This syntax almost looks like regex now. Please let me explain:
196
+
To achieve this we designed the "repeating group" syntax, inspired by regex. Please let me explain:
191
197
192
198
It's expanded upon the previous example, in which `values.0:2` means tuple expansion. We want to repeat this tuple as a group, surrounded by parenthesis, so we wrap it with `()`. Then we apply the same spread operator `..` to expand the vector of tuples. Finally, the trailing `,` means they should be connected with `,`.
193
199
194
-
This repeating group is not fully-generalized yet, but is quite flexible:
200
+
### With structs
195
201
196
202
```rust
197
203
structItem {
@@ -212,7 +218,7 @@ let query = sea_query::raw_query!(
212
218
);
213
219
```
214
220
215
-
This is equivalent to the previous example.
221
+
This is equivalent to the previous example, but uses named parameters.
216
222
217
223
## SQLx Integration
218
224
@@ -246,7 +252,34 @@ let res = sea_query::sqlx::sqlite::query!(
246
252
247
253
Full example can be found [here](https://github.com/SeaQL/sea-query/blob/master/examples/sqlx_sqlite/src/main.rs).
248
254
249
-
It almost feels like a mini ORM ... although [SeaORM π](https://github.com/SeaQL/sea-orm) is still highly recommended by us!
255
+
It almost feels like a mini ORM ... although [SeaORM π](https://github.com/SeaQL/sea-orm) is still highly recommended!
256
+
257
+
### Lightweightness
258
+
259
+
SeaQuery without default features only has a handful of dependencies. But if you want to keep dependencies to an absolute minimum, you can depends on `sea-query-derive` directly.
260
+
261
+
```sh
262
+
$ cargo tree --no-default-features -e normal,build
0 commit comments