genddl

package module
v0.0.0-...-fade26b Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 19 Imported by: 0

README

genddl

Generate RDB DDL by go struct

Install

$ go install github.com/mackee/go-genddl/cmd/genddl@latest

Example

Look example sources.

Usage

1. Write schema struct.

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

//genddl: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"
)

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

//genddl: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.TeamID, 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`);
	}
}
CLI Options
Usage of genddl:
  -driver string
        target driver name. support mysql, pg, sqlite3 (default "mysql")
  -foreignkeyname
        Provides a name for the definition of a foreign-key.
  -innerindex create table
        Placement of index definition. If this specified, the definition was placement inner of create table
  -outerforeignkey
        Placement of foreign key definition. If this specified, the definition was placement end of DDL file.
  -outeruniquekey
        Placement of unique key definition. If this specified, the definition was placement outer of CREATE TABLE.
  -outpath string
        schema target path
  -schemadir string
        schema declaretion directory
  -tablecollate string
        Provides a collate for the definition of tables.
  -uniquename
        Provides a name for the definition of a unique index.
  -withoutdroptable
        If this specified, the DDL file does not contain DROP TABLE statement.

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
	IsNullable bool
	TagMap     map[string]string
}

type Dialect

type Dialect interface {
	ToSqlType(col *ColumnMap) (string, error)
	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 {
	Collate string
}

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, error)

type PostgresqlDialect

type PostgresqlDialect struct {
	Collate string
}

func (PostgresqlDialect) CreateTableSuffix

func (m PostgresqlDialect) CreateTableSuffix() string

func (PostgresqlDialect) DriverName

func (m PostgresqlDialect) DriverName() string

func (PostgresqlDialect) ForeignKey

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

func (PostgresqlDialect) QuoteField

func (m PostgresqlDialect) QuoteField(field string) string

func (PostgresqlDialect) ToSqlType

func (m PostgresqlDialect) ToSqlType(col *ColumnMap) (string, error)

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, error)

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
	EndOfDDLFileIndexes []indexer
}

func NewTableMap

func NewTableMap(name string, structType *ast.StructType, funcs []*ast.FuncDecl, tables map[*ast.StructType]string, ti *types.Info, opts *tableMapOption) (*TableMap, error)

func (*TableMap) WriteDDL

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

type ViewMap

type ViewMap struct {
	Name            string
	Columns         []string
	SelectStatement string
}

func NewViewMap

func NewViewMap(input newViewMapInput) (*ViewMap, error)

func (*ViewMap) WriteDDL

func (vm *ViewMap) 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