增加创建表时支持注释,创建字段和修改字段时支持注释
This commit is contained in:
parent
6fb5873890
commit
49a62df21e
|
@ -206,7 +206,7 @@ func (db *Base) IsColumnExist(queryer core.Queryer, ctx context.Context, tableNa
|
||||||
// AddColumnSQL returns a SQL to add a column
|
// AddColumnSQL returns a SQL to add a column
|
||||||
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string {
|
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
s, _ := ColumnString(db.dialect, col, true)
|
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
|
// 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)
|
b.WriteString(db.rowFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if table.Comment != "" {
|
||||||
|
b.WriteString(" COMMENT='")
|
||||||
|
b.WriteString(table.Comment)
|
||||||
|
b.WriteString("'")
|
||||||
|
}
|
||||||
|
|
||||||
return b.String(), true, nil
|
return b.String(), true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -991,13 +991,37 @@ func (db *postgres) IsTableExist(queryer core.Queryer, ctx context.Context, tabl
|
||||||
db.getSchema(), tableName)
|
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, ".") {
|
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
||||||
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s",
|
addColumnSQL = fmt.Sprintf("ALTER TABLE %s ADD %s", quoter.Quote(tableName), s)
|
||||||
db.quoter.Quote(tableName), db.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 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(tableName), s)
|
||||||
|
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string {
|
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
|
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 {
|
func (db *postgres) Filters() []Filter {
|
||||||
return []Filter{&SeqFilter{Prefix: "$", Start: 1}}
|
return []Filter{&SeqFilter{Prefix: "$", Start: 1}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,29 +352,9 @@ func (session *Session) Sync(beans ...interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// add column comment for postgres
|
|
||||||
if col.Comment != "" && engine.Dialect().URI().DBType == schemas.POSTGRES {
|
|
||||||
// @see: integrations/engine_test.go#TestGetColumns
|
|
||||||
addColumnCommentSql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", session.engine.Quote(tbNameWithSchema), session.engine.Quote(col.Name), col.Comment)
|
|
||||||
_, err = session.Exec(addColumnCommentSql)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// add column comment for postgres
|
|
||||||
if col.Comment != "" && engine.Dialect().URI().DBType == schemas.POSTGRES {
|
|
||||||
// @see: integrations/engine_test.go#TestGetColumns
|
|
||||||
addColumnCommentSql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", session.engine.Quote(tbNameWithSchema), session.engine.Quote(col.Name), col.Comment)
|
|
||||||
_, err = session.Exec(addColumnCommentSql)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
expectedType := engine.dialect.SQLType(col)
|
expectedType := engine.dialect.SQLType(col)
|
||||||
curType := engine.dialect.SQLType(oriCol)
|
curType := engine.dialect.SQLType(oriCol)
|
||||||
|
@ -415,6 +395,8 @@ func (session *Session) Sync(beans ...interface{}) error {
|
||||||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
_, 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 {
|
if col.Default != oriCol.Default {
|
||||||
|
|
|
@ -90,6 +90,14 @@ func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if bean has Comment Method, then set table.Comment
|
||||||
|
if _, ok := t.MethodByName("Comment"); ok {
|
||||||
|
tableCommentFn := v.MethodByName("Comment")
|
||||||
|
if tableCommentFn.Type().String() == "func() string" {
|
||||||
|
table.Comment = fmt.Sprintf("%s", tableCommentFn.Call(nil)[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parser.tableCache.Store(t, table)
|
parser.tableCache.Store(t, table)
|
||||||
|
|
||||||
if parser.cacherMgr.GetDefaultCacher() != nil {
|
if parser.cacherMgr.GetDefaultCacher() != nil {
|
||||||
|
|
Loading…
Reference in New Issue