add table & column comment for postgres(add table comment for mysql) #2067

Merged
lunny merged 12 commits from fanybook/xorm:master into master 2021-11-12 12:58:06 +00:00
Contributor

让 postgres 支持字段注释,只在 v1.2.5 上测试过(不知道怎么 import master)

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

使用方式和 mysql 相同

	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 "表注释"
    }
让 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 "表注释" } ```
fanybook added 1 commit 2021-11-04 06:38:29 +00:00
All checks were successful
continuous-integration/drone/pr Build is passing
6fb5873890
add column comment for postgres
Owner

You should change dialects/postgres.go but not there.

You should change `dialects/postgres.go` but not there.
lunny requested changes 2021-11-05 03:58:34 +00:00
lunny left a comment
Owner

per my comment above.

per my comment above.
fanybook added 1 commit 2021-11-05 07:22:30 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
49a62df21e
增加创建表时支持注释,创建字段和修改字段时支持注释
fanybook requested review from lunny 2021-11-05 07:23:27 +00:00
Author
Contributor

@lunny 已经移到 dialects 了,感觉 master 代码和 v1.2.5 相差很多,接口都变了

现在支持了字段注释和表注释

我发现 dialects/mysql.go 貌似没有用到 schema?mysql 里的库也相当于 pgsql 里的 schema

@lunny 已经移到 dialects 了,感觉 master 代码和 v1.2.5 相差很多,接口都变了 现在支持了字段注释和表注释 我发现 dialects/mysql.go 貌似没有用到 schema?mysql 里的库也相当于 pgsql 里的 schema
fanybook added 1 commit 2021-11-05 07:35:52 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
c9a1093af6
fixed hand error
fanybook changed title from add column comment for postgres to add table & column comment for postgres 2021-11-05 07:37:17 +00:00
fanybook changed title from add table & column comment for postgres to add table & column comment for postgres(add table comment for mysql) 2021-11-05 07:37:49 +00:00
fanybook added 1 commit 2021-11-05 07:43:12 +00:00
All checks were successful
continuous-integration/drone/pr Build is passing
b1d6ec1db5
fixed hand error2
lunny reviewed 2021-11-07 03:51:42 +00:00
tags/parser.go Outdated
@ -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
Owner

This should be moved into parser.Parse

This should be moved into `parser.Parse`
lunny added the
kind
feature
label 2021-11-07 03:52:12 +00:00
Owner

Thank you very much! Could you add some tests here? I think we have some with comment for MySQL.

Thank you very much! Could you add some tests here? I think we have some with comment for MySQL.
fanybook added 1 commit 2021-11-08 03:40:26 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
72efd4ed00
Modify Comment to TableComment & moved into parser.Parse
lunny reviewed 2021-11-08 03:52:18 +00:00
@ -58,0 +67,4 @@
return v.Interface().(TableComment).TableComment()
}
return ""
Owner

You should also consider it's a pointer method.

if v.Kind() == reflect.Ptr {
	v = v.Elem()
    
You should also consider it's a pointer method. ```go if v.Kind() == reflect.Ptr { v = v.Elem() ```
lunny marked this conversation as resolved
lunny reviewed 2021-11-08 03:52:28 +00:00
@ -351,6 +351,7 @@ func (session *Session) Sync(beans ...interface{}) error {
if err = session.addColumn(col.Name); err != nil {
return err
}
Owner

unnecessary change.

unnecessary change.
lunny marked this conversation as resolved
lunny reviewed 2021-11-08 03:53:09 +00:00
@ -26,6 +26,10 @@ func (p ParseTableName2) TableName() string {
return "p_parseTableName"
}
func (p ParseTableName2) TableComment() string {
Owner

Please also add a pointer method

Please also add a pointer method
lunny marked this conversation as resolved
Owner

How about also add simliar test cases for postgres?

How about also add simliar test cases for postgres?
fanybook added 1 commit 2021-11-08 03:58:33 +00:00
All checks were successful
continuous-integration/drone/pr Build is passing
9c081d1648
fmt
fanybook added 1 commit 2021-11-08 05:58:18 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
92b12f29a7
add some test cases
fanybook added 1 commit 2021-11-08 06:02:39 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
b2e1f1c241
remove unnecessary change
Author
Contributor

How about also add simliar test cases for postgres?

parser.Parse only set table.Commet, it will be used in dialects

i don't know how to import master branch in my project

> How about also add simliar test cases for postgres? parser.Parse only set table.Commet, it will be used in dialects i don't know how to import master branch in my project
fanybook added 1 commit 2021-11-08 06:14:13 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
e6817a5423
remove unnecessary chinese change
fanybook added 1 commit 2021-11-08 06:50:17 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing
6a1a245c48
remove unnecessary empty line
fanybook added 1 commit 2021-11-08 09:40:30 +00:00
All checks were successful
continuous-integration/drone/pr Build is passing
b46dc622ff
fixed test case
Author
Contributor

@lunny 看一下,能合并了么

@lunny 看一下,能合并了么
lunny reviewed 2021-11-09 09:58:10 +00:00
@ -1001,0 +1021,4 @@
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
Owner

need a space between two SQLs

need a space between two SQLs
lunny marked this conversation as resolved
Author
Contributor

@lunny has space and semicolon

commentSQL := "; "
commentSQL += "xxxx"
@lunny has space and semicolon ```go commentSQL := "; " commentSQL += "xxxx" ```
Owner

@lunny has space and semicolon

commentSQL := "; "
commentSQL += "xxxx"

I mean in function ModifyColumnSQL

> @lunny has space and semicolon > > ```go > commentSQL := "; " > commentSQL += "xxxx" > ``` I mean in function `ModifyColumnSQL`
Author
Contributor

@lunny has space and semicolon

commentSQL := "; "
commentSQL += "xxxx"

I mean in function ModifyColumnSQL

@lunny 我不太明白,应该在哪加空格,请指点一下

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 has space and semicolon > > > > ```go > > commentSQL := "; " > > commentSQL += "xxxx" > > ``` > > I mean in function `ModifyColumnSQL` @lunny 我不太明白,应该在哪加空格,请指点一下 ```go 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 } ```
Author
Contributor

@lunny 受累看一下能合并了么,之前您说在 ModifyColumnSQL 里,两段 sql 之前应该有一个空格

但是在 commentSQL 初始化的时候,这个空格就已经有了,还有一个分号

like this

commentSQL := "; "
commentSQL += "xxxx"

return modifyColumnSQL + commentSQL
@lunny 受累看一下能合并了么,之前您说在 ModifyColumnSQL 里,两段 sql 之前应该有一个空格 但是在 commentSQL 初始化的时候,这个空格就已经有了,还有一个分号 like this ```go commentSQL := "; " commentSQL += "xxxx" return modifyColumnSQL + commentSQL ```
lunny added 1 commit 2021-11-12 03:35:10 +00:00
All checks were successful
continuous-integration/drone/pr Build is passing
64fbfd0e4b
Merge branch 'master' into master
lunny merged commit aea91cc7de into master 2021-11-12 12:58:06 +00:00
Sign in to join this conversation.
No description provided.