Skip to content

[Golang] More Control Over Generated Code #4313

@milers0

Description

@milers0

What do you want to change?

In Short

I would like additional configurability for Go code generation:

  1. Option to disable generation of db.go
  2. Ability to rename the generated Queries type
  3. Ability to specify a prefix for generated output files

Use case/Motivation
I am building a small wrapper package (database) around SQLite to strictly control what is exposed to consumers.

Currently, I structure it like this:

package database

func NewDatabaseConnection(...) (*Database, error)

func (d *Database) Close()
func (d *Database) Migrate()
func (d *Database) Begin() (*Queries, error) // begin a transaction with &Queries{db: tx}

func (q *Queries) Commit()
func (q *Queries) Rollback()
// sqlc generated: 
func (q *Queries) WithTx() // I would prefer this NOT to be generated
func (q *Queries) ... // the generated queries

Problem 1 - db.go generation
The generated db.go file introduces New(db DBTX) *Queries, WithTx(tx *sql.Tx) *Queries and DBTX interface. For my use case, I do not want these, so I manually delete the file after each code generation.

It would be great to have something like:

gen:
  go:
    emit_db_file: false

Problem 2 - Naming Queries
Currently, sqlc generates Queries. This now 'leaks' into the rest of the public API. It would be great to be able to configure it via:

gen:
  go:
    queries_type_name: Transaction

The methods generated by sqlc would then attach to type Transaction struct { ... } defined in another file in the same package.

Problem 3 - Prefix file names
There is already output_files_suffix:, but no equivalent for prefix. I would like something like:

gen:
  go:
    output_files_prefix: generated_

This makes generated files visually grouped and easier to distinguish from handwritten code.

If you’d like, I recently picked up Golang and I think this might be a doable issue for me.

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions