// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/safedep/vet/ent/predicate"
"github.com/safedep/vet/ent/reportmalware"
"github.com/safedep/vet/ent/reportpackage"
)
// ReportMalwareQuery is the builder for querying ReportMalware entities.
type ReportMalwareQuery struct {
config
ctx *QueryContext
order []reportmalware.OrderOption
inters []Interceptor
predicates []predicate.ReportMalware
withPackage *ReportPackageQuery
withFKs bool
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the ReportMalwareQuery builder.
func (rmq *ReportMalwareQuery) Where(ps ...predicate.ReportMalware) *ReportMalwareQuery {
rmq.predicates = append(rmq.predicates, ps...)
return rmq
}
// Limit the number of records to be returned by this query.
func (rmq *ReportMalwareQuery) Limit(limit int) *ReportMalwareQuery {
rmq.ctx.Limit = &limit
return rmq
}
// Offset to start from.
func (rmq *ReportMalwareQuery) Offset(offset int) *ReportMalwareQuery {
rmq.ctx.Offset = &offset
return rmq
}
// 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 (rmq *ReportMalwareQuery) Unique(unique bool) *ReportMalwareQuery {
rmq.ctx.Unique = &unique
return rmq
}
// Order specifies how the records should be ordered.
func (rmq *ReportMalwareQuery) Order(o ...reportmalware.OrderOption) *ReportMalwareQuery {
rmq.order = append(rmq.order, o...)
return rmq
}
// QueryPackage chains the current query on the "package" edge.
func (rmq *ReportMalwareQuery) QueryPackage() *ReportPackageQuery {
query := (&ReportPackageClient{config: rmq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := rmq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := rmq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(reportmalware.Table, reportmalware.FieldID, selector),
sqlgraph.To(reportpackage.Table, reportpackage.FieldID),
sqlgraph.Edge(sqlgraph.O2O, true, reportmalware.PackageTable, reportmalware.PackageColumn),
)
fromU = sqlgraph.SetNeighbors(rmq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first ReportMalware entity from the query.
// Returns a *NotFoundError when no ReportMalware was found.
func (rmq *ReportMalwareQuery) First(ctx context.Context) (*ReportMalware, error) {
nodes, err := rmq.Limit(1).All(setContextOp(ctx, rmq.ctx, ent.OpQueryFirst))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{reportmalware.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (rmq *ReportMalwareQuery) FirstX(ctx context.Context) *ReportMalware {
node, err := rmq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first ReportMalware ID from the query.
// Returns a *NotFoundError when no ReportMalware ID was found.
func (rmq *ReportMalwareQuery) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = rmq.Limit(1).IDs(setContextOp(ctx, rmq.ctx, ent.OpQueryFirstID)); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{reportmalware.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (rmq *ReportMalwareQuery) FirstIDX(ctx context.Context) int {
id, err := rmq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single ReportMalware entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one ReportMalware entity is found.
// Returns a *NotFoundError when no ReportMalware entities are found.
func (rmq *ReportMalwareQuery) Only(ctx context.Context) (*ReportMalware, error) {
nodes, err := rmq.Limit(2).All(setContextOp(ctx, rmq.ctx, ent.OpQueryOnly))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{reportmalware.Label}
default:
return nil, &NotSingularError{reportmalware.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (rmq *ReportMalwareQuery) OnlyX(ctx context.Context) *ReportMalware {
node, err := rmq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only ReportMalware ID in the query.
// Returns a *NotSingularError when more than one ReportMalware ID is found.
// Returns a *NotFoundError when no entities are found.
func (rmq *ReportMalwareQuery) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = rmq.Limit(2).IDs(setContextOp(ctx, rmq.ctx, ent.OpQueryOnlyID)); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{reportmalware.Label}
default:
err = &NotSingularError{reportmalware.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (rmq *ReportMalwareQuery) OnlyIDX(ctx context.Context) int {
id, err := rmq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of ReportMalwares.
func (rmq *ReportMalwareQuery) All(ctx context.Context) ([]*ReportMalware, error) {
ctx = setContextOp(ctx, rmq.ctx, ent.OpQueryAll)
if err := rmq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*ReportMalware, *ReportMalwareQuery]()
return withInterceptors[[]*ReportMalware](ctx, rmq, qr, rmq.inters)
}
// AllX is like All, but panics if an error occurs.
func (rmq *ReportMalwareQuery) AllX(ctx context.Context) []*ReportMalware {
nodes, err := rmq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of ReportMalware IDs.
func (rmq *ReportMalwareQuery) IDs(ctx context.Context) (ids []int, err error) {
if rmq.ctx.Unique == nil && rmq.path != nil {
rmq.Unique(true)
}
ctx = setContextOp(ctx, rmq.ctx, ent.OpQueryIDs)
if err = rmq.Select(reportmalware.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (rmq *ReportMalwareQuery) IDsX(ctx context.Context) []int {
ids, err := rmq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (rmq *ReportMalwareQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, rmq.ctx, ent.OpQueryCount)
if err := rmq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, rmq, querierCount[*ReportMalwareQuery](), rmq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (rmq *ReportMalwareQuery) CountX(ctx context.Context) int {
count, err := rmq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (rmq *ReportMalwareQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, rmq.ctx, ent.OpQueryExist)
switch _, err := rmq.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 (rmq *ReportMalwareQuery) ExistX(ctx context.Context) bool {
exist, err := rmq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the ReportMalwareQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (rmq *ReportMalwareQuery) Clone() *ReportMalwareQuery {
if rmq == nil {
return nil
}
return &ReportMalwareQuery{
config: rmq.config,
ctx: rmq.ctx.Clone(),
order: append([]reportmalware.OrderOption{}, rmq.order...),
inters: append([]Interceptor{}, rmq.inters...),
predicates: append([]predicate.ReportMalware{}, rmq.predicates...),
withPackage: rmq.withPackage.Clone(),
// clone intermediate query.
sql: rmq.sql.Clone(),
path: rmq.path,
}
}
// WithPackage tells the query-builder to eager-load the nodes that are connected to
// the "package" edge. The optional arguments are used to configure the query builder of the edge.
func (rmq *ReportMalwareQuery) WithPackage(opts ...func(*ReportPackageQuery)) *ReportMalwareQuery {
query := (&ReportPackageClient{config: rmq.config}).Query()
for _, opt := range opts {
opt(query)
}
rmq.withPackage = query
return rmq
}
// 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 {
// AnalysisID string `json:"analysis_id,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.ReportMalware.Query().
// GroupBy(reportmalware.FieldAnalysisID).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (rmq *ReportMalwareQuery) GroupBy(field string, fields ...string) *ReportMalwareGroupBy {
rmq.ctx.Fields = append([]string{field}, fields...)
grbuild := &ReportMalwareGroupBy{build: rmq}
grbuild.flds = &rmq.ctx.Fields
grbuild.label = reportmalware.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 {
// AnalysisID string `json:"analysis_id,omitempty"`
// }
//
// client.ReportMalware.Query().
// Select(reportmalware.FieldAnalysisID).
// Scan(ctx, &v)
func (rmq *ReportMalwareQuery) Select(fields ...string) *ReportMalwareSelect {
rmq.ctx.Fields = append(rmq.ctx.Fields, fields...)
sbuild := &ReportMalwareSelect{ReportMalwareQuery: rmq}
sbuild.label = reportmalware.Label
sbuild.flds, sbuild.scan = &rmq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a ReportMalwareSelect configured with the given aggregations.
func (rmq *ReportMalwareQuery) Aggregate(fns ...AggregateFunc) *ReportMalwareSelect {
return rmq.Select().Aggregate(fns...)
}
func (rmq *ReportMalwareQuery) prepareQuery(ctx context.Context) error {
for _, inter := range rmq.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, rmq); err != nil {
return err
}
}
}
for _, f := range rmq.ctx.Fields {
if !reportmalware.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if rmq.path != nil {
prev, err := rmq.path(ctx)
if err != nil {
return err
}
rmq.sql = prev
}
return nil
}
func (rmq *ReportMalwareQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*ReportMalware, error) {
var (
nodes = []*ReportMalware{}
withFKs = rmq.withFKs
_spec = rmq.querySpec()
loadedTypes = [1]bool{
rmq.withPackage != nil,
}
)
if rmq.withPackage != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, reportmalware.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*ReportMalware).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &ReportMalware{config: rmq.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, rmq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := rmq.withPackage; query != nil {
if err := rmq.loadPackage(ctx, query, nodes, nil,
func(n *ReportMalware, e *ReportPackage) { n.Edges.Package = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (rmq *ReportMalwareQuery) loadPackage(ctx context.Context, query *ReportPackageQuery, nodes []*ReportMalware, init func(*ReportMalware), assign func(*ReportMalware, *ReportPackage)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*ReportMalware)
for i := range nodes {
if nodes[i].report_package_malware_analysis == nil {
continue
}
fk := *nodes[i].report_package_malware_analysis
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(reportpackage.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 "report_package_malware_analysis" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (rmq *ReportMalwareQuery) sqlCount(ctx context.Context) (int, error) {
_spec := rmq.querySpec()
_spec.Node.Columns = rmq.ctx.Fields
if len(rmq.ctx.Fields) > 0 {
_spec.Unique = rmq.ctx.Unique != nil && *rmq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, rmq.driver, _spec)
}
func (rmq *ReportMalwareQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(reportmalware.Table, reportmalware.Columns, sqlgraph.NewFieldSpec(reportmalware.FieldID, field.TypeInt))
_spec.From = rmq.sql
if unique := rmq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if rmq.path != nil {
_spec.Unique = true
}
if fields := rmq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, reportmalware.FieldID)
for i := range fields {
if fields[i] != reportmalware.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := rmq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := rmq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := rmq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := rmq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (rmq *ReportMalwareQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(rmq.driver.Dialect())
t1 := builder.Table(reportmalware.Table)
columns := rmq.ctx.Fields
if len(columns) == 0 {
columns = reportmalware.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if rmq.sql != nil {
selector = rmq.sql
selector.Select(selector.Columns(columns...)...)
}
if rmq.ctx.Unique != nil && *rmq.ctx.Unique {
selector.Distinct()
}
for _, p := range rmq.predicates {
p(selector)
}
for _, p := range rmq.order {
p(selector)
}
if offset := rmq.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 := rmq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// ReportMalwareGroupBy is the group-by builder for ReportMalware entities.
type ReportMalwareGroupBy struct {
selector
build *ReportMalwareQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (rmgb *ReportMalwareGroupBy) Aggregate(fns ...AggregateFunc) *ReportMalwareGroupBy {
rmgb.fns = append(rmgb.fns, fns...)
return rmgb
}
// Scan applies the selector query and scans the result into the given value.
func (rmgb *ReportMalwareGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, rmgb.build.ctx, ent.OpQueryGroupBy)
if err := rmgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ReportMalwareQuery, *ReportMalwareGroupBy](ctx, rmgb.build, rmgb, rmgb.build.inters, v)
}
func (rmgb *ReportMalwareGroupBy) sqlScan(ctx context.Context, root *ReportMalwareQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(rmgb.fns))
for _, fn := range rmgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*rmgb.flds)+len(rmgb.fns))
for _, f := range *rmgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*rmgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := rmgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// ReportMalwareSelect is the builder for selecting fields of ReportMalware entities.
type ReportMalwareSelect struct {
*ReportMalwareQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (rms *ReportMalwareSelect) Aggregate(fns ...AggregateFunc) *ReportMalwareSelect {
rms.fns = append(rms.fns, fns...)
return rms
}
// Scan applies the selector query and scans the result into the given value.
func (rms *ReportMalwareSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, rms.ctx, ent.OpQuerySelect)
if err := rms.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ReportMalwareQuery, *ReportMalwareSelect](ctx, rms.ReportMalwareQuery, rms, rms.inters, v)
}
func (rms *ReportMalwareSelect) sqlScan(ctx context.Context, root *ReportMalwareQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(rms.fns))
for _, fn := range rms.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*rms.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 := rms.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}