add table & column comment for postgres(add table comment for mysql) #2067
|
@ -206,7 +206,7 @@ func (db *Base) IsColumnExist(queryer core.Queryer, ctx context.Context, tableNa
|
|||
// AddColumnSQL returns a SQL to add a column
|
||||
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||
s, _ := ColumnString(db.dialect, col, true)
|
||||
return fmt.Sprintf("ALTER TABLE %v ADD %v", db.dialect.Quoter().Quote(tableName), s)
|
||||
return fmt.Sprintf("ALTER TABLE %s ADD %s", db.dialect.Quoter().Quote(tableName), s)
|
||||
}
|
||||
|
||||
// CreateIndexSQL returns a SQL to create index
|
||||
|
|
|
@ -685,6 +685,12 @@ func (db *mysql) CreateTableSQL(ctx context.Context, queryer core.Queryer, table
|
|||
b.WriteString(db.rowFormat)
|
||||
}
|
||||
|
||||
if table.Comment != "" {
|
||||
b.WriteString(" COMMENT='")
|
||||
b.WriteString(table.Comment)
|
||||
b.WriteString("'")
|
||||
}
|
||||
|
||||
return b.String(), true, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -991,13 +991,37 @@ func (db *postgres) IsTableExist(queryer core.Queryer, ctx context.Context, tabl
|
|||
db.getSchema(), tableName)
|
||||
}
|
||||
|
||||
func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||
func (db *postgres) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||
s, _ := ColumnString(db.dialect, col, true)
|
||||
|
||||
quoter := db.dialect.Quoter()
|
||||
addColumnSQL := ""
|
||||
commentSQL := "; "
|
||||
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
||||
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s",
|
||||
db.quoter.Quote(tableName), db.quoter.Quote(col.Name), db.SQLType(col))
|
||||
addColumnSQL = fmt.Sprintf("ALTER TABLE %s ADD %s", quoter.Quote(tableName), s)
|
||||
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||
return addColumnSQL + commentSQL
|
||||
}
|
||||
return fmt.Sprintf("alter table %s.%s ALTER COLUMN %s TYPE %s",
|
||||
db.quoter.Quote(db.getSchema()), db.quoter.Quote(tableName), db.quoter.Quote(col.Name), db.SQLType(col))
|
||||
|
||||
addColumnSQL = fmt.Sprintf("ALTER TABLE %s.%s ADD %s", quoter.Quote(db.getSchema()), quoter.Quote(tableName), s)
|
||||
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s IS '%s'", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||
return addColumnSQL + commentSQL
|
||||
}
|
||||
|
||||
func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||
quoter := db.dialect.Quoter()
|
||||
modifyColumnSQL := ""
|
||||
commentSQL := "; "
|
||||
|
||||
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
||||
modifyColumnSQL = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s", quoter.Quote(tableName), quoter.Quote(col.Name), db.SQLType(col))
|
||||
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||
return modifyColumnSQL + commentSQL
|
||||
}
|
||||
|
||||
modifyColumnSQL = fmt.Sprintf("ALTER TABLE %s.%s ALTER COLUMN %s TYPE %s", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), db.SQLType(col))
|
||||
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s IS '%s'", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||
return modifyColumnSQL + commentSQL
|
||||
lunny marked this conversation as resolved
|
||||
}
|
||||
|
||||
func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string {
|
||||
|
@ -1302,6 +1326,26 @@ func (db *postgres) GetIndexes(queryer core.Queryer, ctx context.Context, tableN
|
|||
return indexes, nil
|
||||
}
|
||||
|
||||
func (db *postgres) CreateTableSQL(ctx context.Context, queryer core.Queryer, table *schemas.Table, tableName string) (string, bool, error) {
|
||||
quoter := db.dialect.Quoter()
|
||||
if len(db.getSchema()) != 0 && !strings.Contains(tableName, ".") {
|
||||
tableName = fmt.Sprintf("%s.%s", db.getSchema(), tableName)
|
||||
}
|
||||
|
||||
createTableSQL, ok, err := db.Base.CreateTableSQL(ctx, queryer, table, tableName)
|
||||
if err != nil {
|
||||
return "", ok, err
|
||||
}
|
||||
|
||||
commentSql := "; "
|
||||
if table.Comment != "" {
|
||||
// support schema.table -> "schema"."table"
|
||||
commentSql += fmt.Sprintf("COMMENT ON TABLE %s IS '%s'", quoter.Quote(tableName), table.Comment)
|
||||
}
|
||||
|
||||
return createTableSQL + commentSql, true, nil
|
||||
}
|
||||
|
||||
func (db *postgres) Filters() []Filter {
|
||||
return []Filter{&SeqFilter{Prefix: "$", Start: 1}}
|
||||
}
|
||||
|
|
|
@ -14,9 +14,15 @@ type TableName interface {
|
|||
TableName() string
|
||||
}
|
||||
|
||||
type TableComment interface {
|
||||
TableComment() string
|
||||
}
|
||||
|
||||
var (
|
||||
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
|
||||
tvCache sync.Map
|
||||
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
|
||||
tpTableComment = reflect.TypeOf((*TableComment)(nil)).Elem()
|
||||
tvCache sync.Map
|
||||
tcCache sync.Map
|
||||
)
|
||||
|
||||
// GetTableName returns table name
|
||||
|
@ -55,3 +61,40 @@ func GetTableName(mapper Mapper, v reflect.Value) string {
|
|||
|
||||
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()
|
||||
}
|
||||
|
||||
lunny marked this conversation as resolved
lunny
commented
You should also consider it's a pointer method.
You should also consider it's a pointer method.
```go
if v.Kind() == reflect.Ptr {
v = v.Elem()
```
|
||||
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 ""
|
||||
}
|
||||
|
|
|
@ -394,6 +394,8 @@ func (session *Session) Sync(beans ...interface{}) error {
|
|||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||
}
|
||||
}
|
||||
} else if col.Comment != oriCol.Comment {
|
||||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||
}
|
||||
|
||||
if col.Default != oriCol.Default {
|
||||
|
|
|
@ -316,6 +316,7 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
|
|||
table := schemas.NewEmptyTable()
|
||||
table.Type = t
|
||||
table.Name = names.GetTableName(parser.tableMapper, v)
|
||||
table.Comment = names.GetTableComment(v)
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
col, err := parser.parseField(table, i, t.Field(i), v.Field(i))
|
||||
|
|
|
@ -26,6 +26,20 @@ func (p ParseTableName2) TableName() string {
|
|||
return "p_parseTableName"
|
||||
}
|
||||
|
||||
type ParseTableComment struct{}
|
||||
lunny marked this conversation as resolved
Outdated
lunny
commented
Please also add a pointer method Please also add a pointer method
|
||||
|
||||
type ParseTableComment1 struct{}
|
||||
|
||||
type ParseTableComment2 struct{}
|
||||
|
||||
func (p ParseTableComment1) TableComment() string {
|
||||
return "p_parseTableComment1"
|
||||
}
|
||||
|
||||
func (p *ParseTableComment2) TableComment() string {
|
||||
return "p_parseTableComment2"
|
||||
}
|
||||
|
||||
func TestParseTableName(t *testing.T) {
|
||||
parser := NewParser(
|
||||
"xorm",
|
||||
|
@ -47,6 +61,36 @@ func TestParseTableName(t *testing.T) {
|
|||
assert.EqualValues(t, "p_parseTableName", table.Name)
|
||||
}
|
||||
|
||||
func TestParseTableComment(t *testing.T) {
|
||||
parser := NewParser(
|
||||
"xorm",
|
||||
dialects.QueryDialect("mysql"),
|
||||
names.SnakeMapper{},
|
||||
names.SnakeMapper{},
|
||||
caches.NewManager(),
|
||||
)
|
||||
|
||||
table, err := parser.Parse(reflect.ValueOf(new(ParseTableComment)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "", table.Comment)
|
||||
|
||||
table, err = parser.Parse(reflect.ValueOf(new(ParseTableComment1)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "p_parseTableComment1", table.Comment)
|
||||
|
||||
table, err = parser.Parse(reflect.ValueOf(ParseTableComment1{}))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "p_parseTableComment1", table.Comment)
|
||||
|
||||
table, err = parser.Parse(reflect.ValueOf(new(ParseTableComment2)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "p_parseTableComment2", table.Comment)
|
||||
|
||||
table, err = parser.Parse(reflect.ValueOf(ParseTableComment2{}))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "p_parseTableComment2", table.Comment)
|
||||
}
|
||||
|
||||
func TestUnexportField(t *testing.T) {
|
||||
parser := NewParser(
|
||||
"xorm",
|
||||
|
|
Loading…
Reference in New Issue
Block a user
need a space between two SQLs