-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
88 changed files
with
356 additions
and
14,413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## all@可选的命令参数,执行build和run命令。 | ||
all: help | ||
|
||
|
||
## clean@清理编译、日志和缓存等数据。 | ||
.PHONY:clean | ||
clean: | ||
@rm -rf ./logs; | ||
@rm -rf ./log; | ||
@rm -rf ./debug; | ||
@rm -rf ./tmp; | ||
@rm -rf ./temp; | ||
@echo "\033[31m ✅ 清理完毕\033[0m"; | ||
|
||
|
||
## commit <msg>@提交Git(格式:make commit msg=备注内容,msg为可选参数)。 | ||
.PHONY:commit | ||
message:=$(if $(msg),$(msg),"Rebuilded at $$(date '+%Y年%m月%d日 %H时%M分%S秒')") | ||
commit: | ||
@echo "\033[0;34mPush to remote...\033[0m" | ||
@git add . | ||
@git commit -m $(message) | ||
@echo "\033[0;31m 💿 Commit完毕\033[0m" | ||
|
||
|
||
## push <msg>@提交并推送到Git仓库(格式:make push msg=备注内容,msg为可选参数)。 | ||
.PHONY:push | ||
push:commit | ||
@git push #origin master | ||
@echo "\033[0;31m ⬆️ Push完毕\033[0m" | ||
|
||
|
||
## help@查看make帮助。 | ||
.PHONY:help | ||
help:Makefile | ||
@echo "Usage:\n make [command]" | ||
@echo | ||
@echo "Available Commands:" | ||
@sed -n "s/^##//p" $< | column -t -s '@' |grep --color=auto "^[[:space:]][a-z]\+[[:space:]]" | ||
@echo | ||
@echo "For more to see https://makefiletutorial.com/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2021 Hollson. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/yishuihanj/db2go/findSql" | ||
"github.com/yishuihanj/db2go/generator" | ||
) | ||
|
||
var ( | ||
DB *sql.DB | ||
Host, | ||
User, | ||
Pwd, | ||
DbName, | ||
Out string | ||
Port int | ||
Tables []string | ||
Help bool | ||
Columns []*findSql.Column // 模式 | ||
) | ||
|
||
func check() error { | ||
if DbName == "" { | ||
return errors.New("") | ||
} | ||
return nil | ||
} | ||
|
||
func InitDriver() (gen generator.Generator,err error) { | ||
defer func() { | ||
if err != nil { | ||
usage() | ||
} | ||
}() | ||
|
||
return nil, nil | ||
} | ||
|
||
//go:generate go build | ||
func InitCommand() (driver generator.Driver, err error) { | ||
defer func() { | ||
if err != nil { | ||
usage() | ||
} | ||
}() | ||
|
||
if len(os.Args) == 1 { | ||
return generator.Invalid, fmt.Errorf("args is missing") | ||
} | ||
|
||
if os.Args[1] == "mysql" { | ||
fs := mysqlFlag() | ||
args := os.Args[2:] | ||
if err := fs.Parse(args); err != nil || len(args) == 0 || Help { | ||
fs.Usage() | ||
return generator.Invalid, err | ||
} | ||
if err := check(); err != nil { | ||
fs.Usage() | ||
return generator.Invalid, err | ||
} | ||
// next | ||
return generator.Mysql, nil | ||
} | ||
|
||
if os.Args[1] == "pgsql" { | ||
fs := pgFlag() | ||
args := os.Args[2:] | ||
if err := fs.Parse(args); err != nil || len(args) == 0 || Help { | ||
fs.Usage() | ||
return generator.Invalid, err | ||
} | ||
if err := check(); err != nil { | ||
fs.Usage() | ||
return generator.Invalid, err | ||
} | ||
// next | ||
return generator.Postgres, nil | ||
} | ||
return generator.Invalid, fmt.Errorf("unknown args") | ||
} | ||
|
||
func usage() { | ||
fmt.Fprintf(os.Stderr, `「Golang」一个数据库表实体生成工具,支持mysql和postgres | ||
Usage: | ||
db2go <command> dbname=<dbName> [option]... | ||
e.g. ./db2go pgsql -host=localhost -port=5432 -user=postgres -pwd=123456 -dbname=deeplink -gorm=true -package=hello | ||
Command: | ||
mysql 从mysql数据库生成表实体 | ||
pgsql 从postgres数据库生成表实体 | ||
help 查看帮助 | ||
Option: | ||
-host 主机名 | ||
-host 主机名 | ||
-host 主机名 | ||
-host 主机名 | ||
-host 主机名 | ||
-host 主机名 | ||
更多详情,请参考 https://github.com/hollson/db2go | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 Hollson. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/yishuihanj/db2go/dbtogo" | ||
"github.com/yishuihanj/db2go/dialect/gorm" | ||
) | ||
|
||
func mysqlFlag() *flag.FlagSet { | ||
fs := flag.NewFlagSet("mysql", flag.ExitOnError) | ||
fs.StringVar(&Host, "host", "localhost", "主机名") | ||
fs.IntVar(&Port, "port", 0, "端口") | ||
fs.StringVar(&User, "user", "", "用户名") | ||
fs.StringVar(&Pwd, "pwd", "", "密码") | ||
fs.StringVar(&DbName, "dbname", "", "数据库名称") | ||
fs.BoolVar(&gorm.Gorm, "gorm", false, "是否添加gorm标签") | ||
fs.StringVar(&Out, "out", "./model", "输出路径") | ||
fs.StringVar(&dbtogo.Pkg, "package", "model", "go文件包名") | ||
fs.BoolVar(&Help, "help", false, "帮助文档") | ||
return fs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 Hollson. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/yishuihanj/db2go/dbtogo" | ||
"github.com/yishuihanj/db2go/dialect/gorm" | ||
) | ||
|
||
// gorm模式或raw模式 | ||
func pgFlag() *flag.FlagSet { | ||
fs := flag.NewFlagSet("pgsql", flag.ExitOnError) | ||
fs.StringVar(&Host, "host", "localhost", "主机名") | ||
fs.IntVar(&Port, "port", 5432, "端口") | ||
fs.StringVar(&User, "user", "postgres", "用户名") | ||
fs.StringVar(&Pwd, "pwd", "postgres", "密码") | ||
fs.StringVar(&DbName, "dbname", "", "数据库名称") | ||
fs.BoolVar(&gorm.Gorm, "gorm", false, "是否添加gorm标签") | ||
fs.StringVar(&Out, "out", "./model", "输出路径") | ||
fs.StringVar(&dbtogo.Pkg, "package", "model", "go文件包名") | ||
fs.BoolVar(&Help, "help", false, "帮助文档") | ||
return fs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2021 Hollson. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package generator | ||
|
||
type Driver int | ||
|
||
const ( | ||
Invalid Driver = 0 // 无效的 | ||
Mysql = 1 | ||
Postgres = 2 | ||
) | ||
|
||
func (d Driver) String() string { | ||
return []string{"Invalid", "Mysql", "Postgres"}[d] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2021 Hollson. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package generator | ||
|
||
// 代码生成器 | ||
type Generator interface { | ||
Driver() Driver | ||
Host() string // 主机 | ||
Port() int // 端口 | ||
User() string // 用户名 | ||
Password() string // 密码 | ||
DbName() string // 数据库名称 | ||
Check() error // 参数校验 | ||
Ping() error // 测试数据库连通性 | ||
Gen() error // 执行生成 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module github.com/yishuihanj/db2go | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/go-sql-driver/mysql v1.6.0 | ||
github.com/lib/pq v1.10.2 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | ||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= | ||
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package hello | ||
type Books struct { | ||
Aa string `gorm:"column:aa;type:character varying"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package hello | ||
type FibCache struct { | ||
Num int32 `gorm:"column:num;not null;primaryKey;type:integer"` | ||
Fib int32 `gorm:"column:fib;not null;type:integer"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package hello | ||
type Student struct { | ||
Id int `gorm:"column:id;not null;primaryKey;type:serial"` | ||
Name string `gorm:"column:name;type:character varying"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package hello | ||
type TestT struct { | ||
Id int32 `gorm:"column:id;type:integer"` | ||
Name string `gorm:"column:name;type:character varying"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package hello | ||
type TestTable1 struct { | ||
Id int `gorm:"column:id;not null;primaryKey;type:serial"` | ||
NickName string `gorm:"column:nick_name;type:character varying(20);commnet:'我是列注释'"` | ||
Addr string `gorm:"column:addr;type:character varying"` | ||
Age interface{} `gorm:"column:age;default:1;type:smallserial"` | ||
Asset interface{} `gorm:"column:asset;type:numeric(18,2);commnet:'我是列注释'"` | ||
Index interface{} `gorm:"column:index;default:1.0;type:double precision"` | ||
Amount interface{} `gorm:"column:amount;default:(0)::numeric;type:money"` | ||
Nonce int `gorm:"column:nonce;default:0;type:bigserial"` | ||
Birth time.Time `gorm:"column:birth;default:now();type:date;commnet:'我是列注释'"` | ||
CreateTime time.Time `gorm:"column:create_time;default:CURRENT_TIMESTAMP;type:timestamp without time zone"` | ||
UpdateTime time.Time `gorm:"column:update_time;default:(CURRENT_TIMESTAMP)::date;type:timestamp(6) without time zone;commnet:'我是列注释'"` | ||
Tels pq.StringArray `gorm:"column:tels;default:ARRAY['182'::text, '156'::text];type:character varying(11)[]"` | ||
Tags pq.Int64Array `gorm:"column:tags;default:ARRAY[ARRAY[1, 2, 3], ARRAY[4, 5, 6]];type:integer[]"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.