Pārlūkot izejas kodu

update bookstore example for generation prototype

kevin 4 gadi atpakaļ
vecāks
revīzija
b144a2335c

+ 1 - 1
example/bookstore/rpc/add/add.go

@@ -8,9 +8,9 @@ import (
 	"fmt"
 
 	"bookstore/rpc/add/internal/config"
+	add "bookstore/rpc/add/internal/pb"
 	"bookstore/rpc/add/internal/server"
 	"bookstore/rpc/add/internal/svc"
-	add "bookstore/rpc/add/pb"
 
 	"github.com/tal-tech/go-zero/core/conf"
 	"github.com/tal-tech/go-zero/core/logx"

+ 6 - 31
example/bookstore/rpc/add/adder/adder.go

@@ -8,13 +8,15 @@ package adder
 import (
 	"context"
 
-	add "bookstore/rpc/add/pb"
+	add "bookstore/rpc/add/internal/pb"
 
-	"github.com/tal-tech/go-zero/core/jsonx"
 	"github.com/tal-tech/go-zero/zrpc"
 )
 
 type (
+	AddReq  = add.AddReq
+	AddResp = add.AddResp
+
 	Adder interface {
 		Add(ctx context.Context, in *AddReq) (*AddResp, error)
 	}
@@ -31,33 +33,6 @@ func NewAdder(cli zrpc.Client) Adder {
 }
 
 func (m *defaultAdder) Add(ctx context.Context, in *AddReq) (*AddResp, error) {
-	var request add.AddReq
-	bts, err := jsonx.Marshal(in)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	err = jsonx.Unmarshal(bts, &request)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	client := add.NewAdderClient(m.cli.Conn())
-	resp, err := client.Add(ctx, &request)
-	if err != nil {
-		return nil, err
-	}
-
-	var ret AddResp
-	bts, err = jsonx.Marshal(resp)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	err = jsonx.Unmarshal(bts, &ret)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	return &ret, nil
+	adder := add.NewAdderClient(m.cli.Conn())
+	return adder.Add(ctx, in)
 }

+ 0 - 19
example/bookstore/rpc/add/adder/types.go

@@ -1,19 +0,0 @@
-// Code generated by goctl. DO NOT EDIT!
-// Source: add.proto
-
-package adder
-
-import "errors"
-
-var errJsonConvert = errors.New("json convert error")
-
-type (
-	AddReq struct {
-		Book  string `json:"book,omitempty"`
-		Price int64  `json:"price,omitempty"`
-	}
-
-	AddResp struct {
-		Ok bool `json:"ok,omitempty"`
-	}
-)

+ 1 - 1
example/bookstore/rpc/add/internal/logic/addlogic.go

@@ -3,8 +3,8 @@ package logic
 import (
 	"context"
 
+	add "bookstore/rpc/add/internal/pb"
 	"bookstore/rpc/add/internal/svc"
-	add "bookstore/rpc/add/pb"
 	"bookstore/rpc/model"
 
 	"github.com/tal-tech/go-zero/core/logx"

+ 6 - 6
example/bookstore/rpc/add/pb/add.pb.go → example/bookstore/rpc/add/internal/pb/add.pb.go

@@ -14,13 +14,13 @@ It has these top-level messages:
 */
 package add
 
-import proto "github.com/golang/protobuf/proto"
-import fmt "fmt"
-import math "math"
-
 import (
-	context "golang.org/x/net/context"
-	grpc "google.golang.org/grpc"
+	"fmt"
+	"math"
+
+	"github.com/golang/protobuf/proto"
+	"golang.org/x/net/context"
+	"google.golang.org/grpc"
 )
 
 // Reference imports to suppress errors if they are not otherwise used.

+ 1 - 1
example/bookstore/rpc/add/internal/server/adderserver.go

@@ -7,8 +7,8 @@ import (
 	"context"
 
 	"bookstore/rpc/add/internal/logic"
+	add "bookstore/rpc/add/internal/pb"
 	"bookstore/rpc/add/internal/svc"
-	add "bookstore/rpc/add/pb"
 )
 
 type AdderServer struct {

+ 1 - 1
example/bookstore/rpc/check/check.go

@@ -8,9 +8,9 @@ import (
 	"fmt"
 
 	"bookstore/rpc/check/internal/config"
+	check "bookstore/rpc/check/internal/pb"
 	"bookstore/rpc/check/internal/server"
 	"bookstore/rpc/check/internal/svc"
-	check "bookstore/rpc/check/pb"
 
 	"github.com/tal-tech/go-zero/core/conf"
 	"github.com/tal-tech/go-zero/core/logx"

+ 6 - 31
example/bookstore/rpc/check/checker/checker.go

@@ -8,13 +8,15 @@ package checker
 import (
 	"context"
 
-	check "bookstore/rpc/check/pb"
+	check "bookstore/rpc/check/internal/pb"
 
-	"github.com/tal-tech/go-zero/core/jsonx"
 	"github.com/tal-tech/go-zero/zrpc"
 )
 
 type (
+	CheckReq  = check.CheckReq
+	CheckResp = check.CheckResp
+
 	Checker interface {
 		Check(ctx context.Context, in *CheckReq) (*CheckResp, error)
 	}
@@ -31,33 +33,6 @@ func NewChecker(cli zrpc.Client) Checker {
 }
 
 func (m *defaultChecker) Check(ctx context.Context, in *CheckReq) (*CheckResp, error) {
-	var request check.CheckReq
-	bts, err := jsonx.Marshal(in)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	err = jsonx.Unmarshal(bts, &request)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	client := check.NewCheckerClient(m.cli.Conn())
-	resp, err := client.Check(ctx, &request)
-	if err != nil {
-		return nil, err
-	}
-
-	var ret CheckResp
-	bts, err = jsonx.Marshal(resp)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	err = jsonx.Unmarshal(bts, &ret)
-	if err != nil {
-		return nil, errJsonConvert
-	}
-
-	return &ret, nil
+	checker := check.NewCheckerClient(m.cli.Conn())
+	return checker.Check(ctx, in)
 }

+ 0 - 19
example/bookstore/rpc/check/checker/types.go

@@ -1,19 +0,0 @@
-// Code generated by goctl. DO NOT EDIT!
-// Source: check.proto
-
-package checker
-
-import "errors"
-
-var errJsonConvert = errors.New("json convert error")
-
-type (
-	CheckReq struct {
-		Book string `json:"book,omitempty"`
-	}
-
-	CheckResp struct {
-		Found bool  `json:"found,omitempty"`
-		Price int64 `json:"price,omitempty"`
-	}
-)

+ 1 - 1
example/bookstore/rpc/check/internal/logic/checklogic.go

@@ -3,8 +3,8 @@ package logic
 import (
 	"context"
 
+	check "bookstore/rpc/check/internal/pb"
 	"bookstore/rpc/check/internal/svc"
-	check "bookstore/rpc/check/pb"
 
 	"github.com/tal-tech/go-zero/core/logx"
 )

+ 6 - 6
example/bookstore/rpc/check/pb/check.pb.go → example/bookstore/rpc/check/internal/pb/check.pb.go

@@ -14,13 +14,13 @@ It has these top-level messages:
 */
 package check
 
-import proto "github.com/golang/protobuf/proto"
-import fmt "fmt"
-import math "math"
-
 import (
-	context "golang.org/x/net/context"
-	grpc "google.golang.org/grpc"
+	"fmt"
+	"math"
+
+	"github.com/golang/protobuf/proto"
+	"golang.org/x/net/context"
+	"google.golang.org/grpc"
 )
 
 // Reference imports to suppress errors if they are not otherwise used.

+ 1 - 1
example/bookstore/rpc/check/internal/server/checkerserver.go

@@ -7,8 +7,8 @@ import (
 	"context"
 
 	"bookstore/rpc/check/internal/logic"
+	check "bookstore/rpc/check/internal/pb"
 	"bookstore/rpc/check/internal/svc"
-	check "bookstore/rpc/check/pb"
 )
 
 type CheckerServer struct {

+ 125 - 0
tools/goctl/model/sql/gen/testmodel/cache/testuserinfomodel.go

@@ -0,0 +1,125 @@
+package cache
+
+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 (
+	testUserInfoFieldNames          = builderx.FieldNames(&TestUserInfo{})
+	testUserInfoRows                = strings.Join(testUserInfoFieldNames, ",")
+	testUserInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), ",")
+	testUserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
+
+	cacheTestUserInfoIdPrefix         = "cache#TestUserInfo#id#"
+	cacheTestUserInfoNanosecondPrefix = "cache#TestUserInfo#nanosecond#"
+)
+
+type (
+	TestUserInfoModel struct {
+		sqlc.CachedConn
+		table string
+	}
+
+	TestUserInfo struct {
+		Id         int64     `db:"id"`
+		Nanosecond int64     `db:"nanosecond"`
+		Data       string    `db:"data"`
+		CreateTime time.Time `db:"create_time"`
+		UpdateTime time.Time `db:"update_time"`
+	}
+)
+
+func NewTestUserInfoModel(conn sqlx.SqlConn, c cache.CacheConf) *TestUserInfoModel {
+	return &TestUserInfoModel{
+		CachedConn: sqlc.NewConn(conn, c),
+		table:      "test_user_info",
+	}
+}
+
+func (m *TestUserInfoModel) Insert(data TestUserInfo) (sql.Result, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, testUserInfoRowsExpectAutoSet)
+		return conn.Exec(query, data.Nanosecond, data.Data)
+	}, testUserInfoNanosecondKey)
+	return ret, err
+}
+
+func (m *TestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	var resp TestUserInfo
+	err := m.QueryRow(&resp, testUserInfoIdKey, func(conn sqlx.SqlConn, v interface{}) error {
+		query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+		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 *TestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, nanosecond)
+	var resp TestUserInfo
+	err := m.QueryRowIndex(&resp, testUserInfoNanosecondKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
+		query := fmt.Sprintf("select %s from %s where nanosecond = ? limit 1", testUserInfoRows, m.table)
+		if err := conn.QueryRow(&resp, query, nanosecond); 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 *TestUserInfoModel) Update(data TestUserInfo) error {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, data.Id)
+	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("update %s set %s where id = ?", m.table, testUserInfoRowsWithPlaceHolder)
+		return conn.Exec(query, data.Nanosecond, data.Data, data.Id)
+	}, testUserInfoIdKey)
+	return err
+}
+
+func (m *TestUserInfoModel) Delete(id int64) error {
+	data, err := m.FindOne(id)
+	if err != nil {
+		return err
+	}
+
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("delete from %s where id = ?", m.table)
+		return conn.Exec(query, id)
+	}, testUserInfoNanosecondKey, testUserInfoIdKey)
+	return err
+}
+
+func (m *TestUserInfoModel) formatPrimary(primary interface{}) string {
+	return fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, primary)
+}
+
+func (m *TestUserInfoModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
+	query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+	return conn.QueryRow(v, query, primary)
+}

+ 5 - 0
tools/goctl/model/sql/gen/testmodel/cache/vars.go

@@ -0,0 +1,5 @@
+package cache
+
+import "github.com/tal-tech/go-zero/core/stores/sqlx"
+
+var ErrNotFound = sqlx.ErrNotFound

+ 125 - 0
tools/goctl/model/sql/gen/testmodel/camel/TestUserInfoModel.go

@@ -0,0 +1,125 @@
+package camel
+
+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 (
+	testUserInfoFieldNames          = builderx.FieldNames(&TestUserInfo{})
+	testUserInfoRows                = strings.Join(testUserInfoFieldNames, ",")
+	testUserInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), ",")
+	testUserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
+
+	cacheTestUserInfoIdPrefix         = "cache#TestUserInfo#id#"
+	cacheTestUserInfoNanosecondPrefix = "cache#TestUserInfo#nanosecond#"
+)
+
+type (
+	TestUserInfoModel struct {
+		sqlc.CachedConn
+		table string
+	}
+
+	TestUserInfo struct {
+		Id         int64     `db:"id"`
+		Nanosecond int64     `db:"nanosecond"`
+		Data       string    `db:"data"`
+		CreateTime time.Time `db:"create_time"`
+		UpdateTime time.Time `db:"update_time"`
+	}
+)
+
+func NewTestUserInfoModel(conn sqlx.SqlConn, c cache.CacheConf) *TestUserInfoModel {
+	return &TestUserInfoModel{
+		CachedConn: sqlc.NewConn(conn, c),
+		table:      "test_user_info",
+	}
+}
+
+func (m *TestUserInfoModel) Insert(data TestUserInfo) (sql.Result, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, testUserInfoRowsExpectAutoSet)
+		return conn.Exec(query, data.Nanosecond, data.Data)
+	}, testUserInfoNanosecondKey)
+	return ret, err
+}
+
+func (m *TestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	var resp TestUserInfo
+	err := m.QueryRow(&resp, testUserInfoIdKey, func(conn sqlx.SqlConn, v interface{}) error {
+		query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+		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 *TestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, nanosecond)
+	var resp TestUserInfo
+	err := m.QueryRowIndex(&resp, testUserInfoNanosecondKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
+		query := fmt.Sprintf("select %s from %s where nanosecond = ? limit 1", testUserInfoRows, m.table)
+		if err := conn.QueryRow(&resp, query, nanosecond); 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 *TestUserInfoModel) Update(data TestUserInfo) error {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, data.Id)
+	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("update %s set %s where id = ?", m.table, testUserInfoRowsWithPlaceHolder)
+		return conn.Exec(query, data.Nanosecond, data.Data, data.Id)
+	}, testUserInfoIdKey)
+	return err
+}
+
+func (m *TestUserInfoModel) Delete(id int64) error {
+	data, err := m.FindOne(id)
+	if err != nil {
+		return err
+	}
+
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("delete from %s where id = ?", m.table)
+		return conn.Exec(query, id)
+	}, testUserInfoIdKey, testUserInfoNanosecondKey)
+	return err
+}
+
+func (m *TestUserInfoModel) formatPrimary(primary interface{}) string {
+	return fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, primary)
+}
+
+func (m *TestUserInfoModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
+	query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+	return conn.QueryRow(v, query, primary)
+}

+ 5 - 0
tools/goctl/model/sql/gen/testmodel/camel/vars.go

@@ -0,0 +1,5 @@
+package camel
+
+import "github.com/tal-tech/go-zero/core/stores/sqlx"
+
+var ErrNotFound = sqlx.ErrNotFound

+ 88 - 0
tools/goctl/model/sql/gen/testmodel/nocache/testuserinfomodel.go

@@ -0,0 +1,88 @@
+package nocache
+
+import (
+	"database/sql"
+	"fmt"
+	"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 (
+	testUserInfoFieldNames          = builderx.FieldNames(&TestUserInfo{})
+	testUserInfoRows                = strings.Join(testUserInfoFieldNames, ",")
+	testUserInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), ",")
+	testUserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
+)
+
+type (
+	TestUserInfoModel struct {
+		conn  sqlx.SqlConn
+		table string
+	}
+
+	TestUserInfo struct {
+		Id         int64     `db:"id"`
+		Nanosecond int64     `db:"nanosecond"`
+		Data       string    `db:"data"`
+		CreateTime time.Time `db:"create_time"`
+		UpdateTime time.Time `db:"update_time"`
+	}
+)
+
+func NewTestUserInfoModel(conn sqlx.SqlConn) *TestUserInfoModel {
+	return &TestUserInfoModel{
+		conn:  conn,
+		table: "test_user_info",
+	}
+}
+
+func (m *TestUserInfoModel) Insert(data TestUserInfo) (sql.Result, error) {
+	query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, testUserInfoRowsExpectAutoSet)
+	ret, err := m.conn.Exec(query, data.Nanosecond, data.Data)
+	return ret, err
+}
+
+func (m *TestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) {
+	query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+	var resp TestUserInfo
+	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 *TestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) {
+	var resp TestUserInfo
+	query := fmt.Sprintf("select %s from %s where nanosecond = ? limit 1", testUserInfoRows, m.table)
+	err := m.conn.QueryRow(&resp, query, nanosecond)
+	switch err {
+	case nil:
+		return &resp, nil
+	case sqlc.ErrNotFound:
+		return nil, ErrNotFound
+	default:
+		return nil, err
+	}
+}
+
+func (m *TestUserInfoModel) Update(data TestUserInfo) error {
+	query := fmt.Sprintf("update %s set %s where id = ?", m.table, testUserInfoRowsWithPlaceHolder)
+	_, err := m.conn.Exec(query, data.Nanosecond, data.Data, data.Id)
+	return err
+}
+
+func (m *TestUserInfoModel) Delete(id int64) error {
+	query := fmt.Sprintf("delete from %s where id = ?", m.table)
+	_, err := m.conn.Exec(query, id)
+	return err
+}

+ 5 - 0
tools/goctl/model/sql/gen/testmodel/nocache/vars.go

@@ -0,0 +1,5 @@
+package nocache
+
+import "github.com/tal-tech/go-zero/core/stores/sqlx"
+
+var ErrNotFound = sqlx.ErrNotFound

+ 125 - 0
tools/goctl/model/sql/gen/testmodel/snake/test_user_info_model.go

@@ -0,0 +1,125 @@
+package snake
+
+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 (
+	testUserInfoFieldNames          = builderx.FieldNames(&TestUserInfo{})
+	testUserInfoRows                = strings.Join(testUserInfoFieldNames, ",")
+	testUserInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), ",")
+	testUserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
+
+	cacheTestUserInfoIdPrefix         = "cache#TestUserInfo#id#"
+	cacheTestUserInfoNanosecondPrefix = "cache#TestUserInfo#nanosecond#"
+)
+
+type (
+	TestUserInfoModel struct {
+		sqlc.CachedConn
+		table string
+	}
+
+	TestUserInfo struct {
+		Id         int64     `db:"id"`
+		Nanosecond int64     `db:"nanosecond"`
+		Data       string    `db:"data"`
+		CreateTime time.Time `db:"create_time"`
+		UpdateTime time.Time `db:"update_time"`
+	}
+)
+
+func NewTestUserInfoModel(conn sqlx.SqlConn, c cache.CacheConf) *TestUserInfoModel {
+	return &TestUserInfoModel{
+		CachedConn: sqlc.NewConn(conn, c),
+		table:      "test_user_info",
+	}
+}
+
+func (m *TestUserInfoModel) Insert(data TestUserInfo) (sql.Result, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, testUserInfoRowsExpectAutoSet)
+		return conn.Exec(query, data.Nanosecond, data.Data)
+	}, testUserInfoNanosecondKey)
+	return ret, err
+}
+
+func (m *TestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	var resp TestUserInfo
+	err := m.QueryRow(&resp, testUserInfoIdKey, func(conn sqlx.SqlConn, v interface{}) error {
+		query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+		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 *TestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) {
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, nanosecond)
+	var resp TestUserInfo
+	err := m.QueryRowIndex(&resp, testUserInfoNanosecondKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
+		query := fmt.Sprintf("select %s from %s where nanosecond = ? limit 1", testUserInfoRows, m.table)
+		if err := conn.QueryRow(&resp, query, nanosecond); 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 *TestUserInfoModel) Update(data TestUserInfo) error {
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, data.Id)
+	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("update %s set %s where id = ?", m.table, testUserInfoRowsWithPlaceHolder)
+		return conn.Exec(query, data.Nanosecond, data.Data, data.Id)
+	}, testUserInfoIdKey)
+	return err
+}
+
+func (m *TestUserInfoModel) Delete(id int64) error {
+	data, err := m.FindOne(id)
+	if err != nil {
+		return err
+	}
+
+	testUserInfoIdKey := fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, id)
+	testUserInfoNanosecondKey := fmt.Sprintf("%s%v", cacheTestUserInfoNanosecondPrefix, data.Nanosecond)
+	_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
+		query := fmt.Sprintf("delete from %s where id = ?", m.table)
+		return conn.Exec(query, id)
+	}, testUserInfoIdKey, testUserInfoNanosecondKey)
+	return err
+}
+
+func (m *TestUserInfoModel) formatPrimary(primary interface{}) string {
+	return fmt.Sprintf("%s%v", cacheTestUserInfoIdPrefix, primary)
+}
+
+func (m *TestUserInfoModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
+	query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
+	return conn.QueryRow(v, query, primary)
+}

+ 5 - 0
tools/goctl/model/sql/gen/testmodel/snake/vars.go

@@ -0,0 +1,5 @@
+package snake
+
+import "github.com/tal-tech/go-zero/core/stores/sqlx"
+
+var ErrNotFound = sqlx.ErrNotFound