884 lines
22 KiB
Go
884 lines
22 KiB
Go
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
|
|
"github.com/facebook/ent/dialect/sql"
|
|
"github.com/facebook/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebook/ent/schema/field"
|
|
"github.com/kallydev/privacy/ent/predicate"
|
|
"github.com/kallydev/privacy/ent/sfmodel"
|
|
)
|
|
|
|
// SFModelQuery is the builder for querying SFModel entities.
|
|
type SFModelQuery struct {
|
|
config
|
|
limit *int
|
|
offset *int
|
|
order []OrderFunc
|
|
unique []string
|
|
predicates []predicate.SFModel
|
|
// intermediate query (i.e. traversal path).
|
|
sql *sql.Selector
|
|
path func(context.Context) (*sql.Selector, error)
|
|
}
|
|
|
|
// Where adds a new predicate for the builder.
|
|
func (smq *SFModelQuery) Where(ps ...predicate.SFModel) *SFModelQuery {
|
|
smq.predicates = append(smq.predicates, ps...)
|
|
return smq
|
|
}
|
|
|
|
// Limit adds a limit step to the query.
|
|
func (smq *SFModelQuery) Limit(limit int) *SFModelQuery {
|
|
smq.limit = &limit
|
|
return smq
|
|
}
|
|
|
|
// Offset adds an offset step to the query.
|
|
func (smq *SFModelQuery) Offset(offset int) *SFModelQuery {
|
|
smq.offset = &offset
|
|
return smq
|
|
}
|
|
|
|
// Order adds an order step to the query.
|
|
func (smq *SFModelQuery) Order(o ...OrderFunc) *SFModelQuery {
|
|
smq.order = append(smq.order, o...)
|
|
return smq
|
|
}
|
|
|
|
// First returns the first SFModel entity in the query. Returns *NotFoundError when no sfmodel was found.
|
|
func (smq *SFModelQuery) First(ctx context.Context) (*SFModel, error) {
|
|
nodes, err := smq.Limit(1).All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &NotFoundError{sfmodel.Label}
|
|
}
|
|
return nodes[0], nil
|
|
}
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
func (smq *SFModelQuery) FirstX(ctx context.Context) *SFModel {
|
|
node, err := smq.First(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// FirstID returns the first SFModel id in the query. Returns *NotFoundError when no id was found.
|
|
func (smq *SFModelQuery) FirstID(ctx context.Context) (id int, err error) {
|
|
var ids []int
|
|
if ids, err = smq.Limit(1).IDs(ctx); err != nil {
|
|
return
|
|
}
|
|
if len(ids) == 0 {
|
|
err = &NotFoundError{sfmodel.Label}
|
|
return
|
|
}
|
|
return ids[0], nil
|
|
}
|
|
|
|
// FirstIDX is like FirstID, but panics if an error occurs.
|
|
func (smq *SFModelQuery) FirstIDX(ctx context.Context) int {
|
|
id, err := smq.FirstID(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// Only returns the only SFModel entity in the query, returns an error if not exactly one entity was returned.
|
|
func (smq *SFModelQuery) Only(ctx context.Context) (*SFModel, error) {
|
|
nodes, err := smq.Limit(2).All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch len(nodes) {
|
|
case 1:
|
|
return nodes[0], nil
|
|
case 0:
|
|
return nil, &NotFoundError{sfmodel.Label}
|
|
default:
|
|
return nil, &NotSingularError{sfmodel.Label}
|
|
}
|
|
}
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
func (smq *SFModelQuery) OnlyX(ctx context.Context) *SFModel {
|
|
node, err := smq.Only(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// OnlyID returns the only SFModel id in the query, returns an error if not exactly one id was returned.
|
|
func (smq *SFModelQuery) OnlyID(ctx context.Context) (id int, err error) {
|
|
var ids []int
|
|
if ids, err = smq.Limit(2).IDs(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(ids) {
|
|
case 1:
|
|
id = ids[0]
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = &NotSingularError{sfmodel.Label}
|
|
}
|
|
return
|
|
}
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
func (smq *SFModelQuery) OnlyIDX(ctx context.Context) int {
|
|
id, err := smq.OnlyID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// All executes the query and returns a list of SFModels.
|
|
func (smq *SFModelQuery) All(ctx context.Context) ([]*SFModel, error) {
|
|
if err := smq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return smq.sqlAll(ctx)
|
|
}
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
func (smq *SFModelQuery) AllX(ctx context.Context) []*SFModel {
|
|
nodes, err := smq.All(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
// IDs executes the query and returns a list of SFModel ids.
|
|
func (smq *SFModelQuery) IDs(ctx context.Context) ([]int, error) {
|
|
var ids []int
|
|
if err := smq.Select(sfmodel.FieldID).Scan(ctx, &ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
func (smq *SFModelQuery) IDsX(ctx context.Context) []int {
|
|
ids, err := smq.IDs(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// Count returns the count of the given query.
|
|
func (smq *SFModelQuery) Count(ctx context.Context) (int, error) {
|
|
if err := smq.prepareQuery(ctx); err != nil {
|
|
return 0, err
|
|
}
|
|
return smq.sqlCount(ctx)
|
|
}
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
func (smq *SFModelQuery) CountX(ctx context.Context) int {
|
|
count, err := smq.Count(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return count
|
|
}
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
func (smq *SFModelQuery) Exist(ctx context.Context) (bool, error) {
|
|
if err := smq.prepareQuery(ctx); err != nil {
|
|
return false, err
|
|
}
|
|
return smq.sqlExist(ctx)
|
|
}
|
|
|
|
// ExistX is like Exist, but panics if an error occurs.
|
|
func (smq *SFModelQuery) ExistX(ctx context.Context) bool {
|
|
exist, err := smq.Exist(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return exist
|
|
}
|
|
|
|
// Clone returns a duplicate of the query builder, including all associated steps. It can be
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
func (smq *SFModelQuery) Clone() *SFModelQuery {
|
|
if smq == nil {
|
|
return nil
|
|
}
|
|
return &SFModelQuery{
|
|
config: smq.config,
|
|
limit: smq.limit,
|
|
offset: smq.offset,
|
|
order: append([]OrderFunc{}, smq.order...),
|
|
unique: append([]string{}, smq.unique...),
|
|
predicates: append([]predicate.SFModel{}, smq.predicates...),
|
|
// clone intermediate query.
|
|
sql: smq.sql.Clone(),
|
|
path: smq.path,
|
|
}
|
|
}
|
|
|
|
// GroupBy 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 {
|
|
// Name string `json:"name,omitempty"`
|
|
// Count int `json:"count,omitempty"`
|
|
// }
|
|
//
|
|
// client.SFModel.Query().
|
|
// GroupBy(sfmodel.FieldName).
|
|
// Aggregate(ent.Count()).
|
|
// Scan(ctx, &v)
|
|
//
|
|
func (smq *SFModelQuery) GroupBy(field string, fields ...string) *SFModelGroupBy {
|
|
group := &SFModelGroupBy{config: smq.config}
|
|
group.fields = append([]string{field}, fields...)
|
|
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
|
if err := smq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return smq.sqlQuery(), nil
|
|
}
|
|
return group
|
|
}
|
|
|
|
// Select one or more fields from the given query.
|
|
//
|
|
// Example:
|
|
//
|
|
// var v []struct {
|
|
// Name string `json:"name,omitempty"`
|
|
// }
|
|
//
|
|
// client.SFModel.Query().
|
|
// Select(sfmodel.FieldName).
|
|
// Scan(ctx, &v)
|
|
//
|
|
func (smq *SFModelQuery) Select(field string, fields ...string) *SFModelSelect {
|
|
selector := &SFModelSelect{config: smq.config}
|
|
selector.fields = append([]string{field}, fields...)
|
|
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
|
if err := smq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return smq.sqlQuery(), nil
|
|
}
|
|
return selector
|
|
}
|
|
|
|
func (smq *SFModelQuery) prepareQuery(ctx context.Context) error {
|
|
if smq.path != nil {
|
|
prev, err := smq.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
smq.sql = prev
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (smq *SFModelQuery) sqlAll(ctx context.Context) ([]*SFModel, error) {
|
|
var (
|
|
nodes = []*SFModel{}
|
|
_spec = smq.querySpec()
|
|
)
|
|
_spec.ScanValues = func() []interface{} {
|
|
node := &SFModel{config: smq.config}
|
|
nodes = append(nodes, node)
|
|
values := node.scanValues()
|
|
return values
|
|
}
|
|
_spec.Assign = func(values ...interface{}) error {
|
|
if len(nodes) == 0 {
|
|
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
|
}
|
|
node := nodes[len(nodes)-1]
|
|
return node.assignValues(values...)
|
|
}
|
|
if err := sqlgraph.QueryNodes(ctx, smq.driver, _spec); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nodes, nil
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
func (smq *SFModelQuery) sqlCount(ctx context.Context) (int, error) {
|
|
_spec := smq.querySpec()
|
|
return sqlgraph.CountNodes(ctx, smq.driver, _spec)
|
|
}
|
|
|
|
func (smq *SFModelQuery) sqlExist(ctx context.Context) (bool, error) {
|
|
n, err := smq.sqlCount(ctx)
|
|
if err != nil {
|
|
return false, fmt.Errorf("ent: check existence: %v", err)
|
|
}
|
|
return n > 0, nil
|
|
}
|
|
|
|
func (smq *SFModelQuery) querySpec() *sqlgraph.QuerySpec {
|
|
_spec := &sqlgraph.QuerySpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: sfmodel.Table,
|
|
Columns: sfmodel.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: sfmodel.FieldID,
|
|
},
|
|
},
|
|
From: smq.sql,
|
|
Unique: true,
|
|
}
|
|
if ps := smq.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if limit := smq.limit; limit != nil {
|
|
_spec.Limit = *limit
|
|
}
|
|
if offset := smq.offset; offset != nil {
|
|
_spec.Offset = *offset
|
|
}
|
|
if ps := smq.order; len(ps) > 0 {
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector, sfmodel.ValidColumn)
|
|
}
|
|
}
|
|
}
|
|
return _spec
|
|
}
|
|
|
|
func (smq *SFModelQuery) sqlQuery() *sql.Selector {
|
|
builder := sql.Dialect(smq.driver.Dialect())
|
|
t1 := builder.Table(sfmodel.Table)
|
|
selector := builder.Select(t1.Columns(sfmodel.Columns...)...).From(t1)
|
|
if smq.sql != nil {
|
|
selector = smq.sql
|
|
selector.Select(selector.Columns(sfmodel.Columns...)...)
|
|
}
|
|
for _, p := range smq.predicates {
|
|
p(selector)
|
|
}
|
|
for _, p := range smq.order {
|
|
p(selector, sfmodel.ValidColumn)
|
|
}
|
|
if offset := smq.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 := smq.limit; limit != nil {
|
|
selector.Limit(*limit)
|
|
}
|
|
return selector
|
|
}
|
|
|
|
// SFModelGroupBy is the builder for group-by SFModel entities.
|
|
type SFModelGroupBy struct {
|
|
config
|
|
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 (smgb *SFModelGroupBy) Aggregate(fns ...AggregateFunc) *SFModelGroupBy {
|
|
smgb.fns = append(smgb.fns, fns...)
|
|
return smgb
|
|
}
|
|
|
|
// Scan applies the group-by query and scan the result into the given value.
|
|
func (smgb *SFModelGroupBy) Scan(ctx context.Context, v interface{}) error {
|
|
query, err := smgb.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
smgb.sql = query
|
|
return smgb.sqlScan(ctx, v)
|
|
}
|
|
|
|
// ScanX is like Scan, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) ScanX(ctx context.Context, v interface{}) {
|
|
if err := smgb.Scan(ctx, v); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Strings returns list of strings from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Strings(ctx context.Context) ([]string, error) {
|
|
if len(smgb.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelGroupBy.Strings is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []string
|
|
if err := smgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// StringsX is like Strings, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) StringsX(ctx context.Context) []string {
|
|
v, err := smgb.Strings(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// String returns a single string from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) String(ctx context.Context) (_ string, err error) {
|
|
var v []string
|
|
if v, err = smgb.Strings(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelGroupBy.Strings returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// StringX is like String, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) StringX(ctx context.Context) string {
|
|
v, err := smgb.String(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Ints returns list of ints from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Ints(ctx context.Context) ([]int, error) {
|
|
if len(smgb.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelGroupBy.Ints is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []int
|
|
if err := smgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// IntsX is like Ints, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) IntsX(ctx context.Context) []int {
|
|
v, err := smgb.Ints(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Int returns a single int from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Int(ctx context.Context) (_ int, err error) {
|
|
var v []int
|
|
if v, err = smgb.Ints(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelGroupBy.Ints returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// IntX is like Int, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) IntX(ctx context.Context) int {
|
|
v, err := smgb.Int(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64s returns list of float64s from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Float64s(ctx context.Context) ([]float64, error) {
|
|
if len(smgb.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelGroupBy.Float64s is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []float64
|
|
if err := smgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Float64sX is like Float64s, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) Float64sX(ctx context.Context) []float64 {
|
|
v, err := smgb.Float64s(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64 returns a single float64 from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Float64(ctx context.Context) (_ float64, err error) {
|
|
var v []float64
|
|
if v, err = smgb.Float64s(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelGroupBy.Float64s returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Float64X is like Float64, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) Float64X(ctx context.Context) float64 {
|
|
v, err := smgb.Float64(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bools returns list of bools from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Bools(ctx context.Context) ([]bool, error) {
|
|
if len(smgb.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelGroupBy.Bools is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []bool
|
|
if err := smgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// BoolsX is like Bools, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) BoolsX(ctx context.Context) []bool {
|
|
v, err := smgb.Bools(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bool returns a single bool from group-by. It is only allowed when querying group-by with one field.
|
|
func (smgb *SFModelGroupBy) Bool(ctx context.Context) (_ bool, err error) {
|
|
var v []bool
|
|
if v, err = smgb.Bools(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelGroupBy.Bools returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// BoolX is like Bool, but panics if an error occurs.
|
|
func (smgb *SFModelGroupBy) BoolX(ctx context.Context) bool {
|
|
v, err := smgb.Bool(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (smgb *SFModelGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
|
for _, f := range smgb.fields {
|
|
if !sfmodel.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
|
}
|
|
}
|
|
selector := smgb.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return err
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := selector.Query()
|
|
if err := smgb.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
func (smgb *SFModelGroupBy) sqlQuery() *sql.Selector {
|
|
selector := smgb.sql
|
|
columns := make([]string, 0, len(smgb.fields)+len(smgb.fns))
|
|
columns = append(columns, smgb.fields...)
|
|
for _, fn := range smgb.fns {
|
|
columns = append(columns, fn(selector, sfmodel.ValidColumn))
|
|
}
|
|
return selector.Select(columns...).GroupBy(smgb.fields...)
|
|
}
|
|
|
|
// SFModelSelect is the builder for select fields of SFModel entities.
|
|
type SFModelSelect struct {
|
|
config
|
|
fields []string
|
|
// intermediate query (i.e. traversal path).
|
|
sql *sql.Selector
|
|
path func(context.Context) (*sql.Selector, error)
|
|
}
|
|
|
|
// Scan applies the selector query and scan the result into the given value.
|
|
func (sms *SFModelSelect) Scan(ctx context.Context, v interface{}) error {
|
|
query, err := sms.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sms.sql = query
|
|
return sms.sqlScan(ctx, v)
|
|
}
|
|
|
|
// ScanX is like Scan, but panics if an error occurs.
|
|
func (sms *SFModelSelect) ScanX(ctx context.Context, v interface{}) {
|
|
if err := sms.Scan(ctx, v); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Strings returns list of strings from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Strings(ctx context.Context) ([]string, error) {
|
|
if len(sms.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelSelect.Strings is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []string
|
|
if err := sms.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// StringsX is like Strings, but panics if an error occurs.
|
|
func (sms *SFModelSelect) StringsX(ctx context.Context) []string {
|
|
v, err := sms.Strings(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// String returns a single string from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) String(ctx context.Context) (_ string, err error) {
|
|
var v []string
|
|
if v, err = sms.Strings(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelSelect.Strings returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// StringX is like String, but panics if an error occurs.
|
|
func (sms *SFModelSelect) StringX(ctx context.Context) string {
|
|
v, err := sms.String(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Ints returns list of ints from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Ints(ctx context.Context) ([]int, error) {
|
|
if len(sms.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelSelect.Ints is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []int
|
|
if err := sms.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// IntsX is like Ints, but panics if an error occurs.
|
|
func (sms *SFModelSelect) IntsX(ctx context.Context) []int {
|
|
v, err := sms.Ints(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Int returns a single int from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Int(ctx context.Context) (_ int, err error) {
|
|
var v []int
|
|
if v, err = sms.Ints(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelSelect.Ints returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// IntX is like Int, but panics if an error occurs.
|
|
func (sms *SFModelSelect) IntX(ctx context.Context) int {
|
|
v, err := sms.Int(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64s returns list of float64s from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Float64s(ctx context.Context) ([]float64, error) {
|
|
if len(sms.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelSelect.Float64s is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []float64
|
|
if err := sms.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Float64sX is like Float64s, but panics if an error occurs.
|
|
func (sms *SFModelSelect) Float64sX(ctx context.Context) []float64 {
|
|
v, err := sms.Float64s(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64 returns a single float64 from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Float64(ctx context.Context) (_ float64, err error) {
|
|
var v []float64
|
|
if v, err = sms.Float64s(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelSelect.Float64s returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Float64X is like Float64, but panics if an error occurs.
|
|
func (sms *SFModelSelect) Float64X(ctx context.Context) float64 {
|
|
v, err := sms.Float64(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bools returns list of bools from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Bools(ctx context.Context) ([]bool, error) {
|
|
if len(sms.fields) > 1 {
|
|
return nil, errors.New("ent: SFModelSelect.Bools is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []bool
|
|
if err := sms.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// BoolsX is like Bools, but panics if an error occurs.
|
|
func (sms *SFModelSelect) BoolsX(ctx context.Context) []bool {
|
|
v, err := sms.Bools(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bool returns a single bool from selector. It is only allowed when selecting one field.
|
|
func (sms *SFModelSelect) Bool(ctx context.Context) (_ bool, err error) {
|
|
var v []bool
|
|
if v, err = sms.Bools(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{sfmodel.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: SFModelSelect.Bools returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// BoolX is like Bool, but panics if an error occurs.
|
|
func (sms *SFModelSelect) BoolX(ctx context.Context) bool {
|
|
v, err := sms.Bool(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (sms *SFModelSelect) sqlScan(ctx context.Context, v interface{}) error {
|
|
for _, f := range sms.fields {
|
|
if !sfmodel.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
|
}
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := sms.sqlQuery().Query()
|
|
if err := sms.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
func (sms *SFModelSelect) sqlQuery() sql.Querier {
|
|
selector := sms.sql
|
|
selector.Select(selector.Columns(sms.fields...)...)
|
|
return selector
|
|
}
|