genddl

package module
v0.0.0-...-0e8f5ff Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: MIT Imports: 18 Imported by: 0

README

genddl

Generate RDB DDL by go struct

THIS IS A ALPHA QUALITY RELEASE. API MAY CHANGE WITHOUT NOTICE.

Install

$ go get install github.com/mackee/go-genddl/cmd/genddl

Example

Look example sources.

Usage

1. Write schema struct.

//go:generate genddl -outpath=./mysql.sql -driver=mysql

//+table: person
type Person struct { //=> CREATE TABLE `person` (
	ID uint64 `db:"id,primarykey,autoincrement"` //=> `id`            BIGINT unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
	Name string `db:"name,unique"`               //=> `name`          VARCHAR(191) NOT NULL UNIQUE,
	Age uint32 `db:"age,null"`                   //=> `age`           INTEGER unsigned NULL,
	UserCode string `db:"usercode"`              //=> `usercode`      VARCHAR(191) NOT NULL,
	Type uint32 `db:"type"`                      //=> `type`          INTEGER unsigned NOT NULL,
	TeamID uint64 `db:"team_id"`                 //=> `team_id`       BIGINT unsigned NOT NULL,
	CreatedAt time.Time `db:"created_at"`        //=> `created_at`    DATETIME NOT NULL
}

Default NOT NULL. You can add tag null if you want nullable column.

2. Run go generate
$ ls
person.go
$ go generate
$ person.go mysq.sql

Other Features

Indexes

If you want to set indexes, write method for schema struct. It name must be _schemaIndex.

Example:

import (
	"github.com/mackee/go-genddl/index"
)

//+table:team
type Team struct { ... }

//+table:person
type Person struct { ... }

func (s Person) _schemaIndex(methods index.Methods) []index.Definition {
	return []index.Definition{
		methods.PrimaryKey(s.ID, s.CreatedAt),  //=> PRIMARY KEY (`id`, `created_at`),
		methods.Unique(s.UserCode, s.Type),     //=> UNIQUE (`usercode`, `type`),
		methods.ForeignKey(s.TeanID, Team{}.ID, index.ForeignKeyDeleteCascade, index.ForeignKeyUpdateSetDefault),
		    //=> FOREGIN KEY (`team_id`) REFERENCES team(`id`) ON DELETE CASCADE ON UPDATE SET DEFAULT
		methods.Complex(s.Age, s.Name),         //=> CREATE INDEX person_age_name (`age`, `name`);
	}
}

Documentation

Index

Constants

View Source
const (
	MYSQL_DEFAULT_VARCHAR_SIZE = "191"
)

Variables

This section is empty.

Functions

func Run

func Run(from string)

Types

type ColumnMap

type ColumnMap struct {
	Name     string
	TypeName string
	TagMap   map[string]string
}

type Dialect

type Dialect interface {
	ToSqlType(col *ColumnMap) string
	CreateTableSuffix() string
	QuoteField(field string) string
	DriverName() string
	ForeignKey(index.ForeignKeyOption) string
}

type Field

type Field struct {
	ColumnDef string
}

type IndexMap

type IndexMap struct {
	Name       string
	Columns    []string
	Unique     bool
	PrimaryKey bool
	TagMap     map[string]string
}

type MysqlDialect

type MysqlDialect struct {
}

func (MysqlDialect) CreateTableSuffix

func (m MysqlDialect) CreateTableSuffix() string

func (MysqlDialect) DriverName

func (m MysqlDialect) DriverName() string

func (MysqlDialect) ForeignKey

func (m MysqlDialect) ForeignKey(option index.ForeignKeyOption) string

func (MysqlDialect) QuoteField

func (m MysqlDialect) QuoteField(field string) string

func (MysqlDialect) ToSqlType

func (m MysqlDialect) ToSqlType(col *ColumnMap) string

type Sqlite3Dialect

type Sqlite3Dialect struct {
}

func (Sqlite3Dialect) CreateTableSuffix

func (m Sqlite3Dialect) CreateTableSuffix() string

func (Sqlite3Dialect) DriverName

func (m Sqlite3Dialect) DriverName() string

func (Sqlite3Dialect) ForeignKey

func (m Sqlite3Dialect) ForeignKey(option index.ForeignKeyOption) string

func (Sqlite3Dialect) QuoteField

func (m Sqlite3Dialect) QuoteField(field string) string

func (Sqlite3Dialect) ToSqlType

func (m Sqlite3Dialect) ToSqlType(col *ColumnMap) string

type Table

type Table struct {
	Name       string
	Fields     []Field
	PrimaryKey string
}

type TableMap

type TableMap struct {
	Name          string
	Columns       []*ColumnMap
	ColumnIndexes []*IndexMap
	Indexes       []indexer
	Tables        map[*ast.StructType]string
}

func NewTableMap

func NewTableMap(name string, structType *ast.StructType, funcs []*ast.FuncDecl, tables map[*ast.StructType]string) *TableMap

func (*TableMap) WriteDDL

func (tm *TableMap) WriteDDL(w io.Writer, dialect Dialect) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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