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