From 8b01334faf587edc47cb0015b14cde2a4b5b6402 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Sat, 20 May 2017 13:06:25 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix=20#440=20only=20get=20these=20tables=20?= =?UTF-8?q?information=20in=20custom=20the=20option=20=E2=80=98-tables?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/g_appcode.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index f575568..421b407 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -298,7 +298,14 @@ func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, ap defer db.Close() if trans, ok := dbDriver[dbms]; ok { beeLogger.Log.Info("Analyzing database tables...") - tableNames := trans.GetTableNames(db) + var tableNames []string + if len(selectedTableNames) != 0 { + for tableName := range selectedTableNames { + tableNames = append(tableNames, tableName) + } + } else { + tableNames = trans.GetTableNames(db) + } tables := getTableObjects(tableNames, db, trans) mvcPath := new(MvcPath) mvcPath.ModelPath = path.Join(apppath, "models") From f657d22fc7ab3d729d149a8bb63dc62242707c05 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Fri, 26 May 2017 13:30:28 +0800 Subject: [PATCH 2/4] fix tag description,add tag description by getting database table information column comment --- generate/g_appcode.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index 421b407..c1cf068 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -182,6 +182,7 @@ type OrmTag struct { RelFk bool ReverseMany bool RelM2M bool + Comment string //字段注解 } // String returns the source code string for the Table struct @@ -255,7 +256,7 @@ func (tag *OrmTag) String() string { if len(ormOptions) == 0 { return "" } - return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";")) + return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) } func GenerateAppcode(driver, connStr, level, tables, currpath string) { @@ -410,7 +411,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin // retrieve columns colDefRows, err := db.Query( `SELECT - column_name, data_type, column_type, is_nullable, column_default, extra + column_name, data_type, column_type, is_nullable, column_default, extra, column_comment FROM information_schema.columns WHERE @@ -423,12 +424,12 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin for colDefRows.Next() { // datatype as bytes so that SQL values can be retrieved - var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes []byte - if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes); err != nil { + var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes, columnCommentBytes []byte + if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes, &columnCommentBytes); err != nil { beeLogger.Log.Fatal("Could not query INFORMATION_SCHEMA for column information") } - colName, dataType, columnType, isNullable, columnDefault, extra := - string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes) + colName, dataType, columnType, isNullable, columnDefault, extra, columnComment := + string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes), string(columnCommentBytes) // create a column col := new(Column) @@ -441,6 +442,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin // Tag info tag := new(OrmTag) tag.Column = colName + tag.Comment = columnComment if table.Pk == colName { col.Name = "Id" col.Type = "int" From f5471680e4b19b0bddf5734c8861a4d75fe31970 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Tue, 30 May 2017 16:21:45 +0800 Subject: [PATCH 3/4] fix tag description,add tag description by getting database table information column comment --- generate/g_appcode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index c1cf068..a514263 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -182,7 +182,7 @@ type OrmTag struct { RelFk bool ReverseMany bool RelM2M bool - Comment string //字段注解 + Comment string //column comment } // String returns the source code string for the Table struct From 812b8c4e5b824d25b722fddb183b2f9d923685d9 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Tue, 30 May 2017 19:43:40 +0800 Subject: [PATCH 4/4] if tag.Comment is empty, the ORM tag string will not contain description --- generate/g_appcode.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index a514263..42c5445 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -256,7 +256,10 @@ func (tag *OrmTag) String() string { if len(ormOptions) == 0 { return "" } - return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) + if tag.Comment != "" { + return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) + } + return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";")) } func GenerateAppcode(driver, connStr, level, tables, currpath string) {