sqlc-plugin-bulk-go

command module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 18 Imported by: 0

README

sqlc-plugin-bulk-go

A sqlc plugin that automatically generates bulk insert functions for your existing sqlc-generated BULK INSERT queries.

Overview

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:

  1. Identifying all INSERT queries in your sqlc configuration
  2. Generating a bulk version of each INSERT function that accepts a slice of parameters
  3. Creating helper functions to build the SQL query and extract values from the parameter structs

Features

  • Automatically generates bulk insert functions for all INSERT queries
  • Handles parameter extraction from struct fields
  • Builds proper SQL queries with placeholders for multiple rows
  • Maintains type safety with Go generics

Options

The plugin supports the following configuration options:

Option Type Required Description
package string Yes The package name for the generated code

Usage

1. Install the plugin

To use this plugin, you need to install it first. You can do this by running the following command:

 go install github.com/tomtwinkle/sqlc-plugin-bulk-go@latest
2. Configure sqlc.yaml

Add the plugin to your sqlc configuration:

version: "2"
plugins:
  - name: bulkinsert
    process:
      cmd: "sqlc-plugin-bulk-go"
sql:
  - schema: "path/to/schema.sql"
    queries: "path/to/query.sql"
    engine: "postgresql" # or "mysql"
    codegen:
      - plugin: bulkinsert
        out: "path/to/output"
        options:
          package: "db" # Replace with your database package name
  - schema: "path/to/schema.sql"
    queries: "path/to/query.sql"
    engine: "postgresql" # or "mysql"
    gen:
      go:
         # sqlc-gen-go is the default codegen for sqlc
         # https://github.com/sqlc-dev/sqlc-gen-go
3. Generate code

Run sqlc to generate your code:

sqlc generate
4. Use the generated bulk insert functions

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.

// In your db/bulk.sql.go file (generated by this plugin)
package db

// Generated by this plugin
type BulkCreateUserParams []CreateUserParams

func (q *Queries) BulkCreateUser(ctx context.Context, args BulkCreateUserParams) error {
    // Implementation generated by this plugin
    return nil
}
// Example code showing how the generated code would look like

// In your db/models.go file (generated by sqlc)
package db

import (
    "context"
    "database/sql"
)

// Original sqlc query
const createUser = `
INSERT INTO users (id, name, email)
VALUES ($1, $2, $3)
`

// Generated by sqlc
type CreateUserParams struct {
    ID    int64
    Name  string
    Email string
}

type Queries struct {
    db *sql.DB
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
    // Implementation generated by sqlc
    return nil
}
// In your application code
func ExampleUsage() {
    // Create a database connection
    db, err := sql.Open("postgres", "postgresql://user:password@localhost:5432/mydb?sslmode=disable")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // Create a queries object
    queries := &Queries{db: db}

    // Create a context
    ctx := context.Background()

    // Prepare data for bulk insert
    users := BulkCreateUserParams{
        {ID: 1, Name: "User 1", Email: "user1@example.com"},
        {ID: 2, Name: "User 2", Email: "user2@example.com"},
        {ID: 3, Name: "User 3", Email: "user3@example.com"},
    }

    // Execute bulk insert
    err = queries.BulkCreateUser(ctx, users)
    if err != nil {
        panic(err)
    }
}

License

MIT License

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL