github.com/Accefy/pop@v0.0.0-20230428174248-e9f677eab5b9/store.go (about)

     1  package pop
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  
     7  	"github.com/jmoiron/sqlx"
     8  )
     9  
    10  // Store is an interface that must be implemented in order for Pop
    11  // to be able to use the value as a way of talking to a datastore.
    12  type store interface {
    13  	Select(interface{}, string, ...interface{}) error
    14  	Get(interface{}, string, ...interface{}) error
    15  	NamedExec(string, interface{}) (sql.Result, error)
    16  	NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
    17  	Exec(string, ...interface{}) (sql.Result, error)
    18  	PrepareNamed(string) (*sqlx.NamedStmt, error)
    19  	Transaction() (*Tx, error)
    20  	Rollback() error
    21  	Commit() error
    22  	Close() error
    23  
    24  	// Context versions to wrap with contextStore
    25  	SelectContext(context.Context, interface{}, string, ...interface{}) error
    26  	GetContext(context.Context, interface{}, string, ...interface{}) error
    27  	NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
    28  	NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
    29  	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
    30  	PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error)
    31  	TransactionContext(context.Context) (*Tx, error)
    32  	TransactionContextOptions(context.Context, *sql.TxOptions) (*Tx, error)
    33  }
    34  
    35  // ContextStore wraps a store with a Context, so passes it with the functions that don't take it.
    36  type contextStore struct {
    37  	store
    38  	ctx context.Context
    39  }
    40  
    41  func (s contextStore) Transaction() (*Tx, error) {
    42  	return s.store.TransactionContext(s.ctx)
    43  }
    44  func (s contextStore) Select(dest interface{}, query string, args ...interface{}) error {
    45  	return s.store.SelectContext(s.ctx, dest, query, args...)
    46  }
    47  func (s contextStore) Get(dest interface{}, query string, args ...interface{}) error {
    48  	return s.store.GetContext(s.ctx, dest, query, args...)
    49  }
    50  func (s contextStore) NamedExec(query string, arg interface{}) (sql.Result, error) {
    51  	return s.store.NamedExecContext(s.ctx, query, arg)
    52  }
    53  func (s contextStore) Exec(query string, args ...interface{}) (sql.Result, error) {
    54  	return s.store.ExecContext(s.ctx, query, args...)
    55  }
    56  func (s contextStore) PrepareNamed(query string) (*sqlx.NamedStmt, error) {
    57  	return s.store.PrepareNamedContext(s.ctx, query)
    58  }
    59  
    60  func (s contextStore) Context() context.Context {
    61  	return s.ctx
    62  }