The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
guanhui07

这个 golang struct 是用的什么生成的

  •  
  •   guanhui07 · Sep 14, 2022 · 4568 views
    This topic created in 1363 days ago, the information mentioned may be changed or developed.

    gorm

    ddl 语句如下:

    CREATE TABLE `pf_station_info` (
      `id` int unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(20) NOT NULL DEFAULT '' COMMENT '站点名称',
      `location` json NOT NULL COMMENT '站点所在经纬度',
      `ip_address` varchar(15) NOT NULL DEFAULT '' COMMENT '站点 ip 地址,非此 IP 数据不接收',
      `tiger_shaped` char(32) NOT NULL DEFAULT '' COMMENT '握手符号',
      `heartbeat` int unsigned NOT NULL DEFAULT '0' COMMENT '上次心跳时间戳',
      `status` int NOT NULL DEFAULT '0' COMMENT '站点状态:0 正常,非 0 为停用时间戳',
      `add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    

    要生成 如下 gorm struct , not null 以及 comment 也在 tag 里 且 location 类型 是 datatypes.JSON 类型

    type Station struct {
    	Id          int64          `json:"id" gorm:"primaryKey;autoIncrement;comment:主键 id"`
    	Name        string         `gorm:"type:varchar(20);column:name;not null;comment:站点名称" json:"name"`
    	Location    datatypes.JSON `gorm:"type:json;column:location;not null;comment:站点所在经纬度" json:"location"`
    	IpAddress   string         `gorm:"type:varchar(15);column:ip_address;not null;comment:站点 ip 地址,非此 IP 数据不接收" json:"ip_address"`
    	TigerShaped string         `gorm:"type:char(32);column:tiger_shaped;not null;comment:握手符号" json:"tiger_shaped"`
    	Heartbeat   int64          `gorm:"type:int(10);column:heartbeat;not null;comment:上次心跳时间戳" json:"heartbeat"`
    	Status      int64          `gorm:"type:int(11);column:status;comment:站点状态:0 正常,非 0 为停用时间戳" json:"status"`
    	AddTime     time.Time      `gorm:"type:datetime;comment:创建时间" json:"add_time,omitempty"`
    	UpdateTime  time.Time      `gorm:"type:datetime;comment:修改时间" json:"update_time,omitempty"`
    }
    

    有人知道 上面的 struct 是用 什么工具生成的吗?我猜不可能手写吧。

    谷歌搜了下 看了好几个 根据 ddl 生成都不一样 ,比如下面的

    https://www.qetool.com/sql_json_go/sql.html

    http://sql2struct.atotoa.com/

    utools 里的 sql2struct 生成也不一样

    有没大佬推荐个好用点的 或 跟这个 生成一模一样的。

    17 replies    2022-09-15 12:04:45 +08:00
    treblex
        1
    treblex  
       Sep 14, 2022
    orm 不是從 struct 生成 sql 嗎,應該是手寫的吧
    guanhui07
        2
    guanhui07  
    OP
       Sep 14, 2022
    @treblex #1 不能吧 先写 struct 再 根据 struct 生成 ddl 语句 吧。
    那我这个 struct 怎么 生成 ddl 语句 ,用什么工具 转。

    我现在想到是 ddl2struct 网上找的 不太一样 只能改改 ,因为好奇所以找了找 ,没找到 一样的。
    说不定别人自己写的转换工具 ,哈哈
    ikaros
        3
    ikaros  
       Sep 14, 2022
    @guanhui07 gorm v2 的 migrator interface 里面有个 create table 函数, v1 我记得就是在*gorm.DB 里面, 可以用这个函数创建表
    idoubi
        4
    idoubi  
       Sep 14, 2022 via Android
    可能用我这个工具生成完,然后自己改了一些 tag ? https://dou.tools/sql2struct/?
    to2false
        5
    to2false  
       Sep 14, 2022
    推荐官方下的一个工具 https://github.com/go-gorm/gen
    Johngodme
        6
    Johngodme  
       Sep 14, 2022   ❤️ 1
    aeli
        7
    aeli  
       Sep 14, 2022
    copilot 可以根据 sql 语句注释之类的自动生成
    Trim21
        8
    Trim21  
       Sep 14, 2022
    应该是 gorm/gen ,具体字段生成出来的类型是可以自己设置的。
    xioxu
        9
    xioxu  
       Sep 14, 2022
    历史悠久的工具 codeSmith 就可以根据 schema 生成任何代码
    king888
        10
    king888  
       Sep 14, 2022   ❤️ 2
    不用 gorm

    一般用 fraenky8/tables-to-go 生成 struct ,配合不到 500 行自己封装的 CURD 直接撸,至于 Database Design ,AutoMigrate 是直接用的 DbSchema 这款软件,爽的一批
    FrankFang128
        11
    FrankFang128  
       Sep 14, 2022
    @king888 看起来不错
    waltcow
        12
    waltcow  
       Sep 14, 2022
    结合 sql ,copilot 一直 tab 到底
    keepeye
        13
    keepeye  
       Sep 14, 2022
    我都是手写 tag ,用 gorm 的 migrate 创建表
    ElmerZhang
        14
    ElmerZhang  
       Sep 14, 2022
    copilot 就可以,把 DDL 以注释的形式粘在文件开头,然后开始写 struct ,写个一两行之后,后面的就都能用 copilot 补全一路 tab 出来。搞完了再把 DDL 注释删了就行了。
    dalang
        15
    dalang  
       Sep 14, 2022
    lrvy
        16
    lrvy  
       Sep 14, 2022
    gorm 有个 gen 库 https://github.com/go-gorm/gen
    timethinker
        17
    timethinker  
       Sep 15, 2022
    挺有意思,刚才用 JS 手写了一个 DDL 的解析器,可以将这种 CREATE TABLE 转换成 JSON ,然后有了 JSON 以后就可以用来生成代码了,仓促之下只做了 CREATE TABLE 这种 SQL 语句,写的也比较凌乱,可能还有一些 BUG ,不过可以自行拓展。

    地址: https://jsfiddle.net/AlexMaho/b6c1utoe/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3030 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 178ms · UTC 14:40 · PVG 22:40 · LAX 07:40 · JFK 10:40
    ♥ Do have faith in what you're doing.