github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/internal/sql/adapter/sql.go (about)

     1  package adapter
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  
     7  	contextx "github.com/octohelm/x/context"
     8  )
     9  
    10  type SqlDo interface {
    11  	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    12  	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    13  }
    14  
    15  type sqlDoContext struct {
    16  }
    17  
    18  func ContextWithSqlDo(ctx context.Context, db SqlDo) context.Context {
    19  	return contextx.WithValue(ctx, sqlDoContext{}, db)
    20  }
    21  
    22  func SqlDoFromContext(ctx context.Context) SqlDo {
    23  	sqlDo, ok := ctx.Value(sqlDoContext{}).(SqlDo)
    24  	if ok {
    25  		return sqlDo
    26  	}
    27  	return nil
    28  }