feat(ReverseEngine):初始化工程
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"ReverseEngine/entity"
|
||||
"ReverseEngine/static"
|
||||
"ReverseEngine/util"
|
||||
"database/sql"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CheckPathSupport struct{}
|
||||
|
||||
func (*CheckPathSupport) CheckFileDir(path string, tableName string, cover bool) bool {
|
||||
//先查询一下路径是否为文件夹
|
||||
if b := util.IsDirExist(path); !b {
|
||||
log.Println("文件夹路径不存在略过")
|
||||
return false
|
||||
}
|
||||
//如果是不覆盖的情况,判断是否有这个文件,如果有的话直接返回
|
||||
if !cover && util.IsFileExist(strings.Join([]string{path, static.Splice, tableName}, "")) {
|
||||
return false
|
||||
}
|
||||
|
||||
//判断读写权限
|
||||
if !util.HasReadWritePermission(path) {
|
||||
log.Println("没有该文件夹的读写权限:", path)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (*CheckPathSupport) CheckTableExist(dbName string, tableName string, db *sql.DB) bool {
|
||||
var tm entity.TableMessage
|
||||
err := db.QueryRow(strings.Join([]string{"select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=? and TABLE_NAME=?"}, ""), dbName, tableName).Scan(&tm.TableName)
|
||||
//查询失败
|
||||
if err != nil {
|
||||
panic("数据库查询失败:" + err.Error())
|
||||
return false
|
||||
}
|
||||
//查询没有数据
|
||||
if !reflect.ValueOf(tm).IsValid() || tm.TableName == "" {
|
||||
panic("没有表数据信息:" + tableName)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"ReverseEngine/entity"
|
||||
"ReverseEngine/static"
|
||||
"ReverseEngine/util"
|
||||
"database/sql"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CheckSupport struct{}
|
||||
|
||||
func (*CheckSupport) CheckFileDir(path string, tableName string, cover bool) bool {
|
||||
b, realPath := pathExist(path)
|
||||
//先查询一下路径是否为文件夹
|
||||
if !b {
|
||||
log.Println("文件夹路径不存在略过")
|
||||
return false
|
||||
}
|
||||
//如果是不覆盖的情况,判断是否有这个文件,如果有的话直接返回
|
||||
if !cover && util.IsFileExist(strings.Join([]string{path, static.Splice, tableName}, "")) {
|
||||
return false
|
||||
}
|
||||
|
||||
//判断读写权限
|
||||
if !util.HasReadWritePermission(realPath) {
|
||||
log.Println("没有该文件夹的读写权限:", realPath)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (*CheckSupport) CheckTableExist(dbName string, tableName string, db *sql.DB) bool {
|
||||
var tm entity.TableMessage
|
||||
err := db.QueryRow(strings.Join([]string{"select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=? and TABLE_NAME=?"}, ""), dbName, tableName).Scan(&tm.TableName)
|
||||
//查询失败
|
||||
if err != nil {
|
||||
panic("数据库查询失败:" + err.Error())
|
||||
return false
|
||||
}
|
||||
//查询没有数据
|
||||
if !reflect.ValueOf(tm).IsValid() || tm.TableName == "" {
|
||||
panic("没有表数据信息:" + tableName)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func pathExist(path string) (bool, string) {
|
||||
pp := util.GetCurrentPath()
|
||||
realPath := strings.Join([]string{pp, static.Splice, path}, "")
|
||||
return util.IsDirExist(strings.Join([]string{pp, static.Splice, path}, "")), realPath
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DbSupport struct{}
|
||||
|
||||
func (dbc *DbSupport) CreateDbConnection(dbPath string, maxIdleConns int, connMaxLifeTime int) *sql.DB {
|
||||
var db *sql.DB
|
||||
db, _ = sql.Open("mysql", dbPath)
|
||||
|
||||
//设置数据库最大连接数
|
||||
db.SetConnMaxLifetime(time.Duration(connMaxLifeTime))
|
||||
//设置上数据库最大闲置连接数
|
||||
db.SetMaxIdleConns(maxIdleConns)
|
||||
|
||||
if err := db.Ping(); err != nil {
|
||||
panic("数据库连接失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Println("数据库连接成功")
|
||||
return db
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"ReverseEngine/entity"
|
||||
"ReverseEngine/static"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileContentSupport struct{}
|
||||
|
||||
func (FileContentSupport) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string) string {
|
||||
var build strings.Builder
|
||||
pName := splitLast(filePath, static.Splice)
|
||||
|
||||
writePackage(&build, pName) //写文件的引用包
|
||||
writeImportIfTime(&build, hasTime) //如果有time.Time类型的需要写入import
|
||||
buildStructHead(&build, fileName) //写入结构体头
|
||||
|
||||
//对于没有字段的返回空定义的文件
|
||||
if len(fms) == 0 {
|
||||
build.WriteString(static.RightBrace)
|
||||
return build.String()
|
||||
}
|
||||
|
||||
//写中间字段的操作
|
||||
for _, fm := range fms {
|
||||
build.WriteString(" ")
|
||||
build.WriteString(fm.FiledName)
|
||||
build.WriteString(" ")
|
||||
build.WriteString(fm.FieldType)
|
||||
build.WriteString(" ")
|
||||
build.WriteString(generateTag(fm, tagKey))
|
||||
build.WriteString(" ")
|
||||
build.WriteString(static.DoubleInclinedRod)
|
||||
build.WriteString(fm.Comment)
|
||||
build.WriteString(static.LineFeed)
|
||||
}
|
||||
|
||||
build.WriteString(static.RightBrace)
|
||||
|
||||
return build.String()
|
||||
}
|
||||
|
||||
func generateTag(message entity.FieldMessage, tagKey string) string {
|
||||
var build strings.Builder
|
||||
build.WriteString("`")
|
||||
build.WriteString(tagKey)
|
||||
build.WriteString(static.Colon)
|
||||
generateTagValue(message, &build)
|
||||
build.WriteString("`")
|
||||
return build.String()
|
||||
}
|
||||
|
||||
func generateTagValue(message entity.FieldMessage, builder *strings.Builder) {
|
||||
builder.WriteString("\"")
|
||||
if message.IsKey {
|
||||
builder.WriteString("pk ")
|
||||
}
|
||||
if !message.CanNull {
|
||||
builder.WriteString("not null ")
|
||||
}
|
||||
|
||||
builder.WriteString(message.OriginType)
|
||||
builder.WriteString("\"")
|
||||
}
|
||||
|
||||
func buildStructHead(build *strings.Builder, fileName string) {
|
||||
build.WriteString("type ")
|
||||
build.WriteString(fileName)
|
||||
build.WriteString(" struct")
|
||||
build.WriteString(static.LeftBrace)
|
||||
build.WriteString(static.LineFeed)
|
||||
}
|
||||
|
||||
func writeImportIfTime(build *strings.Builder, hasTime bool) {
|
||||
//如果有类型匹配上时间,那么需要在头部加上import
|
||||
if hasTime {
|
||||
build.WriteString("import (")
|
||||
build.WriteString("\n ")
|
||||
build.WriteString("\"time\"")
|
||||
build.WriteString("\n")
|
||||
build.WriteString(")")
|
||||
build.WriteString(static.LineFeed)
|
||||
build.WriteString(static.LineFeed)
|
||||
}
|
||||
}
|
||||
|
||||
func writePackage(build *strings.Builder, pName string) {
|
||||
build.WriteString("package ")
|
||||
build.WriteString(pName)
|
||||
build.WriteString(static.LineFeed)
|
||||
build.WriteString(static.LineFeed)
|
||||
}
|
||||
|
||||
/*
|
||||
返回按照规则切割的最后一个字符串
|
||||
*/
|
||||
func splitLast(filePath string, split string) string {
|
||||
ss := strings.Split(filePath, split)
|
||||
return ss[len(ss)-1]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"ReverseEngine/entity"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type FileContentSUpport2 struct{}
|
||||
|
||||
func (FileContentSUpport2) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string) string {
|
||||
fmt.Println("12321321")
|
||||
return " "
|
||||
}
|
||||
Reference in New Issue
Block a user