sql

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTxNotFound is returned when transaction is not found in context
	ErrTxNotFound = errors.New("transaction not found in context")
)
View Source
var TxManager = &Manager{}

TxManager is the global transaction manager

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a SQL query builder

func New

func New() *Builder

New creates a new SQL builder

func (*Builder) Build

func (b *Builder) Build() (string, []interface{})

Build builds the SQL query string

func (*Builder) From

func (b *Builder) From(table string) *Builder

From specifies the table to query from

func (*Builder) GroupBy

func (b *Builder) GroupBy(fields ...string) *Builder

GroupBy adds a GROUP BY clause

func (*Builder) Having

func (b *Builder) Having(condition string, args ...interface{}) *Builder

Having adds a HAVING clause

func (*Builder) Join

func (b *Builder) Join(table, on string) *Builder

Join adds a JOIN clause

func (*Builder) LeftJoin

func (b *Builder) LeftJoin(table, on string) *Builder

LeftJoin adds a LEFT JOIN clause

func (*Builder) Limit

func (b *Builder) Limit(limit int) *Builder

Limit sets the LIMIT clause

func (*Builder) Offset

func (b *Builder) Offset(offset int) *Builder

Offset sets the OFFSET clause

func (*Builder) OrderBy

func (b *Builder) OrderBy(field string) *Builder

OrderBy adds an ORDER BY clause

func (*Builder) OrderByDesc

func (b *Builder) OrderByDesc(field string) *Builder

OrderByDesc adds a DESC ORDER BY clause

func (*Builder) Reset

func (b *Builder) Reset() *Builder

Reset resets the builder for reuse

func (*Builder) RightJoin

func (b *Builder) RightJoin(table, on string) *Builder

RightJoin adds a RIGHT JOIN clause

func (*Builder) Select

func (b *Builder) Select(fields ...string) *Builder

Select specifies the fields to select

func (*Builder) String

func (b *Builder) String() string

String returns the SQL query string (for debugging)

func (*Builder) Where

func (b *Builder) Where(condition string, args ...interface{}) *Builder

Where adds a WHERE clause

func (*Builder) WhereOr

func (b *Builder) WhereOr(condition string, args ...interface{}) *Builder

WhereOr adds an OR condition to WHERE clause

type Manager

type Manager struct{}

Manager manages database transactions

func (*Manager) ExecuteInTx

func (m *Manager) ExecuteInTx(ctx context.Context, db defs.SQLDatabase, fn func(context.Context) error) error

ExecuteInTx executes a function within a transaction If the function returns an error, the transaction is rolled back Otherwise, the transaction is committed

func (*Manager) ExecuteInTxWithOpts

func (m *Manager) ExecuteInTxWithOpts(ctx context.Context, db defs.SQLDatabase, opts *defs.TxOptions, fn func(context.Context) error) error

ExecuteInTxWithOpts executes a function within a transaction with options

func (*Manager) FromContext

func (m *Manager) FromContext(ctx context.Context) (defs.Transaction, bool)

FromContext retrieves transaction from context

func (*Manager) IsInTx

func (m *Manager) IsInTx(ctx context.Context) bool

IsInTx checks if there's an active transaction in context

func (*Manager) MustFromContext

func (m *Manager) MustFromContext(ctx context.Context) defs.Transaction

MustFromContext retrieves transaction from context or panics

func (*Manager) WithContext

func (m *Manager) WithContext(ctx context.Context, tx defs.Transaction) context.Context

WithContext adds transaction to context

type Pager

type Pager struct {
	Page      int   `json:"page"`       // Current page number (1-based)
	PageSize  int   `json:"page_size"`  // Number of items per page
	Total     int64 `json:"total"`      // Total number of items
	TotalPage int   `json:"total_page"` // Total number of pages
}

Pager represents a pagination result

func NewPager

func NewPager(page, pageSize int) *Pager

NewPager creates a new pager with default values

func (*Pager) HasNext

func (p *Pager) HasNext() bool

HasNext returns true if there is a next page

func (*Pager) HasPrev

func (p *Pager) HasPrev() bool

HasPrev returns true if there is a previous page

func (*Pager) Limit

func (p *Pager) Limit() int

Limit returns the LIMIT value for SQL query

func (*Pager) Offset

func (p *Pager) Offset() int

Offset returns the OFFSET value for SQL query

func (*Pager) PageInfo

func (p *Pager) PageInfo() map[string]interface{}

PageInfo returns pagination information for API response

func (*Pager) SetTotal

func (p *Pager) SetTotal(total int64)

SetTotal sets the total number of items and calculates total pages

func (*Pager) String

func (p *Pager) String() string

String returns the string representation

type PaginateResult

type PaginateResult struct {
	Items interface{} `json:"items"`
	Pager *Pager      `json:"pager"`
}

PaginateResult wraps a slice with pagination info

func NewPaginateResult

func NewPaginateResult(items interface{}, pager *Pager) *PaginateResult

NewPaginateResult creates a new paginated result

Jump to

Keyboard shortcuts

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