pcmt/ent/searchquery_query.go
surtur 74546f996b
All checks were successful
continuous-integration/drone/push Build is passing
ent: add/extend entities, tests, validation
2023-08-19 04:52:15 +02:00

607 lines
17 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"git.dotya.ml/mirre-mt/pcmt/ent/predicate"
"git.dotya.ml/mirre-mt/pcmt/ent/searchquery"
"git.dotya.ml/mirre-mt/pcmt/ent/user"
"github.com/google/uuid"
)
// SearchQueryQuery is the builder for querying SearchQuery entities.
type SearchQueryQuery struct {
config
ctx *QueryContext
order []searchquery.OrderOption
inters []Interceptor
predicates []predicate.SearchQuery
withUser *UserQuery
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the SearchQueryQuery builder.
func (sqq *SearchQueryQuery) Where(ps ...predicate.SearchQuery) *SearchQueryQuery {
sqq.predicates = append(sqq.predicates, ps...)
return sqq
}
// Limit the number of records to be returned by this query.
func (sqq *SearchQueryQuery) Limit(limit int) *SearchQueryQuery {
sqq.ctx.Limit = &limit
return sqq
}
// Offset to start from.
func (sqq *SearchQueryQuery) Offset(offset int) *SearchQueryQuery {
sqq.ctx.Offset = &offset
return sqq
}
// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (sqq *SearchQueryQuery) Unique(unique bool) *SearchQueryQuery {
sqq.ctx.Unique = &unique
return sqq
}
// Order specifies how the records should be ordered.
func (sqq *SearchQueryQuery) Order(o ...searchquery.OrderOption) *SearchQueryQuery {
sqq.order = append(sqq.order, o...)
return sqq
}
// QueryUser chains the current query on the "user" edge.
func (sqq *SearchQueryQuery) QueryUser() *UserQuery {
query := (&UserClient{config: sqq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := sqq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := sqq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(searchquery.Table, searchquery.FieldID, selector),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, searchquery.UserTable, searchquery.UserColumn),
)
fromU = sqlgraph.SetNeighbors(sqq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first SearchQuery entity from the query.
// Returns a *NotFoundError when no SearchQuery was found.
func (sqq *SearchQueryQuery) First(ctx context.Context) (*SearchQuery, error) {
nodes, err := sqq.Limit(1).All(setContextOp(ctx, sqq.ctx, "First"))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{searchquery.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (sqq *SearchQueryQuery) FirstX(ctx context.Context) *SearchQuery {
node, err := sqq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first SearchQuery ID from the query.
// Returns a *NotFoundError when no SearchQuery ID was found.
func (sqq *SearchQueryQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
var ids []uuid.UUID
if ids, err = sqq.Limit(1).IDs(setContextOp(ctx, sqq.ctx, "FirstID")); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{searchquery.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (sqq *SearchQueryQuery) FirstIDX(ctx context.Context) uuid.UUID {
id, err := sqq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single SearchQuery entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one SearchQuery entity is found.
// Returns a *NotFoundError when no SearchQuery entities are found.
func (sqq *SearchQueryQuery) Only(ctx context.Context) (*SearchQuery, error) {
nodes, err := sqq.Limit(2).All(setContextOp(ctx, sqq.ctx, "Only"))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{searchquery.Label}
default:
return nil, &NotSingularError{searchquery.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (sqq *SearchQueryQuery) OnlyX(ctx context.Context) *SearchQuery {
node, err := sqq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only SearchQuery ID in the query.
// Returns a *NotSingularError when more than one SearchQuery ID is found.
// Returns a *NotFoundError when no entities are found.
func (sqq *SearchQueryQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
var ids []uuid.UUID
if ids, err = sqq.Limit(2).IDs(setContextOp(ctx, sqq.ctx, "OnlyID")); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{searchquery.Label}
default:
err = &NotSingularError{searchquery.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (sqq *SearchQueryQuery) OnlyIDX(ctx context.Context) uuid.UUID {
id, err := sqq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of SearchQueries.
func (sqq *SearchQueryQuery) All(ctx context.Context) ([]*SearchQuery, error) {
ctx = setContextOp(ctx, sqq.ctx, "All")
if err := sqq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*SearchQuery, *SearchQueryQuery]()
return withInterceptors[[]*SearchQuery](ctx, sqq, qr, sqq.inters)
}
// AllX is like All, but panics if an error occurs.
func (sqq *SearchQueryQuery) AllX(ctx context.Context) []*SearchQuery {
nodes, err := sqq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of SearchQuery IDs.
func (sqq *SearchQueryQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
if sqq.ctx.Unique == nil && sqq.path != nil {
sqq.Unique(true)
}
ctx = setContextOp(ctx, sqq.ctx, "IDs")
if err = sqq.Select(searchquery.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (sqq *SearchQueryQuery) IDsX(ctx context.Context) []uuid.UUID {
ids, err := sqq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (sqq *SearchQueryQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, sqq.ctx, "Count")
if err := sqq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, sqq, querierCount[*SearchQueryQuery](), sqq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (sqq *SearchQueryQuery) CountX(ctx context.Context) int {
count, err := sqq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (sqq *SearchQueryQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, sqq.ctx, "Exist")
switch _, err := sqq.FirstID(ctx); {
case IsNotFound(err):
return false, nil
case err != nil:
return false, fmt.Errorf("ent: check existence: %w", err)
default:
return true, nil
}
}
// ExistX is like Exist, but panics if an error occurs.
func (sqq *SearchQueryQuery) ExistX(ctx context.Context) bool {
exist, err := sqq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the SearchQueryQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (sqq *SearchQueryQuery) Clone() *SearchQueryQuery {
if sqq == nil {
return nil
}
return &SearchQueryQuery{
config: sqq.config,
ctx: sqq.ctx.Clone(),
order: append([]searchquery.OrderOption{}, sqq.order...),
inters: append([]Interceptor{}, sqq.inters...),
predicates: append([]predicate.SearchQuery{}, sqq.predicates...),
withUser: sqq.withUser.Clone(),
// clone intermediate query.
sql: sqq.sql.Clone(),
path: sqq.path,
}
}
// WithUser tells the query-builder to eager-load the nodes that are connected to
// the "user" edge. The optional arguments are used to configure the query builder of the edge.
func (sqq *SearchQueryQuery) WithUser(opts ...func(*UserQuery)) *SearchQueryQuery {
query := (&UserClient{config: sqq.config}).Query()
for _, opt := range opts {
opt(query)
}
sqq.withUser = query
return sqq
}
// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
// var v []struct {
// Query string `json:"query,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.SearchQuery.Query().
// GroupBy(searchquery.FieldQuery).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (sqq *SearchQueryQuery) GroupBy(field string, fields ...string) *SearchQueryGroupBy {
sqq.ctx.Fields = append([]string{field}, fields...)
grbuild := &SearchQueryGroupBy{build: sqq}
grbuild.flds = &sqq.ctx.Fields
grbuild.label = searchquery.Label
grbuild.scan = grbuild.Scan
return grbuild
}
// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
// var v []struct {
// Query string `json:"query,omitempty"`
// }
//
// client.SearchQuery.Query().
// Select(searchquery.FieldQuery).
// Scan(ctx, &v)
func (sqq *SearchQueryQuery) Select(fields ...string) *SearchQuerySelect {
sqq.ctx.Fields = append(sqq.ctx.Fields, fields...)
sbuild := &SearchQuerySelect{SearchQueryQuery: sqq}
sbuild.label = searchquery.Label
sbuild.flds, sbuild.scan = &sqq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a SearchQuerySelect configured with the given aggregations.
func (sqq *SearchQueryQuery) Aggregate(fns ...AggregateFunc) *SearchQuerySelect {
return sqq.Select().Aggregate(fns...)
}
func (sqq *SearchQueryQuery) prepareQuery(ctx context.Context) error {
for _, inter := range sqq.inters {
if inter == nil {
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
}
if trv, ok := inter.(Traverser); ok {
if err := trv.Traverse(ctx, sqq); err != nil {
return err
}
}
}
for _, f := range sqq.ctx.Fields {
if !searchquery.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if sqq.path != nil {
prev, err := sqq.path(ctx)
if err != nil {
return err
}
sqq.sql = prev
}
return nil
}
func (sqq *SearchQueryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*SearchQuery, error) {
var (
nodes = []*SearchQuery{}
_spec = sqq.querySpec()
loadedTypes = [1]bool{
sqq.withUser != nil,
}
)
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*SearchQuery).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &SearchQuery{config: sqq.config}
nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values)
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, sqq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := sqq.withUser; query != nil {
if err := sqq.loadUser(ctx, query, nodes, nil,
func(n *SearchQuery, e *User) { n.Edges.User = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (sqq *SearchQueryQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*SearchQuery, init func(*SearchQuery), assign func(*SearchQuery, *User)) error {
ids := make([]uuid.UUID, 0, len(nodes))
nodeids := make(map[uuid.UUID][]*SearchQuery)
for i := range nodes {
fk := nodes[i].Owner
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(user.IDIn(ids...))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return fmt.Errorf(`unexpected foreign-key "owner" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (sqq *SearchQueryQuery) sqlCount(ctx context.Context) (int, error) {
_spec := sqq.querySpec()
_spec.Node.Columns = sqq.ctx.Fields
if len(sqq.ctx.Fields) > 0 {
_spec.Unique = sqq.ctx.Unique != nil && *sqq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, sqq.driver, _spec)
}
func (sqq *SearchQueryQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(searchquery.Table, searchquery.Columns, sqlgraph.NewFieldSpec(searchquery.FieldID, field.TypeUUID))
_spec.From = sqq.sql
if unique := sqq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if sqq.path != nil {
_spec.Unique = true
}
if fields := sqq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, searchquery.FieldID)
for i := range fields {
if fields[i] != searchquery.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
if sqq.withUser != nil {
_spec.Node.AddColumnOnce(searchquery.FieldOwner)
}
}
if ps := sqq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := sqq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := sqq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := sqq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (sqq *SearchQueryQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(sqq.driver.Dialect())
t1 := builder.Table(searchquery.Table)
columns := sqq.ctx.Fields
if len(columns) == 0 {
columns = searchquery.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if sqq.sql != nil {
selector = sqq.sql
selector.Select(selector.Columns(columns...)...)
}
if sqq.ctx.Unique != nil && *sqq.ctx.Unique {
selector.Distinct()
}
for _, p := range sqq.predicates {
p(selector)
}
for _, p := range sqq.order {
p(selector)
}
if offset := sqq.ctx.Offset; offset != nil {
// limit is mandatory for offset clause. We start
// with default value, and override it below if needed.
selector.Offset(*offset).Limit(math.MaxInt32)
}
if limit := sqq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// SearchQueryGroupBy is the group-by builder for SearchQuery entities.
type SearchQueryGroupBy struct {
selector
build *SearchQueryQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (sqgb *SearchQueryGroupBy) Aggregate(fns ...AggregateFunc) *SearchQueryGroupBy {
sqgb.fns = append(sqgb.fns, fns...)
return sqgb
}
// Scan applies the selector query and scans the result into the given value.
func (sqgb *SearchQueryGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, sqgb.build.ctx, "GroupBy")
if err := sqgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*SearchQueryQuery, *SearchQueryGroupBy](ctx, sqgb.build, sqgb, sqgb.build.inters, v)
}
func (sqgb *SearchQueryGroupBy) sqlScan(ctx context.Context, root *SearchQueryQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(sqgb.fns))
for _, fn := range sqgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*sqgb.flds)+len(sqgb.fns))
for _, f := range *sqgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*sqgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := sqgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// SearchQuerySelect is the builder for selecting fields of SearchQuery entities.
type SearchQuerySelect struct {
*SearchQueryQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (sqs *SearchQuerySelect) Aggregate(fns ...AggregateFunc) *SearchQuerySelect {
sqs.fns = append(sqs.fns, fns...)
return sqs
}
// Scan applies the selector query and scans the result into the given value.
func (sqs *SearchQuerySelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, sqs.ctx, "Select")
if err := sqs.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*SearchQueryQuery, *SearchQuerySelect](ctx, sqs.SearchQueryQuery, sqs, sqs.inters, v)
}
func (sqs *SearchQuerySelect) sqlScan(ctx context.Context, root *SearchQueryQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(sqs.fns))
for _, fn := range sqs.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*sqs.selector.flds); {
case n == 0 && len(aggregation) > 0:
selector.Select(aggregation...)
case n != 0 && len(aggregation) > 0:
selector.AppendSelect(aggregation...)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := sqs.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}