sqlcodegen

command module
v0.0.0-...-8c09973 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 4 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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