Skip to content

Commit

Permalink
re check
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed Nov 27, 2019
1 parent 7f2972f commit 5cd0ac6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 105 deletions.
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
base:
is_dev : false
out_dir : ./model # 输出目录
singular_table : false # 单表模式:true:禁用表名复数,false:采用表明复数 参考:gorm.SingularTable
simple : false # 简单输出(默认gorm标签不输出)
Expand Down
2 changes: 1 addition & 1 deletion data/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func initConfig() {
os.Exit(1)
} else {
fmt.Println("using config info:")
fmt.Println(tools.GetJsonStr(config.GetMysqlDbInfo()))
fmt.Println(tools.GetJSONStr(config.GetMysqlDbInfo()))
}
}

Expand Down
1 change: 1 addition & 0 deletions data/view/cnf/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var TypeMysqlDicMp = map[string]string{
"json": "string",
"text": "string",
"timestamp": "time.Time",
"double": "float64",
}

// TypeMysqlMatchMp Fuzzy Matching Types.模糊匹配类型
Expand Down
22 changes: 12 additions & 10 deletions data/view/model/genmysql/genmysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ func getPackageInfo(orm *mysqldb.MySqlDB, info *model.DBInfo) {

// Get create SQL statements.获取创建sql语句
rows, err := orm.Raw("show create table " + tabName).Rows()
defer rows.Close()
//defer rows.Close()
if err == nil {
if rows.Next() {
var table, CreateTable string
rows.Scan(&table, &CreateTable)
tab.SQLBuildStr = CreateTable
}
}
// rows.Close()
// ----------end

// build element.构造元素
Expand All @@ -88,15 +89,15 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
keyNums := make(map[string]int)
// get keys
var Keys []keys
orm.Raw("show keys from " + tab).Find(&Keys)
orm.Raw("show keys from " + tab).Scan(&Keys)
for _, v := range Keys {
keyNums[v.KeyName]++
}
// ----------end

var list []genColumns
// Get table annotations.获取表注释
orm.Raw("show FULL COLUMNS from " + tab).Find(&list)
orm.Raw("show FULL COLUMNS from " + tab).Scan(&list)
// filter gorm.Model.过滤 gorm.Model
if filterModel(&list) {
el = append(el, model.ColumusInfo{
Expand All @@ -107,8 +108,8 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)

// ForeignKey
var foreignKeyList []genForeignKey
orm.Raw(`select table_schema,table_name,column_name,referenced_table_schema,referenced_table_name,referenced_column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where table_schema = ? AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ? `, config.GetMysqlDbInfo().Database, tab).Find(&foreignKeyList)
orm.Raw(fmt.Sprintf(`select table_schema,table_name,column_name,referenced_table_schema,referenced_table_name,referenced_column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where table_schema = '%v' AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = '%v'`, config.GetMysqlDbInfo().Database, tab)).Scan(&foreignKeyList)
// ------------------end

for _, v := range list {
Expand Down Expand Up @@ -159,12 +160,13 @@ func getTables(orm *mysqldb.MySqlDB) map[string]string {

// Get column names.获取列名
var tables []string

rows, err := orm.Raw("show tables").Rows()
if err != nil {
fmt.Println(err)
return tbDesc
}
defer rows.Close()
// defer rows.Close()

for rows.Next() {
var table string
Expand All @@ -174,16 +176,16 @@ func getTables(orm *mysqldb.MySqlDB) map[string]string {
}

// Get table annotations.获取表注释
rows, err = orm.Raw("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema=?;",
config.GetMysqlDbInfo().Database).Rows()
rows1, err := orm.Raw("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema= '" + config.GetMysqlDbInfo().Database + "'").Rows()
// defer rows1.Close()
if err != nil {
fmt.Println(err)
return tbDesc
}

for rows.Next() {
for rows1.Next() {
var table, desc string
rows.Scan(&table, &desc)
rows1.Scan(&table, &desc)
tbDesc[table] = desc
}

Expand Down
16 changes: 7 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ module github.com/xxjwxc/gormt
go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/jinzhu/gorm v1.9.10
github.com/leodido/go-urn v1.1.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/jinzhu/gorm v1.9.11
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/xxjwxc/public v0.0.0-20190915135914-aefa9155c004
github.com/xxjwxc/public v0.0.0-20191107073037-ea6b812d567f
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.29.1
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d
gopkg.in/go-playground/validator.v9 v9.30.2
gopkg.in/yaml.v2 v2.2.7
)
Loading

0 comments on commit 5cd0ac6

Please sign in to comment.