One-line command for mysql to go struct

Githubd address

Chinese Documents | DOCUMENT

okcli can generate the specified data table as a corresponding model file and currently supports bringing column names, data types, default values, comments into the specified model.
Like the following files:

import "time"

type (
    Student struct {
        Id         int32     `db:"id"`
        Name       string    `db:"name"`        // the student's name
        Gender     int8      `db:"gender"`      // the student's gender,0-male,1-female,DEFAULT:0
        Age        int8      `db:"age"`         // the student's age,DEFAULT:0
        Class      string    `db:"class"`       // the student's class
        CreateTime time.Time `db:"create_time"` // the column create time,DEFAULT:CURRENT_TIMESTAMP
        UpdateTime time.Time `db:"update_time"` // the column last update time,DEFAULT:CURRENT_TIMESTAMP
    }
)

Dead work

Download different binaries based on your operating system platform

Or the clone source is compiled.

Second, set the downloaded or compiled binary file into an environment variable, for example, the mac OS:
Suppose the binary okcli file is stored in the / usr/local/okcli file directory

$ sudo vi ~/.bash_profile

Add at the end of the file:

$ export OKCLI=/usr/local/okcli
$ export PATH=$PATH:$OKCLI

Finally, execute the following source ~/.bash_profile

Instructions

Database preparation

Take the local database as an example, create new student, course, student_score tables under the demo library with the following sql:

CREATE TABLE `student` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'the student''s name',
  `gender` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'the student''s gender,0-male,1-female',
  `age` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'the student''s age',
  `class` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'the student''s class',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'the column create time',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'the column last update time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='Student info';

CREATE TABLE `course` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'course name',
  `teacher` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'teach person',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;


CREATE TABLE `student_score` (
  `id` int(20) NOT NULL,
  `student_id` int(20) NOT NULL DEFAULT '0',
  `course_id` int(20) NOT NULL DEFAULT '0',
  `score` tinyint(3) unsigned NOT NULL,
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `student_course_unique` (`student_id`,`course_id`),
  KEY `course_idx` (`course_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

okcli command preparation

Command Line Parameters

$ okcli

NAME:
   okcli - a cli tool to generate model

USAGE:
   okcli [global options] command [command options] [arguments...]

VERSION:
   0.0.1

COMMANDS:
   init     generate the json configuration template
   gen      generated from a configuration file
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version

Generate model

1. Profile generation

$ okcli init

After executing the command, a config.json file is generated in the current directory with the following content templates:

// TODO: FILLED IN YOUR REAL VALUE AND DELETE THIS LINE
{
    "username": "root",
    "password": "123456",
    "host": "localhost:3306",
    "database":{
        "name":"demo",
        "tables":["student","course","student_score"]
    },
    "tagPrefix":"db"
}
  • username:

Database Connection Name

  • password:

Database Connection Password

  • host:

Database Connection Address

  • database:

Database Name and Data Table Name List

  • tagPrefix:

The tag prefix in the model to be formatted, defaulting to "db"

Modify the config.json configuration file to the database address you need to connect to, and fill in the database name and table name of the model to be generated.

2. Execute okcli gen command

$ okcli gen -c ./config.json -o ./model

successful,the path: /Users/anqiansong/go/src/okgo/model/student.go
successful,the path: /Users/anqiansong/go/src/okgo/model/course.go
successful,the path: /Users/anqiansong/go/src/okgo/model/studentscore.go
  • c Specify the profile path
  • o Specifies the model file storage directory, defaulting to the current directory

Executing the above command will generate the corresponding model file from the configuration file and store it in the model folder. If the specified model file already exists, you will be prompted

WARNING:the file [student.go] would be override,Y/N?

3. Generate model File

  • Student
package model

import "time"

type (
    Student struct {
        Id         int32     `gorm:"id"`
        Name       string    `gorm:"name"`        // the student's name
        Gender     int8      `gorm:"gender"`      // the student's gender,0-male,1-female,DEFAULT:0
        Age        int8      `gorm:"age"`         // the student's age,DEFAULT:0
        Class      string    `gorm:"class"`       // the student's class
        CreateTime time.Time `gorm:"create_time"` // the column create time,DEFAULT:CURRENT_TIMESTAMP
        UpdateTime time.Time `gorm:"update_time"` // the column last update time,DEFAULT:CURRENT_TIMESTAMP
    }
)
  • Course
package model

import "time"

type (
    Course struct {
        Id         int32     `gorm:"id"`
        Name       string    `gorm:"name"`    // course name
        Teacher    string    `gorm:"teacher"` // teach person
        CreateTime time.Time `gorm:"create_time"`
        UpdateTime time.Time `gorm:"update_time"`
    }
)
  • StudentScore
package model

import "time"

type (
    StudentScore struct {
        Id         int32     `gorm:"id"`
        StudentId  int32     `gorm:"student_id"`
        CourseId   int32     `gorm:"course_id"`
        Score      int8      `gorm:"score"`
        CreateTime time.Time `gorm:"create_time"`
        UpdateTime time.Time `gorm:"update_time"`
    }
)

Matters needing attention

Code formatting is currently supported after the model file is generated, but automatic package import is not supported at this time, so after the model file is generated, you need to
The package checks for imports.

Ending

If it helps you, please help me click on your little star at github to give me the power to move forward.

Keywords: MySQL Database JSON Mac Windows

Added by blacksmoke26 on Sat, 14 Dec 2019 03:10:29 +0200