feat: migrator support tableType.It like ColumnTypes

This commit is contained in:
John Mai 2023-04-11 22:45:15 +08:00
parent 97e2cc11da
commit ab17848c53
3 changed files with 60 additions and 6 deletions

View File

@ -1,10 +1,9 @@
package gorm package gorm
import ( import (
"reflect"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
"reflect"
) )
// Migrator returns migrator // Migrator returns migrator
@ -60,6 +59,16 @@ type Index interface {
Option() string Option() string
} }
// TableType table type interface
type TableType interface {
Catalog() string
Schema() string
Name() string
Type() string
Engine() (engine string, ok bool)
Comment() (comment string, ok bool)
}
// Migrator migrator interface // Migrator migrator interface
type Migrator interface { type Migrator interface {
// AutoMigrate // AutoMigrate
@ -76,7 +85,7 @@ type Migrator interface {
HasTable(dst interface{}) bool HasTable(dst interface{}) bool
RenameTable(oldName, newName interface{}) error RenameTable(oldName, newName interface{}) error
GetTables() (tableList []string, err error) GetTables() (tableList []string, err error)
GetTableComment(tableName string) (comment string, err error) TableType(dst interface{}) (TableType, error)
// Columns // Columns
AddColumn(dst interface{}, field string) error AddColumn(dst interface{}, field string) error

View File

@ -948,7 +948,7 @@ func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return nil return nil
} }
// GetTableComment return table comment // TableType return tableType gorm.TableType and execErr error
func (m Migrator) GetTableComment(tableName string) (string, error) { func (m Migrator) TableType(dst interface{}) (gorm.TableType, error) {
return "", errors.New("not support") return nil, errors.New("not support")
} }

45
migrator/table_type.go Normal file
View File

@ -0,0 +1,45 @@
package migrator
import (
"database/sql"
)
// TableType table type implements TableType interface
type TableType struct {
CatalogValue string
SchemaValue string
NameValue string
TypeValue string
EngineValue sql.NullString
CommentValue sql.NullString
}
// Catalog returns the catalog of the table.
func (ct TableType) Catalog() string {
return ct.CatalogValue
}
// Schema returns the schema of the table.
func (ct TableType) Schema() string {
return ct.SchemaValue
}
// Name returns the name of the table.
func (ct TableType) Name() string {
return ct.NameValue
}
// Type returns the type of the table.
func (ct TableType) Type() string {
return ct.TypeValue
}
// Engine returns the engine of current table.
func (ct TableType) Engine() (engine string, ok bool) {
return ct.EngineValue.String, ct.EngineValue.Valid
}
// Comment returns the comment of current table.
func (ct TableType) Comment() (comment string, ok bool) {
return ct.CommentValue.String, ct.CommentValue.Valid
}