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
import (
"reflect"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"reflect"
)
// Migrator returns migrator
@ -60,6 +59,16 @@ type Index interface {
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
type Migrator interface {
// AutoMigrate
@ -76,7 +85,7 @@ type Migrator interface {
HasTable(dst interface{}) bool
RenameTable(oldName, newName interface{}) error
GetTables() (tableList []string, err error)
GetTableComment(tableName string) (comment string, err error)
TableType(dst interface{}) (TableType, error)
// Columns
AddColumn(dst interface{}, field string) error

View File

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