sql

package
v0.1.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBatch = errors.New("invalid sql batch insert")
View Source
var ErrNotConfigured = errors.New("sql not configured")

Functions

func ClassifyStatement

func ClassifyStatement(statement string) (operation string, target string)

func NewNoop

func NewNoop() *noopDB

func WithTx

func WithTx(ctx context.Context, tx Tx) context.Context

Types

type BatchInsert

type BatchInsert struct {
	Table           string
	Columns         []string
	Rows            [][]any
	Dialect         Dialect
	MaxRows         int
	Ignore          bool
	OnConflict      []string
	OnDuplicateSets []string
}

type BatchStatement

type BatchStatement struct {
	Statement string
	Args      []any
	Rows      int
	Dialect   Dialect
}

func BuildBatchInsertStatements

func BuildBatchInsertStatements(batch BatchInsert) ([]BatchStatement, error)

type DB

type DB interface {
	Exec(ctx context.Context, query string, args ...any) (Result, error)
	Query(ctx context.Context, query string, args ...any) (Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Prepare(ctx context.Context, query string) (Stmt, error)
	Begin(ctx context.Context) (Tx, error)
	WithTransaction(ctx context.Context, fn func(context.Context, Tx) error) error
	BatchInsert(ctx context.Context, batch BatchInsert) (Result, error)
}

type Dialect

type Dialect string
const (
	DialectMySQL    Dialect = "mysql"
	DialectPostgres Dialect = "postgres"
)

type Option

type Option func(*Options)

func WithDriver

func WithDriver(driver string) Option

func WithName

func WithName(name string) Option

func WithQueryHooks

func WithQueryHooks(hooks ...QueryHook) Option

type Options

type Options struct {
	Driver string
	Name   string
	Hooks  []QueryHook
}

func NewOptions

func NewOptions(options ...Option) Options

type Page

type Page struct {
	Number int
	Size   int
	Order  string
}

type Query

type Query struct {
	Statement string
	Args      []any
	Operation string
	Target    string
}

type QueryHook

type QueryHook interface {
	BeforeQuery(ctx context.Context, metadata QueryMetadata) context.Context
	AfterQuery(ctx context.Context, metadata QueryMetadata)
}

type QueryHookFuncs

type QueryHookFuncs struct {
	Before func(ctx context.Context, metadata QueryMetadata) context.Context
	After  func(ctx context.Context, metadata QueryMetadata)
}

func (QueryHookFuncs) AfterQuery

func (h QueryHookFuncs) AfterQuery(ctx context.Context, metadata QueryMetadata)

func (QueryHookFuncs) BeforeQuery

func (h QueryHookFuncs) BeforeQuery(ctx context.Context, metadata QueryMetadata) context.Context

type QueryMetadata

type QueryMetadata struct {
	Name          string
	Driver        string
	Operation     string
	Target        string
	Statement     string
	Args          []any
	BatchRows     int
	InTransaction bool
	RowsAffected  int64
	LastInsertID  int64
	StartedAt     time.Time
	Duration      time.Duration
	Err           error
}

func MetadataForQuery

func MetadataForQuery(name string, driver string, statement string, args ...any) QueryMetadata

func (QueryMetadata) Clone

func (m QueryMetadata) Clone() QueryMetadata

type Result

type Result interface {
	RowsAffected() (int64, error)
	LastInsertID() (int64, error)
}

func Exec

func Exec(ctx context.Context, db DB, query string, args ...any) (Result, error)

type Row

type Row interface {
	Scan(dest ...any) error
}

func QueryRowContext

func QueryRowContext(ctx context.Context, db DB, query string, args ...any) Row

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Close() error
	Err() error
}

func QueryContext

func QueryContext(ctx context.Context, db DB, query string, args ...any) (Rows, error)

type Stmt

type Stmt interface {
	Exec(ctx context.Context, args ...any) (Result, error)
	Query(ctx context.Context, args ...any) (Rows, error)
	QueryRow(ctx context.Context, args ...any) Row
	Close() error
}

type Tx

type Tx interface {
	Exec(ctx context.Context, query string, args ...any) (Result, error)
	Query(ctx context.Context, query string, args ...any) (Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Prepare(ctx context.Context, query string) (Stmt, error)
	Commit() error
	Rollback() error
}

func TxFromContext

func TxFromContext(ctx context.Context) (Tx, bool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL