github.com/paweljw/pop/v5@v5.4.6/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  	Exec(string, ...interface{}) (sql.Result, error)
    17  	PrepareNamed(string) (*sqlx.NamedStmt, error)
    18  	Transaction() (*Tx, error)
    19  	Rollback() error
    20  	Commit() error
    21  	Close() error
    22  
    23  	// Context versions to wrap with contextStore
    24  	SelectContext(context.Context, interface{}, string, ...interface{}) error
    25  	GetContext(context.Context, interface{}, string, ...interface{}) error
    26  	NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
    27  	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
    28  	PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error)
    29  	TransactionContext(context.Context) (*Tx, error)
    30  	TransactionContextOptions(context.Context, *sql.TxOptions) (*Tx, error)
    31  }
    32  
    33  // ContextStore wraps a store with a Context, so passes it with the functions that don't take it.
    34  type contextStore struct {
    35  	store
    36  	ctx context.Context
    37  }
    38  
    39  func (s contextStore) Transaction() (*Tx, error) {
    40  	return s.store.TransactionContext(s.ctx)
    41  }
    42  func (s contextStore) Select(dest interface{}, query string, args ...interface{}) error {
    43  	return s.store.SelectContext(s.ctx, dest, query, args...)
    44  }
    45  func (s contextStore) Get(dest interface{}, query string, args ...interface{}) error {
    46  	return s.store.GetContext(s.ctx, dest, query, args...)
    47  }
    48  func (s contextStore) NamedExec(query string, arg interface{}) (sql.Result, error) {
    49  	return s.store.NamedExecContext(s.ctx, query, arg)
    50  }
    51  func (s contextStore) Exec(query string, args ...interface{}) (sql.Result, error) {
    52  	return s.store.ExecContext(s.ctx, query, args...)
    53  }
    54  func (s contextStore) PrepareNamed(query string) (*sqlx.NamedStmt, error) {
    55  	return s.store.PrepareNamedContext(s.ctx, query)
    56  }
    57  
    58  func (s contextStore) Context() context.Context {
    59  	return s.ctx
    60  }