爱意满满的作品展示区。
idoubi

我写了一个用 SQL 转换 Go struct 的工具

  •  
  •   idoubi · Jun 27, 2022 · 3531 views
    This topic created in 1596 days ago, the information mentioned may be changed or developed.

    Go web 开发中,在操作 db 的时候经常需要写一个 model 来映射需要用到的数据表字段。如果要映射的表字段比较多,在 Go struct 定义时一个一个去写非常麻烦。

    五年前我写了一个叫做 sql2struct 的工具,可以通过 SQL 建表语句,自动转换成 Go struct 。

    之前的版本只提供了 chrome 插件的使用渠道,最近使用新的技术栈重构了一版,新版样式长这样:

    在数据库客户端通过 show create table xxx\G; 导出建表的 SQL 语句,粘贴到左侧的输入框,即可在右侧自动生成 Go struct 定义代码。

    • 支持自定义需要生成的 Go struct tags ;
    • 支持自定义在 Go struct 中需要全部大写的特殊字段;
    • 支持自定义 SQL 字段与 Go struct 字段的映射关系;

    在线使用地址:https://dou.tools/sql2struct/

    源码地址:https://github.com/idoubi/sql2struct

    详细介绍博文:https://idoubi.cc/posts/sql2struct-new-version/

    欢迎各位 V 友体验。现学现写的 typescript + react + vite + pnpm 项目,代码不规范之处,欢迎指点。

    14 replies  •  2022-06-28 12:07:56 +08:00
    nash
    nash
    Jun 27, 2022
    1
    你这个 gorm 的生成是不是差点东西
    idoubi
    idoubi
    OP
    Jun 27, 2022 via iPhone
    2
    @nash 差了啥
    to2false
    to2false
    Jun 27, 2022❤️1
    3
    idoubi
    idoubi
    OP
    Jun 27, 2022
    4
    @to2false 功能挺全的
    sadfQED2
    sadfQED2
    Jun 27, 2022 via Android
    5
    哈哈哈,巧了巧了,一样的工具我也写了一个

    https://tools.jinnrry.com
    cheng6563
    cheng6563
    Jun 27, 2022
    6
    支持 Oracle 吗
    idoubi
    idoubi
    OP
    Jun 27, 2022 via iPhone
    7
    @cheng6563 Oracle 建表语句跟 mysql 差别很大吗?
    idoubi
    idoubi
    OP
    Jun 27, 2022 via iPhone
    8
    @sadfQED2 很棒
    cheng6563
    cheng6563
    Jun 27, 2022
    9
    @idoubi 说大不大说小不小。。
    idoubi
    idoubi
    OP
    Jun 27, 2022 via iPhone
    10
    @cheng6563 你可以试一下。只要满足我那个匹配的正则表达式应该就行
    v2defy
    v2defy
    Jun 28, 2022 via Android
    11
    ikaros
    ikaros
    Jun 28, 2022
    12
    我的需求是反向的,写完 struct 需要转 sql, 一般我都是用 gorm migrator.CreateTable() 然后把 sql 复制出来自己加索引
    waltcow
    waltcow
    Jun 28, 2022
    13
    有空研究下 sql2Protobuf
    idoubi
    idoubi
    OP
    Jun 28, 2022 via iPhone
    14
    @waltcow 确实是刚需
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4607 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 112ms · UTC 01:41 · PVG 09:41 · LAX 17:41 · JFK 20:41
    ♥ Do have faith in what you're doing.