db use context method
This commit is contained in:
parent
be58a35245
commit
3c3ed73971
|
@ -15,6 +15,14 @@ import (
|
||||||
var safeDb = safety.NewVar[*sqlx.DB](nil)
|
var safeDb = safety.NewVar[*sqlx.DB](nil)
|
||||||
var showQuerySql func() bool
|
var showQuerySql func() bool
|
||||||
|
|
||||||
|
func GetSqlxDB() *sqlx.DB {
|
||||||
|
return safeDb.Load()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetSqlxDB(db *sqlx.DB) {
|
||||||
|
safeDb.Store(db)
|
||||||
|
}
|
||||||
|
|
||||||
func InitDb() (*safety.Var[*sqlx.DB], error) {
|
func InitDb() (*safety.Var[*sqlx.DB], error) {
|
||||||
c := config.GetConfig()
|
c := config.GetConfig()
|
||||||
dsn := c.Mysql.Dsn.GetDsn()
|
dsn := c.Mysql.Dsn.GetDsn()
|
||||||
|
|
|
@ -41,14 +41,14 @@ func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ..
|
||||||
if v != "" {
|
if v != "" {
|
||||||
switch v {
|
switch v {
|
||||||
case "string":
|
case "string":
|
||||||
return ToMapSlice(db, dest.(*[]map[string]string), sql, params...)
|
return ToMapSlice(ctx, db, dest.(*[]map[string]string), sql, params...)
|
||||||
case "scanner":
|
case "scanner":
|
||||||
fn := ctx.Value("fn")
|
fn := ctx.Value("fn")
|
||||||
return Scanner[any](db, dest, sql, params...)(fn.(func(any)))
|
return Scanner[any](ctx, db, dest, sql, params...)(fn.(func(any)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.Select(dest, sql, params...)
|
return db.SelectContext(ctx, dest, sql, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error {
|
func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error {
|
||||||
|
@ -57,15 +57,15 @@ func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...an
|
||||||
if v != "" {
|
if v != "" {
|
||||||
switch v {
|
switch v {
|
||||||
case "string":
|
case "string":
|
||||||
return GetToMap(db, dest.(*map[string]string), sql, params...)
|
return GetToMap(ctx, db, dest.(*map[string]string), sql, params...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return db.Get(dest, sql, params...)
|
return db.GetContext(ctx, dest, sql, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Scanner[T any](db *sqlx.DB, v T, s string, params ...any) func(func(T)) error {
|
func Scanner[T any](ctx context.Context, db *sqlx.DB, v T, s string, params ...any) func(func(T)) error {
|
||||||
return func(fn func(T)) error {
|
return func(fn func(T)) error {
|
||||||
rows, err := db.Queryx(s, params...)
|
rows, err := db.QueryxContext(ctx, s, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -80,8 +80,8 @@ func Scanner[T any](db *sqlx.DB, v T, s string, params ...any) func(func(T)) err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToMapSlice[V any](db *sqlx.DB, dest *[]map[string]V, sql string, params ...any) (err error) {
|
func ToMapSlice[V any](ctx context.Context, db *sqlx.DB, dest *[]map[string]V, sql string, params ...any) (err error) {
|
||||||
rows, err := db.Query(sql, params...)
|
rows, err := db.QueryContext(ctx, sql, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -113,8 +113,8 @@ func ToMapSlice[V any](db *sqlx.DB, dest *[]map[string]V, sql string, params ...
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetToMap[V any](db *sqlx.DB, dest *map[string]V, sql string, params ...any) (err error) {
|
func GetToMap[V any](ctx context.Context, db *sqlx.DB, dest *map[string]V, sql string, params ...any) (err error) {
|
||||||
rows := db.QueryRowx(sql, params...)
|
rows := db.QueryRowxContext(ctx, sql, params...)
|
||||||
columns, err := rows.Columns()
|
columns, err := rows.Columns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user