prepare

package
v0.6.12 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MODE_SAFE uint = iota
	MODE_INIT
	MODE_TWO_FILES
	MODE_THREE_FILES
	MODE_QUERY_DISTRI
	MODE_TABLE_DISTRI
)
View Source
const DIR_MODE = 0777

Variables

View Source
var (
	TemplateNotFound   = errors.New("template not found")
	TemplateModeIntros = map[uint]string{
		MODE_SAFE:         "安全模式,与 5 类似,但会在每个文件名前面加一个下划线",
		MODE_INIT:         "只生成 init.go 文件",
		MODE_TWO_FILES:    "除了 init.go 文件, table 和 query 都放入 tables.go 中",
		MODE_THREE_FILES:  "除了 init.go 文件, table 都放入 tables.go 中, query 都放入 queries.go 中",
		MODE_QUERY_DISTRI: "除了 init.go 文件, table 都放入 tables.go 中, query 分开放入对应模型名文件中",
		MODE_TABLE_DISTRI: "除了 init.go 文件, table 和 query 一起放入对应模型名文件中",
	}
)

Functions

func AmendComments

func AmendComments(db *sql.DB, opts Options, verbose bool) error

将代码中的表和字段注释,覆盖数据库的SQL语句注释

func CheckError added in v0.5.7

func CheckError(err error) bool

检查错误

func CollectCode

func CollectCode(opts Options, outname string, verbose bool) (err error)

在目录下所有的go代码中,找出baseModel子类的完整代码写入一个文件

func CreateModels

func CreateModels(conf *Config, db *gorm.DB, mode uint) (names []string, err error)

func FindTables

func FindTables(db *sql.DB, verbose bool) ([]string, []map[string]string)

找出所有表名,并列出每张表的字段定义

func GenInitFile

func GenInitFile(conf *Config, names []string, mode uint) (err error)

func GenNameType added in v0.5.5

func GenNameType(col *schema.ColumnInfo, rule RuleConfig, nps map[string]NullPointer) string

func GenTagComment added in v0.5.5

func GenTagComment(col *schema.ColumnInfo, rule RuleConfig) string

func GetTemplate added in v0.5.5

func GetTemplate(funcMap template.FuncMap, name, fname string, others ...string) *template.Template

func NullPointerMatch added in v0.6.3

func NullPointerMatch(nps map[string]NullPointer, rule RuleConfig, ci *schema.ColumnInfo) bool

func ParseModelComments

func ParseModelComments(filename, prefix string, verbose bool) (map[string]string, map[string](map[string]string))

分析文件代码,找出所有Model注释和其中的字段注释

func ToCamel added in v0.5.7

func ToCamel(name string) string

func ToPlural added in v0.5.7

func ToPlural(name string) string

func ToSingular added in v0.5.7

func ToSingular(name string) string

func ToSnake added in v0.5.7

func ToSnake(name string) string

func WriteInitFile added in v0.6.2

func WriteInitFile(buf bytes.Buffer, fname, driverName string) error

func WriteModelFile added in v0.6.2

func WriteModelFile(buf bytes.Buffer, fname string) error

Types

type AppConfig

type AppConfig struct {
	Debug       bool   `toml:"debug"`
	OutputDir   string `toml:"output_dir"`   // 输出目录,例如 models
	DataFile    string `toml:"data_file"`    // 数据文件,例如 data.toml
	PluralTable bool   `toml:"plural_table"` // 表名使用复数形式
}

应用配置

type Config

type Config struct {
	FileName     string // 文件名
	ConnName     string // 连接名
	Application  AppConfig
	Connections  map[string]ConnConfig
	NullPointers map[string]NullPointer     `toml:"null_pointers"`
	ModelRules   map[string]TableRuleConfig `toml:"model_rules"`
}

配置

func GetConfig

func GetConfig(fileName string) (*Config, error)

解析配置和创建日志

func (Config) GetDSN

func (c Config) GetDSN(name string) (string, string)

func (Config) GetDriverName added in v0.5.5

func (c Config) GetDriverName(name string) string

func (Config) GetRules added in v0.4.3

func (c Config) GetRules(table string) (rules TableRuleConfig)

func (Config) GetTablePrefix added in v0.5.5

func (c Config) GetTablePrefix(name string) string

type ConnConfig

type ConnConfig struct {
	Driver string `toml:"driver"`
	Prefix string `toml:"prefix"`
	redisw.ConnParams
}

连接配置

type Logger added in v0.6.9

type Logger struct {
	Space string
	*log.Logger
}

gorm 之前版本(v1.9.11)日志没有在各部分中间加空格

func NewLogger added in v0.6.9

func NewLogger(out io.Writer, space string) *Logger

func (*Logger) Print added in v0.6.9

func (l *Logger) Print(v ...interface{})

type NullPointer added in v0.6.3

type NullPointer struct {
	UsePointer bool `toml:"use_pointer"` // 是否使用指针类型
	MustIndex  bool `toml:"must_index"`  // 如果使用指针类型,字段必须是索引
	MinLength  int  `toml:"min_length"`  // 如果使用指针类型,字段长度最少为多大
}

NULL字段使用指针类型

func (NullPointer) MatchCond added in v0.6.3

func (np NullPointer) MatchCond(ci *schema.ColumnInfo) bool

type Options added in v0.6.2

type Options struct {
	SourceDir        string // 源码目录,例如 models
	TargetDir        string // 临时目录,例如 cmd/tmp
	TablePrefix      string // 表名前缀,例如 t_
	BaseName         string // 基础Model名称
	OnlyTableComment bool   // 只修改表注释
}

type RuleConfig added in v0.4.3

type RuleConfig struct {
	Name    string `toml:"name"`
	Type    string `toml:"type"`
	Json    string `toml:"json"`
	Tags    string `toml:"tags"`
	Comment string `toml:"comment"`
}

规则配置

func GetRule added in v0.5.5

func GetRule(rules TableRuleConfig, name string) RuleConfig

type TableRuleConfig added in v0.4.3

type TableRuleConfig = map[string]RuleConfig

Jump to

Keyboard shortcuts

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