// 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 ( roleFieldNames = builder.RawFieldNames(&Role{}) roleRows = strings.Join(roleFieldNames, ",") roleRowsExpectAutoSet = strings.Join(stringx.Remove(roleFieldNames, "`id`"), ",") roleRowsWithPlaceHolder = strings.Join(stringx.Remove(roleFieldNames, "`id`"), "=?,") + "=?" cacheRoleIdPrefix = "cache:role:id:" cacheRoleDisplayNamePrefix = "cache:role:displayName:" cacheRoleRoleIdPrefix = "cache:role:roleId:" ) type ( roleModel interface { Insert(ctx context.Context, data *Role) (sql.Result, error) FindOne(ctx context.Context, id int64) (*Role, error) FindOneByDisplayName(ctx context.Context, displayName string) (*Role, error) FindOneByRoleId(ctx context.Context, roleId string) (*Role, error) Update(ctx context.Context, data *Role) error Delete(ctx context.Context, id int64) error } defaultRoleModel struct { sqlc.CachedConn table string } Role struct { Id int64 `db:"id"` // PK RoleId string `db:"role_id"` DisplayName string `db:"display_name"` // 名稱 Status int64 `db:"status"` // 狀態 1: 啟用, 2: 禁用 CreateTime int64 `db:"create_time"` // 創建時間 UpdateTime int64 `db:"update_time"` // 更新時間 } ) func newRoleModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultRoleModel { return &defaultRoleModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`role`", } } func (m *defaultRoleModel) withSession(session sqlx.Session) *defaultRoleModel { return &defaultRoleModel{ CachedConn: m.CachedConn.WithSession(session), table: "`role`", } } func (m *defaultRoleModel) Delete(ctx context.Context, id int64) error { data, err := m.FindOne(ctx, id) if err != nil { return err } roleDisplayNameKey := fmt.Sprintf("%s%v", cacheRoleDisplayNamePrefix, data.DisplayName) roleIdKey := fmt.Sprintf("%s%v", cacheRoleIdPrefix, id) roleRoleIdKey := fmt.Sprintf("%s%v", cacheRoleRoleIdPrefix, data.RoleId) _, 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) }, roleDisplayNameKey, roleIdKey, roleRoleIdKey) return err } func (m *defaultRoleModel) FindOne(ctx context.Context, id int64) (*Role, error) { roleIdKey := fmt.Sprintf("%s%v", cacheRoleIdPrefix, id) var resp Role err := m.QueryRowCtx(ctx, &resp, roleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", roleRows, 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 *defaultRoleModel) FindOneByDisplayName(ctx context.Context, displayName string) (*Role, error) { roleDisplayNameKey := fmt.Sprintf("%s%v", cacheRoleDisplayNamePrefix, displayName) var resp Role err := m.QueryRowIndexCtx(ctx, &resp, roleDisplayNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `display_name` = ? limit 1", roleRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, displayName); 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 *defaultRoleModel) FindOneByRoleId(ctx context.Context, roleId string) (*Role, error) { roleRoleIdKey := fmt.Sprintf("%s%v", cacheRoleRoleIdPrefix, roleId) var resp Role err := m.QueryRowIndexCtx(ctx, &resp, roleRoleIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `role_id` = ? limit 1", roleRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, roleId); 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 *defaultRoleModel) Insert(ctx context.Context, data *Role) (sql.Result, error) { roleDisplayNameKey := fmt.Sprintf("%s%v", cacheRoleDisplayNamePrefix, data.DisplayName) roleIdKey := fmt.Sprintf("%s%v", cacheRoleIdPrefix, data.Id) roleRoleIdKey := fmt.Sprintf("%s%v", cacheRoleRoleIdPrefix, data.RoleId) 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, roleRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.RoleId, data.DisplayName, data.Status, data.CreateTime, data.UpdateTime) }, roleDisplayNameKey, roleIdKey, roleRoleIdKey) return ret, err } func (m *defaultRoleModel) Update(ctx context.Context, newData *Role) error { data, err := m.FindOne(ctx, newData.Id) if err != nil { return err } roleDisplayNameKey := fmt.Sprintf("%s%v", cacheRoleDisplayNamePrefix, data.DisplayName) roleIdKey := fmt.Sprintf("%s%v", cacheRoleIdPrefix, data.Id) roleRoleIdKey := fmt.Sprintf("%s%v", cacheRoleRoleIdPrefix, data.RoleId) _, 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, roleRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, newData.RoleId, newData.DisplayName, newData.Status, newData.CreateTime, newData.UpdateTime, newData.Id) }, roleDisplayNameKey, roleIdKey, roleRoleIdKey) return err } func (m *defaultRoleModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheRoleIdPrefix, primary) } func (m *defaultRoleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", roleRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultRoleModel) tableName() string { return m.table }