db

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDuplicateEntry is returned when a unique constraint is violated.
	ErrDuplicateEntry = apierror.NewAPIError("DUPLICATE_ENTRY")

	// ErrForeignConstraint is returned when a foreign key constraint is
	// violated.
	ErrForeignConstraint = apierror.NewAPIError("FOREIGN_CONSTRAINT_ERROR")

	// ErrNoRows is returned when no rows are found.
	ErrNoRows = apierror.NewAPIError("NO_ROWS")
)

Functions

func InsertedValues

func InsertedValues[Entity any](t *Entity) ([]string, []any)

InsertedValues uses reflection to generate slices of column names and values.

Parameters:

  • t: A pointer to the object to be scanned.

Returns:

  • []string: A slice of column names.
  • []any: A slice of values.

func ScanRow

func ScanRow[Entity any](t *Entity, row database.Row) error

ScanRow uses reflection to build a slice of pointers to the object fields.+

Parameters:

  • t: A pointer to the object to be scanned.
  • row: The row to scan from.

Returns:

  • error: An error if the scan fails.

Types

type AdvisoryLockQuery

type AdvisoryLockQuery interface {
	// AdvisoryLock builds an advisory lock statement.
	AdvisoryLock(lockName string, timeout int) (string, []any, error)

	// AdvisoryUnlock builds an advisory unlock statement.
	AdvisoryUnlock(lockName string) (string, []any, error)
}

AdvisoryLockQuery provides methods for advisory locking.

type ColumnDefinition

type ColumnDefinition struct {
	Name          string  // Column name
	Type          string  // Data type (with length/precision, e.g. "CHAR(36)")
	NotNull       bool    // Whether to add NOT NULL (if false, NULL is allowed)
	Default       *string // Optional default value (pass nil if not needed)
	AutoIncrement bool    // Whether to add AUTO_INCREMENT
	Extra         string  // Extra column options (e.g. "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
	PrimaryKey    bool    // Marks this column as primary key (inline)
	Unique        bool    // Marks this column as UNIQUE (unless already primary key)
}

ColumnDefinition defines the properties for a table column in a table. creation query.

type ColumnSelector

type ColumnSelector struct {
	Table  string
	Column string
}

ColumnSelector represents a column selector.

type ConnFn

type ConnFn func() (database.DB, error)

ConnFn returns a database connection.

type CountOptions

type CountOptions struct {
	Selectors Selectors
	Page      *Page
	Joins     Joins
}

CountOptions is used for count queries.

type CustomRepository

type CustomRepository[Entity any] interface {
	// QueryCustom executes a custom SQL  It returns a slice of T or an
	// error if the query or scan fails.
	QueryCustom(
		ctx context.Context,
		preparer database.Preparer,
		query string,
		parameters []any,
		factoryFn func() Entity,
	) ([]Entity, error)
}

CustomRepository defines methods for executing custom SQL queries and mapping the results into custom entities.

func NewCustomRepo

func NewCustomRepo[T any](
	errorChecker database.ErrorChecker,
) CustomRepository[T]

NewCustomRepo creates a new customRepo.

type DefaultCustomRepo

type DefaultCustomRepo[T any] struct {
	ErrorChecker database.ErrorChecker
}

DefaultCustomRepo implements the CustomRepo interface. It can be used to execute custom SQL queries and map the results into custom entities. It supports both object and scalar values.

func (*DefaultCustomRepo[T]) QueryCustom

func (r *DefaultCustomRepo[T]) QueryCustom(
	ctx context.Context,
	preparer database.Preparer,
	query string,
	parameters []any,
	factoryFn func() T,
) ([]T, error)

QueryCustom executes a custom SQL query and maps the results into a slice of custom entities. It detects is the custom entity is a database.Getter and uses the getter to populate the entity.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the query.
  • query: The SQL query to execute.
  • parameters: The query parameters.
  • factoryFn: A function that creates a custom entity from a database row.

Returns:

  • []T: A slice of custom entities scanned from the query.
  • error: An error if the query fails.

type DefaultMutatorRepo

type DefaultMutatorRepo[Entity database.Mutator] struct {
	MutatorQuery MutatorQuery
	ErrorChecker database.ErrorChecker
}

DefaultMutatorRepo implements mutation operations.

func NewMutatorRepo

func NewMutatorRepo[Entity database.Mutator](
	mutatorQuery MutatorQuery,
	errorChecker database.ErrorChecker,
) *DefaultMutatorRepo[Entity]

NewMutatorRepo creates a new mutatorRepo.

Parameters:

  • ctx: Context to use.
  • mutatorQuery: The query builder to use for the repository.
  • errorChecker: The error checker to use for the repository.

Returns:

  • *mutatorRepo: A new mutatorRepo.

func (*DefaultMutatorRepo[Entity]) Delete

func (r *DefaultMutatorRepo[Entity]) Delete(
	ctx context.Context,
	preparer database.Preparer,
	deleter Entity,
	selectors Selectors,
	deleteOpts *DeleteOptions,
) (int64, error)

Delete builds a delete query and executes it.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • deleter: The entity to delete.
  • selectors: The selectors to use for the delete.
  • deleteOpts: The delete options.

Returns:

  • int64: The number of rows affected by the delete.
  • error: An error if the query fails.

func (*DefaultMutatorRepo[Entity]) Insert

func (r *DefaultMutatorRepo[Entity]) Insert(
	ctx context.Context, preparer database.Preparer, mutator Entity,
) (Entity, error)

Insert builds an insert query and executes it.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • mutator: The entity to insert.

Returns:

  • T: The inserted entity.
  • error: An error if the query fails.

func (*DefaultMutatorRepo[Entity]) Update

func (r *DefaultMutatorRepo[Entity]) Update(
	ctx context.Context,
	preparer database.Preparer,
	updater Entity,
	selectors Selectors,
	updates Updates,
) (int64, error)

Update builds an update query and executes it.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • updater: The entity to update.
  • selectors: The selectors to use for the update.
  • updates: The updates to apply to the entity.

Returns:

  • int64: The number of rows affected by the update.
  • error: An error if the query fails.

type DefaultRawQueryer

type DefaultRawQueryer struct{}

DefaultRawQueryer provides direct query execution.

func NewRawQueryer

func NewRawQueryer() *DefaultRawQueryer

NewRawQueryer creates a new rawQueryer.

Returns:

  • *rawQueryer: A new rawQueryer.

func (*DefaultRawQueryer) Exec

func (rq *DefaultRawQueryer) Exec(
	ctx context.Context,
	preparer database.Preparer,
	query string,
	parameters []any,
) (database.Result, error)

Exec executes a query using a prepared statement.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • query: The SQL query to execute.
  • parameters: The query parameters.

Returns:

  • Result: The Result of the
  • error: An error if the query fails.

func (*DefaultRawQueryer) ExecRaw

func (rq *DefaultRawQueryer) ExecRaw(
	ctx context.Context, db database.DB, query string, parameters []any,
) (database.Result, error)

ExecRaw executes a query directly on the DB.

Parameters:

  • ctx: Context to use.
  • db: The DB to execute the query on.
  • query: The SQL query to execute.
  • parameters: The query parameters.

Returns:

  • Result: The Result of the
  • error: An error if the query fails.

func (*DefaultRawQueryer) Query

func (rq *DefaultRawQueryer) Query(
	ctx context.Context,
	preparer database.Preparer,
	query string,
	parameters []any,
) (database.Rows, error)

Query executes a query that returns rows. The caller is responsible for closing the rows.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • query: The SQL query to execute.
  • parameters: The query parameters.

Returns:

  • Rows: The rows of the
  • error: An error if the query fails.

func (*DefaultRawQueryer) QueryRaw

func (rq *DefaultRawQueryer) QueryRaw(
	ctx context.Context, db database.DB, query string, parameters []any,
) (database.Rows, error)

QueryRaw executes a query directly on the DB without preparation.

Parameters:

  • ctx: Context to use.
  • db: The DB to execute the query on.
  • query: The SQL query to execute.
  • parameters: The query parameters.

Returns:

  • Rows: The rows of the
  • error: An error if the query fails.

type DefaultReaderRepo

type DefaultReaderRepo[Entity database.Getter] struct {
	ReaderQuery  ReaderQuery
	ErrorChecker database.ErrorChecker
}

DefaultReaderRepo implements read operations.

func NewReaderRepo

func NewReaderRepo[Entity database.Getter](
	readerQuery ReaderQuery,
	errorChecker database.ErrorChecker,
) *DefaultReaderRepo[Entity]

NewReaderRepo creates a new readerRepo.

Parameters:

  • ctx: Context to use.
  • readerQuery: The query builder to use for building queries.
  • errorChecker: The ErrorChecker to use for checking errors.

Returns:

  • *readerRepo: A new readerRepo.

func (*DefaultReaderRepo[Entity]) Count

func (r *DefaultReaderRepo[Entity]) Count(
	ctx context.Context,
	preparer database.Preparer,
	selectors Selectors,
	page *Page,
	factoryFn GetterFactoryFn[Entity],
) (int, error)

Count returns the count of matching records.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • selectors: The Selectors to use for the
  • page: The Page to use for the
  • factoryFn: A function that returns a new instance of T.

Returns:

  • int: The count of matching records.
  • error: An error if the query fails.

func (*DefaultReaderRepo[Entity]) GetMany

func (r *DefaultReaderRepo[Entity]) GetMany(
	ctx context.Context,
	preparer database.Preparer,
	factoryFn GetterFactoryFn[Entity],
	getOpts *GetOptions,
) ([]Entity, error)

GetMany retrieves multiple records from the DB.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • factoryFn: A function that returns a new instance of T.
  • getOpts: The GetOptions to use for the

Returns:

  • []T: A slice of entities scanned from the
  • error: An error if the query fails.

func (*DefaultReaderRepo[Entity]) GetOne

func (r *DefaultReaderRepo[Entity]) GetOne(
	ctx context.Context,
	preparer database.Preparer,
	factoryFn GetterFactoryFn[Entity],
	getOpts *GetOptions,
) (Entity, error)

GetOne retrieves a single record from the DB by delegating to dbOps.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • factoryFn: A function that returns a new instance of T.
  • getOpts: The GetOptions to use for the

Returns:

  • T: The entity scanned from the
  • error: An error if the query fails.

func (*DefaultReaderRepo[Entity]) Query

func (r *DefaultReaderRepo[Entity]) Query(
	ctx context.Context,
	preparer database.Preparer,
	query string,
	parameters []any,
	factoryFn GetterFactoryFn[Entity],
) ([]Entity, error)

Query executes a custom SQL query that is already built.

Parameters:

  • ctx: Context to use.
  • preparer: The preparer to use for the
  • query: The SQL query to execute.
  • parameters: The query parameters.
  • factoryFn: A function that returns a new instance of T.

Returns:

  • []T: A slice of entities scanned from the
  • error: An error if the query fails.

type DefaultTxManager

type DefaultTxManager[Entity any] struct{}

DefaultTxManager is the default transaction manager.

func NewTxManager

func NewTxManager[Entity any]() *DefaultTxManager[Entity]

NewTxManager returns a new txManager.

Returns:

  • *txManager[Entity]: The new txManager.

func (*DefaultTxManager[Entity]) WithTransaction

func (t *DefaultTxManager[Entity]) WithTransaction(
	ctx context.Context,
	connFn ConnFn,
	callback database.TxFn[Entity],
) (Entity, error)

WithTransaction wraps a function call in a DB transaction.

Parameters:

  • ctx: Context to use.
  • ctx: The context to use for the transaction.
  • connFn: The function to get a DB connection.
  • callback: The function to call in the transaction.

Returns:

  • Entity: The result of the callback.
  • error: An error if the transaction fails.

type DeleteOptions

type DeleteOptions struct {
	Limit  int
	Orders Orders
}

DeleteOptions is used for delete queries.

type Direction

type Direction string

Direction is used to specify the order of the result set.

const (
	OrderAsc  Direction = "ASC"
	OrderDesc Direction = "DESC"
)

Order directions.

type EntityOption

type EntityOption[T any] func(T)

EntityOption defines a functional option for configuring an entity.

func WithOption

func WithOption[T any](
	field string, value any,
) EntityOption[T]

WithOption is a generic functional option that sets a field on an object. The field parameter should match the object's struct `db` tag. It attempts to automatically handle differences between pointer and non-pointer values. Only single level pointer differences are handled and passing a multi-level pointer field will panic.

Example:

type SomeEntity struct {
    ID   custom.UUID `db:"id"`
    Data string    `db:"data"`
}

field = "data"
value = "example data"

Output:

someEntity.Data = "example data"

Limitations:

  • Only single-level pointer differences are handled. Multi-level pointers (e.g. **SomeEntity) are not supported.
  • If the field is not found in the object, or if the provided value's type is not assignable (even after pointer adjustments), the function will panic.

Parameters:

  • field: The field to set.
  • value: The value to set.

Returns:

  • EntityOption[Entity]: The entity-specific option.

type EntityQueryOptions

type EntityQueryOptions[Entity database.CRUDEntity] struct {
	TableName      string
	EntityFn       func() Entity
	OptionEntityFn OptionEntityFactoryFn[Entity]
	SelectorList   Selectors
	UpdateList     Updates
	Options        []EntityOption[Entity]
}

EntityQueryOptions represents a query for an entity.

func NewEntityQueryOptions

func NewEntityQueryOptions[Entity database.CRUDEntity](
	tableName string,
	entityFn func() Entity,
	optionEntityFn OptionEntityFactoryFn[Entity],
) *EntityQueryOptions[Entity]

NewEntityQueryOptions creates a new NewEntityQuery for an entity.

Parameters:

  • tableName: The name of the table to
  • entityFn: A function that returns the entity to
  • optionEntityFn: A function that returns the entity with the given options.

Returns:

  • *EntityQuery: The new Entity

func (*EntityQueryOptions[Entity]) AddOption

func (q *EntityQueryOptions[Entity]) AddOption(
	field string, value any,
) *EntityQueryOptions[Entity]

Option creates an entity-specific option.

Parameters:

  • field: The field to set.
  • value: The value to set.

Returns:

  • crud.EntityOption[Entity]: The entity-specific option.

func (*EntityQueryOptions[Entity]) AddSelector

func (q *EntityQueryOptions[Entity]) AddSelector(
	field string, predicate Predicate, value any,
) *EntityQueryOptions[Entity]

AddSelector appends a selector to the

Parameters:

  • field: The field to select.
  • predicate: The predicate to apply.
  • value: The value to compare.

Returns:

  • *EntityQuery: The updated Entity

func (*EntityQueryOptions[Entity]) AddUpdate

func (q *EntityQueryOptions[Entity]) AddUpdate(
	field string, value any,
) *EntityQueryOptions[Entity]

AddUpdate appends an update clause to the

Parameters:

  • field: The field to update.
  • value: The value to set.

Returns:

  • *EntityQuery: The updated Entity

func (*EntityQueryOptions[Entity]) Entity

func (q *EntityQueryOptions[Entity]) Entity() Entity

Entity returns the entity that is being queried with the set options.

Returns:

  • Entity: The entity that is being queried.

func (*EntityQueryOptions[Entity]) Selectors

func (q *EntityQueryOptions[Entity]) Selectors() Selectors

Selectors returns the current selectors.

Returns:

  • types.Selectors: The current selectors.

func (*EntityQueryOptions[Entity]) Updates

func (q *EntityQueryOptions[Entity]) Updates() Updates

Updates returns the current updates.

Returns:

  • types.Updates: The current updates.

type GetOptions

type GetOptions struct {
	Selectors   Selectors
	Orders      Orders
	Page        *Page
	Joins       Joins
	Projections Projections
	Lock        bool
}

GetOptions is used for get queries.

type GetterFactoryFn

type GetterFactoryFn[Entity database.Getter] func() Entity

GetterFactoryFn returns a Getter factory function.

type InsertedValuesFn

type InsertedValuesFn func() ([]string, []any)

InsertedValuesFn defines a function that returns column names and values for an insert. This allows deferred evaluation of values and consistent ordering of parameters.

type Join

type Join struct {
	JoinType JoinType
	Table    string
	OnLeft   ColumnSelector
	OnRight  ColumnSelector
}

Join represents a database join clause.

func NewJoin

func NewJoin(
	joinType JoinType, table string, onLeft, onRight ColumnSelector,
) Join

NewJoin creates a new join clause.

Parameters:

  • joinType: The type of join.
  • table: The table name.
  • onLeft: The left column selector.
  • onRight: The right column selector.

Returns:

  • Join: The new join clause.

func (Join) WithJoinType

func (j Join) WithJoinType(joinType JoinType) Join

WithJoinType returns a new join with the provided join type.

Parameters:

  • joinType: The join type.

Returns:

  • Join: The new join.

func (Join) WithOnLeft

func (j Join) WithOnLeft(onLeft ColumnSelector) Join

WithOnLeft returns a new join with the provided left column selector.

Parameters:

  • onLeft: The left column selector.

Returns:

  • Join: The new join.

func (Join) WithOnRight

func (j Join) WithOnRight(onRight ColumnSelector) Join

WithOnRight returns a new join with the provided right column selector.

Parameters:

  • onRight: The right column selector.

Returns:

  • Join: The new join.

func (Join) WithTable

func (j Join) WithTable(table string) Join

WithTable returns a new join with the provided table name.

Parameters:

  • table: The table name.

Returns:

  • Join: The new join.

type JoinType

type JoinType string

JoinType represents the type of join.

const (
	JoinTypeInner JoinType = "INNER"
	JoinTypeLeft  JoinType = "LEFT"
	JoinTypeRight JoinType = "RIGHT"
	JoinTypeFull  JoinType = "FULL"
)

Join types.

type Joins

type Joins []Join

Joins is a list of joins.

type ManyMutatorQuery

type ManyMutatorQuery interface {
	// InsertMany builds a batch INSERT for multiple rows.
	InsertMany(
		table string, valuesFuncs []InsertedValuesFn,
	) (query string, params []any)

	// UpsertMany builds an UPSERT (insert or update) statement.
	UpsertMany(
		table string, valuesFuncs []InsertedValuesFn,
		updateProjections []Projection,
	) (query string, params []any)
}

ManyMutatorQuery provides methods for modifying multiple rows in a query.

type MutatorQuery

type MutatorQuery interface {
	// Insert builds an INSERT statement for a single row.
	Insert(
		table string, insertedValuesFunc InsertedValuesFn,
	) (query string, params []any)

	// UpdateQuery builds an UPDATE statement for given selectors.
	UpdateQuery(
		table string, updates []Update, selectors []Selector,
	) (query string, params []any)

	// Delete builds a DELETE statement for given selectors.
	Delete(
		table string, selectors []Selector, opts *DeleteOptions,
	) (query string, params []any)
}

MutatorQuery provides methods for modifying data in a query.

type MutatorRepository

type MutatorRepository[Entity database.Mutator] interface {
	// Insert builds an insert query and executes it.
	Insert(
		ctx context.Context, preparer database.Preparer, entity Entity,
	) (Entity, error)

	// Update builds an update query and executes it.
	Update(
		ctx context.Context,
		preparer database.Preparer,
		entity Entity,
		selectors Selectors,
		updates Updates,
	) (int64, error)

	// Delete builds a delete query and executes it.
	Delete(
		ctx context.Context,
		preparer database.Preparer,
		entity Entity,
		selectors Selectors,
		deleteOpts *DeleteOptions,
	) (int64, error)
}

MutatorRepository defines mutation-related operations.

type OptionEntityFactoryFn

type OptionEntityFactoryFn[Entity database.CRUDEntity] func(
	opts ...EntityOption[Entity],
) Entity

OptionEntityFactoryFn is a function that returns an entity with the given options.

type Order

type Order struct {
	Table     string
	Field     string
	Direction Direction
}

Order is used to specify the order of the result set.

type Orders

type Orders []Order

Orders is a list of orders.

type Page

type Page struct {
	Offset int
	Limit  int
}

Page is used to specify the page of the result set.

type Predicate

type Predicate string

Predicate represents the predicate of a database selector.

const (
	Greater        Predicate = ">"
	GreaterOrEqual Predicate = ">="
	Equal          Predicate = "="
	NotEqual       Predicate = "!="
	Less           Predicate = "<"
	LessOrEqual    Predicate = "<="
	In             Predicate = "IN"
	NotIn          Predicate = "NOT IN"
	Like           Predicate = "LIKE"
	NotLike        Predicate = "NOT LIKE"
)

Predicates for filtering data.

type Projection

type Projection struct {
	Table  string
	Column string
	Alias  string
}

Projection represents a projected column in a query.

type Projections

type Projections []Projection

Projections is a list of projections.

type Query

Query combines all query-building interfaces.

type RawQueryer

type RawQueryer interface {
	// Exec executes a query using a prepared statement that does not return
	// rows.
	Exec(
		ctx context.Context,
		preparer database.Preparer,
		query string,
		parameters []any,
	) (database.Result, error)

	// ExecRaw executes a query directly on the DB without explicit preparation.
	ExecRaw(
		ctx context.Context,
		db database.DB,
		query string,
		parameters []any,
	) (database.Result, error)

	// Query prepares and executes a query that returns rows. Returns rows.
	// The caller is responsible for closing the returned rows.
	Query(
		ctx context.Context,
		preparer database.Preparer,
		query string,
		parameters []any,
	) (database.Rows, error)

	// QueryRaw executes a query directly on the DB without preparation and
	// returns rows. The caller is responsible for closing the returned rows.
	QueryRaw(ctx context.Context, db database.DB, query string, parameters []any,
	) (database.Rows, error)
}

RawQueryer defines generic methods for executing raw queries and commands.

type ReaderQuery

type ReaderQuery interface {
	// Get builds a SELECT statement with optional filters.
	Get(table string, opts *GetOptions) (query string, params []any)

	// Count builds a SELECT COUNT(*) statement with optional filters.
	Count(table string, opts *CountOptions) (query string, params []any)
}

ReaderQuery provides methods for querying data.

type ReaderRepository

type ReaderRepository[Entity database.Getter] interface {
	// GetOne retrieves a single record from the DB.
	GetOne(
		ctx context.Context,
		preparer database.Preparer,
		factoryFn GetterFactoryFn[Entity],
		getOptions *GetOptions,
	) (Entity, error)

	// GetMany retrieves multiple records from the DB.
	GetMany(
		ctx context.Context,
		preparer database.Preparer,
		factoryFn GetterFactoryFn[Entity],
		getOptions *GetOptions,
	) ([]Entity, error)

	// Count returns a record count.
	Count(
		ctx context.Context,
		preparer database.Preparer,
		selectors Selectors,
		page *Page,
		factoryFn GetterFactoryFn[Entity],
	) (int, error)
}

ReaderRepository defines retrieval-related operations.

type SchemaQuery

type SchemaQuery interface {
	// CreateDatabaseQuery builds a CREATE DATABASE statement.
	CreateDatabaseQuery(
		dbName string, ifNotExists bool, charset string, collate string,
	) (string, []any, error)

	// CreateTableQuery builds a CREATE TABLE statement.
	CreateTableQuery(
		tableName string, ifNotExists bool, columns []ColumnDefinition,
		constraints []string, opts TableOptions,
	) (string, []any, error)

	// UseDatabaseQuery builds a USE DATABASE statement.
	UseDatabaseQuery(dbName string) (string, []any, error)

	// SetVariableQuery builds a SET statement for a variable.
	SetVariableQuery(
		variable string, value string,
	) (string, []any, error)

	// CurrentTimestamp returns the current timestamp expression.
	CurrentTimestamp() *string

	// CurrentTimestamp returns the text type expression.
	TypeText() *string

	// CurrentTimestamp returns the datetime type expression.
	TypeDatetime() *string
}

SchemaQuery provides methods for managing the database schema.

type Selector

type Selector struct {
	Table     string
	Column    string
	Predicate Predicate
	Value     any
}

Selector represents a database selector.

func MustGetSelector

func MustGetSelector(
	tableName string, entity any, column string, predicate Predicate, value any,
) *Selector

MustGetSelector creates a new Selector with the given parameters. It panics if the provided value's type is not assignable to the expected type.

Parameters:

  • tableName: The name of the table to select from.
  • entity: The entity to select from.
  • column: The column to select.
  • predicate: The predicate to apply.
  • value: The value to compare.

Returns:

  • *types.Selector: The new Selector.

func NewSelector

func NewSelector(column string, predicate Predicate, value any) *Selector

NewSelector creates a new selector with the given parameters.

Parameters:

  • column: the column name.
  • predicate: the predicate.
  • value: the value.

Returns:

  • *Selector: The new selector.

func (*Selector) WithColumn

func (s *Selector) WithColumn(column string) *Selector

WithColumn returns a new selector with the provided column name.

Parameters:

  • column: the column name.

Returns:

  • *Selector: The new selector.

func (*Selector) WithPredicate

func (s *Selector) WithPredicate(predicate Predicate) *Selector

WithPredicate returns a new selector with the provided predicate.

Parameters:

  • predicate: the predicate.

Returns:

  • *Selector: The new selector.

func (*Selector) WithTable

func (s *Selector) WithTable(table string) *Selector

WithTable returns a new selector with the provided table name.

Parameters:

  • table: the table name.

Returns:

  • *Selector: The new selector.

func (*Selector) WithValue

func (s *Selector) WithValue(value any) *Selector

WithValue returns a new selector with the provided value.

Parameters:

  • value: the value.

Returns:

  • *Selector: The new selector.

type Selectors

type Selectors []Selector

Selectors represents a list of database selectors.

func NewSelectors

func NewSelectors(selectors ...Selector) Selectors

NewSelectors returns a new list of selectors.

Parameters:

  • selectors: The selectors.

Returns:

  • Selectors: The new list of selectors.

func (Selectors) Add

func (s Selectors) Add(
	column string, predicate Predicate, value any,
) Selectors

Add adds a new selector to the list.

Parameters:

  • column: The column name.
  • predicate: The predicate.
  • value: The value.

Returns:

  • Selectors: The new list of selectors.

func (Selectors) GetByField

func (s Selectors) GetByField(field string) *Selector

GetByField returns selector with the given field.

Parameters:

  • field: the field to search for.

Returns:

  • *Selector: The selector.

func (Selectors) GetByFields

func (s Selectors) GetByFields(fields ...string) []Selector

GetByFields returns selectors with the given fields.

Parameters:

  • fields: the fields to search for.

Returns:

  • []Selector: A list of selectors.

type TableOptions

type TableOptions struct {
	Engine  string // e.g. "InnoDB"
	Charset string // e.g. "utf8mb4"
	Collate string // e.g. "utf8mb4_bin"
}

TableOptions holds additional options for a table creation query.

type TxManager

type TxManager[T any] interface {
	// WithTransaction wraps a function call in a DB transaction.
	WithTransaction(
		ctx context.Context, connFn ConnFn, callback database.TxFn[T],
	) (T, error)
}

TxManager is an interface for transaction management.

type Update

type Update struct {
	Field string
	Value any
}

Update is the options struct used for update queries.

func MustGetUpdate

func MustGetUpdate(entity any, fieldName string, value any) *Update

MustGetUpdate creates a new Update with the given parameters. It panics if the provided value's type is not assignable to the expected type.

Parameters:

  • entity: The entity to update.
  • fieldName: The field to update.
  • value: The value to set.

Returns:

  • *types.Update: The new Update.

func NewUpdate

func NewUpdate(field string, value any) Update

NewUpdate creates a new update field.

Parameters:

  • field: The field.
  • value: The value

Returns:

  • Update: The new update field.

func (Update) WithField

func (u Update) WithField(field string) Update

WithField returns a new update field with the provided field name.

Parameters:

  • field: The field.

Returns:

  • Update: The new update field.

func (Update) WithValue

func (u Update) WithValue(value any) Update

WithValue returns a new update field with the provided value.

Parameters:

  • value: The value.

Returns:

  • Update: The new update field.

type Updates

type Updates []Update

Updates is a list of update fields

func NewUpdates

func NewUpdates(updates ...Update) Updates

NewUpdates creates a new list of updates.

Parameters:

  • updates: The updates.

Returns:

  • Updates: The new list of updates.

func (Updates) Add

func (u Updates) Add(field string, value any) Updates

Add adds a new update field to the list.

Parameters:

  • field: The field.
  • value: The value.

Returns:

  • Updates: The new list of updates.

Jump to

Keyboard shortcuts

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