1 Star 1 Fork 0

vnaki / gt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

根据struct生成数据表SQL语句

Usage

package main

import (
	"fmt"
	"gt"
)

type Model struct {
	Id        int32  `db:"id,omitempty" gen:"pk,ai"`
	CreatedAt string `db:"created_at"`
}

type ThreeStudentModel struct {
	Model
	Name  string `db:"name" gen:"notnull"`
    Content   string `db:"content" gen:"type:text"`
	Score int    `db:"score" gen:"length:1,decimal:1,default:1,notnull,unsigned"`
}

type TwoStudent struct {
	Model
	Name  string `db:"name" gen:"notnull"`
    Content string `db:"content" gen:"type:text"`
	Score int    `db:"score" gen:"length:1,decimal:1,default:1,notnull,unsigned"`
}

func main() {
	b := gt.New()
	b.SetSchema("stu")

	sql, err := b.Model(ThreeStudentModel{})
	fmt.Println(sql, err)

	sql, err = b.Model(TwoStudent{}, "twostu")
	fmt.Println(sql, err)

	b = gt.New()
	b.SetMode(gt.MYSQL)

	sql, err = b.Model(TwoStudent{})
	fmt.Println(sql, err)
}

result output

-- sqlite
CREATE TABLE 'stu'.'three_student'(
'id' integer PRIMARY KEY AUTOINCREMENT,
'created_at' varchar,
'name' varchar NOT NULL,
'content' text,
'score' bigint NOT NULL DEFAULT 1
);

CREATE TABLE 'stu'.'twostu'(
'id' integer PRIMARY KEY AUTOINCREMENT,
'created_at' varchar,
'name' varchar NOT NULL,
'content' varchar,
'score' bigint NOT NULL DEFAULT 1
);

-- mysql
CREATE TABLE `three_student`(
`id` int PRIMARY KEY AUTO_INCREMENT,
`created_at` varchar,
`name` varchar NOT NULL,
`content` text,
`score` bigint UNSIGNED NOT NULL DEFAULT 1
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;

CREATE TABLE `twostu`(
`id` int PRIMARY KEY AUTO_INCREMENT,
`created_at` varchar,
`name` varchar NOT NULL,
`content` varchar,
`score` bigint UNSIGNED NOT NULL DEFAULT 1
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;

Mode

  • MYSQL
  • SQLITE

Tag db

Corresponding data table column name, Id to id, Content to content

type People struct {
    Id        int32  `db:"id,omitempty" gen:"pk,ai"`
    Content   string `db:"content" gen:"type:text"`
}

Tag gen

属性 默认值 说明
type 原生sql数据类型:char,text,mediumint,timestamp,datetime 等
length 数据长度
decimal 2 浮点类型精度
default 默认值
pk 主键
ai 自增
comment 注释
unsigned 无符号
notnull not null

Integer Data Type

数据库数据类型 范围 无符号范围 数据类型
TINYINT -128〜127 0 〜255 int8/uint8
SMALLINT -32768〜32767 0〜65535 int16/uint16
INT (INTEGER) -2147483648〜2147483647 0〜4294967295 int32/uint32
BIGINT -9223372036854775808〜9223372036854775807 0〜18446744073709551615 int64 int / uint64 uint

String Data Type

string -> varchar

time.Time Data Type

time.Time -> datetime
// int int8 int16 int32 int64 byte rune
// uint uint8 uint16 uint32 uint64 byte rune
// float32 float64
// char varchar text
// datetime timestamp

// TINYINT	-128〜127	0 〜255        int8
// SMALLINT	-32768〜32767	0〜65535   int16
// MEDIUMINT	-8388608〜8388607	0〜16777215
// INT (INTEGER)	-2147483648〜2147483647	0〜4294967295   int32
// BIGINT	-9223372036854775808〜9223372036854775807	0〜18446744073709551615 int64 int
//
MIT License Copyright (c) 2023 vnaki Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Generate create table sql by struct, power! 展开 收起
Go
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/vnaki/gt.git
git@gitee.com:vnaki/gt.git
vnaki
gt
gt
master

搜索帮助