ddltransform

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 3 Imported by: 2

README

ddltransform

Parse ddl and transform to gorm model

go report card test status MIT license Go.Dev reference

Desc

Generate the orm model through parse sql to reduce the dependence on the environment

Usage

  1. Use generate model code.
const ddl = `		
CREATE TABLE test_data (
	id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	create_at datetime NOT NULL,
	deleted tinyint(1) NOT NULL,
	version bigint(20) DEFAULT '10' COMMENT 'version info',
	address varchar(255) NOT NULL DEFAULT 'china',
	amount decimal(19,2) DEFAULT NULL,
	wx_mp_app_id varchar(32) DEFAULT NULL,
	contacts varchar(50) DEFAULT NULL,
	PRIMARY KEY (id),
	UNIQUE KEY uk_app_version (wx_mp_app_id, version)
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARACTER SET utf8 COLLATE UTF8_GENERAL_CI ROW_FORMAT=COMPACT COMMENT='' CHECKSUM=0 DELAY_KEY_WRITE=0;
`

code, err := ddltransform.Transform(ddl, ddltransform.Config{
	Parser:      ddltransform.Mysql,
	Transformer: ddltransform.Gorm,
})

// type TestDatum struct {
//     ID        uint64    `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;autoIncrement;NOT NULL"`
//     CreateAt  time.Time `gorm:"column:create_at;type:datetime;NOT NULL"`
//     Deleted   bool      `gorm:"column:deleted;type:tinyint(1);NOT NULL"`
//     Version   int64     `gorm:"column:version;type:bigint(20);default:10;uniqueIndex:uk_app_version;comment:version info"`
//     Address   string    `gorm:"column:address;type:varchar(255);default:china;NOT NULL"`
//     Amount    float64   `gorm:"column:amount;type:decimal(19,2)"`
//     WxMpAppID string    `gorm:"column:wx_mp_app_id;type:varchar(32);uniqueIndex:uk_app_version"`
//     Contacts  string    `gorm:"column:contacts;type:varchar(50)"`
// }
  1. Customize the parser to support more db, or customize the transformer to support more code generate.
type selectTransformer struct {
}

func (*selectTransformer) Name() string {
	return "select_transfomer"
}

func (*selectTransformer) Transform(table string, fields []schema.Field) (modeCode string, err error) {
	layout := "SELECT %s FROM %s"
	cols := make([]string, len(fields))
	for i, f := range fields {
		cols[i] = f.DBName
	}
	modeCode = fmt.Sprintf(layout, strings.Join(cols, ","), table)
	return
}

code, err := ddltransform.Transform(ddl, ddltransform.Config{
	ParserType:  ddltransform.Mysql,
	Transformer: &selectTransformer{},
})

// SELECT id,create_at,deleted,version,address,amount,wx_mp_app_id,contacts FROM test_data

  1. Use command-line to generate model code

ddltcmd start -ps sqlite -tf gorm -p ./sql.ddl

More information about ddltcmd

More Examples

See full list of examples

TODO List

Support Database
  • Mysql
  • Postgresql
Support Orm
  • Gorm

Documentation

Index

Constants

View Source
const (
	// support parser type
	Mysql  ParserType = 1
	Sqlite ParserType = 2
	// support transformer type
	Gorm TransformerType = 1
)

Variables

This section is empty.

Functions

func Transform

func Transform(ddl string, config Config) (code string, err error)

Types

type Config

type Config struct {
	// ParserType support parser type
	ParserType ParserType
	// TransformerType support transformer type
	TransformerType TransformerType
	// Parser customize parser
	Parser parser.Parser
	// Transformer customize transformer
	Transformer transformer.Transformer
}

type ParserType

type ParserType uint

type TransformerType

type TransformerType uint

Directories

Path Synopsis
cmd module
ddltcmd module

Jump to

Keyboard shortcuts

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