From 49a62df21e4e810cdcebbdfd858c9db8dc707b83 Mon Sep 17 00:00:00 2001 From: fanybook Date: Fri, 5 Nov 2021 15:22:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E8=A1=A8?= =?UTF-8?q?=E6=97=B6=E6=94=AF=E6=8C=81=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=AD=97=E6=AE=B5=E5=92=8C=E4=BF=AE=E6=94=B9=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=97=B6=E6=94=AF=E6=8C=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialects/dialect.go | 2 +- dialects/mysql.go | 6 +++++ dialects/postgres.go | 54 ++++++++++++++++++++++++++++++++++++++++---- session_schema.go | 22 ++---------------- tags/parser.go | 8 +++++++ 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/dialects/dialect.go b/dialects/dialect.go index f3aa7470..2d772411 100644 --- a/dialects/dialect.go +++ b/dialects/dialect.go @@ -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 diff --git a/dialects/mysql.go b/dialects/mysql.go index ce3bd705..241aede8 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -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 } diff --git a/dialects/postgres.go b/dialects/postgres.go index 1e99cd9d..25d9946b 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -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(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 { @@ -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}} } diff --git a/session_schema.go b/session_schema.go index 110285c2..b38bafbf 100644 --- a/session_schema.go +++ b/session_schema.go @@ -352,29 +352,9 @@ func (session *Session) Sync(beans ...interface{}) error { 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 } - // 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 expectedType := engine.dialect.SQLType(col) curType := engine.dialect.SQLType(oriCol) @@ -415,6 +395,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 { diff --git a/tags/parser.go b/tags/parser.go index efee11e7..5f816cf3 100644 --- a/tags/parser.go +++ b/tags/parser.go @@ -90,6 +90,14 @@ func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) { 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) if parser.cacherMgr.GetDefaultCacher() != nil {