|
1 | 1 | # sqlc-plugin-bulk-go |
2 | | -Plugin to generate bulk insert function by sqlc |
| 2 | + |
| 3 | +A [sqlc](https://github.com/sqlc-dev/sqlc) plugin that automatically generates bulk insert functions for your existing sqlc-generated BULK INSERT queries. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This plugin analyzes your sqlc-generated BULK INSERT queries and creates corresponding bulk insert functions that can efficiently insert multiple rows in a single database operation. It works by: |
| 8 | + |
| 9 | +1. Identifying all INSERT queries in your sqlc configuration |
| 10 | +2. Generating a bulk version of each INSERT function that accepts a slice of parameters |
| 11 | +3. Creating helper functions to build the SQL query and extract values from the parameter structs |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- Automatically generates bulk insert functions for all INSERT queries |
| 16 | +- Handles parameter extraction from struct fields |
| 17 | +- Builds proper SQL queries with placeholders for multiple rows |
| 18 | +- Maintains type safety with Go generics |
| 19 | + |
| 20 | +## Options |
| 21 | + |
| 22 | +The plugin supports the following configuration options: |
| 23 | + |
| 24 | +| Option | Type | Required | Description | |
| 25 | +|--------|------|----------|-------------| |
| 26 | +| `package` | string | Yes | The package name for the generated code | |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### 1. Configure sqlc.yaml |
| 31 | + |
| 32 | +Add the plugin to your sqlc configuration: |
| 33 | + |
| 34 | +```yaml |
| 35 | +version: "2" |
| 36 | +plugins: |
| 37 | + - name: bulkinsert |
| 38 | + process: |
| 39 | + cmd: "go run github.com/tomtwinkle/sqlc-plugin-bulk-go" |
| 40 | + options: |
| 41 | + package: "db" # Replace with your database package name |
| 42 | + |
| 43 | +sql: |
| 44 | + - schema: "path/to/schema.sql" |
| 45 | + queries: "path/to/query.sql" |
| 46 | + engine: "postgresql" # or "mysql" |
| 47 | + codegen: |
| 48 | + - plugin: bulkinsert |
| 49 | + out: "path/to/output" |
| 50 | +``` |
| 51 | +
|
| 52 | +### 2. Generate code |
| 53 | +
|
| 54 | +Run sqlc to generate your code: |
| 55 | +
|
| 56 | +```bash |
| 57 | +sqlc generate |
| 58 | +``` |
| 59 | + |
| 60 | +### 3. Use the generated bulk insert functions |
| 61 | + |
| 62 | +For each INSERT query in your sqlc configuration, a corresponding bulk insert function will be generated. For example, if you have a query named `CreateUser`, a `BulkCreateUser` function will be generated. |
| 63 | + |
| 64 | +```go |
| 65 | +// In your db/bulk.sql.go file (generated by this plugin) |
| 66 | +package db |
| 67 | + |
| 68 | +// Generated by this plugin |
| 69 | +type BulkCreateUserParams []CreateUserParams |
| 70 | + |
| 71 | +func (q *Queries) BulkCreateUser(ctx context.Context, args BulkCreateUserParams) error { |
| 72 | + // Implementation generated by this plugin |
| 73 | + return nil |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +```go |
| 78 | +// Example code showing how the generated code would look like |
| 79 | + |
| 80 | +// In your db/models.go file (generated by sqlc) |
| 81 | +package db |
| 82 | + |
| 83 | +import ( |
| 84 | + "context" |
| 85 | + "database/sql" |
| 86 | +) |
| 87 | + |
| 88 | +// Original sqlc query |
| 89 | +const createUser = ` |
| 90 | +INSERT INTO users (id, name, email) |
| 91 | +VALUES ($1, $2, $3) |
| 92 | +` |
| 93 | + |
| 94 | +// Generated by sqlc |
| 95 | +type CreateUserParams struct { |
| 96 | + ID int64 |
| 97 | + Name string |
| 98 | + Email string |
| 99 | +} |
| 100 | + |
| 101 | +type Queries struct { |
| 102 | + db *sql.DB |
| 103 | +} |
| 104 | + |
| 105 | +func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error { |
| 106 | + // Implementation generated by sqlc |
| 107 | + return nil |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +```go |
| 112 | +// In your application code |
| 113 | +func ExampleUsage() { |
| 114 | + // Create a database connection |
| 115 | + db, err := sql.Open("postgres", "postgresql://user:password@localhost:5432/mydb?sslmode=disable") |
| 116 | + if err != nil { |
| 117 | + panic(err) |
| 118 | + } |
| 119 | + defer db.Close() |
| 120 | + |
| 121 | + // Create a queries object |
| 122 | + queries := &Queries{db: db} |
| 123 | + |
| 124 | + // Create a context |
| 125 | + ctx := context.Background() |
| 126 | + |
| 127 | + // Prepare data for bulk insert |
| 128 | + users := BulkCreateUserParams{ |
| 129 | + {ID: 1, Name: "User 1", Email: "[email protected]"}, |
| 130 | + {ID: 2, Name: "User 2", Email: "[email protected]"}, |
| 131 | + {ID: 3, Name: "User 3", Email: "[email protected]"}, |
| 132 | + } |
| 133 | + |
| 134 | + // Execute bulk insert |
| 135 | + err = queries.BulkCreateUser(ctx, users) |
| 136 | + if err != nil { |
| 137 | + panic(err) |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +[MIT License](LICENSE) |
0 commit comments