github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/boil/db.go (about)

     1  package boil
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  )
     7  
     8  // Executor can perform SQL queries.
     9  type Executor interface {
    10  	Exec(query string, args ...interface{}) (sql.Result, error)
    11  	Query(query string, args ...interface{}) (*sql.Rows, error)
    12  	QueryRow(query string, args ...interface{}) *sql.Row
    13  }
    14  
    15  // ContextExecutor can perform SQL queries with context
    16  type ContextExecutor interface {
    17  	Executor
    18  
    19  	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    20  	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    21  	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
    22  }
    23  
    24  // Transactor can commit and rollback, on top of being able to execute queries.
    25  type Transactor interface {
    26  	Commit() error
    27  	Rollback() error
    28  
    29  	Executor
    30  }
    31  
    32  // Beginner begins transactions.
    33  type Beginner interface {
    34  	Begin() (*sql.Tx, error)
    35  }
    36  
    37  // Begin a transaction with the current global database handle.
    38  func Begin() (Transactor, error) {
    39  	creator, ok := currentDB.(Beginner)
    40  	if !ok {
    41  		panic("database does not support transactions")
    42  	}
    43  
    44  	return creator.Begin()
    45  }
    46  
    47  // ContextTransactor can commit and rollback, on top of being able to execute
    48  // context-aware queries.
    49  type ContextTransactor interface {
    50  	Commit() error
    51  	Rollback() error
    52  
    53  	ContextExecutor
    54  }
    55  
    56  // ContextBeginner allows creation of context aware transactions with options.
    57  type ContextBeginner interface {
    58  	BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
    59  }
    60  
    61  // BeginTx begins a transaction with the current global database handle.
    62  func BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
    63  	creator, ok := currentDB.(ContextBeginner)
    64  	if !ok {
    65  		panic("database does not support context-aware transactions")
    66  	}
    67  
    68  	return creator.BeginTx(ctx, opts)
    69  }