// Code generated by goctl. DO NOT EDIT. package models import ( "context" "database/sql" "fmt" "strings" "time" "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, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") roleRowsWithPlaceHolder = strings.Join(stringx.Remove(roleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cacheOaRoleIdPrefix = "cache:oa:role:id:" ) type ( roleModel interface { Insert(ctx context.Context, data *Role) (sql.Result, error) FindOne(ctx context.Context, id string) (*Role, error) Update(ctx context.Context, data *Role) error Delete(ctx context.Context, id string) error } defaultRoleModel struct { sqlc.CachedConn table string } Role struct { Id string `db:"id"` Created time.Time `db:"created"` Updated time.Time `db:"updated"` AppId string `db:"app_id"` Name string `db:"name"` Des sql.NullString `db:"des"` UserCount int64 `db:"user_count"` } ) 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) Delete(ctx context.Context, id string) error { oaRoleIdKey := fmt.Sprintf("%s%v", cacheOaRoleIdPrefix, 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) }, oaRoleIdKey) return err } func (m *defaultRoleModel) FindOne(ctx context.Context, id string) (*Role, error) { oaRoleIdKey := fmt.Sprintf("%s%v", cacheOaRoleIdPrefix, id) var resp Role err := m.QueryRowCtx(ctx, &resp, oaRoleIdKey, 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) Insert(ctx context.Context, data *Role) (sql.Result, error) { oaRoleIdKey := fmt.Sprintf("%s%v", cacheOaRoleIdPrefix, 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, roleRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.Id, data.Created, data.Updated, data.AppId, data.Name, data.Des, data.UserCount) }, oaRoleIdKey) return ret, err } func (m *defaultRoleModel) Update(ctx context.Context, data *Role) error { oaRoleIdKey := fmt.Sprintf("%s%v", cacheOaRoleIdPrefix, 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, roleRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, data.Created, data.Updated, data.AppId, data.Name, data.Des, data.UserCount, data.Id) }, oaRoleIdKey) return err } func (m *defaultRoleModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheOaRoleIdPrefix, 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 }