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

https://github.com/johejo/dbtypegen

dbtypegen is a tool that generates Go structures from a database schema.
https://github.com/johejo/dbtypegen

code-generation code-generator database ddl generator go golang schema sql

Last synced: about 2 months ago
JSON representation

dbtypegen is a tool that generates Go structures from a database schema.

Awesome Lists containing this project

README

          

# dbtypegen

[![ci](https://github.com/johejo/dbtypegen/workflows/ci/badge.svg?branch=main)](https://github.com/johejo/dbtypegen/actions?query=workflow%3Aci)
[![Go Reference](https://pkg.go.dev/badge/github.com/johejo/dbtypegen.svg)](https://pkg.go.dev/github.com/johejo/dbtypegen)
[![codecov](https://codecov.io/gh/johejo/dbtypegen/branch/main/graph/badge.svg)](https://codecov.io/gh/johejo/dbtypegen)
[![Go Report Card](https://goreportcard.com/badge/github.com/johejo/dbtypegen)](https://goreportcard.com/report/github.com/johejo/dbtypegen)

dbtypegen is a tool that generates Go structures from a database schema.

## Install

```
go install github.com/johejo/dbtypegen/cmd/dbtypegen
```

## Usage

```
mkdir example
cd example/
go mod init example
```

Create schema DDL

`schema.sql`

```sql
CREATE TABLE `user` (
id BIGINT PRIMARY KEY,
created_at DATETIME(3) NOT NULL,
active BOOLEAN DEFAULT NULL,
name VARCHAR(36) NOT NULL
);
```

```
dbtypegen -schema schema.sql -out ./db_types_gen.go -package example
```

Generated file

```go
// Code generated by dbtypegen, DO NOT EDIT.

package example

import (
"time"
)

// User is the type that represents table `user`.
type User struct {
Id int64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
Active bool `db:"active"`
Name string `db:"name"`
}

// Columns returns all columns as joined string
func (t *User) Columns() string {
return "id,created_at,active,name"
}

// ColumnList returns all columns as slice of string.
func (t *User) ColumnList() []string {
return []string{"id", "created_at", "active", "name"}
}

// TableName returns the name of table.
func (t *User) TableName() string {
return "user"
}

// SelectAll returns a part of query like `SELECT id,name FROM people`.
func (t *User) SelectAll() string {
return "SELECT id,created_at,active,name FROM user"
}

// ScanAll returns field's pointers for row.Scan.
func (t *User) ScanAll() []interface{} {
return []interface{}{&t.Id, &t.CreatedAt, &t.Active, &t.Name}
}
```

INSERT and SELECT

```go
var u User

var b strings.Builder
b.WriteString("INSERT INTO ")
b.WriteString(u.TableName())
b.WriteString(" (")
b.WriteString(u.Columns())
b.WriteString(") ")
b.WriteString("VALUES (?,?,?,?)")
q := b.String()
now := time.Now()
args := []interface{}{1, now, true, "Gopher"}

if _, err := db.ExecContext(ctx, q, args...); err != nil {
panic(err)
}

if err := db.QueryRowContext(ctx, u.SelectAll()+" WHERE id=?", 1).Scan(u.ScanAll()...); err != nil {
panic(err)
}

fmt.Println(u)
// {1 20xx-xx-xx xx:xx:xx.xxx +0000 UTC true Gopher}
```

## License

MIT

## Author

Mitsuo Heijo