github.com/rjgonzale/pop/v5@v5.1.3-dev/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  }
    31  
    32  // ContextStore wraps a store with a Context, so passes it with the functions that don't take it.
    33  type contextStore struct {
    34  	store
    35  	ctx context.Context
    36  }
    37  
    38  func (s contextStore) Transaction() (*Tx, error) {
    39  	return s.store.TransactionContext(s.ctx)
    40  }
    41  func (s contextStore) Select(dest interface{}, query string, args ...interface{}) error {
    42  	return s.store.SelectContext(s.ctx, dest, query, args...)
    43  }
    44  func (s contextStore) Get(dest interface{}, query string, args ...interface{}) error {
    45  	return s.store.GetContext(s.ctx, dest, query, args...)
    46  }
    47  func (s contextStore) NamedExec(query string, arg interface{}) (sql.Result, error) {
    48  	return s.store.NamedExecContext(s.ctx, query, arg)
    49  }
    50  func (s contextStore) Exec(query string, args ...interface{}) (sql.Result, error) {
    51  	return s.store.ExecContext(s.ctx, query, args...)
    52  }
    53  func (s contextStore) PrepareNamed(query string) (*sqlx.NamedStmt, error) {
    54  	return s.store.PrepareNamedContext(s.ctx, query)
    55  }