MCPcopy Create free account
hub / github.com/cloudreve/cloudreve / loadEntities

Method loadEntities

ent/file_query.go:855–915  ·  view source on GitHub ↗
(ctx context.Context, query *EntityQuery, nodes []*File, init func(*File), assign func(*File, *Entity))

Source from the content-addressed store, hash-verified

853 return nil
854}
855func (fq *FileQuery) loadEntities(ctx context.Context, query *EntityQuery, nodes []*File, init func(*File), assign func(*File, *Entity)) error {
856 edgeIDs := make([]driver.Value, len(nodes))
857 byID := make(map[int]*File)
858 nids := make(map[int]map[*File]struct{})
859 for i, node := range nodes {
860 edgeIDs[i] = node.ID
861 byID[node.ID] = node
862 if init != nil {
863 init(node)
864 }
865 }
866 query.Where(func(s *sql.Selector) {
867 joinT := sql.Table(file.EntitiesTable)
868 s.Join(joinT).On(s.C(entity.FieldID), joinT.C(file.EntitiesPrimaryKey[1]))
869 s.Where(sql.InValues(joinT.C(file.EntitiesPrimaryKey[0]), edgeIDs...))
870 columns := s.SelectedColumns()
871 s.Select(joinT.C(file.EntitiesPrimaryKey[0]))
872 s.AppendSelect(columns...)
873 s.SetDistinct(false)
874 })
875 if err := query.prepareQuery(ctx); err != nil {
876 return err
877 }
878 qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
879 return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
880 assign := spec.Assign
881 values := spec.ScanValues
882 spec.ScanValues = func(columns []string) ([]any, error) {
883 values, err := values(columns[1:])
884 if err != nil {
885 return nil, err
886 }
887 return append([]any{new(sql.NullInt64)}, values...), nil
888 }
889 spec.Assign = func(columns []string, values []any) error {
890 outValue := int(values[0].(*sql.NullInt64).Int64)
891 inValue := int(values[1].(*sql.NullInt64).Int64)
892 if nids[inValue] == nil {
893 nids[inValue] = map[*File]struct{}{byID[outValue]: {}}
894 return assign(columns[1:], values[1:])
895 }
896 nids[inValue][byID[outValue]] = struct{}{}
897 return nil
898 }
899 })
900 })
901 neighbors, err := withInterceptors[[]*Entity](ctx, query, qr, query.inters)
902 if err != nil {
903 return err
904 }
905 for _, n := range neighbors {
906 nodes, ok := nids[n.ID]
907 if !ok {
908 return fmt.Errorf(`unexpected "entities" node returned %v`, n.ID)
909 }
910 for kn := range nodes {
911 assign(kn, n)
912 }

Callers 1

sqlAllMethod · 0.95

Calls 7

WhereMethod · 0.95
SelectMethod · 0.95
withInterceptorsFunction · 0.85
JoinMethod · 0.80
initFunction · 0.50
prepareQueryMethod · 0.45
sqlAllMethod · 0.45

Tested by

no test coverage detected