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
Copy file name to clipboardExpand all lines: README.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,3 +76,49 @@ class Status(str, enum.Enum):
76
76
OPEN = "op!en"
77
77
CLOSED = "clo@sed"
78
78
```
79
+
80
+
### Bulk Inserts with `:copyfrom`
81
+
82
+
Use the `:copyfrom` command to generate batch insert methods that leverage SQLAlchemy’s executemany behavior via `Connection.execute()` with a list of parameter mappings.
83
+
84
+
SQL (example):
85
+
86
+
```sql
87
+
-- name: CreateUsersBatch :copyfrom
88
+
INSERT INTO users (email, name) VALUES ($1, $2);
89
+
```
90
+
91
+
Generated methods:
92
+
93
+
```py
94
+
def create_users_batch(self, arg_list: List[Any]) -> int
95
+
async def create_users_batch(self, arg_list: List[Any]) -> int
96
+
```
97
+
98
+
Call with a list of dicts using positional parameter keys `p1..pN` (the generator converts `$1`/`@name` to `:pN`):
When a typed params struct is emitted (e.g., many parameters or config thresholds), the method accepts `List[<QueryName>Params]`. The generator converts items to dicts internally:
Implementation note: sync and async use `conn.execute(sqlalchemy.text(SQL), list_of_dicts)` and `await async_conn.execute(...)` respectively; SQLAlchemy performs efficient batch inserts under the hood.
0 commit comments