gorm-gen

snowid_method.go

package gorm_gen

import (
	"snowf"
	"gorm.io/gorm"
)

// SnowIDMethod
// Deprecated: recommend to use SnowIDMethod2
type SnowIDMethod struct {
	ID int64 `gorm:"column:id;type:bigint(20);primaryKey;comment:唯一 ID (雪花)" json:"id"` // 唯一 ID (雪花)
}

// TableName
// Deprecated: recommend to use SnowIDMethod2.TableName
func (t SnowIDMethod) TableName() string {
	return "@@table"
}

// BeforeCreate 在创建记录前生成雪花 ID
// Deprecated: recommend to use SnowIDMethod2.BeforeCreate
func (t *SnowIDMethod) BeforeCreate(db *gorm.DB) error {
	if t.ID <= 0 {
		t.ID = snowf.GetIDInt64(t.TableName())
	}
	return nil
}

type SnowIDMethod2 struct {
	ID snowf.ID `gorm:"column:id;type:bigint(20);primaryKey;comment:唯一 ID (雪花)" json:"id"` // 唯一 ID (雪花)
}

// TableName 获取表名
func (t SnowIDMethod2) TableName() string {
	return "@@table"
}

// BeforeCreate 在创建记录前生成雪花 ID
func (t *SnowIDMethod2) BeforeCreate(db *gorm.DB) error {
	if t.ID <= 0 {
		t.ID = snowf.GetID(t.TableName())
	}
	return nil
}

table_methods.go

package gorm_gen

import (
	"fmt"
)

type CommonMethod struct {
}

// TableName 获取表名
func (t CommonMethod) TableName() string {
	return "@@table"
}

func (t CommonMethod) TableColumn(column string) string {
	if "" == column {
		return ""
	}

	return fmt.Sprintf("%s.%s", t.TableName(), column)
}

func (t CommonMethod) TableColumns(columns interface{}) []string {
	tableColumnNames := make([]string, 0)

	switch v := columns.(type) {
	case []string:
		for _, column := range v {
			tableColumnNames = append(tableColumnNames, fmt.Sprintf("%s.%s", t.TableName(), column))
		}
	case string:
		tableColumnNames = []string{fmt.Sprintf("%s.%s", t.TableName(), v)}
	}

	return tableColumnNames
}

posted @ 2026-06-04 11:22  干炸小黄鱼  阅读(15)  评论(0)    收藏  举报