3 Star 0 Fork 0

广东宇凌网络科技有限公司 / simple-iot-basic-rpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
productevent_query.go 18.44 KB
一键复制 编辑 原始数据 按行查看 历史
HienGee 提交于 2024-04-26 11:19 . init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
// 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"
"gitee.com/yuLingNet/simple-iot-basic-rpc/ent/predicate"
"gitee.com/yuLingNet/simple-iot-basic-rpc/ent/product"
"gitee.com/yuLingNet/simple-iot-basic-rpc/ent/productevent"
uuid "github.com/gofrs/uuid/v5"
)
// ProductEventQuery is the builder for querying ProductEvent entities.
type ProductEventQuery struct {
config
ctx *QueryContext
order []productevent.OrderOption
inters []Interceptor
predicates []predicate.ProductEvent
withProduct *ProductQuery
withFKs bool
modifiers []func(*sql.Selector)
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the ProductEventQuery builder.
func (peq *ProductEventQuery) Where(ps ...predicate.ProductEvent) *ProductEventQuery {
peq.predicates = append(peq.predicates, ps...)
return peq
}
// Limit the number of records to be returned by this query.
func (peq *ProductEventQuery) Limit(limit int) *ProductEventQuery {
peq.ctx.Limit = &limit
return peq
}
// Offset to start from.
func (peq *ProductEventQuery) Offset(offset int) *ProductEventQuery {
peq.ctx.Offset = &offset
return peq
}
// 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 (peq *ProductEventQuery) Unique(unique bool) *ProductEventQuery {
peq.ctx.Unique = &unique
return peq
}
// Order specifies how the records should be ordered.
func (peq *ProductEventQuery) Order(o ...productevent.OrderOption) *ProductEventQuery {
peq.order = append(peq.order, o...)
return peq
}
// QueryProduct chains the current query on the "product" edge.
func (peq *ProductEventQuery) QueryProduct() *ProductQuery {
query := (&ProductClient{config: peq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := peq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := peq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(productevent.Table, productevent.FieldID, selector),
sqlgraph.To(product.Table, product.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, productevent.ProductTable, productevent.ProductColumn),
)
fromU = sqlgraph.SetNeighbors(peq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first ProductEvent entity from the query.
// Returns a *NotFoundError when no ProductEvent was found.
func (peq *ProductEventQuery) First(ctx context.Context) (*ProductEvent, error) {
nodes, err := peq.Limit(1).All(setContextOp(ctx, peq.ctx, "First"))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{productevent.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (peq *ProductEventQuery) FirstX(ctx context.Context) *ProductEvent {
node, err := peq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first ProductEvent ID from the query.
// Returns a *NotFoundError when no ProductEvent ID was found.
func (peq *ProductEventQuery) FirstID(ctx context.Context) (id uint64, err error) {
var ids []uint64
if ids, err = peq.Limit(1).IDs(setContextOp(ctx, peq.ctx, "FirstID")); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{productevent.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (peq *ProductEventQuery) FirstIDX(ctx context.Context) uint64 {
id, err := peq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single ProductEvent entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one ProductEvent entity is found.
// Returns a *NotFoundError when no ProductEvent entities are found.
func (peq *ProductEventQuery) Only(ctx context.Context) (*ProductEvent, error) {
nodes, err := peq.Limit(2).All(setContextOp(ctx, peq.ctx, "Only"))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{productevent.Label}
default:
return nil, &NotSingularError{productevent.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (peq *ProductEventQuery) OnlyX(ctx context.Context) *ProductEvent {
node, err := peq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only ProductEvent ID in the query.
// Returns a *NotSingularError when more than one ProductEvent ID is found.
// Returns a *NotFoundError when no entities are found.
func (peq *ProductEventQuery) OnlyID(ctx context.Context) (id uint64, err error) {
var ids []uint64
if ids, err = peq.Limit(2).IDs(setContextOp(ctx, peq.ctx, "OnlyID")); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{productevent.Label}
default:
err = &NotSingularError{productevent.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (peq *ProductEventQuery) OnlyIDX(ctx context.Context) uint64 {
id, err := peq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of ProductEvents.
func (peq *ProductEventQuery) All(ctx context.Context) ([]*ProductEvent, error) {
ctx = setContextOp(ctx, peq.ctx, "All")
if err := peq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*ProductEvent, *ProductEventQuery]()
return withInterceptors[[]*ProductEvent](ctx, peq, qr, peq.inters)
}
// AllX is like All, but panics if an error occurs.
func (peq *ProductEventQuery) AllX(ctx context.Context) []*ProductEvent {
nodes, err := peq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of ProductEvent IDs.
func (peq *ProductEventQuery) IDs(ctx context.Context) (ids []uint64, err error) {
if peq.ctx.Unique == nil && peq.path != nil {
peq.Unique(true)
}
ctx = setContextOp(ctx, peq.ctx, "IDs")
if err = peq.Select(productevent.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (peq *ProductEventQuery) IDsX(ctx context.Context) []uint64 {
ids, err := peq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (peq *ProductEventQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, peq.ctx, "Count")
if err := peq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, peq, querierCount[*ProductEventQuery](), peq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (peq *ProductEventQuery) CountX(ctx context.Context) int {
count, err := peq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (peq *ProductEventQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, peq.ctx, "Exist")
switch _, err := peq.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 (peq *ProductEventQuery) ExistX(ctx context.Context) bool {
exist, err := peq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the ProductEventQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (peq *ProductEventQuery) Clone() *ProductEventQuery {
if peq == nil {
return nil
}
return &ProductEventQuery{
config: peq.config,
ctx: peq.ctx.Clone(),
order: append([]productevent.OrderOption{}, peq.order...),
inters: append([]Interceptor{}, peq.inters...),
predicates: append([]predicate.ProductEvent{}, peq.predicates...),
withProduct: peq.withProduct.Clone(),
// clone intermediate query.
sql: peq.sql.Clone(),
path: peq.path,
}
}
// WithProduct tells the query-builder to eager-load the nodes that are connected to
// the "product" edge. The optional arguments are used to configure the query builder of the edge.
func (peq *ProductEventQuery) WithProduct(opts ...func(*ProductQuery)) *ProductEventQuery {
query := (&ProductClient{config: peq.config}).Query()
for _, opt := range opts {
opt(query)
}
peq.withProduct = query
return peq
}
// 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 {
// CreatedAt time.Time `json:"created_at,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.ProductEvent.Query().
// GroupBy(productevent.FieldCreatedAt).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (peq *ProductEventQuery) GroupBy(field string, fields ...string) *ProductEventGroupBy {
peq.ctx.Fields = append([]string{field}, fields...)
grbuild := &ProductEventGroupBy{build: peq}
grbuild.flds = &peq.ctx.Fields
grbuild.label = productevent.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 {
// CreatedAt time.Time `json:"created_at,omitempty"`
// }
//
// client.ProductEvent.Query().
// Select(productevent.FieldCreatedAt).
// Scan(ctx, &v)
func (peq *ProductEventQuery) Select(fields ...string) *ProductEventSelect {
peq.ctx.Fields = append(peq.ctx.Fields, fields...)
sbuild := &ProductEventSelect{ProductEventQuery: peq}
sbuild.label = productevent.Label
sbuild.flds, sbuild.scan = &peq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a ProductEventSelect configured with the given aggregations.
func (peq *ProductEventQuery) Aggregate(fns ...AggregateFunc) *ProductEventSelect {
return peq.Select().Aggregate(fns...)
}
func (peq *ProductEventQuery) prepareQuery(ctx context.Context) error {
for _, inter := range peq.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, peq); err != nil {
return err
}
}
}
for _, f := range peq.ctx.Fields {
if !productevent.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if peq.path != nil {
prev, err := peq.path(ctx)
if err != nil {
return err
}
peq.sql = prev
}
return nil
}
func (peq *ProductEventQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*ProductEvent, error) {
var (
nodes = []*ProductEvent{}
withFKs = peq.withFKs
_spec = peq.querySpec()
loadedTypes = [1]bool{
peq.withProduct != nil,
}
)
if peq.withProduct != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, productevent.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*ProductEvent).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &ProductEvent{config: peq.config}
nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values)
}
if len(peq.modifiers) > 0 {
_spec.Modifiers = peq.modifiers
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, peq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := peq.withProduct; query != nil {
if err := peq.loadProduct(ctx, query, nodes, nil,
func(n *ProductEvent, e *Product) { n.Edges.Product = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (peq *ProductEventQuery) loadProduct(ctx context.Context, query *ProductQuery, nodes []*ProductEvent, init func(*ProductEvent), assign func(*ProductEvent, *Product)) error {
ids := make([]uuid.UUID, 0, len(nodes))
nodeids := make(map[uuid.UUID][]*ProductEvent)
for i := range nodes {
if nodes[i].product_events == nil {
continue
}
fk := *nodes[i].product_events
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(product.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 "product_events" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (peq *ProductEventQuery) sqlCount(ctx context.Context) (int, error) {
_spec := peq.querySpec()
if len(peq.modifiers) > 0 {
_spec.Modifiers = peq.modifiers
}
_spec.Node.Columns = peq.ctx.Fields
if len(peq.ctx.Fields) > 0 {
_spec.Unique = peq.ctx.Unique != nil && *peq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, peq.driver, _spec)
}
func (peq *ProductEventQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(productevent.Table, productevent.Columns, sqlgraph.NewFieldSpec(productevent.FieldID, field.TypeUint64))
_spec.From = peq.sql
if unique := peq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if peq.path != nil {
_spec.Unique = true
}
if fields := peq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, productevent.FieldID)
for i := range fields {
if fields[i] != productevent.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := peq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := peq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := peq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := peq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (peq *ProductEventQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(peq.driver.Dialect())
t1 := builder.Table(productevent.Table)
columns := peq.ctx.Fields
if len(columns) == 0 {
columns = productevent.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if peq.sql != nil {
selector = peq.sql
selector.Select(selector.Columns(columns...)...)
}
if peq.ctx.Unique != nil && *peq.ctx.Unique {
selector.Distinct()
}
for _, m := range peq.modifiers {
m(selector)
}
for _, p := range peq.predicates {
p(selector)
}
for _, p := range peq.order {
p(selector)
}
if offset := peq.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 := peq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// Modify adds a query modifier for attaching custom logic to queries.
func (peq *ProductEventQuery) Modify(modifiers ...func(s *sql.Selector)) *ProductEventSelect {
peq.modifiers = append(peq.modifiers, modifiers...)
return peq.Select()
}
// ProductEventGroupBy is the group-by builder for ProductEvent entities.
type ProductEventGroupBy struct {
selector
build *ProductEventQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (pegb *ProductEventGroupBy) Aggregate(fns ...AggregateFunc) *ProductEventGroupBy {
pegb.fns = append(pegb.fns, fns...)
return pegb
}
// Scan applies the selector query and scans the result into the given value.
func (pegb *ProductEventGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, pegb.build.ctx, "GroupBy")
if err := pegb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ProductEventQuery, *ProductEventGroupBy](ctx, pegb.build, pegb, pegb.build.inters, v)
}
func (pegb *ProductEventGroupBy) sqlScan(ctx context.Context, root *ProductEventQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(pegb.fns))
for _, fn := range pegb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*pegb.flds)+len(pegb.fns))
for _, f := range *pegb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*pegb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := pegb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// ProductEventSelect is the builder for selecting fields of ProductEvent entities.
type ProductEventSelect struct {
*ProductEventQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (pes *ProductEventSelect) Aggregate(fns ...AggregateFunc) *ProductEventSelect {
pes.fns = append(pes.fns, fns...)
return pes
}
// Scan applies the selector query and scans the result into the given value.
func (pes *ProductEventSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, pes.ctx, "Select")
if err := pes.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ProductEventQuery, *ProductEventSelect](ctx, pes.ProductEventQuery, pes, pes.inters, v)
}
func (pes *ProductEventSelect) sqlScan(ctx context.Context, root *ProductEventQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(pes.fns))
for _, fn := range pes.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*pes.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 := pes.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// Modify adds a query modifier for attaching custom logic to queries.
func (pes *ProductEventSelect) Modify(modifiers ...func(s *sql.Selector)) *ProductEventSelect {
pes.modifiers = append(pes.modifiers, modifiers...)
return pes
}
Go
1
https://gitee.com/yuLingNet/simple-iot-basic-rpc.git
git@gitee.com:yuLingNet/simple-iot-basic-rpc.git
yuLingNet
simple-iot-basic-rpc
simple-iot-basic-rpc
0ce7f3c9409c

搜索帮助