Documentation
¶
Index ¶
- Variables
- func Exec(ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, args ...any) error
- func ExecWithTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions, ...) error
- func IsNil[T comparable](v T) bool
- func Nil[T any]() T
- func Query[T any](ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, newReceiver func() T, ...) ([]T, error)
- func QueryOne[T any](ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, newReceiver func() T, ...) (T, error)
- func QueryVal[T any](ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, args ...any) (T, error)
- func QueryWithTx[T any](ctx context.Context, db *sql.DB, opts *sql.TxOptions, ...) (T, error)
- func ToSliceOfAny[T any](slice ...T) []any
- type BaseStmt
- type Dao
- type DaoBuilder
- type DaoExecStmt
- type DaoQueryOneStmt
- type DaoQueryPageStmt
- type DaoQueryStmt
- type DaoQueryValStmt
- type Entity
- type ExecStmt
- type GenericEntity
- type Page
- type Paging
- type QueryOneStmt
- type QueryPageStmt
- type QueryStmt
- type QueryValStmt
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when an entity cannot be found ErrNotFound = errors.New("gosql: entity not found") // ErrVersionMismatch is returned when an entity's version doesn't match the expected version ErrVersionMismatch = errors.New("gosql: version mismatch - entity was modified") )
var ( RO = &sql.TxOptions{ReadOnly: true} // RW represents read-write transaction options RW = &sql.TxOptions{ReadOnly: false} // TxKey is the context key used to store and retrieve transaction objects TxKey = txKey{} )
RO represents read-only transaction options
Functions ¶
func ExecWithTx ¶
func ExecWithTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions, operation func(context.Context, *sql.Tx) error) error
ExecWithTx executes an operation within a transaction If a transaction already exists in the context, it will be reused
func IsNil ¶
func IsNil[T comparable](v T) bool
IsNil checks if a value is the zero value of its type
func Query ¶
func Query[T any](ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, newReceiver func() T, dstFields func(T) []any, args ...any) ([]T, error)
Query executes a SQL query and returns a slice of results
func QueryOne ¶
func QueryOne[T any](ctx context.Context, tx *sql.Tx, stmt *sql.Stmt, newReceiver func() T, dstFields func(T) []any, args ...any) (T, error)
QueryOne executes a SQL query and returns a single result
func QueryWithTx ¶
func QueryWithTx[T any](ctx context.Context, db *sql.DB, opts *sql.TxOptions, operation func(context.Context, *sql.Tx) (T, error)) (T, error)
QueryWithTx executes an operation that returns a result within a transaction If a transaction already exists in the context, it will be reused
func ToSliceOfAny ¶
ToSliceOfAny converts a slice of values to a slice of interface{} values
Types ¶
type Dao ¶
type Dao[T Entity] interface { Save(ctx context.Context, entities ...T) error FindById(ctx context.Context, id uuid.UUID) (T, error) FindOneByStmt(ctx context.Context, stmt *QueryOneStmt[T], args ...any) (T, error) ListByStmt(ctx context.Context, stmt *QueryStmt[T], args ...any) ([]T, error) ListAll(ctx context.Context) ([]T, error) ListPageByStmt(ctx context.Context, stmt *QueryPageStmt[T], paging Paging, args ...any) (Page[T], error) ListPage(ctx context.Context, paging Paging) (Page[T], error) Delete(ctx context.Context, entities ...T) error DeleteCascade(ctx context.Context, entities ...T) error DeleteByIds(ctx context.Context, ids ...uuid.UUID) error DeleteByIdsCascade(ctx context.Context, ids ...uuid.UUID) error Close(ctx context.Context) error }
Dao defines the interface for data access objects that manage entities
type DaoBuilder ¶
type DaoBuilder[T Entity] struct { //DB: SQL database connection to use for all operations DB *sql.DB //InsertStmt: Statement for inserting new entities InsertStmt *DaoExecStmt //UpdateStmt: Statement for updating existing entities UpdateStmt *DaoExecStmt //GetByIdStmt: Statement for retrieving a single entity by ID GetByIdStmt *DaoQueryOneStmt[T] //ListAllStmt: Statement for retrieving all entities ListAllStmt *DaoQueryStmt[T] //ListAllPageStmt: Statement for retrieving paginated results of all entities ListAllPageStmt *DaoQueryPageStmt[T] //DeleteByIdStmt: Statement for deleting entity by its ID DeleteByIdStmt *DaoExecStmt //NewReceiver: Function that returns a new instance of the entity NewReceiver func() T //Receive: Function that returns the arguments for the update statement for a given entity Receive func(T) []any //InsertArgs: Function that returns the arguments for the insert statement for a given entity InsertArgs func(T) []any //UpdateArgs: Function that returns the arguments for the update statement for a given entity UpdateArgs func(T) []any //SaveChildren: Function that saves child entities associated with the parent entity SaveChildren func(ctx context.Context, tx *sql.Tx, e T) error //LoadChildren: Function that loads child entities associated with the parent entity LoadChildren func(ctx context.Context, tx *sql.Tx, e T) error //DeleteChildren: Function that deletes child entities associated with the parent entity DeleteChildren func(ctx context.Context, tx *sql.Tx, e T) error }
DaoBuilder builds new Dao[T] object with the provided parameters. All of the parameters are mandatory.
type DaoExecStmt ¶ added in v1.1.0
DaoExecStmt represents a statement that executes a command without returning rows
func (*DaoExecStmt) ToStmt ¶ added in v1.1.0
func (s *DaoExecStmt) ToStmt() *ExecStmt
ToStmt converts a DaoExecStmt to an ExecStmt that can be used to execute an SQL command
type DaoQueryOneStmt ¶ added in v1.1.0
DaoQueryOneStmt represents a statement that returns a single entity
func (*DaoQueryOneStmt[T]) ToStmt ¶ added in v1.1.0
func (s *DaoQueryOneStmt[T]) ToStmt(newReceiver func() T, receive func(T) []any) *QueryOneStmt[T]
ToStmt converts a DaoQueryOneStmt to a QueryOneStmt that can be used to execute an SQL query
type DaoQueryPageStmt ¶ added in v1.1.0
type DaoQueryPageStmt[T any] struct { CountStmt *DaoQueryValStmt[int] QueryStmt *DaoQueryStmt[T] }
DaoQueryPageStmt represents a statement that returns a paginated result set
func (*DaoQueryPageStmt[T]) ToStmt ¶ added in v1.1.0
func (s *DaoQueryPageStmt[T]) ToStmt(newReceiver func() T, receive func(T) []any) *QueryPageStmt[T]
ToStmt converts a DaoQueryPageStmt to a QueryPageStmt that can be used to execute an SQL query
type DaoQueryStmt ¶ added in v1.1.0
DaoQueryStmt represents a statement that returns multiple entities
func (*DaoQueryStmt[T]) ToStmt ¶ added in v1.1.0
func (s *DaoQueryStmt[T]) ToStmt(newReceiver func() T, receive func(T) []any) *QueryStmt[T]
ToStmt converts a DaoQueryStmt to a QueryStmt that can be used to execute an SQL query
type DaoQueryValStmt ¶ added in v1.1.0
DaoQueryValStmt represents a statement that returns a single scalar value
func (*DaoQueryValStmt[T]) ToStmt ¶ added in v1.1.0
func (s *DaoQueryValStmt[T]) ToStmt() *QueryValStmt[T]
ToStmt converts a DaoQueryValStmt to a QueryValStmt that can be used to execute an SQL query
type Entity ¶
type Entity interface {
comparable
GetID() uuid.UUID
SetID(uuid.UUID)
GetVersion() uuid.UUID
SetVersion(uuid.UUID)
Equals(another any) bool
}
Entity defines the interface for database entities that can be managed by the DAO
type ExecStmt ¶
type ExecStmt struct {
BaseStmt
}
ExecStmt represents a statement that executes a command without returning rows
type GenericEntity ¶
type GenericEntity struct {
ID uuid.UUID `json:"id" yaml:"id"`
Version uuid.UUID `json:"version" yaml:"version"`
}
GenericEntity is a base implementation of the Entity interface
func (*GenericEntity) GetID ¶
func (e *GenericEntity) GetID() uuid.UUID
GetID returns the entity's ID
func (*GenericEntity) GetVersion ¶
func (e *GenericEntity) GetVersion() uuid.UUID
GetVersion returns the entity's version
func (*GenericEntity) SetID ¶
func (e *GenericEntity) SetID(id uuid.UUID)
SetID sets the entity's ID
func (*GenericEntity) SetVersion ¶
func (e *GenericEntity) SetVersion(version uuid.UUID)
SetVersion sets the entity's version
type Page ¶
type Page[T any] struct { Items []T `json:"items" yaml:"items"` TotalPages int `json:"totalPages" yaml:"totalPages"` }
Page represents a paginated result set of items
type Paging ¶
type Paging struct {
PageNum int `json:"pageNum" yaml:"pageNum"`
PageSize int `json:"pageSize" yaml:"pageSize"`
}
Paging represents pagination parameters
func (Paging) GetOffset ¶
GetOffset calculates the offset for SQL queries based on page number and size
func (Paging) GetTotalPages ¶
GetTotalPages calculates the total number of pages based on total rows and page size
type QueryOneStmt ¶
QueryOneStmt represents a statement that returns a single entity
type QueryPageStmt ¶
type QueryPageStmt[T any] struct { CountStmt *QueryValStmt[int] QueryStmt *QueryStmt[T] }
QueryPageStmt represents a statement that returns a paginated result set
type QueryValStmt ¶
QueryValStmt represents a statement that returns a single scalar value