155 lines
5.3 KiB
Go
Executable File
155 lines
5.3 KiB
Go
Executable File
// 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 (
|
|
accountFieldNames = builder.RawFieldNames(&Account{})
|
|
accountRows = strings.Join(accountFieldNames, ",")
|
|
accountRowsExpectAutoSet = strings.Join(stringx.Remove(accountFieldNames, "`id`"), ",")
|
|
accountRowsWithPlaceHolder = strings.Join(stringx.Remove(accountFieldNames, "`id`"), "=?,") + "=?"
|
|
|
|
cacheAccountIdPrefix = "cache:account:id:"
|
|
cacheAccountAccountPrefix = "cache:account:account:"
|
|
)
|
|
|
|
type (
|
|
accountModel interface {
|
|
Insert(ctx context.Context, data *Account) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*Account, error)
|
|
FindOneByAccount(ctx context.Context, account string) (*Account, error)
|
|
Update(ctx context.Context, data *Account) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultAccountModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
Account struct {
|
|
Id int64 `db:"id"`
|
|
Account string `db:"account"`
|
|
Token string `db:"token"`
|
|
Platform int64 `db:"platform"` // 平台類型 1. ark 2. google
|
|
CreateTime int64 `db:"create_time"`
|
|
UpdateTime int64 `db:"update_time"`
|
|
}
|
|
)
|
|
|
|
func newAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultAccountModel {
|
|
return &defaultAccountModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`account`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultAccountModel) withSession(session sqlx.Session) *defaultAccountModel {
|
|
return &defaultAccountModel{
|
|
CachedConn: m.CachedConn.WithSession(session),
|
|
table: "`account`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultAccountModel) Delete(ctx context.Context, id int64) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
|
|
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, id)
|
|
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
return conn.ExecCtx(ctx, query, id)
|
|
}, accountAccountKey, accountIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultAccountModel) FindOne(ctx context.Context, id int64) (*Account, error) {
|
|
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, id)
|
|
var resp Account
|
|
err := m.QueryRowCtx(ctx, &resp, accountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", accountRows, 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 *defaultAccountModel) FindOneByAccount(ctx context.Context, account string) (*Account, error) {
|
|
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, account)
|
|
var resp Account
|
|
err := m.QueryRowIndexCtx(ctx, &resp, accountAccountKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
query := fmt.Sprintf("select %s from %s where `account` = ? limit 1", accountRows, m.table)
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, account); 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 *defaultAccountModel) Insert(ctx context.Context, data *Account) (sql.Result, error) {
|
|
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
|
|
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id)
|
|
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 (?, ?, ?, ?, ?)", m.table, accountRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query, data.Account, data.Token, data.Platform, data.CreateTime, data.UpdateTime)
|
|
}, accountAccountKey, accountIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultAccountModel) Update(ctx context.Context, newData *Account) error {
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
|
|
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id)
|
|
_, 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` = ?", m.table, accountRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query, newData.Account, newData.Token, newData.Platform, newData.CreateTime, newData.UpdateTime, newData.Id)
|
|
}, accountAccountKey, accountIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultAccountModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheAccountIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultAccountModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", accountRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultAccountModel) tableName() string {
|
|
return m.table
|
|
}
|