Skip to content

Commit

Permalink
Rebuilded at 2021年07月09日 15时21分27秒
Browse files Browse the repository at this point in the history
  • Loading branch information
hollson committed Jul 9, 2021
1 parent 2a7d5d3 commit e44f549
Show file tree
Hide file tree
Showing 88 changed files with 356 additions and 14,413 deletions.
41 changes: 41 additions & 0 deletions Makefile
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/"
114 changes: 114 additions & 0 deletions cmd/command.go
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
`)
}
26 changes: 26 additions & 0 deletions cmd/flag_mysql.go
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
}
27 changes: 27 additions & 0 deletions cmd/flag_pgsql.go
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
}
Binary file added db2go
Binary file not shown.
5 changes: 3 additions & 2 deletions dbtogo/db_to_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"strings"

"github.com/yishuihanj/db2go/dialect/gorm"
"github.com/yishuihanj/db2go/findSql"
"github.com/yishuihanj/db2go/gormtogo"

)

var Pkg string
Expand All @@ -17,7 +18,7 @@ func ColumnsToStruct(_tableName string, columns []*findSql.Column) string {
singleString := fmt.Sprintf("\t%s\t%s", splitUnderline(column.ColumnName), typeConvert(column.ColumnType))

//
singleString = singleString + gormtogo.AddGormTag(column) + "\n"
singleString = singleString + gorm.AddGormTag(column) + "\n"
columnString += singleString

}
Expand Down
4 changes: 2 additions & 2 deletions gormtogo/gorm_to_go.go → dialect/gorm/gorm_to_go.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gormtogo
package gorm

import (

Expand All @@ -12,7 +12,7 @@ var Gorm bool

//向字段中添加 Gorm tag
func AddGormTag(column *findSql.Column) string {
//如果没有开启gorm 则 不需要转换
//如果没有开启gorm 则 不需要转换 // dogner
if !Gorm {
return ""
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ CASE
pg_attrdef ad
WHERE
ad.adrelid = A.attrelid
AND ad.adnum = A.attnum
AND ad.adsrc = 'nextval(''' || ( pg_get_serial_sequence ( A.attrelid :: regclass :: TEXT, A.attname ) ) :: regclass || '''::regclass)'
) THEN
AND ad.adnum = A.attnum) THEN
CASE
A.atttypid
WHEN 'int' :: regtype THEN
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion create_file.go → generator/create_file.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package generator

import (
"fmt"
Expand Down
17 changes: 17 additions & 0 deletions generator/driver.go
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]
}
18 changes: 18 additions & 0 deletions generator/generator.go
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 // 执行生成
}
5 changes: 5 additions & 0 deletions go.mod
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
)
4 changes: 4 additions & 0 deletions go.sum
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=
4 changes: 4 additions & 0 deletions go_output/books.go
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"`
}
5 changes: 5 additions & 0 deletions go_output/fib_cache.go
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"`
}
5 changes: 5 additions & 0 deletions go_output/student.go
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"`
}
5 changes: 5 additions & 0 deletions go_output/test_t.go
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"`
}
16 changes: 16 additions & 0 deletions go_output/test_table1.go
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[]"`
}
4 changes: 2 additions & 2 deletions interface_sql/interface_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"errors"
"strings"

"github.com/yishuihanj/db2go/mysql"
"github.com/yishuihanj/db2go/pgsql"
"github.com/yishuihanj/db2go/dialect/mysql"
"github.com/yishuihanj/db2go/dialect/pgsql"
)

type SqlInterface interface {
Expand Down
Loading

0 comments on commit e44f549

Please sign in to comment.