db2struct

将一个 mysql 表转换为一个 golang 结构。(Converts a mysql table into a golang struct)

  • 所有者: Shelnutt2/db2struct
  • 平台: Linux, Mac, Windows
  • 許可證: GNU General Public License v3.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

db2struct Build Status Coverage Status GoDoc

The db2struct package produces a usable golang struct from a given database table for use in a .go file.

By reading details from the database about the column structure, db2struct generates a go compatible struct type
with the required column names, data types, and annotations.

Generated datatypes include support for nullable columns sql.NullX types or guregu null.X types
and the expected basic built in go types.

Db2Struct is based/inspired by the work of ChimeraCoder's gojson package
gojson

Usage

go get github.com/Shelnutt2/db2struct/cmd/db2struct
db2struct --host localhost -d test -t test_table --package myGoPackage --struct testTable -p --user testUser

Example

MySQL table named users with four columns: id (int), user_name (varchar(255)), number_of_logins (int(11),nullable), and LAST_NAME (varchar(255), nullable)

Example below uses guregu's null package, but without the option it procuded the sql.NullInt64 and so on.

db2struct --host localhost -d example.com -t users --package example --struct user -p --user exampleUser --guregu --gorm

Output:


package example

type User struct {
  ID              int   `gorm:"column:id"`
  UserName        string `gorm:"column:user_name"`
  NumberOfLogins  null.Int `gorm:"column:number_of_logins"`
  LastName        null.String `gorm:"column:LAST_NAME"`
}

Supported Databases

Currently Supported

  • MariaDB
  • MySQL

Planned Support

  • PostgreSQL
  • Oracle
  • Microsoft SQL Server

MariaDB/MySQL

Structures are created by querying the INFORMATION_SCHEMA.Columns table and then formatting the types, column names,
and metadata to create a usable go compatible struct type.

Supported Datatypes

Currently only a limited number of MariaDB/MySQL datatypes are supported. Initial support includes:

  • tinyint (sql.NullInt64 or null.Int)
  • int (sql.NullInt64 or null.Int)
  • smallint (sql.NullInt64 or null.Int)
  • mediumint (sql.NullInt64 or null.Int)
  • bigint (sql.NullInt64 or null.Int)
  • decimal (sql.NullFloat64 or null.Float)
  • float (sql.NullFloat64 or null.Float)
  • double (sql.NullFloat64 or null.Float)
  • datetime (null.Time)
  • time (null.Time)
  • date (null.Time)
  • timestamp (null.Time)
  • var (sql.String or null.String)
  • enum (sql.String or null.String)
  • varchar (sql.String or null.String)
  • longtext (sql.String or null.String)
  • mediumtext (sql.String or null.String)
  • text (sql.String or null.String)
  • tinytext (sql.String or null.String)
  • binary
  • blob
  • longblob
  • mediumblob
  • varbinary
  • json

主要指標

概覽
名稱與所有者Shelnutt2/db2struct
主編程語言Go
編程語言Go (語言數: 1)
平台Linux, Mac, Windows
許可證GNU General Public License v3.0
所有者活动
創建於2016-06-02 11:07:24
推送於2023-03-06 23:47:57
最后一次提交2023-03-06 18:47:54
發布數3
最新版本名稱v1.0.2 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數550
關注者數18
派生數123
提交數82
已啟用問題?
問題數18
打開的問題數9
拉請求數21
打開的拉請求數3
關閉的拉請求數3
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?