https://github.com/omigo/light
Generate Go/Golang database/sql query code, spirit from MyBatis/iBatis.
https://github.com/omigo/light
generate gobatis ibatis mybatis mysql orm osm sql
Last synced: 2 months ago
JSON representation
Generate Go/Golang database/sql query code, spirit from MyBatis/iBatis.
- Host: GitHub
- URL: https://github.com/omigo/light
- Owner: omigo
- Created: 2016-04-27T06:36:06.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2021-02-07T03:37:03.000Z (about 5 years ago)
- Last Synced: 2024-06-21T06:08:17.279Z (almost 2 years ago)
- Topics: generate, gobatis, ibatis, mybatis, mysql, orm, osm, sql
- Language: Go
- Homepage:
- Size: 3.51 MB
- Stars: 84
- Watchers: 9
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
light [](https://api.travis-ci.org/omigo/light.svg?branch=master)
=====
`light` is a tool for generating database query code from go source file with
interface methods commented with SQLs and Variables.
`Interface methods commented with SQL and variables` => `go generate`=> `Database query code implementation`

### Usage
Install `light` tool. Make sure $GOBIN in your $PATH environment.
`go get -u -v github.com/omigo/light`
Run `light -h`, check install.
# light -h
Usage of light:
-log
Generated file with log
Define a interface, and comment methods with SQLs and Variables, then write a directive `//go:generate light`.
```go
//go:generate light -log -timeout 30
type User interface {
// UPDATE users
// SET [username=?,]
// [phone=?,]
// [address=?,]
// [status=?,]
// [birthday=?,]
// updated=CURRENT_TIMESTAMP
// WHERE id=?
Update(u *model.User) (int64, error)
}
```
After that, run `go generate ./...`, code generated.
# go generate ./...
Source file /Users/Arstd/Reposits/src/github.com/omigo/light/example/store/user.go
Generated file /Users/Arstd/Reposits/src/github.com/omigo/light/example/store/user.light.go
```go
type UserStore struct{}
func (*UserStore) Update(u *model.User) (int64, error) {
var buf bytes.Buffer
var args []interface{}
buf.WriteString(`UPDATE users SET `)
if u.Username != "" {
buf.WriteString(`username=?, `)
args = append(args, u.Username)
}
if u.Phone != "" {
buf.WriteString(`phone=?, `)
args = append(args, null.String(&u.Phone))
}
if u.Address != nil {
buf.WriteString(`address=?, `)
args = append(args, u.Address)
}
if u.Status != 0 {
buf.WriteString(`status=?, `)
args = append(args, null.Uint8(&u.Status))
}
if u.Birthday != nil {
buf.WriteString(`birthday=?, `)
args = append(args, u.Birthday)
}
buf.WriteString(`updated=CURRENT_TIMESTAMP WHERE id=? `)
args = append(args, null.Uint64(&u.Id))
query := buf.String()
log.Debug(query)
log.Debug(args...)
res, err := db.Exec(query, args...)
if err != nil {
log.Error(query)
log.Error(args...)
log.Error(err)
return 0, err
}
return res.RowsAffected()
}
```
### More
Complete demo in example.
Source interface: [example/store/user.go](example/store/user.go)
Generated code: [example/store/user.light.go](example/store/user.light.go)