Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dynajoe/sql-gen-go


https://github.com/dynajoe/sql-gen-go

Last synced: 21 days ago
JSON representation

Awesome Lists containing this project

README

        

# SQL Gen Go

This package takes a directory and builds go types to be used with generated sql query builder.

## Example:

Assume this directory structure

```
./sql
/authors
create.sql
fetch.sql
list.sql
```

This tool will build an output like this (annotated in comments):

```go
// Code generated by go generate; DO NOT EDIT.
package sql

// Based on the directory and file authors/create.sql
// parameters from the sql file e.g. :bio and :name are available as part of the struct.
// in the future these params will be able to be annotated with primitives or sql.* types.
type AuthorsCreate struct {
Bio interface{}
Name interface{}
}

// authors/fetch.sql
type AuthorsFetch struct {
AuthorId interface{}
}

// authors/list.sql (no parameters)
type AuthorsList struct {
}

func (p AuthorsCreate) Build() (string, []interface{}) {
return "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING *;", []interface{}{
p.Name,
p.Bio,
}
}

func (p AuthorsFetch) Build() (string, []interface{}) {
return "SELECT * FROM authors\nWHERE id = $1 LIMIT 1;", []interface{}{
p.AuthorId,
}
}

func (p AuthorsList) Build() (string, []interface{}) {
return "SELECT * FROM authors\nORDER BY name;", []interface{}{}
}
```

And usage:

```go
package main

import "github.com/dynajoe/sql-gen-go/sql"

func main() {
query, params := (sql.AuthorsCreate{
Name: "Joe",
Bio: "I like databases",
}).Build()

// Use query and params with your sql driver of choice
}
```