query

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package query holds the query builders used in an entity schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregate added in v0.0.6

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

Aggregate is one aggregate function over one column.

func (Aggregate) GetField added in v0.0.6

func (a Aggregate) GetField() string

GetField returns the aggregated column name.

func (Aggregate) GetFunc added in v0.0.6

func (a Aggregate) GetFunc() Func

GetFunc returns the aggregate function.

type CreateOperations added in v0.0.5

type CreateOperations interface {
	QueryBuilder
	// Upsert updates the existing row when the given fields collide. The fields must be
	// a primary key or a unique constraint, no fields means the primary key.
	Upsert(fields ...string) CreateOperations
	// Ignore keeps the existing row instead of updating it. Needs an Upsert.
	Ignore() CreateOperations
	// Name overrides the auto-generated query/method name
	Name(name string) CreateOperations
	// Contracts limits the query to the given layers, sqlc or proto.
	Contracts(contracts ...entlite.Layer) CreateOperations
}

CreateOperations exposes the modifiers available on a Create or CreateBulk query.

func Create

func Create() CreateOperations

Create inserts one record.

func CreateBulk

func CreateBulk() CreateOperations

CreateBulk inserts many records in one call.

type Func added in v0.0.6

type Func string

Func tells which sql aggregate folds a column.

const (
	// FuncSum adds up the column values, SUM().
	FuncSum Func = "sum"
	// FuncAvg averages the column values, AVG().
	FuncAvg Func = "avg"
	// FuncMin takes the smallest column value, MIN().
	FuncMin Func = "min"
	// FuncMax takes the largest column value, MAX().
	FuncMax Func = "max"
)

type ListAllOperations added in v0.0.5

type ListAllOperations interface {
	QueryBuilder
	// Count also returns how many rows match, counted before Limit. An empty page reports 0.
	Count() ListAllOperations
	// Distinct returns the deduplicated values of the given columns instead of whole
	// rows. Every column is part of the key, so sorting is limited to them.
	Distinct(fields ...string) ListAllOperations
	// GroupBy returns one row per distinct combination of the given columns, an
	// aggregate folds each group. Sort by a grouped column or by an aggregate
	// column, which is named like sum_duration_ms.
	GroupBy(fields ...string) ListAllOperations
	// Sum adds up the given column, per group or over the whole table without a GroupBy.
	Sum(field string) ListAllOperations
	// Avg averages the given column, per group or over the whole table without a GroupBy.
	Avg(field string) ListAllOperations
	// Min takes the smallest value of the given column, per group or over the whole table.
	Min(field string) ListAllOperations
	// Max takes the largest value of the given column, per group or over the whole table.
	Max(field string) ListAllOperations
	// Asc appends a sort column, ascending.
	Asc(field string) ListAllOperations
	// Desc appends a sort column, descending.
	Desc(field string) ListAllOperations
	// Limit takes the row count from the caller, Limit(rows) sets it in the query.
	Limit(rows ...int) ListAllOperations
	// Offset asks the caller how many rows to skip. Needs a Limit.
	Offset() ListAllOperations
	// Name overrides the auto-generated query/method name
	Name(name string) ListAllOperations
	// Contracts limits the query to the given layers, sqlc or proto.
	Contracts(contracts ...entlite.Layer) ListAllOperations
}

ListAllOperations exposes the modifiers available on a ListAll query.

func ListAll

func ListAll() ListAllOperations

ListAll reads every record of the table.

type ListByOperations

type ListByOperations interface {
	QueryBuilder
	// Count also returns how many rows match, counted before Limit. An empty page reports 0.
	Count() ListByOperations
	// Distinct returns the deduplicated values of the given columns instead of whole
	// rows. Every column is part of the key, so sorting is limited to them.
	Distinct(fields ...string) ListByOperations
	// GroupBy returns one row per distinct combination of the given columns, an
	// aggregate folds each group. Sort by a grouped column or by an aggregate
	// column, which is named like sum_duration_ms.
	GroupBy(fields ...string) ListByOperations
	// Sum adds up the given column, per group or over the whole table without a GroupBy.
	Sum(field string) ListByOperations
	// Avg averages the given column, per group or over the whole table without a GroupBy.
	Avg(field string) ListByOperations
	// Min takes the smallest value of the given column, per group or over the whole table.
	Min(field string) ListByOperations
	// Max takes the largest value of the given column, per group or over the whole table.
	Max(field string) ListByOperations
	// Asc appends a sort column, ascending.
	Asc(field string) ListByOperations
	// Desc appends a sort column, descending.
	Desc(field string) ListByOperations
	// Limit takes the row count from the caller, Limit(rows) sets it in the query.
	Limit(rows ...int) ListByOperations
	// Offset asks the caller how many rows to skip. Needs a Limit.
	Offset() ListByOperations
	// Name overrides the auto-generated query/method name
	Name(name string) ListByOperations
	// Contracts limits the query to the given layers, sqlc or proto.
	Contracts(contracts ...entlite.Layer) ListByOperations
}

ListByOperations exposes the modifiers available on a ListBy query.

func ListBy

func ListBy(args ...interface{}) ListByOperations

ListBy lists records with filters. Takes field names, which default to Eq, or Filter values, e.g. ListBy("org_id") or ListBy(filter.Range("age"), filter.Search("name"))

type OrderColumn added in v0.0.5

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

OrderColumn is a single sort column together with its direction.

func (OrderColumn) GetName added in v0.0.5

func (c OrderColumn) GetName() string

GetName returns the column name.

func (OrderColumn) IsDesc added in v0.0.5

func (c OrderColumn) IsDesc() bool

IsDesc reports if the column is sorted descending.

type Query

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

Query holds the state of one query.

func (Query) Contracts added in v0.0.3

func (q Query) Contracts(contracts ...entlite.Layer) QueryOperations

Contracts limits the query to the given layers, sqlc or proto.

func (Query) GetAggregates added in v0.0.6

func (q Query) GetAggregates() []Aggregate

GetAggregates returns the aggregates in chain order, or nil when there is none.

func (Query) GetContracts added in v0.0.3

func (q Query) GetContracts() []entlite.Layer

GetContracts returns the layers the query belongs to.

func (Query) GetDistinct added in v0.0.6

func (q Query) GetDistinct() []string

GetDistinct returns the deduplicated columns, or nil when the query returns rows.

func (Query) GetFields

func (q Query) GetFields() []string

GetFields returns the fields of a GetBy query.

func (Query) GetFilters

func (q Query) GetFilters() []filter.Filter

GetFilters returns the filters of a ListBy query.

func (Query) GetGroupBy added in v0.0.6

func (q Query) GetGroupBy() []string

GetGroupBy returns the grouped columns, or nil when the query returns rows.

func (Query) GetLimit added in v0.0.5

func (q Query) GetLimit() int

GetLimit returns the fixed limit, or 0 when the caller sets it.

func (Query) GetName

func (q Query) GetName() string

GetName returns the custom query name, or "" when auto-generated.

func (Query) GetOrderBy

func (q Query) GetOrderBy() []OrderColumn

GetOrderBy returns the sort columns in order, or nil when there is none.

func (Query) GetType

func (q Query) GetType() Type

GetType returns the query kind.

func (Query) GetUpsertFields added in v0.0.5

func (q Query) GetUpsertFields() []string

GetUpsertFields returns the conflict target, or nil for the primary key.

func (Query) HasCount

func (q Query) HasCount() bool

HasCount reports if a list query also returns a count.

func (Query) HasLimit added in v0.0.5

func (q Query) HasLimit() bool

HasLimit reports if the query limits the returned rows.

func (Query) HasOffset added in v0.0.5

func (q Query) HasOffset() bool

HasOffset reports if the query skips rows.

func (Query) HasUpsert added in v0.0.5

func (q Query) HasUpsert() bool

HasUpsert reports if a create query updates the row it collides with.

func (Query) HasUpsertIgnore added in v0.0.5

func (q Query) HasUpsertIgnore() bool

HasUpsertIgnore reports if an upsert keeps the existing row instead of updating it.

func (Query) Name

func (q Query) Name(name string) QueryOperations

Name overrides the auto-generated query/method name

func (Query) Query

func (Query) Query()

marker method for sealed interface

type QueryBuilder

type QueryBuilder interface {
	Query()
}

QueryBuilder is implemented by every query.

func DefaultCRUD

func DefaultCRUD() QueryBuilder

DefaultCRUD expands to several queries, so it cannot be named

type QueryOperations

type QueryOperations interface {
	QueryBuilder
	// Name overrides the auto-generated query/method name
	Name(name string) QueryOperations
	// Contracts limits the query to the given layers, sqlc or proto.
	Contracts(contracts ...entlite.Layer) QueryOperations
}

QueryOperations exposes the modifiers available on a plain query.

func Delete

func Delete() QueryOperations

Delete deletes one record by primary key.

func DeleteAll

func DeleteAll() QueryOperations

DeleteAll deletes every record of the table.

func Get

func Get() QueryOperations

Get reads one record by primary key.

func GetBy

func GetBy(fields ...string) QueryOperations

GetBy gets a record by one or more fields, e.g. GetBy("id") or GetBy("org_id", "email")

func Update

func Update() QueryOperations

Update updates one record by primary key.

type Type

type Type string

Type tells which kind of query is described.

const (
	// TypeDefaultCRUD expands to the common create, read, update and delete queries.
	TypeDefaultCRUD Type = "default_crud"
	// TypeCreate inserts one row.
	TypeCreate Type = "create"
	// TypeCreateBulk inserts many rows in one call.
	TypeCreateBulk Type = "create_bulk"
	// TypeGet reads one row by primary key.
	TypeGet Type = "get"
	// TypeUpdate updates one row by primary key.
	TypeUpdate Type = "update"
	// TypeDelete deletes one row by primary key.
	TypeDelete Type = "delete"
	// TypeDeleteAll deletes every row of the table.
	TypeDeleteAll Type = "delete_all"
	// TypeListAll reads every row of the table.
	TypeListAll Type = "list_all"
	// TypeGetBy reads one row by the given fields.
	TypeGetBy Type = "get_by"
	// TypeListBy reads many rows by the given filters.
	TypeListBy Type = "list_by"
)

Jump to

Keyboard shortcuts

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