GithubHelp home page GithubHelp logo

db2struct's Introduction

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.

NOTE: If you wish to use a unix socket instead of a TCP socket, specify the hostname as unix: then the path to the named socket. For example:

--host unix:/tmp/mysql.sock

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

db2struct's People

Contributors

dependabot[bot] avatar shelnutt2 avatar snagles avatar suzukikiwamu avatar tomontime avatar xiangdong1987 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

db2struct's Issues

Update docs

Cleanup, show usage examples, example output, add guregu null type info.

Reorganize to support multiple DB Types

Right now mysql/mariadb is hardcode. This needs to be reworked so we can support multiple db types.

  • Move mysql specific util to mysql_utils.go file
  • Remove hardcoded mysql commands from main.go
    • Move column select function to mysql_utils.
  • Update readme and comments

Redirect output includes prompts and errors

When piping output to a file
db2struct -h host -u user -p -t whatever... >> ~/test.go
any errors messages, and the "Password:" prompt, are also sent to the file, the latter causing an apparent freeze unless you realise and press something!

You can use -p <password> on the command line which helps but does not solve the underlying issue.

Otherwise, thanks for writing this - it has saved me a heck of a lot of typing!!! Feature request - option to specify "all tables" or a list of them.

Suggestions

Can you update your tool so that it can,

  1. Generate struct for all tables in a database
  2. The struct name should make the first character uppercase

command not found

how to use it? when haved get ths db2struct , i run the command like:
db2struct --host localhost -d test -t test_table --package myGoPackage --struct testTable -p --user testUser
in the gopath , i get the db2struct: command not found

fields out of order

when I run db2struct the original table field order is not respected, is there a way to generate the fields where this original order is respected?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.