2022-08-29 07:16:12 +00:00
|
|
|
// Code generated by ent, DO NOT EDIT.
|
2021-01-01 20:34:47 +00:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2021-03-24 06:37:51 +00:00
|
|
|
"entgo.io/ent/dialect/sql"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
|
|
"entgo.io/ent/schema/field"
|
2021-01-01 20:34:47 +00:00
|
|
|
"github.com/dexidp/dex/storage/ent/db/devicerequest"
|
|
|
|
"github.com/dexidp/dex/storage/ent/db/predicate"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeviceRequestQuery is the builder for querying DeviceRequest entities.
|
|
|
|
type DeviceRequestQuery struct {
|
|
|
|
config
|
|
|
|
limit *int
|
|
|
|
offset *int
|
2021-04-30 14:31:49 +00:00
|
|
|
unique *bool
|
2021-01-01 20:34:47 +00:00
|
|
|
order []OrderFunc
|
|
|
|
fields []string
|
|
|
|
predicates []predicate.DeviceRequest
|
|
|
|
// intermediate query (i.e. traversal path).
|
|
|
|
sql *sql.Selector
|
|
|
|
path func(context.Context) (*sql.Selector, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Where adds a new predicate for the DeviceRequestQuery builder.
|
|
|
|
func (drq *DeviceRequestQuery) Where(ps ...predicate.DeviceRequest) *DeviceRequestQuery {
|
|
|
|
drq.predicates = append(drq.predicates, ps...)
|
|
|
|
return drq
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit adds a limit step to the query.
|
|
|
|
func (drq *DeviceRequestQuery) Limit(limit int) *DeviceRequestQuery {
|
|
|
|
drq.limit = &limit
|
|
|
|
return drq
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset adds an offset step to the query.
|
|
|
|
func (drq *DeviceRequestQuery) Offset(offset int) *DeviceRequestQuery {
|
|
|
|
drq.offset = &offset
|
|
|
|
return drq
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:31:49 +00:00
|
|
|
// 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 (drq *DeviceRequestQuery) Unique(unique bool) *DeviceRequestQuery {
|
|
|
|
drq.unique = &unique
|
|
|
|
return drq
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:34:47 +00:00
|
|
|
// Order adds an order step to the query.
|
|
|
|
func (drq *DeviceRequestQuery) Order(o ...OrderFunc) *DeviceRequestQuery {
|
|
|
|
drq.order = append(drq.order, o...)
|
|
|
|
return drq
|
|
|
|
}
|
|
|
|
|
|
|
|
// First returns the first DeviceRequest entity from the query.
|
|
|
|
// Returns a *NotFoundError when no DeviceRequest was found.
|
|
|
|
func (drq *DeviceRequestQuery) First(ctx context.Context) (*DeviceRequest, error) {
|
|
|
|
nodes, err := drq.Limit(1).All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil, &NotFoundError{devicerequest.Label}
|
|
|
|
}
|
|
|
|
return nodes[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) FirstX(ctx context.Context) *DeviceRequest {
|
|
|
|
node, err := drq.First(ctx)
|
|
|
|
if err != nil && !IsNotFound(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstID returns the first DeviceRequest ID from the query.
|
|
|
|
// Returns a *NotFoundError when no DeviceRequest ID was found.
|
|
|
|
func (drq *DeviceRequestQuery) FirstID(ctx context.Context) (id int, err error) {
|
|
|
|
var ids []int
|
|
|
|
if ids, err = drq.Limit(1).IDs(ctx); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ids) == 0 {
|
|
|
|
err = &NotFoundError{devicerequest.Label}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return ids[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstIDX is like FirstID, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) FirstIDX(ctx context.Context) int {
|
|
|
|
id, err := drq.FirstID(ctx)
|
|
|
|
if err != nil && !IsNotFound(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only returns a single DeviceRequest entity found by the query, ensuring it only returns one.
|
2022-03-07 09:15:01 +00:00
|
|
|
// Returns a *NotSingularError when more than one DeviceRequest entity is found.
|
2021-01-01 20:34:47 +00:00
|
|
|
// Returns a *NotFoundError when no DeviceRequest entities are found.
|
|
|
|
func (drq *DeviceRequestQuery) Only(ctx context.Context) (*DeviceRequest, error) {
|
|
|
|
nodes, err := drq.Limit(2).All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch len(nodes) {
|
|
|
|
case 1:
|
|
|
|
return nodes[0], nil
|
|
|
|
case 0:
|
|
|
|
return nil, &NotFoundError{devicerequest.Label}
|
|
|
|
default:
|
|
|
|
return nil, &NotSingularError{devicerequest.Label}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) OnlyX(ctx context.Context) *DeviceRequest {
|
|
|
|
node, err := drq.Only(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyID is like Only, but returns the only DeviceRequest ID in the query.
|
2022-03-07 09:15:01 +00:00
|
|
|
// Returns a *NotSingularError when more than one DeviceRequest ID is found.
|
2021-01-01 20:34:47 +00:00
|
|
|
// Returns a *NotFoundError when no entities are found.
|
|
|
|
func (drq *DeviceRequestQuery) OnlyID(ctx context.Context) (id int, err error) {
|
|
|
|
var ids []int
|
|
|
|
if ids, err = drq.Limit(2).IDs(ctx); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch len(ids) {
|
|
|
|
case 1:
|
|
|
|
id = ids[0]
|
|
|
|
case 0:
|
|
|
|
err = &NotFoundError{devicerequest.Label}
|
|
|
|
default:
|
|
|
|
err = &NotSingularError{devicerequest.Label}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) OnlyIDX(ctx context.Context) int {
|
|
|
|
id, err := drq.OnlyID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// All executes the query and returns a list of DeviceRequests.
|
|
|
|
func (drq *DeviceRequestQuery) All(ctx context.Context) ([]*DeviceRequest, error) {
|
|
|
|
if err := drq.prepareQuery(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return drq.sqlAll(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) AllX(ctx context.Context) []*DeviceRequest {
|
|
|
|
nodes, err := drq.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDs executes the query and returns a list of DeviceRequest IDs.
|
|
|
|
func (drq *DeviceRequestQuery) IDs(ctx context.Context) ([]int, error) {
|
|
|
|
var ids []int
|
|
|
|
if err := drq.Select(devicerequest.FieldID).Scan(ctx, &ids); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) IDsX(ctx context.Context) []int {
|
|
|
|
ids, err := drq.IDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the count of the given query.
|
|
|
|
func (drq *DeviceRequestQuery) Count(ctx context.Context) (int, error) {
|
|
|
|
if err := drq.prepareQuery(ctx); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return drq.sqlCount(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) CountX(ctx context.Context) int {
|
|
|
|
count, err := drq.Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
|
|
func (drq *DeviceRequestQuery) Exist(ctx context.Context) (bool, error) {
|
|
|
|
if err := drq.prepareQuery(ctx); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return drq.sqlExist(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistX is like Exist, but panics if an error occurs.
|
|
|
|
func (drq *DeviceRequestQuery) ExistX(ctx context.Context) bool {
|
|
|
|
exist, err := drq.Exist(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns a duplicate of the DeviceRequestQuery builder, including all associated steps. It can be
|
|
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
|
|
func (drq *DeviceRequestQuery) Clone() *DeviceRequestQuery {
|
|
|
|
if drq == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &DeviceRequestQuery{
|
|
|
|
config: drq.config,
|
|
|
|
limit: drq.limit,
|
|
|
|
offset: drq.offset,
|
|
|
|
order: append([]OrderFunc{}, drq.order...),
|
|
|
|
predicates: append([]predicate.DeviceRequest{}, drq.predicates...),
|
|
|
|
// clone intermediate query.
|
2022-03-07 09:15:01 +00:00
|
|
|
sql: drq.sql.Clone(),
|
|
|
|
path: drq.path,
|
|
|
|
unique: drq.unique,
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// UserCode string `json:"user_code,omitempty"`
|
|
|
|
// Count int `json:"count,omitempty"`
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// client.DeviceRequest.Query().
|
|
|
|
// GroupBy(devicerequest.FieldUserCode).
|
|
|
|
// Aggregate(db.Count()).
|
|
|
|
// Scan(ctx, &v)
|
|
|
|
//
|
|
|
|
func (drq *DeviceRequestQuery) GroupBy(field string, fields ...string) *DeviceRequestGroupBy {
|
2022-08-29 07:16:12 +00:00
|
|
|
grbuild := &DeviceRequestGroupBy{config: drq.config}
|
|
|
|
grbuild.fields = append([]string{field}, fields...)
|
|
|
|
grbuild.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
2021-01-01 20:34:47 +00:00
|
|
|
if err := drq.prepareQuery(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-24 06:37:51 +00:00
|
|
|
return drq.sqlQuery(ctx), nil
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
2022-08-29 07:16:12 +00:00
|
|
|
grbuild.label = devicerequest.Label
|
|
|
|
grbuild.flds, grbuild.scan = &grbuild.fields, grbuild.Scan
|
|
|
|
return grbuild
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// UserCode string `json:"user_code,omitempty"`
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// client.DeviceRequest.Query().
|
|
|
|
// Select(devicerequest.FieldUserCode).
|
|
|
|
// Scan(ctx, &v)
|
|
|
|
//
|
2021-09-13 13:48:02 +00:00
|
|
|
func (drq *DeviceRequestQuery) Select(fields ...string) *DeviceRequestSelect {
|
|
|
|
drq.fields = append(drq.fields, fields...)
|
2022-08-29 07:16:12 +00:00
|
|
|
selbuild := &DeviceRequestSelect{DeviceRequestQuery: drq}
|
|
|
|
selbuild.label = devicerequest.Label
|
|
|
|
selbuild.flds, selbuild.scan = &drq.fields, selbuild.Scan
|
|
|
|
return selbuild
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (drq *DeviceRequestQuery) prepareQuery(ctx context.Context) error {
|
|
|
|
for _, f := range drq.fields {
|
|
|
|
if !devicerequest.ValidColumn(f) {
|
|
|
|
return &ValidationError{Name: f, err: fmt.Errorf("db: invalid field %q for query", f)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if drq.path != nil {
|
|
|
|
prev, err := drq.path(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
drq.sql = prev
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 07:16:12 +00:00
|
|
|
func (drq *DeviceRequestQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*DeviceRequest, error) {
|
2021-01-01 20:34:47 +00:00
|
|
|
var (
|
|
|
|
nodes = []*DeviceRequest{}
|
|
|
|
_spec = drq.querySpec()
|
|
|
|
)
|
|
|
|
_spec.ScanValues = func(columns []string) ([]interface{}, error) {
|
2022-08-29 07:16:12 +00:00
|
|
|
return (*DeviceRequest).scanValues(nil, columns)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
_spec.Assign = func(columns []string, values []interface{}) error {
|
2022-08-29 07:16:12 +00:00
|
|
|
node := &DeviceRequest{config: drq.config}
|
|
|
|
nodes = append(nodes, node)
|
2021-01-01 20:34:47 +00:00
|
|
|
return node.assignValues(columns, values)
|
|
|
|
}
|
2022-08-29 07:16:12 +00:00
|
|
|
for i := range hooks {
|
|
|
|
hooks[i](ctx, _spec)
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
if err := sqlgraph.QueryNodes(ctx, drq.driver, _spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drq *DeviceRequestQuery) sqlCount(ctx context.Context) (int, error) {
|
|
|
|
_spec := drq.querySpec()
|
2022-03-07 09:15:01 +00:00
|
|
|
_spec.Node.Columns = drq.fields
|
|
|
|
if len(drq.fields) > 0 {
|
|
|
|
_spec.Unique = drq.unique != nil && *drq.unique
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
return sqlgraph.CountNodes(ctx, drq.driver, _spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drq *DeviceRequestQuery) sqlExist(ctx context.Context) (bool, error) {
|
|
|
|
n, err := drq.sqlCount(ctx)
|
|
|
|
if err != nil {
|
2021-03-24 06:37:51 +00:00
|
|
|
return false, fmt.Errorf("db: check existence: %w", err)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
return n > 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drq *DeviceRequestQuery) querySpec() *sqlgraph.QuerySpec {
|
|
|
|
_spec := &sqlgraph.QuerySpec{
|
|
|
|
Node: &sqlgraph.NodeSpec{
|
|
|
|
Table: devicerequest.Table,
|
|
|
|
Columns: devicerequest.Columns,
|
|
|
|
ID: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt,
|
|
|
|
Column: devicerequest.FieldID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
From: drq.sql,
|
|
|
|
Unique: true,
|
|
|
|
}
|
2021-04-30 14:31:49 +00:00
|
|
|
if unique := drq.unique; unique != nil {
|
|
|
|
_spec.Unique = *unique
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
if fields := drq.fields; len(fields) > 0 {
|
|
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
|
|
_spec.Node.Columns = append(_spec.Node.Columns, devicerequest.FieldID)
|
|
|
|
for i := range fields {
|
|
|
|
if fields[i] != devicerequest.FieldID {
|
|
|
|
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ps := drq.predicates; len(ps) > 0 {
|
|
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
|
|
for i := range ps {
|
|
|
|
ps[i](selector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit := drq.limit; limit != nil {
|
|
|
|
_spec.Limit = *limit
|
|
|
|
}
|
|
|
|
if offset := drq.offset; offset != nil {
|
|
|
|
_spec.Offset = *offset
|
|
|
|
}
|
|
|
|
if ps := drq.order; len(ps) > 0 {
|
|
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
|
|
for i := range ps {
|
2021-04-30 14:31:49 +00:00
|
|
|
ps[i](selector)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _spec
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:37:51 +00:00
|
|
|
func (drq *DeviceRequestQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
2021-01-01 20:34:47 +00:00
|
|
|
builder := sql.Dialect(drq.driver.Dialect())
|
|
|
|
t1 := builder.Table(devicerequest.Table)
|
2021-09-13 13:48:02 +00:00
|
|
|
columns := drq.fields
|
|
|
|
if len(columns) == 0 {
|
|
|
|
columns = devicerequest.Columns
|
|
|
|
}
|
|
|
|
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
2021-01-01 20:34:47 +00:00
|
|
|
if drq.sql != nil {
|
|
|
|
selector = drq.sql
|
2021-09-13 13:48:02 +00:00
|
|
|
selector.Select(selector.Columns(columns...)...)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
2022-03-07 09:15:01 +00:00
|
|
|
if drq.unique != nil && *drq.unique {
|
|
|
|
selector.Distinct()
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
for _, p := range drq.predicates {
|
|
|
|
p(selector)
|
|
|
|
}
|
|
|
|
for _, p := range drq.order {
|
2021-04-30 14:31:49 +00:00
|
|
|
p(selector)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if offset := drq.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 := drq.limit; limit != nil {
|
|
|
|
selector.Limit(*limit)
|
|
|
|
}
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceRequestGroupBy is the group-by builder for DeviceRequest entities.
|
|
|
|
type DeviceRequestGroupBy struct {
|
|
|
|
config
|
2022-08-29 07:16:12 +00:00
|
|
|
selector
|
2021-01-01 20:34:47 +00:00
|
|
|
fields []string
|
|
|
|
fns []AggregateFunc
|
|
|
|
// intermediate query (i.e. traversal path).
|
|
|
|
sql *sql.Selector
|
|
|
|
path func(context.Context) (*sql.Selector, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate adds the given aggregation functions to the group-by query.
|
|
|
|
func (drgb *DeviceRequestGroupBy) Aggregate(fns ...AggregateFunc) *DeviceRequestGroupBy {
|
|
|
|
drgb.fns = append(drgb.fns, fns...)
|
|
|
|
return drgb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan applies the group-by query and scans the result into the given value.
|
|
|
|
func (drgb *DeviceRequestGroupBy) Scan(ctx context.Context, v interface{}) error {
|
|
|
|
query, err := drgb.path(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
drgb.sql = query
|
|
|
|
return drgb.sqlScan(ctx, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drgb *DeviceRequestGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
|
|
|
for _, f := range drgb.fields {
|
|
|
|
if !devicerequest.ValidColumn(f) {
|
|
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selector := drgb.sqlQuery()
|
|
|
|
if err := selector.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows := &sql.Rows{}
|
|
|
|
query, args := selector.Query()
|
|
|
|
if err := drgb.driver.Query(ctx, query, args, rows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return sql.ScanSlice(rows, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drgb *DeviceRequestGroupBy) sqlQuery() *sql.Selector {
|
2021-09-13 13:48:02 +00:00
|
|
|
selector := drgb.sql.Select()
|
|
|
|
aggregation := make([]string, 0, len(drgb.fns))
|
2021-01-01 20:34:47 +00:00
|
|
|
for _, fn := range drgb.fns {
|
2021-09-13 13:48:02 +00:00
|
|
|
aggregation = append(aggregation, fn(selector))
|
|
|
|
}
|
|
|
|
// If no columns were selected in a custom aggregation function, the default
|
|
|
|
// selection is the fields used for "group-by", and the aggregation functions.
|
|
|
|
if len(selector.SelectedColumns()) == 0 {
|
|
|
|
columns := make([]string, 0, len(drgb.fields)+len(drgb.fns))
|
|
|
|
for _, f := range drgb.fields {
|
|
|
|
columns = append(columns, selector.C(f))
|
|
|
|
}
|
2022-03-07 09:15:01 +00:00
|
|
|
columns = append(columns, aggregation...)
|
2021-09-13 13:48:02 +00:00
|
|
|
selector.Select(columns...)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
2021-09-13 13:48:02 +00:00
|
|
|
return selector.GroupBy(selector.Columns(drgb.fields...)...)
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceRequestSelect is the builder for selecting fields of DeviceRequest entities.
|
|
|
|
type DeviceRequestSelect struct {
|
|
|
|
*DeviceRequestQuery
|
2022-08-29 07:16:12 +00:00
|
|
|
selector
|
2021-01-01 20:34:47 +00:00
|
|
|
// intermediate query (i.e. traversal path).
|
|
|
|
sql *sql.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
|
|
func (drs *DeviceRequestSelect) Scan(ctx context.Context, v interface{}) error {
|
|
|
|
if err := drs.prepareQuery(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-24 06:37:51 +00:00
|
|
|
drs.sql = drs.DeviceRequestQuery.sqlQuery(ctx)
|
2021-01-01 20:34:47 +00:00
|
|
|
return drs.sqlScan(ctx, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drs *DeviceRequestSelect) sqlScan(ctx context.Context, v interface{}) error {
|
|
|
|
rows := &sql.Rows{}
|
2021-09-13 13:48:02 +00:00
|
|
|
query, args := drs.sql.Query()
|
2021-01-01 20:34:47 +00:00
|
|
|
if err := drs.driver.Query(ctx, query, args, rows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return sql.ScanSlice(rows, v)
|
|
|
|
}
|