Files
go-zero/tools/goctl/model/sql/README.MD

210 lines
7.0 KiB
Plaintext
Raw Normal View History

2020-08-16 23:28:01 +08:00
# Goctl Model
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
goctl model 为go-zero下的工具模块中的组件之一目前支持识别mysql ddl进行model层代码生成通过命令行或者idea插件即将支持可以有选择地生成带redis cache或者不带redis cache的代码逻辑。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
# 快速开始
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
```
$ goctl model -src ./sql/user.sql -dir ./model -c true
```
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
详情用法请参考[model example](https://github.com/tal-tech/go-zero/tools/goctl/model/sql/example)
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
执行上述命令后即可快速生成CURD代码。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
```
model
│   ├── error.go
│   ├── usercoursemodel.go
│   └── usermodel.go
```
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
> 注意这里的目录结构中有usercoursemodel.go目录在example中我为了体现带cache与不带cache代码的区别因此将sql文件分别使用了独立的sql文件(user.sql&course.sql)在实际项目开发中你可以将ddl建表语句放在一个sql文件中`goctl model`会自动解析并分割最终按照每个ddl建表语句为单位生成独立的go文件。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
* 生成代码示例
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
``` go
package model
2020-07-29 17:11:41 +08:00
import (
2020-08-16 23:28:01 +08:00
"database/sql"
2020-07-29 17:11:41 +08:00
"strings"
"time"
2020-08-16 23:28:01 +08:00
"github.com/tal-tech/go-zero/core/stores/sqlc"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/stores/sqlx"
"github.com/tal-tech/go-zero/core/stringx"
2020-08-16 23:28:01 +08:00
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
2020-07-29 17:11:41 +08:00
)
var (
2020-08-16 23:28:01 +08:00
userCourseFieldNames = builderx.FieldNames(&UserCourse{})
userCourseRows = strings.Join(userCourseFieldNames, ",")
userCourseRowsExpectAutoSet = strings.Join(stringx.Remove(userCourseFieldNames, "id", "create_time", "update_time"), ",")
userCourseRowsWithPlaceHolder = strings.Join(stringx.Remove(userCourseFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
2020-07-29 17:11:41 +08:00
)
type (
2020-08-16 23:28:01 +08:00
UserCourseModel struct {
2020-07-29 17:11:41 +08:00
conn sqlx.SqlConn
table string
}
2020-08-16 23:28:01 +08:00
UserCourse struct {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户id
CourseName string `db:"course_name"` // 课程名称
CreateTime time.Time `db:"create_time"`
UpdateTime time.Time `db:"update_time"`
2020-07-29 17:11:41 +08:00
}
)
2020-08-16 23:28:01 +08:00
func NewUserCourseModel(conn sqlx.SqlConn, table string) *UserCourseModel {
return &UserCourseModel{
conn: conn,
table: table,
2020-07-29 17:11:41 +08:00
}
}
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) Insert(data UserCourse) (sql.Result, error) {
query := `insert into ` + m.table + `(` + userCourseRowsExpectAutoSet + `) value (?, ?)`
return m.conn.Exec(query, data.UserId, data.CourseName)
}
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) FindOne(id int64) (*UserCourse, error) {
query := `select ` + userCourseRows + ` from ` + m.table + ` where id = ? limit 1`
var resp UserCourse
err := m.conn.QueryRow(&resp, query, id)
2020-07-29 17:11:41 +08:00
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) FindOneByUserId(userId int64) (*UserCourse, error) {
var resp UserCourse
query := `select ` + userCourseRows + ` from ` + m.table + ` where user_id limit 1`
err := m.conn.QueryRow(&resp, query, userId)
2020-07-29 17:11:41 +08:00
switch err {
case nil:
return &resp, nil
2020-08-16 23:28:01 +08:00
case sqlc.ErrNotFound:
2020-07-29 17:11:41 +08:00
return nil, ErrNotFound
default:
return nil, err
}
2020-08-16 23:28:01 +08:00
}
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) FindOneByCourseName(courseName string) (*UserCourse, error) {
var resp UserCourse
query := `select ` + userCourseRows + ` from ` + m.table + ` where course_name limit 1`
err := m.conn.QueryRow(&resp, query, courseName)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
2020-07-29 17:11:41 +08:00
return nil, err
}
}
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) Update(data UserCourse) error {
query := `update ` + m.table + ` set ` + userCourseRowsWithPlaceHolder + ` where id = ?`
_, err := m.conn.Exec(query, data.UserId, data.CourseName, data.Id)
return err
2020-07-29 17:11:41 +08:00
}
2020-08-16 23:28:01 +08:00
func (m *UserCourseModel) Delete(id int64) error {
query := `delete from ` + m.table + ` where id = ?`
_, err := m.conn.Exec(query, id)
return err
2020-07-29 17:11:41 +08:00
}
2020-08-16 23:28:01 +08:00
```
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
# 用法
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
```
$ goctl model -h
```
```
NAME:
goctl model - generate model code
USAGE:
goctl model [command options] [arguments...]
OPTIONS:
--src value, -s value the file path of the ddl source file
--dir value, -d value the target dir
--cache, -c generate code with cache [optional]
--idea for idea plugin [optional]
```
# 生成规则
* 默认规则
我们默认用户在建表时会创建createTime、updateTime字段(忽略大小写、下划线命名风格)且默认值均为`CURRENT_TIMESTAMP`而updateTime支持`ON UPDATE CURRENT_TIMESTAMP`,对于这两个字段生成`insert`、`update`时会被移除,不在赋值范畴内,当然,如果你不需要这两个字段那也无大碍。
* 带缓存模式
2020-07-29 17:11:41 +08:00
```
2020-08-16 23:28:01 +08:00
$ goctl model -src {filename} -dir {dir} -cache true
2020-07-29 17:11:41 +08:00
```
2020-08-16 23:28:01 +08:00
目前仅支持redis缓存如果选择带缓存模式即生成的`FindOne(ByXxx)`&`Delete`代码会生成带缓存逻辑的代码目前仅支持单索引字段除全文索引外对于联合索引我们默认认为不需要带缓存且不属于通用型代码因此没有放在代码生成行列如example中user表中的`id`、`name`、`mobile`字段均属于单字段索引。
* 不带缓存模式
```
$ goctl model -src {filename} -dir {dir}
```
or
```
$ goctl model -src {filename} -dir {dir} -cache false
```
生成代码仅基本的CURD结构。
# 缓存
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
对于缓存这一块我选择用一问一答的形式进行罗列。我想这样能够更清晰的描述model中缓存的功能。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
* 缓存会缓存哪些信息?
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
对于主键字段缓存,会缓存整个结构体信息,而对于单索引字段(除全文索引)则缓存主键字段值。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
* 数据有更新(`update`)操作会清空缓存吗?
但仅清空主键缓存的信息why这里就不做详细赘述了。
* 为什么不按照单索引字段生成`updateByXxx`和`deleteByXxx`的代码?
理论上是没任何问题但是我们认为对于model层的数据操作均是以整个结构体为单位包括查询我不建议只查询某部分字段不反对否则我们的缓存就没有意义了。
* 为什么不支持`findPageLimit`、`findAll`这么模式代码生层?
目前我认为除了基本的CURD外其他的代码均属于<i>业务型</i>代码,这个我觉得开发人员根据业务需要进行编写更好。
# QA
* goctl model支持根据数据库连接后选择表生成代码吗
目前暂时不支持,在后面会向这个方向扩展。
2020-07-29 17:11:41 +08:00
2020-08-16 23:28:01 +08:00
* goctl model除了命令行模式支持插件模式吗
很快支持idea插件。