1 Star 0 Fork 0

GarlicBoris/gozero-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.go 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
yanwc 提交于 2024-11-20 19:26 +08:00 . refactor:error
package sqlx
import (
"context"
"database/sql"
"errors"
"gitee.com/yanwc/gozero-utils/errx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
func ExecSql(session sqlx.Session, pre func() (string, []interface{}, error), done func(result sql.Result) error, errLabel string) error {
return ExecSqlCtx(context.Background(), session, pre, done, errLabel)
}
func ExecSqlCtx(ctx context.Context, session sqlx.Session, pre func() (string, []interface{}, error), done func(result sql.Result) error, errLabel string) error {
execSql, args, err := pre()
if err != nil {
return errx.New(errx.DB_QUERY, errx.WithMsgOption(errLabel), errx.WithErrorOption(err))
}
result, err := session.ExecCtx(ctx, execSql, args...)
if err != nil {
return errx.New(errx.DB, errx.WithMsgOption(errLabel), errx.WithErrorOption(err))
}
if done != nil {
if err := done(result); err != nil {
return err
}
}
return nil
}
func ExecInsertSql(session sqlx.Session, pre func() (string, []interface{}, error), done func(lastInsertId int64) error, errLabel string) error {
return ExecSqlCtx(context.Background(), session, pre, func(result sql.Result) error {
lastInsertId, err := result.LastInsertId()
if err != nil {
return err
}
if err := done(lastInsertId); err != nil {
return err
}
return nil
}, errLabel)
}
func queryRowCtx(ctx context.Context, session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string, must bool) error {
query, args, err := pre()
if err != nil {
return errx.New(errx.DB_QUERY, errx.WithMsgOption(errLabel), errx.WithErrorOption(err))
}
err = session.QueryRowCtx(ctx, data, query, args...)
if err != nil {
if errors.Is(err, sqlx.ErrNotFound) {
if must {
return errx.New(errx.DB_NOT_FOUND, errx.WithMsgOption(errLabel))
}
} else {
return errx.New(errx.DB_QUERY, errx.WithMsgOption(errLabel), errx.WithErrorOption(err))
}
}
return nil
}
func QueryRowCtx(ctx context.Context, session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
return queryRowCtx(ctx, session, pre, data, errLabel, false)
}
func QueryRowCtxMust(ctx context.Context, session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
return queryRowCtx(ctx, session, pre, data, errLabel, true)
}
func QueryRow(session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
return queryRowCtx(context.Background(), session, pre, data, errLabel, false)
}
func QueryRowMust(session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
return queryRowCtx(context.Background(), session, pre, data, errLabel, true)
}
func QueryRowsCtx(ctx context.Context, session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
query, args, err := pre()
if err != nil {
return errx.New(errx.DB_QUERY, errx.WithMsgOption(errLabel), errx.WithErrorOption(err))
}
err = session.QueryRowsCtx(ctx, data, query, args...)
if err != nil {
if errors.Is(err, sqlx.ErrNotFound) {
return errx.New(errx.DB_NOT_FOUND, errx.WithMsgOption(errLabel))
} else {
}
}
return nil
}
func QueryRows(session sqlx.Session, pre func() (string, []interface{}, error), data interface{}, errLabel string) error {
return QueryRowsCtx(context.Background(), session, pre, data, errLabel)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yanwc/gozero-utils.git
git@gitee.com:yanwc/gozero-utils.git
yanwc
gozero-utils
gozero-utils
v1.3.78

搜索帮助