mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-08 07:30:01 +08:00
optimize unit test & add document
This commit is contained in:
@@ -1,182 +1,80 @@
|
||||
<div style="text-align: center;"><h1>Sql生成工具说明文档</h1></div>
|
||||
# Goctl Model
|
||||
|
||||
<h2>前言</h2>
|
||||
在当前Sql代码生成工具是基于sqlc生成的逻辑。
|
||||
goctl model 为go-zero下的工具模块中的组件之一,目前支持识别mysql ddl进行model层代码生成,通过命令行或者idea插件(即将支持)可以有选择地生成带redis cache或者不带redis cache的代码逻辑。
|
||||
|
||||
<h2>关键字</h2>
|
||||
# 快速开始
|
||||
|
||||
+ 查询类型(前暂不支持同一字段多种类型混合生成,如按照campus_id查询单结果又查询All或者Limit)
|
||||
- 单结果查询
|
||||
- FindOne(主键特有)
|
||||
- FindOneByXxx
|
||||
- 多结果查询
|
||||
- FindAllByXxx
|
||||
- FindLimitByXxx
|
||||
- withCache
|
||||
- withoutCache
|
||||
```
|
||||
$ goctl model -src ./sql/user.sql -dir ./model -c true
|
||||
```
|
||||
|
||||
<h2>准备工作</h2>
|
||||
详情用法请参考[model example](https://github.com/tal-tech/go-zero/tools/goctl/model/sql/example)
|
||||
|
||||
- table
|
||||
执行上述命令后即可快速生成CURD代码。
|
||||
|
||||
```
|
||||
CREATE TABLE `user_info` (
|
||||
`id` bigint(20) NOT NULL COMMENT '主键',
|
||||
`campus_id` bigint(20) DEFAULT NULL COMMENT '整校id',
|
||||
`name` varchar(255) DEFAULT NULL COMMENT '用户姓名',
|
||||
`id_number` varchar(255) DEFAULT NULL COMMENT '身份证',
|
||||
`age` int(10) DEFAULT NULL COMMENT '年龄',
|
||||
`gender` tinyint(1) DEFAULT NULL COMMENT '性别,0-男,1-女,2-不限',
|
||||
`mobile` varchar(20) DEFAULT NULL COMMENT '手机号',
|
||||
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
```
|
||||
```
|
||||
model
|
||||
│ ├── error.go
|
||||
│ ├── usercoursemodel.go
|
||||
│ └── usermodel.go
|
||||
```
|
||||
|
||||
<h2>imports生成</h2>
|
||||
imports代码生成对应model中包的引入管理,仅使用于晓黑板项目中(非相对路径动态生成),目前受`withCache`参数的影响,除此之外其实为固定代码。
|
||||
> 注意:这里的目录结构中有usercoursemodel.go目录,在example中我为了体现带cache与不带cache代码的区别,因此将sql文件分别使用了独立的sql文件(user.sql&course.sql),在实际项目开发中你可以将ddl建表语句放在一个sql文件中,`goctl model`会自动解析并分割,最终按照每个ddl建表语句为单位生成独立的go文件。
|
||||
|
||||
- withCache
|
||||
* 生成代码示例
|
||||
|
||||
```
|
||||
``` go
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql""fmt"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"xiao/service/shared/builderx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
```
|
||||
|
||||
- withoutCache
|
||||
|
||||
```
|
||||
import (
|
||||
"database/sql""fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"xiao/service/shared/builderx"
|
||||
)
|
||||
```
|
||||
|
||||
<h2>vars生成</h2>
|
||||
|
||||
vars部分对应model中var声明的包含的代码块,由`table`名和`withCache`来决定其中的代码生成内容,`withCache`决定是否要生成缓存key变量的声明。
|
||||
|
||||
- withCache
|
||||
|
||||
```
|
||||
var (
|
||||
UserInfoFieldNames = builderx.FieldNames(&UserInfo{})
|
||||
UserInfoRows = strings.Join(UserInfoFieldNames, ",")
|
||||
UserInfoRowsExpectAutoSet = strings.Join(stringx.Remove(UserInfoFieldNames, "id", "create_time", "update_time"), ",")
|
||||
UserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(UserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
cacheUserInfoIdPrefix = "cache#userInfo#id#"
|
||||
cacheUserInfoCampusIdPrefix = "cache#userInfo#campusId#"
|
||||
cacheUserInfoNamePrefix = "cache#userInfo#name#"
|
||||
cacheUserInfoMobilePrefix = "cache#userInfo#mobile#"
|
||||
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"), "=?,") + "=?"
|
||||
)
|
||||
```
|
||||
|
||||
- withoutCache
|
||||
|
||||
```
|
||||
var (
|
||||
UserInfoFieldNames = builderx.FieldNames(&UserInfo{})
|
||||
UserInfoRows = strings.Join(UserInfoFieldNames, ",")
|
||||
UserInfoRowsExpectAutoSet = strings.Join(stringx.Remove(UserInfoFieldNames, "id", "create_time", "update_time"), ",")
|
||||
UserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(UserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
)
|
||||
```
|
||||
|
||||
<h2>types生成</h2>
|
||||
|
||||
ypes部分对应model中type声明的包含的代码块,由`table`名和`withCache`来决定其中的代码生成内容,`withCache`决定引入sqlc还是sqlx。
|
||||
|
||||
- withCache
|
||||
```
|
||||
type (
|
||||
UserInfoModel struct {
|
||||
conn sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserInfo struct {
|
||||
Id int64 `db:"id"` // 主键id
|
||||
CampusId int64 `db:"campus_id"` // 整校id
|
||||
Name string `db:"name"` // 用户姓名
|
||||
IdNumber string `db:"id_number"` // 身份证
|
||||
Age int64 `db:"age"` // 年龄
|
||||
Gender int64 `db:"gender"` // 性别,0-男,1-女,2-不限
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- withoutCache
|
||||
```
|
||||
type (
|
||||
UserInfoModel struct {
|
||||
UserCourseModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserInfo struct {
|
||||
Id int64 `db:"id"` // 主键id
|
||||
CampusId int64 `db:"campus_id"` // 整校id
|
||||
Name string `db:"name"` // 用户姓名
|
||||
IdNumber string `db:"id_number"` // 身份证
|
||||
Age int64 `db:"age"` // 年龄
|
||||
Gender int64 `db:"gender"` // 性别,0-男,1-女,2-不限
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
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"`
|
||||
}
|
||||
)
|
||||
```
|
||||
<h2>New生成</h2>
|
||||
new生成对应model中struct的New函数,受`withCache`影响决定是否要引入cacheRedis
|
||||
|
||||
- withCache
|
||||
```
|
||||
func NewUserInfoModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserInfoModel {
|
||||
return &UserInfoModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
func NewUserCourseModel(conn sqlx.SqlConn, table string) *UserCourseModel {
|
||||
return &UserCourseModel{
|
||||
conn: conn,
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
```
|
||||
- withoutCache
|
||||
```
|
||||
func NewUserInfoModel(conn sqlx.SqlConn, table string) *UserInfoModel {
|
||||
return &UserInfoModel{conn: conn, table: table}
|
||||
|
||||
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)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<h2>FindOne查询生成</h2>
|
||||
FindOne查询代码生成仅对主键有效。如`user_info`中生成的FindOne如下:
|
||||
|
||||
- withCache
|
||||
|
||||
```
|
||||
func (m *UserInfoModel) FindOne(id int64) (*UserInfo, error) {
|
||||
idKey := fmt.Sprintf("%s%v", cacheUserInfoIdPrefix, id)
|
||||
var resp UserInfo
|
||||
err := m.QueryRow(&resp, idKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
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)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
@@ -186,245 +84,126 @@ FindOne查询代码生成仅对主键有效。如`user_info`中生成的FindOne
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- withoutCache
|
||||
|
||||
```
|
||||
func (m *UserInfoModel) FindOne(id int64) (*UserInfo, error) {
|
||||
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where id = ? limit 1`
|
||||
var resp UserInfo
|
||||
err := m.conn.QueryRow(&resp, query, id)
|
||||
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)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
<h2>FindOneByXxx查询生成</h2>
|
||||
|
||||
FindOneByXxx查询生成可以按照单个字段查询、多个字段以AND关系且表达式符号为`=`的查询(下称:组合查询),对除主键之外的字段有效,对于单个字段可以用`withCache`来控制是否需要缓存,这里的缓存只缓存主键,并不缓存整个struct,注意:这里有一个隐藏的规则,如果单个字段查询需要cache,那么主键一定有cache;多个字段组成的`组合查询`一律没有缓存处理,<strong><i>且组合查询不能相互嵌套</i></strong>,否则会报`circle query with other fields`错误,下面我们按场景来依次查看对应代码生成后的示例。
|
||||
|
||||
>注:目前暂不支持除equals之外的条件查询。
|
||||
|
||||
+ 单字段查询
|
||||
以name查询为例
|
||||
- withCache
|
||||
```
|
||||
func (m *UserInfoModel) FindOneByName(name string) (*UserInfo, error) {
|
||||
nameKey := fmt.Sprintf("%s%v", cacheUserInfoNamePrefix, name)
|
||||
var id string
|
||||
err := m.GetCache(key, &id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if id != "" {
|
||||
return m.FindOne(id)
|
||||
}
|
||||
var resp UserInfo
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where name = ? limit 1`
|
||||
err = m.QueryRowNoCache(&resp, query, name)
|
||||
switch err {
|
||||
case nil:
|
||||
err = m.SetCache(nameKey, resp.Id)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
```
|
||||
- withoutCache
|
||||
|
||||
```
|
||||
func (m *UserInfoModel) FindOneByName(name string) (*UserInfo, error) {
|
||||
var resp UserInfo
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where name = ? limit 1`
|
||||
err = m.conn.QueryRow(&resp, query, name)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- 组合查询
|
||||
以`campus_id`和`id_number`查询为例。
|
||||
|
||||
```
|
||||
func (m *UserInfoModel) FindOneByCampusIdAndIdNumber(campusId int64,idNumber string) (*UserInfo, error) {
|
||||
var resp UserInfo
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where campus_id = ? AND id_number = ? limit 1`
|
||||
err = m.QueryRowNoCache(&resp, query, campusId, idNumber)
|
||||
// err = m.conn.QueryRows(&resp, query, campusId, idNumber)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
```
|
||||
<h2>FindAllByXxx生成</h2>
|
||||
FindAllByXxx查询和FindOneByXxx功能相似,只是FindOneByXxx限制了limit等于1,而FindAllByXxx是查询所有,以两个例子来说明
|
||||
|
||||
- 查询单个字段`name`等于某值的所有数据
|
||||
```
|
||||
func (m *UserInfoModel) FindAllByName(name string) ([]*UserInfo, error) {
|
||||
var resp []*UserInfo
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where name = ?`
|
||||
err := m.QueryRowsNoCache(&resp, query, name)
|
||||
// err := m.conn.QueryRows(&resp, query, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
```
|
||||
- 查询多个组合字段`campus_id`等于某值且`gender`等于某值的所有数据
|
||||
```
|
||||
func (m *UserInfoModel) FindAllByCampusIdAndGender(campusId int64,gender int64) ([]*UserInfo, error) {
|
||||
var resp []*UserInfo
|
||||
query := `select ` + userInfoRows + ` from ` + m.table + `where campus_id = ? AND gender = ?`
|
||||
err := m.QueryRowsNoCache(&resp, query, campusId, gender)
|
||||
// err := m.conn.QueryRows(&resp, query, campusId, gender)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
```
|
||||
|
||||
<h2>FindLimitByXxx生成</h2>
|
||||
FindLimitByXxx查询和FindAllByXxx功能相似,只是FindAllByXxx限制了limit,除此之外还会生成查询对应Count总数的代码,而FindAllByXxx是查询所有数据,以几个例子来说明
|
||||
|
||||
- 查询`gender`等于某值的分页数据,按照`create_time`降序
|
||||
```
|
||||
func (m *UserInfoModel) FindLimitByGender(gender int64, page, limit int) ([]*UserInfo, error) {
|
||||
var resp []*UserInfo
|
||||
query := `select ` + userInfoRows + `from ` + m.table + `where gender = ? order by create_time DESC limit ?,?`
|
||||
err := m.QueryRowsNoCache(&resp, query, gender, (page-1)*limit, limit)
|
||||
// err := m.conn.QueryRows(&resp, query, gender, (page-1)*limit, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (m *UserInfoModel) FindAllCountByGender(gender int64) (int64, error) {
|
||||
var count int64
|
||||
query := `select count(1) from ` + m.table + `where gender = ? `
|
||||
err := m.QueryRowsNoCache(&count, query, gender)
|
||||
// err := m.conn.QueryRow(&count, query, gender)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
```
|
||||
- 查询`gender`等于某值的分页数据,按照`create_time`降序、`update_time`生序排序
|
||||
```
|
||||
func (m *UserInfoModel) FindLimitByGender(gender int64, page, limit int) ([]*UserInfo, error) {
|
||||
var resp []*UserInfo
|
||||
query := `select ` + userInfoRows + `from ` + m.table + `where gender = ? order by create_time DESC,update_time ASC limit ?,?`
|
||||
err := m.QueryRowsNoCache(&resp, query, gender, (page-1)*limit, limit)
|
||||
// err := m.conn.QueryRows(&resp, query, gender, (page-1)*limit, limit)
|
||||
if err != nil {
|
||||
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:
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (m *UserInfoModel) FindAllCountByGender(gender int64) (int64, error) {
|
||||
var count int64
|
||||
query := `select count(1) from ` + m.table + `where gender = ? `
|
||||
err := m.QueryRowNoCache(&count, query, gender)
|
||||
// err := m.conn.QueryRow(&count, query, gender)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
```
|
||||
- 查询`gender`等于某值且`campus_id`为某值按照`create_time`降序的分页数据
|
||||
```
|
||||
func (m *UserInfoModel) FindLimitByGenderAndCampusId(gender int64,campusId int64, page, limit int) ([]*UserInfo, error) {
|
||||
var resp []*UserInfo
|
||||
query := `select ` + userInfoRows + `from ` + m.table + `where gender = ? AND campus_id = ? order by create_time DESC limit ?,?`
|
||||
err := m.QueryRowsNoCache(&resp, query, gender, campusId, (page-1)*limit, limit)
|
||||
// err := m.conn.QueryRows(&resp, query, gender, campusId, (page-1)*limit, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (m *UserInfoModel) FindAllCountByGenderAndCampusId(gender int64,campusId int64) (int64, error) {
|
||||
var count int64
|
||||
query := `select count(1) from ` + m.table + `where gender = ? AND campus_id = ? `
|
||||
err := m.QueryRowsNoCache(&count, query, gender, campusId)
|
||||
// err := m.conn.QueryRow(&count, query, gender, campusId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
```
|
||||
|
||||
<h2>Delete生成</h2>
|
||||
Delete代码根据`withCache`的不同可以生成带缓存逻辑代码和不带缓存逻辑代码,<strong><i>Delete代码生成仅按照主键删除</i></strong>。从FindOneByXxx方法描述得知,非主键`withCache`了那么主键会强制被cache,因此在delete时也会删除主键cache。
|
||||
|
||||
- withCache
|
||||
根据`mobile`查询用户信息
|
||||
|
||||
```
|
||||
func (m *UserInfoModel) Delete(userId int64) error {
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUserInfoUserIdPrefix, userId)
|
||||
mobileKey := fmt.Sprintf("%s%v", cacheUserInfoMobilePrefix, mobile)
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + + `where user_id = ?`
|
||||
return conn.Exec(query, userId)
|
||||
}, userIdKey, mobileKey)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCourseModel) Delete(id int64) error {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
_, err := m.conn.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
# 用法
|
||||
|
||||
```
|
||||
$ 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`时会被移除,不在赋值范畴内,当然,如果你不需要这两个字段那也无大碍。
|
||||
* 带缓存模式
|
||||
|
||||
```
|
||||
- withoutCache
|
||||
$ goctl model -src {filename} -dir {dir} -cache true
|
||||
```
|
||||
func (m *UserInfoModel) Delete(userId int64) error {
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + + `where user_id = ?`
|
||||
return conn.Exec(query, userId)
|
||||
}, )
|
||||
return err
|
||||
}
|
||||
```
|
||||
<h2>Insert生成</h2>
|
||||
|
||||
目前仅支持redis缓存,如果选择带缓存模式,即生成的`FindOne(ByXxx)`&`Delete`代码会生成带缓存逻辑的代码,目前仅支持单索引字段(除全文索引外),对于联合索引我们默认认为不需要带缓存,且不属于通用型代码,因此没有放在代码生成行列,如example中user表中的`id`、`name`、`mobile`字段均属于单字段索引。
|
||||
|
||||
<h2>Update生成</h2>
|
||||
* 不带缓存模式
|
||||
|
||||
```
|
||||
$ goctl model -src {filename} -dir {dir}
|
||||
```
|
||||
or
|
||||
```
|
||||
$ goctl model -src {filename} -dir {dir} -cache false
|
||||
```
|
||||
生成代码仅基本的CURD结构。
|
||||
|
||||
<h2>待完善(TODO)</h2>
|
||||
# 缓存
|
||||
|
||||
- 同一字段多种查询方式代码生成(优先级较高)
|
||||
- 条件查询
|
||||
- 范围查询
|
||||
- ...
|
||||
对于缓存这一块我选择用一问一答的形式进行罗列。我想这样能够更清晰的描述model中缓存的功能。
|
||||
|
||||
<h2>反馈与建议</h2>
|
||||
* 缓存会缓存哪些信息?
|
||||
|
||||
- 无
|
||||
对于主键字段缓存,会缓存整个结构体信息,而对于单索引字段(除全文索引)则缓存主键字段值。
|
||||
|
||||
* 数据有更新(`update`)操作会清空缓存吗?
|
||||
|
||||
会,但仅清空主键缓存的信息,why?这里就不做详细赘述了。
|
||||
|
||||
* 为什么不按照单索引字段生成`updateByXxx`和`deleteByXxx`的代码?
|
||||
|
||||
理论上是没任何问题,但是我们认为,对于model层的数据操作均是以整个结构体为单位,包括查询,我不建议只查询某部分字段(不反对),否则我们的缓存就没有意义了。
|
||||
|
||||
* 为什么不支持`findPageLimit`、`findAll`这么模式代码生层?
|
||||
|
||||
目前,我认为除了基本的CURD外,其他的代码均属于<i>业务型</i>代码,这个我觉得开发人员根据业务需要进行编写更好。
|
||||
|
||||
# QA
|
||||
|
||||
* goctl model支持根据数据库连接后选择表生成代码吗?
|
||||
|
||||
目前暂时不支持,在后面会向这个方向扩展。
|
||||
|
||||
* goctl model除了命令行模式,支持插件模式吗?
|
||||
|
||||
很快支持idea插件。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package command
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -9,6 +10,17 @@ func Mysql(ctx *cli.Context) error {
|
||||
src := ctx.String("src")
|
||||
dir := ctx.String("dir")
|
||||
cache := ctx.Bool("cache")
|
||||
generator := gen.NewDefaultGenerator(src, dir)
|
||||
return generator.Start(cache)
|
||||
idea := ctx.Bool("idea")
|
||||
var log console.Console
|
||||
if idea {
|
||||
log = console.NewIdeaConsole()
|
||||
} else {
|
||||
log = console.NewColorConsole()
|
||||
}
|
||||
generator := gen.NewDefaultGenerator(src, dir, gen.WithConsoleOption(log))
|
||||
err := generator.Start(cache)
|
||||
if err != nil {
|
||||
log.Error("%v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
22
tools/goctl/model/sql/example/config/config.go
Normal file
22
tools/goctl/model/sql/example/config/config.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/redis"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
logx.LogConf
|
||||
Mysql struct {
|
||||
DataSource string
|
||||
Table struct {
|
||||
User string
|
||||
Course string
|
||||
}
|
||||
}
|
||||
CacheRedis cache.CacheConf
|
||||
Redis redis.RedisConf
|
||||
}
|
||||
)
|
||||
23
tools/goctl/model/sql/example/etc/config.json
Normal file
23
tools/goctl/model/sql/example/etc/config.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"ServiceName": "model.test",
|
||||
"Mode": "console",
|
||||
"Mysql": {
|
||||
"DataSource": "root:AQS910422@(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai",
|
||||
"Table": {
|
||||
"User": "user",
|
||||
"Course": "user_course"
|
||||
}
|
||||
},
|
||||
"CacheRedis": [
|
||||
{
|
||||
"Host": "127.0.0.1:6379",
|
||||
"Type": "node",
|
||||
"Pass": ""
|
||||
}
|
||||
],
|
||||
"Redis": {
|
||||
"Host": "127.0.0.1:6379",
|
||||
"Type": "node",
|
||||
"Pass": ""
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,7 @@
|
||||
#!/bin/bash
|
||||
go run .
|
||||
|
||||
# generate usermodel with cache
|
||||
goctl model -src ./sql/user.sql -dir ./model -c true
|
||||
|
||||
# generate usercoursemodel without cache
|
||||
goctl model -src ./sql/course.sql -dir ./model
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||
)
|
||||
|
||||
// go run .
|
||||
func main() {
|
||||
// generating with cache
|
||||
withCacheGenerator := gen.NewDefaultGenerator("./test.sql", "./withcachemodel")
|
||||
lang.Must(withCacheGenerator.Start(true))
|
||||
|
||||
// generating without cache
|
||||
withoutGenerator := gen.NewDefaultGenerator("./test.sql", "./withoutcachemodel")
|
||||
lang.Must(withoutGenerator.Start(false))
|
||||
}
|
||||
100
tools/goctl/model/sql/example/model/usercoursemodel.go
Executable file
100
tools/goctl/model/sql/example/model/usercoursemodel.go
Executable file
@@ -0,0 +1,100 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
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"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
UserCourseModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserCourseModel(conn sqlx.SqlConn, table string) *UserCourseModel {
|
||||
return &UserCourseModel{
|
||||
conn: conn,
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (m *UserCourseModel) Delete(id int64) error {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
_, err := m.conn.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
146
tools/goctl/model/sql/example/model/usermodel.go
Executable file
146
tools/goctl/model/sql/example/model/usermodel.go
Executable file
@@ -0,0 +1,146 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userFieldNames = builderx.FieldNames(&User{})
|
||||
userRows = strings.Join(userFieldNames, ",")
|
||||
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
|
||||
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
cacheUserMobilePrefix = "cache#User#mobile#"
|
||||
cacheUserIdPrefix = "cache#User#id#"
|
||||
cacheUserNamePrefix = "cache#User#name#"
|
||||
)
|
||||
|
||||
type (
|
||||
UserModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
User struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"` // 用户名称
|
||||
Password string `db:"password"` // 用户密码
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
Gender string `db:"gender"` // 男|女|未公开
|
||||
Nickname string `db:"nickname"` // 用户昵称
|
||||
CreateTime time.Time `db:"create_time"`
|
||||
UpdateTime time.Time `db:"update_time"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserModel {
|
||||
return &UserModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) Insert(data User) (sql.Result, error) {
|
||||
query := `insert into ` + m.table + `(` + userRowsExpectAutoSet + `) value (?, ?, ?, ?, ?)`
|
||||
return m.ExecNoCache(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
||||
}
|
||||
|
||||
func (m *UserModel) FindOne(id int64) (*User, error) {
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
||||
var resp User
|
||||
err := m.QueryRow(&resp, userIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := `select ` + userRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) FindOneByName(name string) (*User, error) {
|
||||
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, name)
|
||||
var resp User
|
||||
err := m.QueryRowIndex(&resp, userNameKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userRows + ` from ` + m.table + ` where name = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) FindOneByMobile(mobile string) (*User, error) {
|
||||
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)
|
||||
var resp User
|
||||
err := m.QueryRowIndex(&resp, userMobileKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userRows + ` from ` + m.table + ` where mobile = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, mobile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) Update(data User) error {
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `update ` + m.table + ` set ` + userRowsWithPlaceHolder + ` where id = ?`
|
||||
return conn.Exec(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
||||
}, userIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserModel) Delete(id int64) error {
|
||||
data, err := m.FindOne(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
||||
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, data.Name)
|
||||
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)
|
||||
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
return conn.Exec(query, id)
|
||||
}, userIdKey, userNameKey, userMobileKey)
|
||||
return err
|
||||
}
|
||||
164
tools/goctl/model/sql/example/model_test.go
Normal file
164
tools/goctl/model/sql/example/model_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package example
|
||||
|
||||
// todo: In order to pass the go test on github,
|
||||
// todo: the code is commented. If you need go test,
|
||||
// todo: modify the configuration file and delete the comment and execute it.
|
||||
|
||||
//import (
|
||||
// "flag"
|
||||
// "fmt"
|
||||
// "testing"
|
||||
//
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// "github.com/tal-tech/go-zero/core/conf"
|
||||
// "github.com/tal-tech/go-zero/core/jsonx"
|
||||
// "github.com/tal-tech/go-zero/core/logx"
|
||||
// "github.com/tal-tech/go-zero/core/stores/redis"
|
||||
// "github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
// "github.com/tal-tech/go-zero/tools/goctl/model/sql/example/config"
|
||||
// "github.com/tal-tech/go-zero/tools/goctl/model/sql/example/model"
|
||||
//)
|
||||
//
|
||||
//var (
|
||||
// userModel *model.UserModel
|
||||
// courseModel *model.UserCourseModel
|
||||
// // verify redis cache
|
||||
// r *redis.Redis
|
||||
//)
|
||||
//var configFile = flag.String("f", "etc/config.json", "the config file")
|
||||
//
|
||||
//func TestMain(m *testing.M) {
|
||||
// flag.Parse()
|
||||
// var c config.Config
|
||||
// conf.MustLoad(*configFile, &c)
|
||||
//
|
||||
// logx.MustSetup(c.LogConf)
|
||||
// conn := sqlx.NewMysql(c.Mysql.DataSource)
|
||||
// if conn == nil {
|
||||
// return
|
||||
// }
|
||||
// userModel = model.NewUserModel(conn, c.CacheRedis, c.Mysql.Table.User)
|
||||
// courseModel = model.NewUserCourseModel(conn, c.Mysql.Table.Course)
|
||||
// r = redis.NewRedis(c.Redis.Host, c.Redis.Type, c.Redis.Pass)
|
||||
// if userModel == nil || courseModel == nil || r == nil {
|
||||
// return
|
||||
// }
|
||||
// m.Run()
|
||||
//}
|
||||
//
|
||||
//// cache model
|
||||
//func TestUser(t *testing.T) {
|
||||
// var user model.User
|
||||
// user.Name = "test"
|
||||
// user.Password = "123456"
|
||||
// user.Mobile = "136****0001"
|
||||
// user.Gender = "男"
|
||||
// user.Nickname = "Keson"
|
||||
// insert, err := userModel.Insert(user)
|
||||
// assert.Nil(t, err)
|
||||
// id, err := insert.LastInsertId()
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
// // select
|
||||
// ret, err := userModel.FindOneByName("test")
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, user.Mobile, ret.Mobile)
|
||||
//
|
||||
// // should cache
|
||||
// // expected primary key
|
||||
// var redisId int64
|
||||
// err = get(&redisId, "cache#User#name#test")
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, ret.Id, redisId)
|
||||
//
|
||||
// //expected user from cache
|
||||
// var redisData model.User
|
||||
// err = get(&redisData, fmt.Sprintf("cache#User#id#%v", redisId))
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, redisData.Nickname, user.Nickname)
|
||||
//
|
||||
// // update
|
||||
// ret.Nickname = "Keson after"
|
||||
// err = userModel.Update(*ret)
|
||||
// assert.Nil(t, err)
|
||||
// // expected cache delete
|
||||
// exist, err := r.Exists(fmt.Sprintf("cache#User#id#%v", ret.Id))
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, false, exist)
|
||||
//
|
||||
// // select
|
||||
// ret, err = userModel.FindOne(id)
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, "Keson after", ret.Nickname)
|
||||
//
|
||||
// // delete
|
||||
// err = userModel.Delete(ret.Id)
|
||||
// assert.Nil(t, err)
|
||||
// exist, err = r.Exists(fmt.Sprintf("cache#User#id#%v", ret.Id))
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, false, exist)
|
||||
//
|
||||
// // verify
|
||||
// _, err = userModel.FindOne(id)
|
||||
// assert.Equal(t, model.ErrNotFound, err)
|
||||
//
|
||||
//}
|
||||
//
|
||||
//// no cache model
|
||||
//func TestCourse(t *testing.T) {
|
||||
// // new user
|
||||
// insert, err := userModel.Insert(model.User{
|
||||
// Name: "courseUser",
|
||||
// Password: "123456",
|
||||
// Mobile: "136****1111",
|
||||
// Gender: "男",
|
||||
// Nickname: "Keson",
|
||||
// })
|
||||
// assert.Nil(t, err)
|
||||
// id, err := insert.LastInsertId()
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
// var course model.UserCourse
|
||||
// course.Id = id
|
||||
// course.CourseName = "Java"
|
||||
//
|
||||
// courseInsert, err := courseModel.Insert(course)
|
||||
// assert.Nil(t, err)
|
||||
// courseId, err := courseInsert.LastInsertId()
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
// // select
|
||||
// ret, err := courseModel.FindOne(courseId)
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, course.CourseName, ret.CourseName)
|
||||
//
|
||||
// // update
|
||||
// ret.CourseName = "Golang"
|
||||
// err = courseModel.Update(*ret)
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
// // select
|
||||
// ret, err = courseModel.FindOne(courseId)
|
||||
// assert.Nil(t, err)
|
||||
// assert.Equal(t, "Golang", ret.CourseName)
|
||||
//
|
||||
// // delete
|
||||
// err = courseModel.Delete(ret.Id)
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
// // verify
|
||||
// _, err = courseModel.FindOne(courseId)
|
||||
// assert.Equal(t, model.ErrNotFound, err)
|
||||
//
|
||||
// // clean user
|
||||
// err = userModel.Delete(id)
|
||||
// assert.Nil(t, err)
|
||||
//}
|
||||
//
|
||||
//func get(data interface{}, key string) error {
|
||||
// v, err := r.Get(key)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return jsonx.Unmarshal([]byte(v), data)
|
||||
//}
|
||||
11
tools/goctl/model/sql/example/sql/course.sql
Normal file
11
tools/goctl/model/sql/example/sql/course.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- 用户选修课 --
|
||||
CREATE TABLE `user_course` (
|
||||
`id` bigint(10) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` bigint(10) NOT NULL DEFAULT '0' COMMENT '用户id',
|
||||
`course_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '课程名称',
|
||||
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `user_id_unique` (`user_id`),
|
||||
KEY `course_name_index` (`course_name`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
15
tools/goctl/model/sql/example/sql/user.sql
Normal file
15
tools/goctl/model/sql/example/sql/user.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
-- 用户表 --
|
||||
CREATE TABLE `user` (
|
||||
`id` bigint(10) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',
|
||||
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',
|
||||
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
|
||||
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',
|
||||
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
|
||||
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `name_index` (`name`),
|
||||
KEY `mobile_index` (`mobile`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
4
tools/goctl/model/sql/example/test.sh
Normal file
4
tools/goctl/model/sql/example/test.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd .
|
||||
go test -v
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
-- user_snake
|
||||
CREATE TABLE `user_snake` (
|
||||
`id` bigint(10) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',
|
||||
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',
|
||||
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
|
||||
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',
|
||||
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
|
||||
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `name_index` (`name`),
|
||||
KEY `mobile_index` (`mobile`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
-- userCamel (disable AUTO_INCREMENT)
|
||||
CREATE TABLE `userCamel` (
|
||||
`id` bigint(10) NOT NULL,
|
||||
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',
|
||||
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',
|
||||
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
|
||||
`gender` char(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',
|
||||
`nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
|
||||
`createTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updateTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `nameIndex` (`name`),
|
||||
KEY `mobile_index` (`mobile`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
@@ -1,147 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userCamelFieldNames = builderx.FieldNames(&UserCamel{})
|
||||
userCamelRows = strings.Join(userCamelFieldNames, ",")
|
||||
userCamelRowsExpectAutoSet = strings.Join(stringx.Remove(userCamelFieldNames, "create_time", "update_time"), ",")
|
||||
userCamelRowsWithPlaceHolder = strings.Join(stringx.Remove(userCamelFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
cacheUserCamelIdPrefix = "cache#UserCamel#id#"
|
||||
cacheUserCamelNamePrefix = "cache#UserCamel#name#"
|
||||
cacheUserCamelMobilePrefix = "cache#UserCamel#mobile#"
|
||||
)
|
||||
|
||||
type (
|
||||
UserCamelModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserCamel struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"` // 用户名称
|
||||
Password string `db:"password"` // 用户密码
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
Gender string `db:"gender"` // 男|女|未公开
|
||||
Nickname string `db:"nickname"` // 用户昵称
|
||||
CreateTime time.Time `db:"createTime"`
|
||||
UpdateTime time.Time `db:"updateTime"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserCamelModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserCamelModel {
|
||||
return &UserCamelModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Insert(data UserCamel) error {
|
||||
query := `insert into ` + m.table + `(` + userCamelRowsExpectAutoSet + `) value (?, ?, ?, ?, ?, ?)`
|
||||
_, err := m.ExecNoCache(query, data.Id, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOne(id int64) (*UserCamel, error) {
|
||||
userCamelIdKey := fmt.Sprintf("%s%v", cacheUserCamelIdPrefix, id)
|
||||
var resp UserCamel
|
||||
err := m.QueryRow(&resp, userCamelIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOneByName(name string) (*UserCamel, error) {
|
||||
userCamelNameKey := fmt.Sprintf("%s%v", cacheUserCamelNamePrefix, name)
|
||||
var resp UserCamel
|
||||
err := m.QueryRowIndex(&resp, userCamelNameKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserCamelIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where name = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOneByMobile(mobile string) (*UserCamel, error) {
|
||||
userCamelMobileKey := fmt.Sprintf("%s%v", cacheUserCamelMobilePrefix, mobile)
|
||||
var resp UserCamel
|
||||
err := m.QueryRowIndex(&resp, userCamelMobileKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserCamelIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where mobile = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, mobile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Update(data UserCamel) error {
|
||||
userCamelIdKey := fmt.Sprintf("%s%v", cacheUserCamelIdPrefix, data.Id)
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `update ` + m.table + ` set ` + userCamelRowsWithPlaceHolder + ` where id = ?`
|
||||
return conn.Exec(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
||||
}, userCamelIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Delete(id int64) error {
|
||||
data, err := m.FindOne(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userCamelIdKey := fmt.Sprintf("%s%v", cacheUserCamelIdPrefix, id)
|
||||
userCamelNameKey := fmt.Sprintf("%s%v", cacheUserCamelNamePrefix, data.Name)
|
||||
userCamelMobileKey := fmt.Sprintf("%s%v", cacheUserCamelMobilePrefix, data.Mobile)
|
||||
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
return conn.Exec(query, id)
|
||||
}, userCamelIdKey, userCamelNameKey, userCamelMobileKey)
|
||||
return err
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userSnakeFieldNames = builderx.FieldNames(&UserSnake{})
|
||||
userSnakeRows = strings.Join(userSnakeFieldNames, ",")
|
||||
userSnakeRowsExpectAutoSet = strings.Join(stringx.Remove(userSnakeFieldNames, "id", "create_time", "update_time"), ",")
|
||||
userSnakeRowsWithPlaceHolder = strings.Join(stringx.Remove(userSnakeFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
cacheUserSnakeIdPrefix = "cache#UserSnake#id#"
|
||||
cacheUserSnakeNamePrefix = "cache#UserSnake#name#"
|
||||
cacheUserSnakeMobilePrefix = "cache#UserSnake#mobile#"
|
||||
)
|
||||
|
||||
type (
|
||||
UserSnakeModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserSnake struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"` // 用户名称
|
||||
Password string `db:"password"` // 用户密码
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
Gender string `db:"gender"` // 男|女|未公开
|
||||
Nickname string `db:"nickname"` // 用户昵称
|
||||
CreateTime time.Time `db:"create_time"`
|
||||
UpdateTime time.Time `db:"update_time"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserSnakeModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserSnakeModel {
|
||||
return &UserSnakeModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Insert(data UserSnake) error {
|
||||
query := `insert into ` + m.table + `(` + userSnakeRowsExpectAutoSet + `) value (?, ?, ?, ?, ?)`
|
||||
_, err := m.ExecNoCache(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOne(id int64) (*UserSnake, error) {
|
||||
userSnakeIdKey := fmt.Sprintf("%s%v", cacheUserSnakeIdPrefix, id)
|
||||
var resp UserSnake
|
||||
err := m.QueryRow(&resp, userSnakeIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOneByName(name string) (*UserSnake, error) {
|
||||
userSnakeNameKey := fmt.Sprintf("%s%v", cacheUserSnakeNamePrefix, name)
|
||||
var resp UserSnake
|
||||
err := m.QueryRowIndex(&resp, userSnakeNameKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserSnakeIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where name = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOneByMobile(mobile string) (*UserSnake, error) {
|
||||
userSnakeMobileKey := fmt.Sprintf("%s%v", cacheUserSnakeMobilePrefix, mobile)
|
||||
var resp UserSnake
|
||||
err := m.QueryRowIndex(&resp, userSnakeMobileKey, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheUserSnakeIdPrefix, primary)
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where mobile = ? limit 1`
|
||||
if err := conn.QueryRow(&resp, query, mobile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
return conn.QueryRow(v, query, primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Update(data UserSnake) error {
|
||||
userSnakeIdKey := fmt.Sprintf("%s%v", cacheUserSnakeIdPrefix, data.Id)
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `update ` + m.table + ` set ` + userSnakeRowsWithPlaceHolder + ` where id = ?`
|
||||
return conn.Exec(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
||||
}, userSnakeIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Delete(id int64) error {
|
||||
data, err := m.FindOne(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userSnakeIdKey := fmt.Sprintf("%s%v", cacheUserSnakeIdPrefix, id)
|
||||
userSnakeNameKey := fmt.Sprintf("%s%v", cacheUserSnakeNamePrefix, data.Name)
|
||||
userSnakeMobileKey := fmt.Sprintf("%s%v", cacheUserSnakeMobilePrefix, data.Mobile)
|
||||
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
return conn.Exec(query, id)
|
||||
}, userSnakeMobileKey, userSnakeIdKey, userSnakeNameKey)
|
||||
return err
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package model
|
||||
|
||||
import "github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
|
||||
var (
|
||||
ErrNotFound = sqlx.ErrNotFound
|
||||
)
|
||||
@@ -1,104 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userCamelFieldNames = builderx.FieldNames(&UserCamel{})
|
||||
userCamelRows = strings.Join(userCamelFieldNames, ",")
|
||||
userCamelRowsExpectAutoSet = strings.Join(stringx.Remove(userCamelFieldNames, "create_time", "update_time"), ",")
|
||||
userCamelRowsWithPlaceHolder = strings.Join(stringx.Remove(userCamelFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
UserCamelModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserCamel struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"` // 用户名称
|
||||
Password string `db:"password"` // 用户密码
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
Gender string `db:"gender"` // 男|女|未公开
|
||||
Nickname string `db:"nickname"` // 用户昵称
|
||||
CreateTime time.Time `db:"createTime"`
|
||||
UpdateTime time.Time `db:"updateTime"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserCamelModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserCamelModel {
|
||||
return &UserCamelModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Insert(data UserCamel) error {
|
||||
query := `insert into ` + m.table + `(` + userCamelRowsExpectAutoSet + `) value (?, ?, ?, ?, ?, ?)`
|
||||
_, err := m.ExecNoCache(query, data.Id, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOne(id int64) (*UserCamel, error) {
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
var resp UserCamel
|
||||
err := m.QueryRowNoCache(&resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOneByName(name string) (*UserCamel, error) {
|
||||
var resp UserCamel
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where name limit 1`
|
||||
err := m.QueryRowNoCache(&resp, query, name)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) FindOneByMobile(mobile string) (*UserCamel, error) {
|
||||
var resp UserCamel
|
||||
query := `select ` + userCamelRows + ` from ` + m.table + ` where mobile limit 1`
|
||||
err := m.QueryRowNoCache(&resp, query, mobile)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Update(data UserCamel) error {
|
||||
query := `update ` + m.table + ` set ` + userCamelRowsWithPlaceHolder + ` where id = ?`
|
||||
_, err := m.ExecNoCache(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserCamelModel) Delete(id int64) error {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
_, err := m.ExecNoCache(query, id)
|
||||
return err
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userSnakeFieldNames = builderx.FieldNames(&UserSnake{})
|
||||
userSnakeRows = strings.Join(userSnakeFieldNames, ",")
|
||||
userSnakeRowsExpectAutoSet = strings.Join(stringx.Remove(userSnakeFieldNames, "id", "create_time", "update_time"), ",")
|
||||
userSnakeRowsWithPlaceHolder = strings.Join(stringx.Remove(userSnakeFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
UserSnakeModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
UserSnake struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"` // 用户名称
|
||||
Password string `db:"password"` // 用户密码
|
||||
Mobile string `db:"mobile"` // 手机号
|
||||
Gender string `db:"gender"` // 男|女|未公开
|
||||
Nickname string `db:"nickname"` // 用户昵称
|
||||
CreateTime time.Time `db:"create_time"`
|
||||
UpdateTime time.Time `db:"update_time"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserSnakeModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *UserSnakeModel {
|
||||
return &UserSnakeModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Insert(data UserSnake) error {
|
||||
query := `insert into ` + m.table + `(` + userSnakeRowsExpectAutoSet + `) value (?, ?, ?, ?, ?)`
|
||||
_, err := m.ExecNoCache(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOne(id int64) (*UserSnake, error) {
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where id = ? limit 1`
|
||||
var resp UserSnake
|
||||
err := m.QueryRowNoCache(&resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOneByName(name string) (*UserSnake, error) {
|
||||
var resp UserSnake
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where name limit 1`
|
||||
err := m.QueryRowNoCache(&resp, query, name)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) FindOneByMobile(mobile string) (*UserSnake, error) {
|
||||
var resp UserSnake
|
||||
query := `select ` + userSnakeRows + ` from ` + m.table + ` where mobile limit 1`
|
||||
err := m.QueryRowNoCache(&resp, query, mobile)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Update(data UserSnake) error {
|
||||
query := `update ` + m.table + ` set ` + userSnakeRowsWithPlaceHolder + ` where id = ?`
|
||||
_, err := m.ExecNoCache(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *UserSnakeModel) Delete(id int64) error {
|
||||
query := `delete from ` + m.table + ` where id = ?`
|
||||
_, err := m.ExecNoCache(query, id)
|
||||
return err
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
const (
|
||||
pwd = "."
|
||||
createTableFlag = `(?m)CREATE\s+TABLE`
|
||||
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -47,11 +47,9 @@ func NewDefaultGenerator(src, dir string, opt ...Option) *defaultGenerator {
|
||||
return generator
|
||||
}
|
||||
|
||||
func WithConsoleOption(idea bool) Option {
|
||||
func WithConsoleOption(c console.Console) Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
if idea {
|
||||
generator.Console = console.NewIdeaConsole()
|
||||
}
|
||||
generator.Console = c
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +86,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
|
||||
name := fmt.Sprintf("%smodel.go", strings.ToLower(stringx.From(tableName).Snake2Camel()))
|
||||
filename := filepath.Join(dirAbs, name)
|
||||
if util.FileExists(filename) {
|
||||
g.Warning("%s already exists", name)
|
||||
g.Warning("%s already exists,ignored.", name)
|
||||
continue
|
||||
}
|
||||
err = ioutil.WriteFile(filename, []byte(code), os.ModePerm)
|
||||
@@ -98,9 +96,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
|
||||
}
|
||||
// generate error file
|
||||
filename := filepath.Join(dirAbs, "error.go")
|
||||
if util.FileExists(filename) {
|
||||
g.Warning("error.go already exists")
|
||||
} else {
|
||||
if !util.FileExists(filename) {
|
||||
err = ioutil.WriteFile(filename, []byte(template.Error), os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -144,10 +140,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
importsCode, err := genImports(withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
importsCode := genImports(withCache)
|
||||
var table Table
|
||||
table.Table = in
|
||||
table.CacheKey = m
|
||||
@@ -156,15 +149,15 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
typesCode, err := genTypes(table)
|
||||
typesCode, err := genTypes(table, withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
newCode, err := genNew(table)
|
||||
newCode, err := genNew(table, withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
insertCode, err := genInsert(table)
|
||||
insertCode, err := genInsert(table, withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -2,17 +2,12 @@ package gen
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
|
||||
func genImports(withCache bool) (string, error) {
|
||||
output, err := templatex.With("import").
|
||||
Parse(template.Imports).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
func genImports(withCache bool) string {
|
||||
if withCache {
|
||||
return template.Imports
|
||||
} else {
|
||||
return template.ImportsNoCache
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
|
||||
func genInsert(table Table) (string, error) {
|
||||
func genInsert(table Table, withCache bool) (string, error) {
|
||||
expressions := make([]string, 0)
|
||||
expressionValues := make([]string, 0)
|
||||
for _, filed := range table.Fields {
|
||||
@@ -26,6 +26,7 @@ func genInsert(table Table) (string, error) {
|
||||
output, err := templatex.With("insert").
|
||||
Parse(template.Insert).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": camel,
|
||||
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
|
||||
"expression": strings.Join(expressions, ", "),
|
||||
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
|
||||
func genNew(table Table) (string, error) {
|
||||
func genNew(table Table, withCache bool) (string, error) {
|
||||
output, err := templatex.With("new").
|
||||
Parse(template.New).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": table.Name.Snake2Camel(),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
|
||||
func genTypes(table Table) (string, error) {
|
||||
func genTypes(table Table, withCache bool) (string, error) {
|
||||
fields := table.Fields
|
||||
fieldsString, err := genFields(fields)
|
||||
if err != nil {
|
||||
@@ -14,6 +14,7 @@ func genTypes(table Table) (string, error) {
|
||||
output, err := templatex.With("types").
|
||||
Parse(template.Types).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": table.Name.Snake2Camel(),
|
||||
"fields": fieldsString,
|
||||
})
|
||||
|
||||
27
tools/goctl/model/sql/parser/parser_test.go
Normal file
27
tools/goctl/model/sql/parser/parser_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParsePlainText(t *testing.T) {
|
||||
_, err := Parse("plain text")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestParseSelect(t *testing.T) {
|
||||
_, err := Parse("select * from user")
|
||||
assert.Equal(t, unSupportDDL, err)
|
||||
}
|
||||
|
||||
func TestParseCreateTable(t *testing.T) {
|
||||
_, err := Parse("CREATE TABLE `user_snake` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestParseCreateTable2(t *testing.T) {
|
||||
_, err := Parse("create table `user_snake` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -11,7 +11,7 @@ func (m *{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}}
|
||||
query := ` + "`" + `delete from ` + "` +" + ` m.table + ` + " `" + ` where {{.originalPrimaryKey}} = ?` + "`" + `
|
||||
return conn.Exec(query, {{.lowerStartCamelPrimaryKey}})
|
||||
}, {{.keyValues}}){{else}}query := ` + "`" + `delete from ` + "` +" + ` m.table + ` + " `" + ` where {{.originalPrimaryKey}} = ?` + "`" + `
|
||||
_,err:=m.ExecNoCache(query, {{.lowerStartCamelPrimaryKey}}){{end}}
|
||||
_,err:=m.conn.Exec(query, {{.lowerStartCamelPrimaryKey}}){{end}}
|
||||
return err
|
||||
}
|
||||
`
|
||||
|
||||
@@ -18,7 +18,7 @@ func (m *{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrimaryKey}}
|
||||
return nil, err
|
||||
}{{else}}query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalPrimaryKey}} = ? limit 1` + "`" + `
|
||||
var resp {{.upperStartCamelObject}}
|
||||
err := m.QueryRowNoCache(&resp, query, {{.lowerStartCamelPrimaryKey}})
|
||||
err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelPrimaryKey}})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
@@ -57,7 +57,7 @@ func (m *{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{
|
||||
}
|
||||
}{{else}}var resp {{.upperStartCamelObject}}
|
||||
query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalField}} limit 1` + "`" + `
|
||||
err := m.QueryRowNoCache(&resp, query, {{.lowerStartCamelField}})
|
||||
err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelField}})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
|
||||
@@ -1,16 +1,28 @@
|
||||
package template
|
||||
|
||||
var Imports = `
|
||||
import (
|
||||
{{if .withCache}}"database/sql"
|
||||
"fmt"{{end}}
|
||||
var (
|
||||
Imports = `import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
`
|
||||
ImportsNoCache = `import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
`
|
||||
)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package template
|
||||
|
||||
var Insert = `
|
||||
func (m *{{.upperStartCamelObject}}Model) Insert(data {{.upperStartCamelObject}}) error {
|
||||
func (m *{{.upperStartCamelObject}}Model) Insert(data {{.upperStartCamelObject}}) (sql.Result, error) {
|
||||
query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "`(` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) value ({{.expression}})` " + `
|
||||
_, err := m.ExecNoCache(query, {{.expressionValues}})
|
||||
return err
|
||||
return m.{{if .withCache}}ExecNoCache{{else}}conn.Exec{{end}}(query, {{.expressionValues}})
|
||||
}
|
||||
`
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package template
|
||||
|
||||
var New = `
|
||||
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn, c cache.CacheConf, table string) *{{.upperStartCamelObject}}Model {
|
||||
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn,{{if .withCache}} c cache.CacheConf,{{end}} table string) *{{.upperStartCamelObject}}Model {
|
||||
return &{{.upperStartCamelObject}}Model{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package template
|
||||
var Types = `
|
||||
type (
|
||||
{{.upperStartCamelObject}}Model struct {
|
||||
sqlc.CachedConn
|
||||
{{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}}
|
||||
table string
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ func (m *{{.upperStartCamelObject}}Model) Update(data {{.upperStartCamelObject}}
|
||||
query := ` + "`" + `update ` + "` +" + `m.table +` + "` " + `set ` + "` +" + `{{.lowerStartCamelObject}}RowsWithPlaceHolder` + " + `" + ` where {{.originalPrimaryKey}} = ?` + "`" + `
|
||||
return conn.Exec(query, {{.expressionValues}})
|
||||
}, {{.primaryKeyVariable}}){{else}}query := ` + "`" + `update ` + "` +" + `m.table +` + "` " + `set ` + "` +" + `{{.lowerStartCamelObject}}RowsWithPlaceHolder` + " + `" + ` where {{.originalPrimaryKey}} = ?` + "`" + `
|
||||
_,err:=m.ExecNoCache(query, {{.expressionValues}}){{end}}
|
||||
_,err:=m.conn.Exec(query, {{.expressionValues}}){{end}}
|
||||
return err
|
||||
}
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user