db2struct

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2019 License: GPL-3.0 Imports: 12 Imported by: 0

README

db2struct

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

# install null.v3
go get gopkg.in/guregu/null.v3

# install db2struct
go get github.com/hlf513/db2struct/db2struct

# run command
db2struct --host localhost -d test -t test_table --package myGoPackage --struct testTable -p password --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 --user mysqlUser -p password -d database -t table --package example --struct user --guregu --gorm --json --created_at CreatedAt --updated_at UpdatedAt 
# --host 数据库地址
# --user 数据库用户名
# -p     数据库密码
# -t     数据库表
# --package 生成的 go 包名,默认是 model
# --struct 生成的 go 结构名,默认是 表名
# --guregu 使用 guregu 的 null 包
# --gorm  添加 gorm 标签,以及常用方法
# --json  添加 json 标签
# --created_at 创建时间字段
# --updated_at 更新事件字段

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug level logging

Functions

func FmtFieldName

func FmtFieldName(s string) string

func Generate

func Generate(columnTypes map[string]map[string]string, tableName string, structName string, pkgName string, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool, createdKey, updatedKey string) ([]byte, error)

写入不同目录的文件中(分层)

func GenerateOne

func GenerateOne(columnTypes map[string]map[string]string, tableName string, structName string, pkgName string, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool, createdKey, updatedKey string) ([]byte, error)

Generate Given a Column map with datatypes and a name structName, attempts to generate a struct definition 写入一个文件

func GetColumnsFromMysqlTable

func GetColumnsFromMysqlTable(mariadbUser string, mariadbPassword string, mariadbHost string, mariadbPort int, mariadbDatabase string, mariadbTable string) (*map[string]map[string]string, error)

GetColumnsFromMysqlTable Select column details from information schema and return map of map

func Lcfirst

func Lcfirst(str string) string

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL