Skip to content

Commit

Permalink
Merge pull request #131 from viwii/master
Browse files Browse the repository at this point in the history
替朋友合并mssql
  • Loading branch information
xxjwxc authored Apr 11, 2021
2 parents c9fdd14 + 3b9d9a7 commit 8dba9d3
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 2 deletions.
7 changes: 5 additions & 2 deletions data/view/gtools/gtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/xxjwxc/gormt/data/config"

"github.com/xxjwxc/gormt/data/view/model/genmssql"
"github.com/xxjwxc/gormt/data/view/model/genmysql"
"github.com/xxjwxc/gormt/data/view/model/gensqlite"
"github.com/xxjwxc/public/tools"
Expand All @@ -32,10 +33,12 @@ func showCmd() {
// fmt.Println(tt)
var modeldb model.IModel
switch config.GetDbInfo().Type {
case 0:
case 0: // mysql
modeldb = genmysql.GetModel()
case 1:
case 1: // sqllite
modeldb = gensqlite.GetModel()
case 2: //
modeldb = genmssql.GetModel()
}
if modeldb == nil {
mylog.Error(fmt.Errorf("modeldb not fund : please check db_info.type (0:mysql , 1:sqlite , 2:mssql) "))
Expand Down
100 changes: 100 additions & 0 deletions data/view/model/genmssql/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package genmssql

import (
"strings"

"github.com/xxjwxc/gormt/data/config"
"github.com/xxjwxc/public/mylog"

"github.com/xxjwxc/gormt/data/view/model"
)

// filterModel filter.过滤 gorm.Model
func filterModel(list *[]genColumns) bool {
if config.GetDBTag() != "gorm" {
return false
}

var _temp []genColumns
num := 0
for _, v := range *list {
if strings.EqualFold(v.Field, "id") ||
strings.EqualFold(v.Field, "created_at") ||
strings.EqualFold(v.Field, "updated_at") ||
strings.EqualFold(v.Field, "deleted_at") {
num++
} else {
_temp = append(_temp, v)
}
}

if num >= 4 {
*list = _temp
return true
}

return false
}

// fixForeignKey fix foreign key.过滤外键
func fixForeignKey(list []genForeignKey, columuName string, result *[]model.ForeignKey) {
for _, v := range list {
if strings.EqualFold(v.ColumnName, columuName) { // find it .找到了
*result = append(*result, model.ForeignKey{
TableName: v.ReferencedTableName,
ColumnName: v.ReferencedColumnName,
})
}
}
}

// GetModel get model interface. 获取model接口
func GetModel() model.IModel {
//now just support mysql
return &MssqlModel
}

// FixNotes 分析元素表注释
func FixNotes(em *model.ColumnsInfo, note string) {
b0 := FixElementTag(em, note) // gorm
b1 := FixForeignKeyTag(em, em.Notes) // 外键
if !b0 && b1 { // 补偿
FixElementTag(em, em.Notes) // gorm
}
}

// FixElementTag 分析元素表注释
func FixElementTag(em *model.ColumnsInfo, note string) bool {
matches := noteRegex.FindStringSubmatch(note)
if len(matches) < 2 {
em.Notes = note
return false
}

mylog.Infof("get one gorm tag:(%v) ==> (%v)", em.BaseInfo.Name, matches[1])
em.Notes = note[len(matches[0]):]
em.Gormt = matches[1]
return true
}

// FixForeignKeyTag 分析元素表注释(外键)
func FixForeignKeyTag(em *model.ColumnsInfo, note string) bool {
matches := foreignKeyRegex.FindStringSubmatch(note) // foreign key 外键
if len(matches) < 2 {
em.Notes = note
return false
}
em.Notes = note[len(matches[0]):]

// foreign key 外键
tmp := strings.Split(matches[1], ".")
if len(tmp) > 0 {
mylog.Infof("get one foreign key:(%v) ==> (%v)", em.BaseInfo.Name, matches[1])
em.ForeignKeyList = append(em.ForeignKeyList, model.ForeignKey{
TableName: tmp[0],
ColumnName: tmp[1],
})
}

return true
}
46 changes: 46 additions & 0 deletions data/view/model/genmssql/def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package genmssql

import "regexp"

// genColumns show full columns
type genColumns struct {
Field string `gorm:"column:Field"`
Type string `gorm:"column:Type"`
Key string `gorm:"column:Key"`
Desc string `gorm:"column:Comment"`
Null string `gorm:"column:Null"`
Default *string `gorm:"column:Default"`
}

//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 ='matrix' AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = 'credit_card' ;
// genForeignKey Foreign key of db info . 表的外键信息
type genForeignKey struct {
TableSchema string `gorm:"column:table_schema"` // Database of columns.列所在的数据库
TableName string `gorm:"column:table_name"` // Data table of column.列所在的数据表
ColumnName string `gorm:"column:column_name"` // Column names.列名
ReferencedTableSchema string `gorm:"column:referenced_table_schema"` // The database where the index is located.该索引所在的数据库
ReferencedTableName string `gorm:"column:referenced_table_name"` // Affected tables . 该索引受影响的表
ReferencedColumnName string `gorm:"column:referenced_column_name"` // Which column of the affected table.该索引受影响的表的哪一列
}

/////////////////////////////////////////////////////////////////////////

// TableDescription 表及表注释
type TableDescription struct {
Name string `gorm:"column:name"` // 表名
Value string `gorm:"column:value"` // 表注释
}

type ColumnKeys struct {
ID int `gorm:"column:id"`
Name string `gorm:"column:name"` // 列名
Pk int `gorm:"column:pk"` // 是否主键
Type string `gorm:"column:tp"` // 类型
Length int `gorm:"column:len"` // 长度
Isnull int `gorm:"column:isnull"` // 是否为空
Desc string `gorm:"column:des"` // 列注释
}

var noteRegex = regexp.MustCompile(`^\[@gorm\s(\S+)+\]`)
var foreignKeyRegex = regexp.MustCompile(`^\[@fk\s(\S+)+\]`)
Loading

0 comments on commit 8dba9d3

Please sign in to comment.