2024-07-20 15:37:01 +00:00
|
|
|
// Code generated by ent, DO NOT EDIT.
|
|
|
|
|
|
|
|
package ent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
|
|
"entgo.io/ent/schema/field"
|
|
|
|
"leafdev.top/leaf/rag/ent/document"
|
|
|
|
"leafdev.top/leaf/rag/ent/documentblock"
|
|
|
|
"leafdev.top/leaf/rag/ent/predicate"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DocumentBlockQuery is the builder for querying DocumentBlock entities.
|
|
|
|
type DocumentBlockQuery struct {
|
|
|
|
config
|
|
|
|
ctx *QueryContext
|
|
|
|
order []documentblock.OrderOption
|
|
|
|
inters []Interceptor
|
|
|
|
predicates []predicate.DocumentBlock
|
|
|
|
withDocument *DocumentQuery
|
|
|
|
// intermediate query (i.e. traversal path).
|
|
|
|
sql *sql.Selector
|
|
|
|
path func(context.Context) (*sql.Selector, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Where adds a new predicate for the DocumentBlockQuery builder.
|
|
|
|
func (dbq *DocumentBlockQuery) Where(ps ...predicate.DocumentBlock) *DocumentBlockQuery {
|
|
|
|
dbq.predicates = append(dbq.predicates, ps...)
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the number of records to be returned by this query.
|
|
|
|
func (dbq *DocumentBlockQuery) Limit(limit int) *DocumentBlockQuery {
|
|
|
|
dbq.ctx.Limit = &limit
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset to start from.
|
|
|
|
func (dbq *DocumentBlockQuery) Offset(offset int) *DocumentBlockQuery {
|
|
|
|
dbq.ctx.Offset = &offset
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (dbq *DocumentBlockQuery) Unique(unique bool) *DocumentBlockQuery {
|
|
|
|
dbq.ctx.Unique = &unique
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order specifies how the records should be ordered.
|
|
|
|
func (dbq *DocumentBlockQuery) Order(o ...documentblock.OrderOption) *DocumentBlockQuery {
|
|
|
|
dbq.order = append(dbq.order, o...)
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryDocument chains the current query on the "document" edge.
|
|
|
|
func (dbq *DocumentBlockQuery) QueryDocument() *DocumentQuery {
|
|
|
|
query := (&DocumentClient{config: dbq.config}).Query()
|
|
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
|
|
if err := dbq.prepareQuery(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
selector := dbq.sqlQuery(ctx)
|
|
|
|
if err := selector.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
step := sqlgraph.NewStep(
|
|
|
|
sqlgraph.From(documentblock.Table, documentblock.FieldID, selector),
|
|
|
|
sqlgraph.To(document.Table, document.FieldID),
|
|
|
|
sqlgraph.Edge(sqlgraph.O2M, false, documentblock.DocumentTable, documentblock.DocumentColumn),
|
|
|
|
)
|
|
|
|
fromU = sqlgraph.SetNeighbors(dbq.driver.Dialect(), step)
|
|
|
|
return fromU, nil
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
// First returns the first DocumentBlock entity from the query.
|
|
|
|
// Returns a *NotFoundError when no DocumentBlock was found.
|
|
|
|
func (dbq *DocumentBlockQuery) First(ctx context.Context) (*DocumentBlock, error) {
|
|
|
|
nodes, err := dbq.Limit(1).All(setContextOp(ctx, dbq.ctx, "First"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil, &NotFoundError{documentblock.Label}
|
|
|
|
}
|
|
|
|
return nodes[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) FirstX(ctx context.Context) *DocumentBlock {
|
|
|
|
node, err := dbq.First(ctx)
|
|
|
|
if err != nil && !IsNotFound(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstID returns the first DocumentBlock ID from the query.
|
|
|
|
// Returns a *NotFoundError when no DocumentBlock ID was found.
|
|
|
|
func (dbq *DocumentBlockQuery) FirstID(ctx context.Context) (id int, err error) {
|
|
|
|
var ids []int
|
|
|
|
if ids, err = dbq.Limit(1).IDs(setContextOp(ctx, dbq.ctx, "FirstID")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ids) == 0 {
|
|
|
|
err = &NotFoundError{documentblock.Label}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return ids[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstIDX is like FirstID, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) FirstIDX(ctx context.Context) int {
|
|
|
|
id, err := dbq.FirstID(ctx)
|
|
|
|
if err != nil && !IsNotFound(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only returns a single DocumentBlock entity found by the query, ensuring it only returns one.
|
|
|
|
// Returns a *NotSingularError when more than one DocumentBlock entity is found.
|
|
|
|
// Returns a *NotFoundError when no DocumentBlock entities are found.
|
|
|
|
func (dbq *DocumentBlockQuery) Only(ctx context.Context) (*DocumentBlock, error) {
|
|
|
|
nodes, err := dbq.Limit(2).All(setContextOp(ctx, dbq.ctx, "Only"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch len(nodes) {
|
|
|
|
case 1:
|
|
|
|
return nodes[0], nil
|
|
|
|
case 0:
|
|
|
|
return nil, &NotFoundError{documentblock.Label}
|
|
|
|
default:
|
|
|
|
return nil, &NotSingularError{documentblock.Label}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) OnlyX(ctx context.Context) *DocumentBlock {
|
|
|
|
node, err := dbq.Only(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyID is like Only, but returns the only DocumentBlock ID in the query.
|
|
|
|
// Returns a *NotSingularError when more than one DocumentBlock ID is found.
|
|
|
|
// Returns a *NotFoundError when no entities are found.
|
|
|
|
func (dbq *DocumentBlockQuery) OnlyID(ctx context.Context) (id int, err error) {
|
|
|
|
var ids []int
|
|
|
|
if ids, err = dbq.Limit(2).IDs(setContextOp(ctx, dbq.ctx, "OnlyID")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch len(ids) {
|
|
|
|
case 1:
|
|
|
|
id = ids[0]
|
|
|
|
case 0:
|
|
|
|
err = &NotFoundError{documentblock.Label}
|
|
|
|
default:
|
|
|
|
err = &NotSingularError{documentblock.Label}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) OnlyIDX(ctx context.Context) int {
|
|
|
|
id, err := dbq.OnlyID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// All executes the query and returns a list of DocumentBlocks.
|
|
|
|
func (dbq *DocumentBlockQuery) All(ctx context.Context) ([]*DocumentBlock, error) {
|
|
|
|
ctx = setContextOp(ctx, dbq.ctx, "All")
|
|
|
|
if err := dbq.prepareQuery(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
qr := querierAll[[]*DocumentBlock, *DocumentBlockQuery]()
|
|
|
|
return withInterceptors[[]*DocumentBlock](ctx, dbq, qr, dbq.inters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) AllX(ctx context.Context) []*DocumentBlock {
|
|
|
|
nodes, err := dbq.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDs executes the query and returns a list of DocumentBlock IDs.
|
|
|
|
func (dbq *DocumentBlockQuery) IDs(ctx context.Context) (ids []int, err error) {
|
|
|
|
if dbq.ctx.Unique == nil && dbq.path != nil {
|
|
|
|
dbq.Unique(true)
|
|
|
|
}
|
|
|
|
ctx = setContextOp(ctx, dbq.ctx, "IDs")
|
|
|
|
if err = dbq.Select(documentblock.FieldID).Scan(ctx, &ids); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) IDsX(ctx context.Context) []int {
|
|
|
|
ids, err := dbq.IDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the count of the given query.
|
|
|
|
func (dbq *DocumentBlockQuery) Count(ctx context.Context) (int, error) {
|
|
|
|
ctx = setContextOp(ctx, dbq.ctx, "Count")
|
|
|
|
if err := dbq.prepareQuery(ctx); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return withInterceptors[int](ctx, dbq, querierCount[*DocumentBlockQuery](), dbq.inters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
|
|
func (dbq *DocumentBlockQuery) CountX(ctx context.Context) int {
|
|
|
|
count, err := dbq.Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
|
|
func (dbq *DocumentBlockQuery) Exist(ctx context.Context) (bool, error) {
|
|
|
|
ctx = setContextOp(ctx, dbq.ctx, "Exist")
|
|
|
|
switch _, err := dbq.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 (dbq *DocumentBlockQuery) ExistX(ctx context.Context) bool {
|
|
|
|
exist, err := dbq.Exist(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns a duplicate of the DocumentBlockQuery builder, including all associated steps. It can be
|
|
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
|
|
func (dbq *DocumentBlockQuery) Clone() *DocumentBlockQuery {
|
|
|
|
if dbq == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &DocumentBlockQuery{
|
|
|
|
config: dbq.config,
|
|
|
|
ctx: dbq.ctx.Clone(),
|
|
|
|
order: append([]documentblock.OrderOption{}, dbq.order...),
|
|
|
|
inters: append([]Interceptor{}, dbq.inters...),
|
|
|
|
predicates: append([]predicate.DocumentBlock{}, dbq.predicates...),
|
|
|
|
withDocument: dbq.withDocument.Clone(),
|
|
|
|
// clone intermediate query.
|
|
|
|
sql: dbq.sql.Clone(),
|
|
|
|
path: dbq.path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDocument tells the query-builder to eager-load the nodes that are connected to
|
|
|
|
// the "document" edge. The optional arguments are used to configure the query builder of the edge.
|
|
|
|
func (dbq *DocumentBlockQuery) WithDocument(opts ...func(*DocumentQuery)) *DocumentBlockQuery {
|
|
|
|
query := (&DocumentClient{config: dbq.config}).Query()
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(query)
|
|
|
|
}
|
|
|
|
dbq.withDocument = query
|
|
|
|
return dbq
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-07-21 09:24:43 +00:00
|
|
|
// Content string `json:"content,omitempty"`
|
2024-07-20 15:37:01 +00:00
|
|
|
// Count int `json:"count,omitempty"`
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// client.DocumentBlock.Query().
|
2024-07-21 09:24:43 +00:00
|
|
|
// GroupBy(documentblock.FieldContent).
|
2024-07-20 15:37:01 +00:00
|
|
|
// Aggregate(ent.Count()).
|
|
|
|
// Scan(ctx, &v)
|
|
|
|
func (dbq *DocumentBlockQuery) GroupBy(field string, fields ...string) *DocumentBlockGroupBy {
|
|
|
|
dbq.ctx.Fields = append([]string{field}, fields...)
|
|
|
|
grbuild := &DocumentBlockGroupBy{build: dbq}
|
|
|
|
grbuild.flds = &dbq.ctx.Fields
|
|
|
|
grbuild.label = documentblock.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 {
|
2024-07-21 09:24:43 +00:00
|
|
|
// Content string `json:"content,omitempty"`
|
2024-07-20 15:37:01 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// client.DocumentBlock.Query().
|
2024-07-21 09:24:43 +00:00
|
|
|
// Select(documentblock.FieldContent).
|
2024-07-20 15:37:01 +00:00
|
|
|
// Scan(ctx, &v)
|
|
|
|
func (dbq *DocumentBlockQuery) Select(fields ...string) *DocumentBlockSelect {
|
|
|
|
dbq.ctx.Fields = append(dbq.ctx.Fields, fields...)
|
|
|
|
sbuild := &DocumentBlockSelect{DocumentBlockQuery: dbq}
|
|
|
|
sbuild.label = documentblock.Label
|
|
|
|
sbuild.flds, sbuild.scan = &dbq.ctx.Fields, sbuild.Scan
|
|
|
|
return sbuild
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate returns a DocumentBlockSelect configured with the given aggregations.
|
|
|
|
func (dbq *DocumentBlockQuery) Aggregate(fns ...AggregateFunc) *DocumentBlockSelect {
|
|
|
|
return dbq.Select().Aggregate(fns...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) prepareQuery(ctx context.Context) error {
|
|
|
|
for _, inter := range dbq.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, dbq); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range dbq.ctx.Fields {
|
|
|
|
if !documentblock.ValidColumn(f) {
|
|
|
|
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbq.path != nil {
|
|
|
|
prev, err := dbq.path(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dbq.sql = prev
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*DocumentBlock, error) {
|
|
|
|
var (
|
|
|
|
nodes = []*DocumentBlock{}
|
|
|
|
_spec = dbq.querySpec()
|
|
|
|
loadedTypes = [1]bool{
|
|
|
|
dbq.withDocument != nil,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
_spec.ScanValues = func(columns []string) ([]any, error) {
|
|
|
|
return (*DocumentBlock).scanValues(nil, columns)
|
|
|
|
}
|
|
|
|
_spec.Assign = func(columns []string, values []any) error {
|
|
|
|
node := &DocumentBlock{config: dbq.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, dbq.driver, _spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
if query := dbq.withDocument; query != nil {
|
|
|
|
if err := dbq.loadDocument(ctx, query, nodes,
|
|
|
|
func(n *DocumentBlock) { n.Edges.Document = []*Document{} },
|
|
|
|
func(n *DocumentBlock, e *Document) { n.Edges.Document = append(n.Edges.Document, e) }); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) loadDocument(ctx context.Context, query *DocumentQuery, nodes []*DocumentBlock, init func(*DocumentBlock), assign func(*DocumentBlock, *Document)) error {
|
|
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
|
|
nodeids := make(map[int]*DocumentBlock)
|
|
|
|
for i := range nodes {
|
|
|
|
fks = append(fks, nodes[i].ID)
|
|
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
|
|
if init != nil {
|
|
|
|
init(nodes[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query.withFKs = true
|
|
|
|
query.Where(predicate.Document(func(s *sql.Selector) {
|
|
|
|
s.Where(sql.InValues(s.C(documentblock.DocumentColumn), fks...))
|
|
|
|
}))
|
|
|
|
neighbors, err := query.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, n := range neighbors {
|
|
|
|
fk := n.document_block_document
|
|
|
|
if fk == nil {
|
|
|
|
return fmt.Errorf(`foreign-key "document_block_document" is nil for node %v`, n.ID)
|
|
|
|
}
|
|
|
|
node, ok := nodeids[*fk]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(`unexpected referenced foreign-key "document_block_document" returned %v for node %v`, *fk, n.ID)
|
|
|
|
}
|
|
|
|
assign(node, n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) sqlCount(ctx context.Context) (int, error) {
|
|
|
|
_spec := dbq.querySpec()
|
|
|
|
_spec.Node.Columns = dbq.ctx.Fields
|
|
|
|
if len(dbq.ctx.Fields) > 0 {
|
|
|
|
_spec.Unique = dbq.ctx.Unique != nil && *dbq.ctx.Unique
|
|
|
|
}
|
|
|
|
return sqlgraph.CountNodes(ctx, dbq.driver, _spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) querySpec() *sqlgraph.QuerySpec {
|
|
|
|
_spec := sqlgraph.NewQuerySpec(documentblock.Table, documentblock.Columns, sqlgraph.NewFieldSpec(documentblock.FieldID, field.TypeInt))
|
|
|
|
_spec.From = dbq.sql
|
|
|
|
if unique := dbq.ctx.Unique; unique != nil {
|
|
|
|
_spec.Unique = *unique
|
|
|
|
} else if dbq.path != nil {
|
|
|
|
_spec.Unique = true
|
|
|
|
}
|
|
|
|
if fields := dbq.ctx.Fields; len(fields) > 0 {
|
|
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
|
|
_spec.Node.Columns = append(_spec.Node.Columns, documentblock.FieldID)
|
|
|
|
for i := range fields {
|
|
|
|
if fields[i] != documentblock.FieldID {
|
|
|
|
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ps := dbq.predicates; len(ps) > 0 {
|
|
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
|
|
for i := range ps {
|
|
|
|
ps[i](selector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit := dbq.ctx.Limit; limit != nil {
|
|
|
|
_spec.Limit = *limit
|
|
|
|
}
|
|
|
|
if offset := dbq.ctx.Offset; offset != nil {
|
|
|
|
_spec.Offset = *offset
|
|
|
|
}
|
|
|
|
if ps := dbq.order; len(ps) > 0 {
|
|
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
|
|
for i := range ps {
|
|
|
|
ps[i](selector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _spec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbq *DocumentBlockQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
|
|
|
builder := sql.Dialect(dbq.driver.Dialect())
|
|
|
|
t1 := builder.Table(documentblock.Table)
|
|
|
|
columns := dbq.ctx.Fields
|
|
|
|
if len(columns) == 0 {
|
|
|
|
columns = documentblock.Columns
|
|
|
|
}
|
|
|
|
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
|
|
|
if dbq.sql != nil {
|
|
|
|
selector = dbq.sql
|
|
|
|
selector.Select(selector.Columns(columns...)...)
|
|
|
|
}
|
|
|
|
if dbq.ctx.Unique != nil && *dbq.ctx.Unique {
|
|
|
|
selector.Distinct()
|
|
|
|
}
|
|
|
|
for _, p := range dbq.predicates {
|
|
|
|
p(selector)
|
|
|
|
}
|
|
|
|
for _, p := range dbq.order {
|
|
|
|
p(selector)
|
|
|
|
}
|
|
|
|
if offset := dbq.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 := dbq.ctx.Limit; limit != nil {
|
|
|
|
selector.Limit(*limit)
|
|
|
|
}
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentBlockGroupBy is the group-by builder for DocumentBlock entities.
|
|
|
|
type DocumentBlockGroupBy struct {
|
|
|
|
selector
|
|
|
|
build *DocumentBlockQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate adds the given aggregation functions to the group-by query.
|
|
|
|
func (dbgb *DocumentBlockGroupBy) Aggregate(fns ...AggregateFunc) *DocumentBlockGroupBy {
|
|
|
|
dbgb.fns = append(dbgb.fns, fns...)
|
|
|
|
return dbgb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
|
|
func (dbgb *DocumentBlockGroupBy) Scan(ctx context.Context, v any) error {
|
|
|
|
ctx = setContextOp(ctx, dbgb.build.ctx, "GroupBy")
|
|
|
|
if err := dbgb.build.prepareQuery(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return scanWithInterceptors[*DocumentBlockQuery, *DocumentBlockGroupBy](ctx, dbgb.build, dbgb, dbgb.build.inters, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbgb *DocumentBlockGroupBy) sqlScan(ctx context.Context, root *DocumentBlockQuery, v any) error {
|
|
|
|
selector := root.sqlQuery(ctx).Select()
|
|
|
|
aggregation := make([]string, 0, len(dbgb.fns))
|
|
|
|
for _, fn := range dbgb.fns {
|
|
|
|
aggregation = append(aggregation, fn(selector))
|
|
|
|
}
|
|
|
|
if len(selector.SelectedColumns()) == 0 {
|
|
|
|
columns := make([]string, 0, len(*dbgb.flds)+len(dbgb.fns))
|
|
|
|
for _, f := range *dbgb.flds {
|
|
|
|
columns = append(columns, selector.C(f))
|
|
|
|
}
|
|
|
|
columns = append(columns, aggregation...)
|
|
|
|
selector.Select(columns...)
|
|
|
|
}
|
|
|
|
selector.GroupBy(selector.Columns(*dbgb.flds...)...)
|
|
|
|
if err := selector.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows := &sql.Rows{}
|
|
|
|
query, args := selector.Query()
|
|
|
|
if err := dbgb.build.driver.Query(ctx, query, args, rows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return sql.ScanSlice(rows, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentBlockSelect is the builder for selecting fields of DocumentBlock entities.
|
|
|
|
type DocumentBlockSelect struct {
|
|
|
|
*DocumentBlockQuery
|
|
|
|
selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate adds the given aggregation functions to the selector query.
|
|
|
|
func (dbs *DocumentBlockSelect) Aggregate(fns ...AggregateFunc) *DocumentBlockSelect {
|
|
|
|
dbs.fns = append(dbs.fns, fns...)
|
|
|
|
return dbs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
|
|
func (dbs *DocumentBlockSelect) Scan(ctx context.Context, v any) error {
|
|
|
|
ctx = setContextOp(ctx, dbs.ctx, "Select")
|
|
|
|
if err := dbs.prepareQuery(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return scanWithInterceptors[*DocumentBlockQuery, *DocumentBlockSelect](ctx, dbs.DocumentBlockQuery, dbs, dbs.inters, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbs *DocumentBlockSelect) sqlScan(ctx context.Context, root *DocumentBlockQuery, v any) error {
|
|
|
|
selector := root.sqlQuery(ctx)
|
|
|
|
aggregation := make([]string, 0, len(dbs.fns))
|
|
|
|
for _, fn := range dbs.fns {
|
|
|
|
aggregation = append(aggregation, fn(selector))
|
|
|
|
}
|
|
|
|
switch n := len(*dbs.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 := dbs.driver.Query(ctx, query, args, rows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return sql.ScanSlice(rows, v)
|
|
|
|
}
|