mtsmfm / sqlcodegen

Generate Go structs from SQL string literals

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sqlcodegen

Generate Go structs from SQL string literals.

Currently supports only PostgreSQL.

Usage

  1. Run init to generate sqlcodegen.yml
go run github.com/mtsmfm/sqlcodegen init
  1. Write a code which runs SQL with plain string literal and comment // sqlcodegen <struct name> just before the literal
var results []interface{}
// sqlcodegen Foo
err = db.Select(&results, "SELECT id FROM users")
  1. Run generate command
export DATABASE_URL=postgres://user:password@postgres/db?sslmode=disable
go run github.com/mtsmfm/sqlcodegen generate

Above command generates:

// Code generated by sqlcodegen, DO NOT EDIT.

package sqlstructs

/*
	SELECT id FROM users LIMIT 5
*/
type Foo struct {
	Id int `db:"id" json:"id"`
}
  1. Use struct
var results []sqlstructs.Foo
err = db.Select(&results, "SELECT id FROM users")

TODOs

  • Support MySQL
  • Add more default typemap

About

Generate Go structs from SQL string literals

License:MIT License


Languages

Language:Go 89.8%Language:Dockerfile 10.2%