1 Star 1 Fork 0

vnaki / gt

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
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.

About

Generate create table sql by struct, power! expand collapse
Cancel

Releases (1)

All

Contributors

All

Activities

Load More
can not load any more
Go
1
https://gitee.com/vnaki/gt.git
git@gitee.com:vnaki/gt.git
vnaki
gt
gt
master

Search

53164aa7 5694891 3bd8fe86 5694891