|
|
@ -7,7 +7,7 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
* 通过ddl生成
|
|
|
|
* 通过ddl生成
|
|
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
```shell script
|
|
|
|
goctl model mysql ddl -src="./*.sql" -dir="./sql/model" -c=true
|
|
|
|
goctl model mysql ddl -src="./*.sql" -dir="./sql/model" -c
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
执行上述命令后即可快速生成CURD代码。
|
|
|
|
执行上述命令后即可快速生成CURD代码。
|
|
|
@ -49,19 +49,31 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
|
|
|
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
|
|
|
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
|
|
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
|
|
|
|
|
|
|
|
|
|
|
cacheUserIdPrefix = "cache#User#id#"
|
|
|
|
cacheUserPrefix = "cache#User#user#"
|
|
|
|
cacheUserNamePrefix = "cache#User#name#"
|
|
|
|
cacheUserNamePrefix = "cache#User#name#"
|
|
|
|
cacheUserMobilePrefix = "cache#User#mobile#"
|
|
|
|
cacheUserMobilePrefix = "cache#User#mobile#"
|
|
|
|
|
|
|
|
cacheUserIdPrefix = "cache#User#id#"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
UserModel struct {
|
|
|
|
UserModel interface {
|
|
|
|
|
|
|
|
Insert(data User) (sql.Result, error)
|
|
|
|
|
|
|
|
FindOne(id int64) (*User, error)
|
|
|
|
|
|
|
|
FindOneByUser(user string) (*User, error)
|
|
|
|
|
|
|
|
FindOneByName(name string) (*User, error)
|
|
|
|
|
|
|
|
FindOneByMobile(mobile string) (*User, error)
|
|
|
|
|
|
|
|
Update(data User) error
|
|
|
|
|
|
|
|
Delete(id int64) error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defaultUserModel struct {
|
|
|
|
sqlc.CachedConn
|
|
|
|
sqlc.CachedConn
|
|
|
|
table string
|
|
|
|
table string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
User struct {
|
|
|
|
User struct {
|
|
|
|
Id int64 `db:"id"`
|
|
|
|
Id int64 `db:"id"`
|
|
|
|
|
|
|
|
User string `db:"user"` // 用户
|
|
|
|
Name string `db:"name"` // 用户名称
|
|
|
|
Name string `db:"name"` // 用户名称
|
|
|
|
Password string `db:"password"` // 用户密码
|
|
|
|
Password string `db:"password"` // 用户密码
|
|
|
|
Mobile string `db:"mobile"` // 手机号
|
|
|
|
Mobile string `db:"mobile"` // 手机号
|
|
|
@ -72,24 +84,25 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf) *UserModel {
|
|
|
|
func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf) UserModel {
|
|
|
|
return &UserModel{
|
|
|
|
return &defaultUserModel{
|
|
|
|
CachedConn: sqlc.NewConn(conn, c),
|
|
|
|
CachedConn: sqlc.NewConn(conn, c),
|
|
|
|
table: "user",
|
|
|
|
table: "user",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) Insert(data User) (sql.Result, error) {
|
|
|
|
func (m *defaultUserModel) Insert(data User) (sql.Result, error) {
|
|
|
|
|
|
|
|
userKey := fmt.Sprintf("%s%v", cacheUserPrefix, data.User)
|
|
|
|
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, data.Name)
|
|
|
|
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, data.Name)
|
|
|
|
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)
|
|
|
|
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)
|
|
|
|
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
|
|
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
|
|
|
|
return conn.Exec(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
|
|
|
return conn.Exec(query, data.User, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname)
|
|
|
|
}, userNameKey, userMobileKey)
|
|
|
|
}, userMobileKey, userKey, userNameKey)
|
|
|
|
return ret, err
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) FindOne(id int64) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOne(id int64) (*User, error) {
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRow(&resp, userIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
|
|
|
err := m.QueryRow(&resp, userIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
|
|
@ -106,7 +119,27 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) FindOneByName(name string) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOneByUser(user string) (*User, error) {
|
|
|
|
|
|
|
|
userKey := fmt.Sprintf("%s%v", cacheUserPrefix, user)
|
|
|
|
|
|
|
|
var resp User
|
|
|
|
|
|
|
|
err := m.QueryRowIndex(&resp, userKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
|
|
|
|
|
query := fmt.Sprintf("select %s from %s where user = ? limit 1", userRows, m.table)
|
|
|
|
|
|
|
|
if err := conn.QueryRow(&resp, query, user); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
|
|
|
|
}, m.queryPrimary)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
|
|
|
|
return nil, ErrNotFound
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOneByName(name string) (*User, error) {
|
|
|
|
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, name)
|
|
|
|
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, name)
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowIndex(&resp, userNameKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
|
err := m.QueryRowIndex(&resp, userNameKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
@ -126,7 +159,7 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) FindOneByMobile(mobile string) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOneByMobile(mobile string) (*User, error) {
|
|
|
|
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)
|
|
|
|
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowIndex(&resp, userMobileKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
|
err := m.QueryRowIndex(&resp, userMobileKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
@ -146,39 +179,41 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) Update(data User) error {
|
|
|
|
func (m *defaultUserModel) Update(data User) error {
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)
|
|
|
|
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("update %s set %s where id = ?", m.table, userRowsWithPlaceHolder)
|
|
|
|
query := fmt.Sprintf("update %s set %s where id = ?", m.table, userRowsWithPlaceHolder)
|
|
|
|
return conn.Exec(query, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
|
|
|
return conn.Exec(query, data.User, data.Name, data.Password, data.Mobile, data.Gender, data.Nickname, data.Id)
|
|
|
|
}, userIdKey)
|
|
|
|
}, userIdKey)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) Delete(id int64) error {
|
|
|
|
func (m *defaultUserModel) Delete(id int64) error {
|
|
|
|
data, err := m.FindOne(id)
|
|
|
|
data, err := m.FindOne(id)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)
|
|
|
|
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
|
|
|
userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
|
|
|
|
|
|
|
|
userKey := fmt.Sprintf("%s%v", cacheUserPrefix, data.User)
|
|
|
|
userNameKey := fmt.Sprintf("%s%v", cacheUserNamePrefix, data.Name)
|
|
|
|
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) {
|
|
|
|
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("delete from %s where id = ?", m.table)
|
|
|
|
query := fmt.Sprintf("delete from %s where id = ?", m.table)
|
|
|
|
return conn.Exec(query, id)
|
|
|
|
return conn.Exec(query, id)
|
|
|
|
}, userMobileKey, userIdKey, userNameKey)
|
|
|
|
}, userIdKey, userKey, userNameKey, userMobileKey)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) formatPrimary(primary interface{}) string {
|
|
|
|
func (m *defaultUserModel) formatPrimary(primary interface{}) string {
|
|
|
|
return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
|
|
|
|
return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *UserModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
|
|
|
|
func (m *defaultUserModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
|
|
|
|
query := fmt.Sprintf("select %s from %s where id = ? limit 1", userRows, m.table)
|
|
|
|
query := fmt.Sprintf("select %s from %s where id = ? limit 1", userRows, m.table)
|
|
|
|
return conn.QueryRow(v, query, primary)
|
|
|
|
return conn.QueryRow(v, query, primary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 用法
|
|
|
|
## 用法
|
|
|
@ -211,7 +246,7 @@ OPTIONS:
|
|
|
|
* ddl
|
|
|
|
* ddl
|
|
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
```shell script
|
|
|
|
goctl model mysql -src={patterns} -dir={dir} -cache=true
|
|
|
|
goctl model mysql -src={patterns} -dir={dir} -cache
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
help
|
|
|
|
help
|
|
|
@ -226,10 +261,9 @@ OPTIONS:
|
|
|
|
OPTIONS:
|
|
|
|
OPTIONS:
|
|
|
|
--src value, -s value the path or path globbing patterns of the ddl
|
|
|
|
--src value, -s value the path or path globbing patterns of the ddl
|
|
|
|
--dir value, -d value the target dir
|
|
|
|
--dir value, -d value the target dir
|
|
|
|
--style value the file naming style, lower|camel|underline,default is lower
|
|
|
|
--style value the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]
|
|
|
|
--cache, -c generate code with cache [optional]
|
|
|
|
--cache, -c generate code with cache [optional]
|
|
|
|
--idea for idea plugin [optional]
|
|
|
|
--idea for idea plugin [optional]
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
* datasource
|
|
|
|
* datasource
|
|
|
@ -252,9 +286,10 @@ OPTIONS:
|
|
|
|
--table value, -t value the table or table globbing patterns in the database
|
|
|
|
--table value, -t value the table or table globbing patterns in the database
|
|
|
|
--cache, -c generate code with cache [optional]
|
|
|
|
--cache, -c generate code with cache [optional]
|
|
|
|
--dir value, -d value the target dir
|
|
|
|
--dir value, -d value the target dir
|
|
|
|
--style value the file naming style, lower|camel|snake, default is lower
|
|
|
|
--style value the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]
|
|
|
|
--idea for idea plugin [optional]
|
|
|
|
--idea for idea plugin [optional]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
示例用法请参考[用法](./example/generator.sh)
|
|
|
|
示例用法请参考[用法](./example/generator.sh)
|
|
|
@ -281,13 +316,13 @@ OPTIONS:
|
|
|
|
* ddl
|
|
|
|
* ddl
|
|
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
```shell script
|
|
|
|
goctl model -src={patterns} -dir={dir} -cache=false
|
|
|
|
goctl model -src={patterns} -dir={dir}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
* datasource
|
|
|
|
* datasource
|
|
|
|
|
|
|
|
|
|
|
|
```shell script
|
|
|
|
```shell script
|
|
|
|
goctl model mysql datasource -url={datasource} -table={patterns} -dir={dir} -cache=false
|
|
|
|
goctl model mysql datasource -url={datasource} -table={patterns} -dir={dir}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
生成代码仅基本的CURD结构。
|
|
|
|
生成代码仅基本的CURD结构。
|
|
|
|