Skip to content

Commit

Permalink
support sqlite
Browse files Browse the repository at this point in the history
支持sqlite
  • Loading branch information
xxjwxc committed Sep 22, 2020
1 parent 9893478 commit 36b6de2
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 67 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
all: # 构建
make tar
windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
mac:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gormt main.go
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o gormt main.go
tar: # 打包
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
tar czvf gormt_windows.zip gormt.exe config.yml
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
tar czvf gormt_mac.zip gormt config.yml
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gormt main.go
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o gormt main.go
tar czvf gormt_linux.zip gormt config.yml
clear:
test ! -d model/ || rm -rf model/*
Expand Down
5 changes: 3 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ is_foreign_key : true # 是否导出外键关联
is_gui : false # 是否ui模式显示
is_table_name : false # 是否直接生成表名函数
is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型
mysql_info:
host : 127.0.0.1
db_info:
host : 127.0.0.1 # type=1的时候,host为yml文件全路径
port : 3306
username : root
password : 123456
database : oauth_db
type: 0 # 数据库类型:0:mysql , 1:sqlite , 2:mssql

8 changes: 4 additions & 4 deletions data/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gopkg.in/go-playground/validator.v9"
)

var mysqlInfo config.MysqlDbInfo
var mysqlInfo config.DBInfo
var outDir string
var singularTable bool
var foreignKey bool
Expand Down Expand Up @@ -78,20 +78,20 @@ func init() {
func initConfig() {
MergeMysqlDbInfo()
validate := validator.New()
err := validate.Struct(config.GetMysqlDbInfo())
err := validate.Struct(config.GetDbInfo())
if err != nil {
mylog.Info("Can't read cmd: using (-h, --help) to get more info")
mylog.Error(err)
os.Exit(1)
} else {
mylog.Info("using database info:")
mylog.JSON(config.GetMysqlDbInfo())
mylog.JSON(config.GetDbInfo())
}
}

// MergeMysqlDbInfo merge parm
func MergeMysqlDbInfo() {
var tmp = config.GetMysqlDbInfo()
var tmp = config.GetDbInfo()
if len(mysqlInfo.Database) > 0 {
tmp.Database = mysqlInfo.Database
}
Expand Down
65 changes: 33 additions & 32 deletions data/config/MyIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,51 @@ import (
// Config custom config struct
type Config struct {
CfgBase `yaml:"base"`
MySQLInfo MysqlDbInfo `yaml:"mysql_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
SingularTable bool `yaml:"singular_table"`
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
}

// MysqlDbInfo mysql database information. mysql 数据库信息
type MysqlDbInfo struct {
DBInfo DBInfo `yaml:"db_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
SingularTable bool `yaml:"singular_table"`
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
}

// DBInfo mysql database information. mysql 数据库信息
type DBInfo struct {
Host string `validate:"required"` // Host. 地址
Port int `validate:"required"` // Port 端口号
Username string `validate:"required"` // Username 用户名
Port int // Port 端口号
Username string // Username 用户名
Password string // Password 密码
Database string `validate:"required"` // Database 数据库名
Database string // Database 数据库名
Type int // 数据库类型:0:mysql 1:配置
}

// SetMysqlDbInfo Update MySQL configuration information
func SetMysqlDbInfo(info *MysqlDbInfo) {
_map.MySQLInfo = *info
func SetMysqlDbInfo(info *DBInfo) {
_map.DBInfo = *info
}

// GetMysqlDbInfo Get MySQL configuration information .获取mysql配置信息
func GetMysqlDbInfo() MysqlDbInfo {
return _map.MySQLInfo
// GetDbInfo Get configuration information .获取数据配置信息
func GetDbInfo() DBInfo {
return _map.DBInfo
}

// GetMysqlConStr Get MySQL connection string.获取mysql 连接字符串
func GetMysqlConStr() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local&interpolateParams=True",
_map.MySQLInfo.Username,
_map.MySQLInfo.Password,
_map.MySQLInfo.Host,
_map.MySQLInfo.Port,
_map.MySQLInfo.Database,
_map.DBInfo.Username,
_map.DBInfo.Password,
_map.DBInfo.Host,
_map.DBInfo.Port,
_map.DBInfo.Database,
)
}

Expand Down
7 changes: 4 additions & 3 deletions data/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _map = Config{
CfgBase: CfgBase{
IsDev: false,
},
MySQLInfo: MysqlDbInfo{
DBInfo: DBInfo{
Host: "127.0.0.1",
Port: 3306,
Username: "root",
Expand Down Expand Up @@ -65,11 +65,12 @@ func InitFile(filename string) error {
if _, e := os.Stat(filename); e != nil {
fmt.Println("init default config file: ", filename)
if err := SaveToFile(); err == nil {
fmt.Println("done,please restart.")
InitFile(filename)
return nil
} else {
fmt.Println("shit,fail", err)
}
os.Exit(0)
// os.Exit(0)
}
bs, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion data/dlg/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package dlg

import (
"fmt"
"os/exec"

"github.com/jroimartin/gocui"
"github.com/xxjwxc/gormt/data/view/model"
"github.com/xxjwxc/gormt/data/view/model/genmysql"
"github.com/xxjwxc/gormt/data/view/model/gensqlite"

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

"github.com/xxjwxc/public/mylog"
"github.com/xxjwxc/public/tools"
)

Expand Down Expand Up @@ -72,7 +75,18 @@ func getBool(bstr string) bool {
}

func generate(g *gocui.Gui, v *gocui.View) {
modeldb := genmysql.GetMysqlModel()
var modeldb model.IModel
switch config.GetDbInfo().Type {
case 0:
modeldb = genmysql.GetModel()
case 1:
modeldb = gensqlite.GetModel()
}
if modeldb == nil {
mylog.Error(fmt.Errorf("modeldb not fund : please check db_info.type (0:mysql , 1:sqlite , 2:mssql) "))
return
}

pkg := modeldb.GenModel()
// just for test
// out, _ := json.Marshal(pkg)
Expand Down
14 changes: 8 additions & 6 deletions data/dlg/cui.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,18 @@ func enterSet(g *gocui.Gui, v *gocui.View) error {
// add input field
form.AddInputField("out_dir", SLocalize("out_dir"), formPart[0], formPart[1]).SetText(config.GetOutDir()).
AddValidate("required input", requireValidator)
form.AddInputField("db_host", SLocalize("db_host"), formPart[0], formPart[1]).SetText(config.GetMysqlDbInfo().Host).
form.AddInputField("db_host", SLocalize("db_host"), formPart[0], formPart[1]).SetText(config.GetDbInfo().Host).
AddValidate("required input", requireValidator)
form.AddInputField("db_port", SLocalize("db_port"), formPart[0], formPart[1]).SetText(tools.AsString(config.GetMysqlDbInfo().Port)).
form.AddInputField("db_port", SLocalize("db_port"), formPart[0], formPart[1]).SetText(tools.AsString(config.GetDbInfo().Port)).
AddValidate("required input", requireValidator)
form.AddInputField("db_usename", SLocalize("db_usename"), formPart[0], formPart[1]).SetText(config.GetMysqlDbInfo().Username).
form.AddInputField("db_usename", SLocalize("db_usename"), formPart[0], formPart[1]).SetText(config.GetDbInfo().Username).
AddValidate("required input", requireValidator)
form.AddInputField("db_pwd", SLocalize("db_pwd"), formPart[0], formPart[1]).SetText(config.GetMysqlDbInfo().Password).
form.AddInputField("db_pwd", SLocalize("db_pwd"), formPart[0], formPart[1]).SetText(config.GetDbInfo().Password).
SetMask().SetMaskKeybinding(gocui.KeyCtrlA).
AddValidate("required input", requireValidator)
form.AddInputField("db_name", SLocalize("db_name"), formPart[0], formPart[1]).SetText(config.GetMysqlDbInfo().Database).
form.AddInputField("db_name", SLocalize("db_name"), formPart[0], formPart[1]).SetText(config.GetDbInfo().Database).
AddValidate("required input", requireValidator)
form.AddInputField("db_type", SLocalize("db_type"), formPart[0], formPart[1]).SetText(tools.AsString(config.GetDbInfo().Type)).
AddValidate("required input", requireValidator)

// add select
Expand Down Expand Up @@ -264,7 +266,7 @@ func buttonSave(g *gocui.Gui, v *gocui.View) error {
mp := form.GetFieldTexts()
config.SetOutDir(mp["out_dir"])

var dbInfo config.MysqlDbInfo
var dbInfo config.DBInfo
dbInfo.Host = mp["db_host"]
port, err := strconv.Atoi(mp["db_port"])
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions data/view/cnf/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var TypeMysqlDicMp = map[string]string{
"blob": "[]byte",
"mediumblob": "[]byte",
"longblob": "[]byte",
"integer": "int64",
}

// TypeMysqlMatchMp Fuzzy Matching Types.模糊匹配类型
Expand All @@ -58,6 +59,7 @@ var TypeMysqlMatchMp = map[string]string{
`^(enum)[(](.)+[)]`: "string",
`^(varchar)[(]\d+[)]`: "string",
`^(varbinary)[(]\d+[)]`: "[]byte",
`^(blob)[(]\d+[)]`: "[]byte",
`^(binary)[(]\d+[)]`: "[]byte",
`^(decimal)[(]\d+,\d+[)]`: "float64",
`^(mediumint)[(]\d+[)]`: "string",
Expand All @@ -66,4 +68,6 @@ var TypeMysqlMatchMp = map[string]string{
`^(float)[(]\d+,\d+[)] unsigned`: "float64",
`^(datetime)[(]\d+[)]`: "time.Time",
`^(bit)[(]\d+[)]`: "[]uint8",
`^(text)[(]\d+[)]`: "string",
`^(integer)[(]\d+[)]`: "int",
}
16 changes: 15 additions & 1 deletion data/view/gtools/gtools.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gtools

import (
"fmt"
"os/exec"

"github.com/xxjwxc/public/mylog"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/xxjwxc/gormt/data/config"

"github.com/xxjwxc/gormt/data/view/model/genmysql"
"github.com/xxjwxc/gormt/data/view/model/gensqlite"
"github.com/xxjwxc/public/tools"
)

Expand All @@ -28,8 +30,20 @@ func showCmd() {
// tt.Nickname = "ticket_001"
// orm.Where("nickname = ?", "ticket_001").Find(&tt)
// fmt.Println(tt)
modeldb := genmysql.GetMysqlModel()
var modeldb model.IModel
switch config.GetDbInfo().Type {
case 0:
modeldb = genmysql.GetModel()
case 1:
modeldb = gensqlite.GetModel()
}
if modeldb == nil {
mylog.Error(fmt.Errorf("modeldb not fund : please check db_info.type (0:mysql , 1:sqlite , 2:mssql) "))
return
}

pkg := modeldb.GenModel()
// gencnf.GenOutPut(&pkg)
// just for test
// out, _ := json.Marshal(pkg)
// tools.WriteFile("test.txt", []string{string(out)}, true)
Expand Down
Loading

0 comments on commit 36b6de2

Please sign in to comment.