github.com/RevenueMonster/sqlike@v1.0.6/sqlike/transaction.go (about)

     1  package sqlike
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"errors"
     7  
     8  	"github.com/RevenueMonster/sqlike/sql/codec"
     9  	"github.com/RevenueMonster/sqlike/sql/dialect"
    10  	"github.com/RevenueMonster/sqlike/sql/driver"
    11  	sqlstmt "github.com/RevenueMonster/sqlike/sql/stmt"
    12  	"github.com/RevenueMonster/sqlike/sqlike/logs"
    13  )
    14  
    15  // SessionContext :
    16  type SessionContext interface {
    17  	context.Context
    18  	Table(name string) *Table
    19  	Prepare(query string) (*sql.Stmt, error)
    20  	Exec(query string, args ...interface{}) (sql.Result, error)
    21  	Query(query string, args ...interface{}) (*sql.Rows, error)
    22  	QueryRow(query string, args ...interface{}) *sql.Row
    23  	QueryStmt(query interface{}) (*Result, error)
    24  }
    25  
    26  // Transaction :
    27  type Transaction struct {
    28  	// transaction context
    29  	context.Context
    30  
    31  	// database name
    32  	dbName string
    33  
    34  	// default primary key
    35  	pk      string
    36  	client  *Client
    37  	driver  *sql.Tx
    38  	dialect dialect.Dialect
    39  	codec   codec.Codecer
    40  	logger  logs.Logger
    41  }
    42  
    43  // Prepare : PrepareContext creates a prepared statement for use within a transaction.
    44  func (tx *Transaction) Prepare(query string) (*sql.Stmt, error) {
    45  	return tx.driver.PrepareContext(tx, query)
    46  }
    47  
    48  // Exec : ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
    49  func (tx *Transaction) Exec(query string, args ...interface{}) (sql.Result, error) {
    50  	return tx.driver.ExecContext(tx, query, args...)
    51  }
    52  
    53  // Query : QueryContext executes a query that returns rows, typically a SELECT.
    54  func (tx *Transaction) Query(query string, args ...interface{}) (*sql.Rows, error) {
    55  	return tx.driver.QueryContext(tx, query, args...)
    56  }
    57  
    58  // QueryRow : QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
    59  func (tx *Transaction) QueryRow(query string, args ...interface{}) *sql.Row {
    60  	return tx.driver.QueryRowContext(tx, query, args...)
    61  }
    62  
    63  // Table :
    64  func (tx *Transaction) Table(name string) *Table {
    65  	return &Table{
    66  		dbName:  tx.dbName,
    67  		name:    name,
    68  		pk:      tx.pk,
    69  		client:  tx.client,
    70  		driver:  tx.driver,
    71  		dialect: tx.dialect,
    72  		codec:   tx.codec,
    73  		logger:  tx.logger,
    74  	}
    75  }
    76  
    77  // QueryStmt : QueryStmt support complex and advance query statement, make sure you executes a query that returns rows, typically a SELECT.
    78  func (tx *Transaction) QueryStmt(query interface{}) (*Result, error) {
    79  	if query == nil {
    80  		return nil, errors.New("sqlike: empty query statement")
    81  	}
    82  
    83  	stmt := sqlstmt.AcquireStmt(tx.dialect)
    84  	defer sqlstmt.ReleaseStmt(stmt)
    85  	if err := tx.dialect.SelectStmt(stmt, query); err != nil {
    86  		return nil, err
    87  	}
    88  	rows, err := driver.Query(
    89  		tx,
    90  		tx.driver,
    91  		stmt,
    92  		getLogger(tx.logger, true),
    93  	)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	rslt := new(Result)
    98  	rslt.cache = tx.client.cache
    99  	rslt.codec = tx.codec
   100  	rslt.rows = rows
   101  	rslt.columns, rslt.err = rows.Columns()
   102  	return rslt, rslt.err
   103  }
   104  
   105  // RollbackTransaction : Rollback aborts the transaction.
   106  func (tx *Transaction) RollbackTransaction() error {
   107  	return tx.driver.Rollback()
   108  }
   109  
   110  // CommitTransaction : Commit commits the transaction.
   111  func (tx *Transaction) CommitTransaction() error {
   112  	return tx.driver.Commit()
   113  }