Skip to main content

mysql code generation

Overview

mysql code generation support is generated from sql files and database links and supports generating a cache logic code.

mysql generated code content with golang structure corresponding to the data table, CURD operation methods, cache logic, etc. More detailed database code generated reference goctl model

Task Targets

  1. familiar with goctl generating mysql code use commands to learn about currently supported instructions and features
  2. Preliminary understanding of the format of goctl generating mysql code
  3. Preliminary master build process from sql file to mysql code

Preparing

  1. Complete golang installation
  2. Complete goctl installation

Code Generation

  1. Execute the following instructions to store sample sql files in local user.sql.

    CREATE TABLE user (
    id bigint AUTO_INCREMENT,
    name varchar(255) NULL COMMENT 'The username',
    password varchar(255) NOT NULL DEFAULT '' COMMENT 'The user password',
    mobile varchar(255) NOT NULL DEFAULT '' COMMENT 'The mobile phone number',
    gender char(10) NOT NULL DEFAULT 'male' COMMENT 'gender,male|female|unknown',
    nickname varchar(255) NULL DEFAULT '' COMMENT 'The nickname',
    type tinyint(1) NULL DEFAULT 0 COMMENT 'The user type, 0:normal,1:vip, for test golang keyword',
    create_at timestamp NULL,
    update_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE mobile_index (mobile),
    UNIQUE name_index (name),
    PRIMARY KEY (id)
    ) ENGINE = InnoDB COLLATE utf8mb4_general_ci COMMENT 'user table';
  2. Create workspace and directory

    $ mkdir -p ~/workspace/model/mysql
  3. Move the user.sql file stored above to ~/workspace/model/mysql directory

  4. Generate model code

    $ cd ~/workspace/model/mysql
    $ goctl model mysql ddl --src user.sql --dir .
    Done.

    When you see Done. output is generated successfully, then we see the code content generated:

    # list files in current directory
    $ ls
    user.sql usermodel.go usermodel_gen.go vars.go
    # view directory tree
    $ tree
    .
    ├── user.sql
    ├── usermodel.go
    ├── usermodel_gen.go
    └── vars.go

    0 directories, 4 files
  5. Code View

    CREATE TABLE user (
    id bigint AUTO_INCREMENT,
    name varchar(255) NULL COMMENT 'The username',
    password varchar(255) NOT NULL DEFAULT '' COMMENT 'The user password',
    mobile varchar(255) NOT NULL DEFAULT '' COMMENT 'The mobile phone number',
    gender char(10) NOT NULL DEFAULT 'male' COMMENT 'gender,male|female|unknown',
    nickname varchar(255) NULL DEFAULT '' COMMENT 'The nickname',
    type tinyint(1) NULL DEFAULT 0 COMMENT 'The user type, 0:normal,1:vip, for test golang keyword',
    create_at timestamp NULL,
    update_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE mobile_index (mobile),
    UNIQUE name_index (name),
    PRIMARY KEY (id)
    ) ENGINE = InnoDB COLLATE utf8mb4_general_ci COMMENT 'user table';