From eefbc32c86aedc44f9db1762b3e804d0ae629593 Mon Sep 17 00:00:00 2001 From: sulinehk Date: Sat, 17 Feb 2024 19:43:23 +0800 Subject: [PATCH] generate user model --- go.mod | 1 + go.sum | 4 + service/user/rpc/model/app_user_model.go | 27 ++++ service/user/rpc/model/app_user_model_gen.go | 151 +++++++++++++++++++ service/user/rpc/model/vars.go | 5 + 5 files changed, 188 insertions(+) create mode 100755 service/user/rpc/model/app_user_model.go create mode 100755 service/user/rpc/model/app_user_model_gen.go create mode 100644 service/user/rpc/model/vars.go diff --git a/go.mod b/go.mod index 34b6840..79625d3 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.4 // indirect + github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.3 // indirect diff --git a/go.sum b/go.sum index d6ab6a1..fe75e30 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= +github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE= github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= github.com/alicebob/miniredis/v2 v2.31.1 h1:7XAt0uUg3DtwEKW5ZAGa+K7FZV2DdKQo5K/6TTnfX8Y= @@ -40,6 +42,8 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU= github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/service/user/rpc/model/app_user_model.go b/service/user/rpc/model/app_user_model.go new file mode 100755 index 0000000..f8ac301 --- /dev/null +++ b/service/user/rpc/model/app_user_model.go @@ -0,0 +1,27 @@ +package model + +import ( + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +var _ AppUserModel = (*customAppUserModel)(nil) + +type ( + // AppUserModel is an interface to be customized, add more methods here, + // and implement the added methods in customAppUserModel. + AppUserModel interface { + appUserModel + } + + customAppUserModel struct { + *defaultAppUserModel + } +) + +// NewAppUserModel returns a model for the database table. +func NewAppUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) AppUserModel { + return &customAppUserModel{ + defaultAppUserModel: newAppUserModel(conn, c, opts...), + } +} diff --git a/service/user/rpc/model/app_user_model_gen.go b/service/user/rpc/model/app_user_model_gen.go new file mode 100755 index 0000000..105f423 --- /dev/null +++ b/service/user/rpc/model/app_user_model_gen.go @@ -0,0 +1,151 @@ +// Code generated by goctl. DO NOT EDIT. + +package model + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + appUserFieldNames = builder.RawFieldNames(&AppUser{}, true) + appUserRows = strings.Join(appUserFieldNames, ",") + appUserRowsExpectAutoSet = strings.Join(stringx.Remove(appUserFieldNames, "id", "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at"), ",") + appUserRowsWithPlaceHolder = builder.PostgreSqlJoin(stringx.Remove(appUserFieldNames, "id", "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at")) + + cachePublicAppUserIdPrefix = "cache:public:appUser:id:" + cachePublicAppUserUuidPrefix = "cache:public:appUser:uuid:" +) + +type ( + appUserModel interface { + Insert(ctx context.Context, data *AppUser) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*AppUser, error) + FindOneByUuid(ctx context.Context, uuid string) (*AppUser, error) + Update(ctx context.Context, data *AppUser) error + Delete(ctx context.Context, id int64) error + } + + defaultAppUserModel struct { + sqlc.CachedConn + table string + } + + AppUser struct { + Id int64 `db:"id"` + Uuid string `db:"uuid"` + Created sql.NullTime `db:"created"` + Url sql.NullString `db:"url"` + SelfLink sql.NullString `db:"self_link"` + DisplayName sql.NullString `db:"display_name"` + About sql.NullString `db:"about"` + Language sql.NullString `db:"language"` + Country sql.NullString `db:"country"` + Variant sql.NullString `db:"variant"` + } +) + +func newAppUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultAppUserModel { + return &defaultAppUserModel{ + CachedConn: sqlc.NewConn(conn, c, opts...), + table: `"public"."app_user"`, + } +} + +func (m *defaultAppUserModel) Delete(ctx context.Context, id int64) error { + data, err := m.FindOne(ctx, id) + if err != nil { + return err + } + + publicAppUserIdKey := fmt.Sprintf("%s%v", cachePublicAppUserIdPrefix, id) + publicAppUserUuidKey := fmt.Sprintf("%s%v", cachePublicAppUserUuidPrefix, data.Uuid) + _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("delete from %s where id = $1", m.table) + return conn.ExecCtx(ctx, query, id) + }, publicAppUserIdKey, publicAppUserUuidKey) + return err +} + +func (m *defaultAppUserModel) FindOne(ctx context.Context, id int64) (*AppUser, error) { + publicAppUserIdKey := fmt.Sprintf("%s%v", cachePublicAppUserIdPrefix, id) + var resp AppUser + err := m.QueryRowCtx(ctx, &resp, publicAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := fmt.Sprintf("select %s from %s where id = $1 limit 1", appUserRows, m.table) + return conn.QueryRowCtx(ctx, v, query, id) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultAppUserModel) FindOneByUuid(ctx context.Context, uuid string) (*AppUser, error) { + publicAppUserUuidKey := fmt.Sprintf("%s%v", cachePublicAppUserUuidPrefix, uuid) + var resp AppUser + err := m.QueryRowIndexCtx(ctx, &resp, publicAppUserUuidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { + query := fmt.Sprintf("select %s from %s where uuid = $1 limit 1", appUserRows, m.table) + if err := conn.QueryRowCtx(ctx, &resp, query, uuid); 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 *defaultAppUserModel) Insert(ctx context.Context, data *AppUser) (sql.Result, error) { + publicAppUserIdKey := fmt.Sprintf("%s%v", cachePublicAppUserIdPrefix, data.Id) + publicAppUserUuidKey := fmt.Sprintf("%s%v", cachePublicAppUserUuidPrefix, data.Uuid) + ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("insert into %s (%s) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)", m.table, appUserRowsExpectAutoSet) + return conn.ExecCtx(ctx, query, data.Uuid, data.Created, data.Url, data.SelfLink, data.DisplayName, data.About, data.Language, data.Country, data.Variant) + }, publicAppUserIdKey, publicAppUserUuidKey) + return ret, err +} + +func (m *defaultAppUserModel) Update(ctx context.Context, newData *AppUser) error { + data, err := m.FindOne(ctx, newData.Id) + if err != nil { + return err + } + + publicAppUserIdKey := fmt.Sprintf("%s%v", cachePublicAppUserIdPrefix, data.Id) + publicAppUserUuidKey := fmt.Sprintf("%s%v", cachePublicAppUserUuidPrefix, data.Uuid) + _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("update %s set %s where id = $1", m.table, appUserRowsWithPlaceHolder) + return conn.ExecCtx(ctx, query, newData.Id, newData.Uuid, newData.Created, newData.Url, newData.SelfLink, newData.DisplayName, newData.About, newData.Language, newData.Country, newData.Variant) + }, publicAppUserIdKey, publicAppUserUuidKey) + return err +} + +func (m *defaultAppUserModel) formatPrimary(primary any) string { + return fmt.Sprintf("%s%v", cachePublicAppUserIdPrefix, primary) +} + +func (m *defaultAppUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { + query := fmt.Sprintf("select %s from %s where id = $1 limit 1", appUserRows, m.table) + return conn.QueryRowCtx(ctx, v, query, primary) +} + +func (m *defaultAppUserModel) tableName() string { + return m.table +} diff --git a/service/user/rpc/model/vars.go b/service/user/rpc/model/vars.go new file mode 100644 index 0000000..69ca814 --- /dev/null +++ b/service/user/rpc/model/vars.go @@ -0,0 +1,5 @@ +package model + +import "github.com/zeromicro/go-zero/core/stores/sqlx" + +var ErrNotFound = sqlx.ErrNotFound