做外贸一般总浏览的网站,佛山网站搜索排名,做网站的用什么软件呢,最新网页游戏大全sql2struct
一个根据CREATE TABLE建表语句生成对应的Go语言结构体的工具#xff0c;暂只支持 MySQL 表。
开发目的
在 github 中找到一些 sql2struct#xff0c;但要么是 chrome 插件#xff0c;要么是在线工具#xff0c;要么是需要连接 MySQL#xff0c;…sql2struct
一个根据CREATE TABLE建表语句生成对应的Go语言结构体的工具暂只支持 MySQL 表。
开发目的
在 github 中找到一些 sql2struct但要么是 chrome 插件要么是在线工具要么是需要连接 MySQL不是很方便。本 sql2struct 根据 SQL 文件中的建表语句来生成 Go 的 struct可集成到 Makefile 等中方便使用。
安装方法
go install github.com/eyjian/sql2structlatest执行成功后在 $GOPATH/bin 目录下可找到 sql2struct
# file go env GOPATH/bin/sql2struct
/root/go/bin/sql2struct: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped使用示例
sql2struct % cat example-01.sql
DROP TABLE t_products;
CREATE TABLE t_products (f_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT 商品ID,f_name VARCHAR(255) NOT NULL COMMENT 商品名称,f_description TEXT,f_price DECIMAL(10, 2) NOT NULL,f_weight FLOAT NOT NULL COMMENT 商品重量kg,f_quantity SMALLINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 商品库存数量,f_is_active TINYINT(1) NOT NULL DEFAULT 1 COMMENT 商品是否激活0 - 未激活1 - 激活,f_rating DOUBLE COMMENT 商品评分,f_created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 商品创建时间,f_updated_at DATETIME ON UPDATE CURRENT_TIMESTAMP COMMENT 商品更新时间,UNIQUE INDEX idx_name_at (f_name),INDEX idx_created_at (f_created_at),KEY idx_updated_at (f_updated_at)
) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COMMENT商品表;
sql2struct %
sql2struct % ./sql2struct -sf./example-01.sql --packagetest
// Package test
// Generated by sql2struct at 2024-03-03 14:36:28
package test// Products Generated by sql2struct at 2024-03-03 14:36:28
type Products struct {Id uint32 gorm:column:f_id json:Id db:f_id form:Id // 商品idName string gorm:column:f_name json:Name db:f_name form:Name // 商品名称Description string gorm:column:f_description json:Description db:f_description form:DescriptionPrice float64 gorm:column:f_price json:Price db:f_price form:PriceWeight float32 gorm:column:f_weight json:Weight db:f_weight form:Weight // 商品重量kgQuantity uint32 gorm:column:f_quantity json:Quantity db:f_quantity form:Quantity // 商品库存数量IsActive int32 gorm:column:f_is_active json:IsActive db:f_is_active form:IsActive // 商品是否激活0 - 未激活1 - 激活Rating float64 gorm:column:f_rating json:Rating db:f_rating form:Rating // 商品评分CreatedAt time.Time gorm:column:f_created_at json:CreatedAt db:f_created_at form:CreatedAt // 商品创建时间UpdatedAt time.Time gorm:column:f_updated_at json:UpdatedAt db:f_updated_at form:UpdatedAt // 商品更新时间
}使用约束
sql 中的分割须为空格而不能是 TAB命令行参数–sf指定的 sql 文件只能包含一个create table建表语句不指定同一个 sql 文件含多个建表语句但大写或者小写不影响生成的时为排版的需要自行格式化生成的 Go 结构体中字段名、类型、注释等信息都是从 sql 语句中解析出来的如果 sql 语句中的字段名、类型、注释等信息不规范生成的 Go 结构体也会不规范
使用提示
建议将 sql2struct 放到 PATH 指定的目录比如 /usr/bin/ 或 $GOPATH/bin/ 目录下以便在任何地方都可以直接使用运行成功程序退出码为 0否则为非 0Shell 中可通过$?”的值来区分结果直接屏幕输出可重定向到文件中通过重定向可实现多个 SQL 文件对应一个 Go 代码文件默认不输出取表名函数可通过参数–with-tablename-func开启默认 json 和 form 两种 tag 会去掉字段名的前缀部分但可通过命令行参数-json-with-prefix”和-form-with-prefix分别控制
Makefile 中应用示例
sql2struct % cat Makefile.example
all: sql sql-01 sql-02 sql-03.PHONY: sqlsql:rm -f example.gosql-01: example-01.sqlsql2struct -sf$ -packagemain -with-tablename-functrue example.gosql-02: example-02.sqlecho example.gosql2struct -sf$ -with-tablename-functrue example.gosql-03: example-03.sqlecho example.gosql2struct -sf$ -json-with-prefixtrue example.go