types

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package types contains the core database interface definitions for go-bricks. These interfaces are separate from the main database package to avoid import cycles and to make them easily accessible for mocking and testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	// Query execution
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

	// Prepared statements
	Prepare(ctx context.Context, query string) (Statement, error)

	// Transaction support
	Begin(ctx context.Context) (Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)

	// Health and diagnostics
	Health(ctx context.Context) error
	Stats() (map[string]any, error)

	// Connection management
	Close() error

	// Database-specific features
	DatabaseType() string

	// Migration support
	GetMigrationTable() string
	CreateMigrationTable(ctx context.Context) error
}

Interface defines the common database operations supported by the framework. This is the main interface that applications and modules should depend on for database operations, allowing for easy mocking and testing.

type QueryBuilderInterface added in v0.8.1

type QueryBuilderInterface interface {
	// Vendor information
	Vendor() string

	// Query builders
	Select(columns ...string) SelectQueryBuilder
	Insert(table string) squirrel.InsertBuilder
	InsertWithColumns(table string, columns ...string) squirrel.InsertBuilder
	Update(table string) squirrel.UpdateBuilder
	Delete(table string) squirrel.DeleteBuilder

	// Vendor-specific helpers
	BuildCaseInsensitiveLike(column, value string) squirrel.Sqlizer
	BuildUpsert(table string, conflictColumns []string, insertColumns, updateColumns map[string]any) (query string, args []any, err error)

	// Database function builders
	BuildCurrentTimestamp() string
	BuildUUIDGeneration() string
	BuildBooleanValue(value bool) any

	// Identifier escaping
	EscapeIdentifier(identifier string) string
}

QueryBuilderInterface defines the interface for vendor-specific SQL query building. This interface allows for dependency injection and mocking of query builders, enabling unit testing of business logic that constructs queries without actually generating SQL strings.

type Row added in v0.9.0

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

Row represents a single result set row with basic scanning behaviour.

func NewRowFromSQL added in v0.9.0

func NewRowFromSQL(row *sql.Row) Row

NewRowFromSQL wraps the provided *sql.Row in a Row. If row is nil, NewRowFromSQL returns nil.

type SelectQueryBuilder added in v0.11.0

type SelectQueryBuilder interface {
	// Core SELECT builder methods
	From(from ...string) SelectQueryBuilder
	Join(join string, rest ...any) SelectQueryBuilder
	LeftJoin(join string, rest ...any) SelectQueryBuilder
	RightJoin(join string, rest ...any) SelectQueryBuilder
	InnerJoin(join string, rest ...any) SelectQueryBuilder
	CrossJoin(join string, rest ...any) SelectQueryBuilder
	GroupBy(groupBys ...string) SelectQueryBuilder
	Having(pred any, rest ...any) SelectQueryBuilder
	OrderBy(orderBys ...string) SelectQueryBuilder
	Limit(limit uint64) SelectQueryBuilder
	Offset(offset uint64) SelectQueryBuilder
	Paginate(limit, offset uint64) SelectQueryBuilder

	// Type-safe WHERE clause methods
	WhereEq(column string, value any) SelectQueryBuilder
	WhereNotEq(column string, value any) SelectQueryBuilder
	WhereLt(column string, value any) SelectQueryBuilder
	WhereLte(column string, value any) SelectQueryBuilder
	WhereGt(column string, value any) SelectQueryBuilder
	WhereGte(column string, value any) SelectQueryBuilder
	WhereIn(column string, values any) SelectQueryBuilder
	WhereNotIn(column string, values any) SelectQueryBuilder
	WhereLike(column, pattern string) SelectQueryBuilder
	WhereNull(column string) SelectQueryBuilder
	WhereNotNull(column string) SelectQueryBuilder
	WhereBetween(column string, lowerBound, upperBound any) SelectQueryBuilder
	WhereRaw(condition string, args ...any) SelectQueryBuilder

	// SQL generation
	ToSQL() (sql string, args []any, err error)
}

SelectQueryBuilder defines the interface for enhanced SELECT query building with type safety. This interface extends basic squirrel.SelectBuilder functionality with additional methods for type-safe WHERE clauses, JOIN operations, and vendor-specific query features.

type Statement

type Statement interface {
	// Query execution
	Query(ctx context.Context, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, args ...any) Row
	Exec(ctx context.Context, args ...any) (sql.Result, error)

	// Statement management
	Close() error
}

Statement defines the interface for prepared statements

type Tx

type Tx interface {
	// Query execution within transaction
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

	// Prepared statements within transaction
	Prepare(ctx context.Context, query string) (Statement, error)

	// Transaction control
	Commit() error
	Rollback() error
}

Tx defines the interface for database transactions

type Vendor added in v0.9.0

type Vendor = string

Database vendor identifiers shared across the database packages.

const (
	PostgreSQL Vendor = "postgresql"
	Oracle     Vendor = "oracle"
	MongoDB    Vendor = "mongodb"
)

Jump to

Keyboard shortcuts

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