gorm_model

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: MIT Imports: 6 Imported by: 1

README

gorm-model

一键生成关联数据表的gorm model

安装到项目中
go get -u github.com/MQEnergy/gorm-model
注意:
默认生成model的package 为models,默认生成到main.go所在根目录的models目录中
方法
1、生成全部数据表对应的model
GenerateAllModel(db *gorm.DB, dbName string)
// 调用方式
package main
import gorm_model "github.com/MQEnergy/gorm-model"
func main() {
  // dbName 是数据库名称
  gorm_model.GenerateAllModel(*gorm.DB, dbName)
}
2、生成单个数据表对应的model
GenerateSingleModel(db *gorm.DB, tbName string, table Table)
package main
import gorm_model "github.com/MQEnergy/gorm-model"

func main() {
	// dbName 数据库名称 tbName 数据表名称
    table = gorm_model.GetSingleTable(*gorm.DB, dbName, tbName)
    // tbName 是数据表名称
    err := gorm_model.GenerateSingleModel(*gorm.DB, tbName, table)
    if err != nil {
        panic(err)
    }
}
3、获取单个表信息和字段信息
GetSingleTable(db *gorm.DB, dbName string, tbName string)
4、根据表获取表字段
GetFieldsByTable(db *gorm.DB, tbName string)
5、获取数据库中所有表信息和字段信息
GetAllTables(db *gorm.DB, dbName string)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAllModel

func GenerateAllModel(db *gorm.DB, dbName string)

GenerateAllModel 生成所有model的结构体

func GenerateSingleModel

func GenerateSingleModel(db *gorm.DB, tbName string, table Table) error

GenerateSingleModel 生成单个model的结构体

func ParseFieldTypeByTable

func ParseFieldTypeByTable(fieldType string) string

ParseFieldTypeByTable 转义数据库字段类型到model

func ParseFieldsByTable

func ParseFieldsByTable(tbName string, tbComment string, fields []Field) string

ParseFieldsByTable 转换数据表字段的类型

Types

type Field

type Field struct {
	Field      string `gorm:"column:Field"`      // 字段
	Type       string `gorm:"column:Type"`       // 类型
	Collation  string `gorm:"column:Collation"`  // 编码
	Null       string `gorm:"column:Null"`       // 是否为空
	Key        string `gorm:"column:Key"`        // 主键
	Default    string `gorm:"column:Default"`    // 默认值
	Extra      string `gorm:"column:Extra"`      // 额外类型
	Privileges string `gorm:"column:Privileges"` // 权限
	Comment    string `gorm:"column:Comment"`    // 注释
}

Field 表字段

func GetFieldsByTable

func GetFieldsByTable(db *gorm.DB, tbName string) []Field

GetFieldsByTable 根据表获取表字段

type Table

type Table struct {
	TableName    string `gorm:"column:TABLE_NAME"`    // 表名
	TableComment string `gorm:"column:TABLE_COMMENT"` // 表名注释
}

Table 表

func GetAllTables

func GetAllTables(db *gorm.DB, dbName string) []Table

GetAllTables 获取所有表信息和字段信息

func GetSingleTable

func GetSingleTable(db *gorm.DB, dbName string, tbName string) Table

GetSingleTable 获取单个表信息和字段信息

Jump to

Keyboard shortcuts

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