mysql导出表结构 创建_登录 - SegmentFault 思否

使用过grpc的同学都知道,写proto文件比较繁琐,尤其是写message,对应很多字段,为此写了一个简单的从mysql直接读取表结构,生成proto文件的工具。

工具的使用很简单,需要简单的配置,即可运行生成proto文件。

项目地址:https://github.com/guyan0319/...

准备

使用前需先安装依赖包go-sql-driver/mysql

$ go get -u github.com/go-sql-driver/mysql

使用说明:

func main() {

//模板文件存放路径

tpl := "d:/gopath/src/mysql-to-proto/template/proto.go.tpl"

//生成proto文件路径

file := "d:/gopath/src/mysql-to-proto/sso.proto"

//数据库名,这里填你自己的数据库名

dbName := "user"

//配置连接数据库信息

db, err := Connect("mysql", "root:123456@tcp(127.0.0.1:3306)/"+dbName+"?charset=utf8mb4&parseTime=true")

//Table names to be excluded

//需要排除表,这里的表不会生成对应的proto文件

exclude := map[string]int{"user_log": 1}

if err != nil {

fmt.Println(err)

}

if IsFile(file) {

fmt.Fprintf(os.Stderr, "Fatal error: ", "proto file already exist")

return

}

t := Table{}

//配置message,Cat

t.Message = map[string]Detail{

"Filter": {

Name: "Filter",

Cat: "custom",

Attr: []AttrDetail{{

TypeName: "uint64", //类型

AttrName: "id",//字段

}},

},

"Request": {

Name: "Request",

Cat: "all",

},

"Response": {

Name: "Response",

Cat: "custom",

Attr: []AttrDetail{

{

TypeName: "uint64",

AttrName: "id",

},

{

TypeName: "bool",

AttrName: "success",

},

},

},

}

//pachage名称

t.PackageModels = "sso"

//service名称

t.ServiceName = "Sso"

//配置services里面的rpc

t.Method = map[string]MethodDetail{

"Get": {Request: t.Message["Filter"], Response: t.Message["Request"]},

"Create": {Request: t.Message["Request"], Response: t.Message["Response"]},

"Update": {Request: t.Message["Request"], Response: t.Message["Response"]},

}

//处理数据库表字段属性

t.TableColumn(db, dbName, exclude)

//生成proto

t.Generate(file, tpl)

}

links

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值