gomodel

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 2 Imported by: 1

README

GoModel 是一个生成模型与CRUD相关代码的工具,目前仅支持 MySQL

安装

$ go install github.com/metauro/gomodel/cmd/gomodel@latest

配置

$HOME/.gomodel.toml下配置以下内容

[mysql]
username = "root"
password = "root"
host = "localhost"
port = 3306
database = "db"

创建表

CREATE TABLE IF NOT EXISTS `test`
(
    `id`        INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `create_at` DATETIME    NOT NULL DEFAULT NOW(),
    `update_at` DATETIME    NOT NULL DEFAULT NOW() ON UPDATE NOW(),
    `key`       VARCHAR(64) NOT NULL
);

生成代码

执行以下命令,选择要生成的表后回车

$ gomodel gen

使用

生成完毕后即可使用

package main

import (
	"github.com/metauro/gomodel"
)

func main() {
	db, err := gomodel.Open("mysql", "dsn")
	if err != nil {
		panic(err)
	}
	repo := model.NewTestRepo(db)
	// 插入单条数据
	repo.Insert(&model.Test{
		Key: "a",
	}).MustExec()
	// 插入多条数据
	repo.Insert(&model.Test{
		Key: "a",
	}, &model.Test{
		Key: "b",
	}).MustExec()
	// 更新数据
	repo.Update().SetKey("update").WhereKeyEqual("a").MustExec()
	// 查询一条数据
	repo.Select().WhereKeyEqual("a").MustGet()
	// 查询多条数据
	repo.Select().MustSelect()
	// 删除数据
	repo.Delete().WhereKeyEqual("a").Limit(1).MustExec()
}

Documentation

Index

Constants

View Source
const VERSION = "v0.9.0"

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook added in v0.8.0

type Hook interface {
	Before(info QueryInfo) error
	After(info QueryInfo) error
}

type Op added in v0.8.0

type Op uint
const (
	OpInsert Op = 1 << iota // insert data
	OpUpdate                // update data
	OpSelect                // select data
	OpDelete                // delete data
)

func (Op) String added in v0.8.0

func (i Op) String() string

type QueryInfo added in v0.8.0

type QueryInfo interface {
	Context() context.Context
	SetContext(ctx context.Context)
	Table() string
	SetTable(table string)
	Op() Op
	// Query return sql string
	Query() string
	// Fields return select/update/insert fields
	Fields() []string
	AddField(field string)
	// Args return sql args
	Args() []interface{}
	AddArg(arg interface{})
	// Value return sql execute result
	Value() interface{}
	// Err return sql execute error
	Err() error
}

Directories

Path Synopsis
cli module
cmd
gomodel module
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL