Basics: Generating table types
Before you can use sq, you must generate your table definitions from your database. First, download your dialect-specific code generator:
# Postgres
go get -u github.com/bokwoon95/go-structured-query/cmd/sqgen-postgres
# MySQL
go get -u github.com/bokwoon95/go-structured-query/cmd/sqgen-mysql
Generate tables from your database (replace [name], [pass], [dbname] as well as your database port number accordingly):
# for more options, check out --help
# Postgres
sqgen-postgres tables --database 'postgres://[name]:[pass]@localhost:5432/[dbname]?sslmode=disable'
# MySQL
sqgen-postgres tables --database '[name]:[pass]@tcp(127.0.0.1:3306)/[dbname]' --schema [dbname]
The above example will by default generate a directory called tables
in the current directory containing a Go package called tables
(and a file tables.go
), which contains all your table definitions. If you need to change any of these defaults, check out the --help option.
Once you have generated your table definitions, check out the Basic Examples in the Query Building section to see how you can use those definitions.