From c3719e0452c8148176b3ce909fa0a248bb3b58a7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 14 Jun 2017 14:06:38 +0800 Subject: [PATCH] add comment support on create table --- .sql | 2 ++ tag.go | 9 +++++++++ tag_test.go | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .sql diff --git a/.sql b/.sql new file mode 100644 index 00000000..8b0c3683 --- /dev/null +++ b/.sql @@ -0,0 +1,2 @@ +/*Generated by xorm v0.6.2.0605 2017-06-14 14:05:59, from sqlite3 to SQLITE3*/ + diff --git a/tag.go b/tag.go index 4b0e3f54..e1c821fb 100644 --- a/tag.go +++ b/tag.go @@ -54,6 +54,7 @@ var ( "UNIQUE": UniqueTagHandler, "CACHE": CacheTagHandler, "NOCACHE": NoCacheTagHandler, + "COMMENT": CommentTagHandler, } ) @@ -192,6 +193,14 @@ func UniqueTagHandler(ctx *tagContext) error { return nil } +// CommentTagHandler add comment to column +func CommentTagHandler(ctx *tagContext) error { + if len(ctx.params) > 0 { + ctx.col.Comment = strings.Trim(ctx.params[0], "' ") + } + return nil +} + // SQLTypeTagHandler describes SQL Type tag handler func SQLTypeTagHandler(ctx *tagContext) error { ctx.col.SQLType = core.SQLType{Name: ctx.tagName} diff --git a/tag_test.go b/tag_test.go index fd32f13c..4fedd0e7 100644 --- a/tag_test.go +++ b/tag_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/go-xorm/core" "github.com/stretchr/testify/assert" ) @@ -234,4 +235,38 @@ func TestAutoIncrTag(t *testing.T) { assert.False(t, cols[0].IsAutoIncrement) assert.True(t, cols[0].IsPrimaryKey) assert.Equal(t, "id", cols[0].Name) -} \ No newline at end of file +} + +func TestTagComment(t *testing.T) { + assert.NoError(t, prepareEngine()) + // FIXME: only support mysql + if testEngine.dialect.DriverName() != core.MYSQL { + return + } + + type TestComment1 struct { + Id int64 `xorm:"comment(主键)"` + } + + assert.NoError(t, testEngine.Sync2(new(TestComment1))) + + tables, err := testEngine.DBMetas() + assert.NoError(t, err) + assert.EqualValues(t, 1, len(tables)) + assert.EqualValues(t, 1, len(tables[0].Columns())) + assert.EqualValues(t, "主键", tables[0].Columns()[0].Comment) + + assert.NoError(t, testEngine.DropTables(new(TestComment1))) + + type TestComment2 struct { + Id int64 `xorm:"comment('主键')"` + } + + assert.NoError(t, testEngine.Sync2(new(TestComment2))) + + tables, err = testEngine.DBMetas() + assert.NoError(t, err) + assert.EqualValues(t, 1, len(tables)) + assert.EqualValues(t, 1, len(tables[0].Columns())) + assert.EqualValues(t, "主键", tables[0].Columns()[0].Comment) +}