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

Merged
lunny merged 12 commits from fanybook/xorm:master into master 2 months ago

让 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 3 months ago
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 3 months ago
lunny left a comment

per my comment above.

fanybook added 1 commit 3 months ago
fanybook requested review from lunny 3 months ago
Poster

@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 3 months ago
c9a1093af6 fixed hand error
fanybook changed title from add column comment for postgres to add table & column comment for postgres 3 months ago
fanybook changed title from add table & column comment for postgres to add table & column comment for postgres(add table comment for mysql) 3 months ago
fanybook added 1 commit 3 months ago
b1d6ec1db5 fixed hand error2
lunny reviewed 3 months ago
tags/parser.go Outdated
return nil, err
}
// if bean has Comment Method, then set table.Comment
lunny commented 3 months ago
Owner

This should be moved into parser.Parse

This should be moved into `parser.Parse`
lunny added the
kind/feature
label 3 months ago
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 3 months ago
lunny reviewed 3 months ago
return v.Interface().(TableComment).TableComment()
}
return ""
lunny commented 3 months ago
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 3 months ago
if err = session.addColumn(col.Name); err != nil {
return err
}
lunny commented 3 months ago
Owner

unnecessary change.

unnecessary change.
lunny marked this conversation as resolved
lunny reviewed 3 months ago
return "p_parseTableName"
}
func (p ParseTableName2) TableComment() string {
lunny commented 3 months ago
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 3 months ago
9c081d1648 fmt
fanybook added 1 commit 3 months ago
92b12f29a7 add some test cases
fanybook added 1 commit 3 months ago
b2e1f1c241 remove unnecessary change
Poster

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 3 months ago
e6817a5423 remove unnecessary chinese change
fanybook added 1 commit 3 months ago
6a1a245c48 remove unnecessary empty line
fanybook added 1 commit 2 months ago
b46dc622ff fixed test case
Poster

@lunny 看一下,能合并了么

@lunny 看一下,能合并了么
lunny reviewed 2 months ago
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 commented 2 months ago
Owner

need a space between two SQLs

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

@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`
Poster

@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 } ```
Poster

@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 2 months ago
64fbfd0e4b Merge branch 'master' into master
lunny merged commit aea91cc7de into master 2 months ago

Reviewers

lunny was requested for review 3 months ago
continuous-integration/drone/pr Build is passing
The pull request has been merged as aea91cc7de.
Sign in to join this conversation.
Loading…
There is no content yet.