xorm/names/table_name.go
fanybook aea91cc7de
All checks were successful
continuous-integration/drone/push Build is passing
add table & column comment for postgres(add table comment for mysql) (#2067)
让 postgres 支持字段注释,只在 v1.2.5 上测试过(不知道怎么 import master)

发现 master 分支好像大改了?模式表名带 schema 了

使用方式和 mysql 相同
```go
	type User struct {
		Id int64	`xorm:"pk autoincr"`
		Name string `json:"name" xorm:"not null default '' varchar(50) index(name_age) comment('用户 (it''s) 1; 名')"`
		Salt string
		Age int		`json:"age" xorm:"not null default 0 int(10) index(name_age) comment('年龄')"`
		Passwd string `xorm:"varchar(200)"`
		CreatedAt time.Time `xorm:"created"`
		UpdatedAt time.Time `xorm:"updated"`
	}

	_ = engine.Sync(new(User))

    func (model User) TableComment() string {
    	return "表注释"
    }
```

Co-authored-by: fanybook <fanybook@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: #2067
Co-authored-by: fanybook <fanybook@noreply.gitea.io>
Co-committed-by: fanybook <fanybook@noreply.gitea.io>
2021-11-12 20:58:05 +08:00

101 lines
2.2 KiB
Go

// Copyright 2020 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package names
import (
"reflect"
"sync"
)
// TableName table name interface to define customerize table name
type TableName interface {
TableName() string
}
type TableComment interface {
TableComment() string
}
var (
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
tpTableComment = reflect.TypeOf((*TableComment)(nil)).Elem()
tvCache sync.Map
tcCache sync.Map
)
// GetTableName returns table name
func GetTableName(mapper Mapper, v reflect.Value) string {
if v.Type().Implements(tpTableName) {
return v.Interface().(TableName).TableName()
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
if v.Type().Implements(tpTableName) {
return v.Interface().(TableName).TableName()
}
} else if v.CanAddr() {
v1 := v.Addr()
if v1.Type().Implements(tpTableName) {
return v1.Interface().(TableName).TableName()
}
} else {
name, ok := tvCache.Load(v.Type())
if ok {
if name.(string) != "" {
return name.(string)
}
} else {
v2 := reflect.New(v.Type())
if v2.Type().Implements(tpTableName) {
tableName := v2.Interface().(TableName).TableName()
tvCache.Store(v.Type(), tableName)
return tableName
}
tvCache.Store(v.Type(), "")
}
}
return mapper.Obj2Table(v.Type().Name())
}
// GetTableComment returns table comment
func GetTableComment(v reflect.Value) string {
if v.Type().Implements(tpTableComment) {
return v.Interface().(TableComment).TableComment()
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
if v.Type().Implements(tpTableComment) {
return v.Interface().(TableComment).TableComment()
}
} else if v.CanAddr() {
v1 := v.Addr()
if v1.Type().Implements(tpTableComment) {
return v1.Interface().(TableComment).TableComment()
}
} else {
comment, ok := tcCache.Load(v.Type())
if ok {
if comment.(string) != "" {
return comment.(string)
}
} else {
v2 := reflect.New(v.Type())
if v2.Type().Implements(tpTableComment) {
tableComment := v2.Interface().(TableComment).TableComment()
tcCache.Store(v.Type(), tableComment)
return tableComment
}
tcCache.Store(v.Type(), "")
}
}
return ""
}