gormx

package module
v0.0.0-...-3a6575c Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 10 Imported by: 0

README

gormx

Example:

import (
	"fmt"
	"testing"

	"github.com/daqiancode/gormx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct {
	Id   int64 `gorm:"primaryKey"`
	Name string
}
type Product struct {
	Id   int64 `gorm:"primaryKey"`
	Name string
}
type UserProduct struct {
	Id        int64 `gorm:"primaryKey"`
	Uid       int64 `gorm:"index;fk:User,ondelete=SET NULL,onupdate=CASCADE;"`
	ProductId int64 `gorm:"index;fk:Product.Id"`
}

// go clean -testcache
func TestMakeFK(t *testing.T) {
	conUrl := "root:123456@tcp(localhost:3306)/gormx_test?charset=utf8&parseTime=True&loc=Local"
	err := gormx.CreateDB("mysql", conUrl)
	if err != nil {
		fmt.Println(err)
	}
	// defer gormx.DropDB("mysql", conUrl)
	db, err := gorm.Open(mysql.New(mysql.Config{
		DSN: conUrl,
	}), &gorm.Config{})
	if err != nil {
		t.Fatal(err)
	}
	tables := []interface{}{&User{}, &Product{}, &UserProduct{}}
	ddl := gormx.NewDDL(db)
	err = db.AutoMigrate(tables...)
	if err != nil {
		t.Fatal(err)
	}
	ddl.AddTables(tables...)
	ddl.MakeFKs()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDB

func CreateDB(driverName, connectionUrl string) error

func CreateDBWithConUrl

func CreateDBWithConUrl(driverName, connectionUrl, dbName string) error

func DropDB

func DropDB(driverName, connectionUrl string) error

func DropDBWithConUrl

func DropDBWithConUrl(driverName, connectionUrl, dbName string) error

Types

type DDL

type DDL struct {
	DefaultOnDelete FKAction
	DefaultOnUpdate FKAction
	// contains filtered or unexported fields
}

func NewDDL

func NewDDL(db *gorm.DB) *DDL

func (*DDL) AddFKs

func (s *DDL) AddFKs(table interface{})

func (*DDL) AddTables

func (s *DDL) AddTables(tables ...interface{})

func (*DDL) ForeignKeyCheck

func (s *DDL) ForeignKeyCheck(enable bool) error

func (*DDL) GetSchema

func (s *DDL) GetSchema(obj interface{}) *schema.Schema

func (*DDL) GetSchemaByStructName

func (s *DDL) GetSchemaByStructName(structName string) *schema.Schema

func (*DDL) GetTableName

func (s *DDL) GetTableName(tableStruct interface{}) string

func (*DDL) GetTablePK

func (s *DDL) GetTablePK(tableStruct interface{}) string

func (*DDL) MakeFKs

func (s *DDL) MakeFKs()

func (*DDL) MatchTableName

func (s *DDL) MatchTableName(structType reflect.Type, tableName string) bool

func (*DDL) ParseFKInfo

func (s *DDL) ParseFKInfo(tag string) FKInfo

tag: eg1. fk:User.Id,ondelete=CASCADE,onupdate=CASCADE -> ALTER TABLE User ADD CONSTRAINT fkname FOREIGN KEY (id) REFERENCES on delete CASCADE,on update CASCADE eg2. fk:User,ondelete=SET NULL,onupdate=CASCADE -> ALTER TABLE User ADD CONSTRAINT fkname FOREIGN KEY (id) REFERENCES on delete CASCADE,on update CASCADE

func (*DDL) Range

func (s *DDL) Range(f func(structType reflect.Type, tableSchema *schema.Schema) bool)

type FKAction

type FKAction string
const (
	FKEmpty    FKAction = ""
	FKCascade  FKAction = "CASCADE"
	FKNoAction FKAction = "NO ACTION"
	FKSetNull  FKAction = "SET NULL"
	FKRestrict FKAction = "RESTRICT"
)

type FKInfo

type FKInfo struct {
	Table    string
	Field    string
	OnDelete FKAction
	OnUpdate FKAction
}

func (*FKInfo) DropFKSql

func (s *FKInfo) DropFKSql(srcTable, srcField string) string

func (*FKInfo) FKName

func (s *FKInfo) FKName(srcTable, srcField string) string

func (*FKInfo) FKSql

func (s *FKInfo) FKSql(srcTable, srcField string) string

Jump to

Keyboard shortcuts

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