at main 15 kB view raw
1// Code generated by ent, DO NOT EDIT. 2 3package ent 4 5import ( 6 "context" 7 "fmt" 8 "math" 9 10 "entgo.io/ent" 11 "entgo.io/ent/dialect/sql" 12 "entgo.io/ent/dialect/sql/sqlgraph" 13 "entgo.io/ent/schema/field" 14 "tangled.sh/seiso.moe/aletheia.directory/ent/predicate" 15 "tangled.sh/seiso.moe/aletheia.directory/ent/syncstatus" 16) 17 18// SyncStatusQuery is the builder for querying SyncStatus entities. 19type SyncStatusQuery struct { 20 config 21 ctx *QueryContext 22 order []syncstatus.OrderOption 23 inters []Interceptor 24 predicates []predicate.SyncStatus 25 // intermediate query (i.e. traversal path). 26 sql *sql.Selector 27 path func(context.Context) (*sql.Selector, error) 28} 29 30// Where adds a new predicate for the SyncStatusQuery builder. 31func (ssq *SyncStatusQuery) Where(ps ...predicate.SyncStatus) *SyncStatusQuery { 32 ssq.predicates = append(ssq.predicates, ps...) 33 return ssq 34} 35 36// Limit the number of records to be returned by this query. 37func (ssq *SyncStatusQuery) Limit(limit int) *SyncStatusQuery { 38 ssq.ctx.Limit = &limit 39 return ssq 40} 41 42// Offset to start from. 43func (ssq *SyncStatusQuery) Offset(offset int) *SyncStatusQuery { 44 ssq.ctx.Offset = &offset 45 return ssq 46} 47 48// Unique configures the query builder to filter duplicate records on query. 49// By default, unique is set to true, and can be disabled using this method. 50func (ssq *SyncStatusQuery) Unique(unique bool) *SyncStatusQuery { 51 ssq.ctx.Unique = &unique 52 return ssq 53} 54 55// Order specifies how the records should be ordered. 56func (ssq *SyncStatusQuery) Order(o ...syncstatus.OrderOption) *SyncStatusQuery { 57 ssq.order = append(ssq.order, o...) 58 return ssq 59} 60 61// First returns the first SyncStatus entity from the query. 62// Returns a *NotFoundError when no SyncStatus was found. 63func (ssq *SyncStatusQuery) First(ctx context.Context) (*SyncStatus, error) { 64 nodes, err := ssq.Limit(1).All(setContextOp(ctx, ssq.ctx, ent.OpQueryFirst)) 65 if err != nil { 66 return nil, err 67 } 68 if len(nodes) == 0 { 69 return nil, &NotFoundError{syncstatus.Label} 70 } 71 return nodes[0], nil 72} 73 74// FirstX is like First, but panics if an error occurs. 75func (ssq *SyncStatusQuery) FirstX(ctx context.Context) *SyncStatus { 76 node, err := ssq.First(ctx) 77 if err != nil && !IsNotFound(err) { 78 panic(err) 79 } 80 return node 81} 82 83// FirstID returns the first SyncStatus ID from the query. 84// Returns a *NotFoundError when no SyncStatus ID was found. 85func (ssq *SyncStatusQuery) FirstID(ctx context.Context) (id int, err error) { 86 var ids []int 87 if ids, err = ssq.Limit(1).IDs(setContextOp(ctx, ssq.ctx, ent.OpQueryFirstID)); err != nil { 88 return 89 } 90 if len(ids) == 0 { 91 err = &NotFoundError{syncstatus.Label} 92 return 93 } 94 return ids[0], nil 95} 96 97// FirstIDX is like FirstID, but panics if an error occurs. 98func (ssq *SyncStatusQuery) FirstIDX(ctx context.Context) int { 99 id, err := ssq.FirstID(ctx) 100 if err != nil && !IsNotFound(err) { 101 panic(err) 102 } 103 return id 104} 105 106// Only returns a single SyncStatus entity found by the query, ensuring it only returns one. 107// Returns a *NotSingularError when more than one SyncStatus entity is found. 108// Returns a *NotFoundError when no SyncStatus entities are found. 109func (ssq *SyncStatusQuery) Only(ctx context.Context) (*SyncStatus, error) { 110 nodes, err := ssq.Limit(2).All(setContextOp(ctx, ssq.ctx, ent.OpQueryOnly)) 111 if err != nil { 112 return nil, err 113 } 114 switch len(nodes) { 115 case 1: 116 return nodes[0], nil 117 case 0: 118 return nil, &NotFoundError{syncstatus.Label} 119 default: 120 return nil, &NotSingularError{syncstatus.Label} 121 } 122} 123 124// OnlyX is like Only, but panics if an error occurs. 125func (ssq *SyncStatusQuery) OnlyX(ctx context.Context) *SyncStatus { 126 node, err := ssq.Only(ctx) 127 if err != nil { 128 panic(err) 129 } 130 return node 131} 132 133// OnlyID is like Only, but returns the only SyncStatus ID in the query. 134// Returns a *NotSingularError when more than one SyncStatus ID is found. 135// Returns a *NotFoundError when no entities are found. 136func (ssq *SyncStatusQuery) OnlyID(ctx context.Context) (id int, err error) { 137 var ids []int 138 if ids, err = ssq.Limit(2).IDs(setContextOp(ctx, ssq.ctx, ent.OpQueryOnlyID)); err != nil { 139 return 140 } 141 switch len(ids) { 142 case 1: 143 id = ids[0] 144 case 0: 145 err = &NotFoundError{syncstatus.Label} 146 default: 147 err = &NotSingularError{syncstatus.Label} 148 } 149 return 150} 151 152// OnlyIDX is like OnlyID, but panics if an error occurs. 153func (ssq *SyncStatusQuery) OnlyIDX(ctx context.Context) int { 154 id, err := ssq.OnlyID(ctx) 155 if err != nil { 156 panic(err) 157 } 158 return id 159} 160 161// All executes the query and returns a list of SyncStatusSlice. 162func (ssq *SyncStatusQuery) All(ctx context.Context) ([]*SyncStatus, error) { 163 ctx = setContextOp(ctx, ssq.ctx, ent.OpQueryAll) 164 if err := ssq.prepareQuery(ctx); err != nil { 165 return nil, err 166 } 167 qr := querierAll[[]*SyncStatus, *SyncStatusQuery]() 168 return withInterceptors[[]*SyncStatus](ctx, ssq, qr, ssq.inters) 169} 170 171// AllX is like All, but panics if an error occurs. 172func (ssq *SyncStatusQuery) AllX(ctx context.Context) []*SyncStatus { 173 nodes, err := ssq.All(ctx) 174 if err != nil { 175 panic(err) 176 } 177 return nodes 178} 179 180// IDs executes the query and returns a list of SyncStatus IDs. 181func (ssq *SyncStatusQuery) IDs(ctx context.Context) (ids []int, err error) { 182 if ssq.ctx.Unique == nil && ssq.path != nil { 183 ssq.Unique(true) 184 } 185 ctx = setContextOp(ctx, ssq.ctx, ent.OpQueryIDs) 186 if err = ssq.Select(syncstatus.FieldID).Scan(ctx, &ids); err != nil { 187 return nil, err 188 } 189 return ids, nil 190} 191 192// IDsX is like IDs, but panics if an error occurs. 193func (ssq *SyncStatusQuery) IDsX(ctx context.Context) []int { 194 ids, err := ssq.IDs(ctx) 195 if err != nil { 196 panic(err) 197 } 198 return ids 199} 200 201// Count returns the count of the given query. 202func (ssq *SyncStatusQuery) Count(ctx context.Context) (int, error) { 203 ctx = setContextOp(ctx, ssq.ctx, ent.OpQueryCount) 204 if err := ssq.prepareQuery(ctx); err != nil { 205 return 0, err 206 } 207 return withInterceptors[int](ctx, ssq, querierCount[*SyncStatusQuery](), ssq.inters) 208} 209 210// CountX is like Count, but panics if an error occurs. 211func (ssq *SyncStatusQuery) CountX(ctx context.Context) int { 212 count, err := ssq.Count(ctx) 213 if err != nil { 214 panic(err) 215 } 216 return count 217} 218 219// Exist returns true if the query has elements in the graph. 220func (ssq *SyncStatusQuery) Exist(ctx context.Context) (bool, error) { 221 ctx = setContextOp(ctx, ssq.ctx, ent.OpQueryExist) 222 switch _, err := ssq.FirstID(ctx); { 223 case IsNotFound(err): 224 return false, nil 225 case err != nil: 226 return false, fmt.Errorf("ent: check existence: %w", err) 227 default: 228 return true, nil 229 } 230} 231 232// ExistX is like Exist, but panics if an error occurs. 233func (ssq *SyncStatusQuery) ExistX(ctx context.Context) bool { 234 exist, err := ssq.Exist(ctx) 235 if err != nil { 236 panic(err) 237 } 238 return exist 239} 240 241// Clone returns a duplicate of the SyncStatusQuery builder, including all associated steps. It can be 242// used to prepare common query builders and use them differently after the clone is made. 243func (ssq *SyncStatusQuery) Clone() *SyncStatusQuery { 244 if ssq == nil { 245 return nil 246 } 247 return &SyncStatusQuery{ 248 config: ssq.config, 249 ctx: ssq.ctx.Clone(), 250 order: append([]syncstatus.OrderOption{}, ssq.order...), 251 inters: append([]Interceptor{}, ssq.inters...), 252 predicates: append([]predicate.SyncStatus{}, ssq.predicates...), 253 // clone intermediate query. 254 sql: ssq.sql.Clone(), 255 path: ssq.path, 256 } 257} 258 259// GroupBy is used to group vertices by one or more fields/columns. 260// It is often used with aggregate functions, like: count, max, mean, min, sum. 261// 262// Example: 263// 264// var v []struct { 265// Key string `json:"key,omitempty"` 266// Count int `json:"count,omitempty"` 267// } 268// 269// client.SyncStatus.Query(). 270// GroupBy(syncstatus.FieldKey). 271// Aggregate(ent.Count()). 272// Scan(ctx, &v) 273func (ssq *SyncStatusQuery) GroupBy(field string, fields ...string) *SyncStatusGroupBy { 274 ssq.ctx.Fields = append([]string{field}, fields...) 275 grbuild := &SyncStatusGroupBy{build: ssq} 276 grbuild.flds = &ssq.ctx.Fields 277 grbuild.label = syncstatus.Label 278 grbuild.scan = grbuild.Scan 279 return grbuild 280} 281 282// Select allows the selection one or more fields/columns for the given query, 283// instead of selecting all fields in the entity. 284// 285// Example: 286// 287// var v []struct { 288// Key string `json:"key,omitempty"` 289// } 290// 291// client.SyncStatus.Query(). 292// Select(syncstatus.FieldKey). 293// Scan(ctx, &v) 294func (ssq *SyncStatusQuery) Select(fields ...string) *SyncStatusSelect { 295 ssq.ctx.Fields = append(ssq.ctx.Fields, fields...) 296 sbuild := &SyncStatusSelect{SyncStatusQuery: ssq} 297 sbuild.label = syncstatus.Label 298 sbuild.flds, sbuild.scan = &ssq.ctx.Fields, sbuild.Scan 299 return sbuild 300} 301 302// Aggregate returns a SyncStatusSelect configured with the given aggregations. 303func (ssq *SyncStatusQuery) Aggregate(fns ...AggregateFunc) *SyncStatusSelect { 304 return ssq.Select().Aggregate(fns...) 305} 306 307func (ssq *SyncStatusQuery) prepareQuery(ctx context.Context) error { 308 for _, inter := range ssq.inters { 309 if inter == nil { 310 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") 311 } 312 if trv, ok := inter.(Traverser); ok { 313 if err := trv.Traverse(ctx, ssq); err != nil { 314 return err 315 } 316 } 317 } 318 for _, f := range ssq.ctx.Fields { 319 if !syncstatus.ValidColumn(f) { 320 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 321 } 322 } 323 if ssq.path != nil { 324 prev, err := ssq.path(ctx) 325 if err != nil { 326 return err 327 } 328 ssq.sql = prev 329 } 330 return nil 331} 332 333func (ssq *SyncStatusQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*SyncStatus, error) { 334 var ( 335 nodes = []*SyncStatus{} 336 _spec = ssq.querySpec() 337 ) 338 _spec.ScanValues = func(columns []string) ([]any, error) { 339 return (*SyncStatus).scanValues(nil, columns) 340 } 341 _spec.Assign = func(columns []string, values []any) error { 342 node := &SyncStatus{config: ssq.config} 343 nodes = append(nodes, node) 344 return node.assignValues(columns, values) 345 } 346 for i := range hooks { 347 hooks[i](ctx, _spec) 348 } 349 if err := sqlgraph.QueryNodes(ctx, ssq.driver, _spec); err != nil { 350 return nil, err 351 } 352 if len(nodes) == 0 { 353 return nodes, nil 354 } 355 return nodes, nil 356} 357 358func (ssq *SyncStatusQuery) sqlCount(ctx context.Context) (int, error) { 359 _spec := ssq.querySpec() 360 _spec.Node.Columns = ssq.ctx.Fields 361 if len(ssq.ctx.Fields) > 0 { 362 _spec.Unique = ssq.ctx.Unique != nil && *ssq.ctx.Unique 363 } 364 return sqlgraph.CountNodes(ctx, ssq.driver, _spec) 365} 366 367func (ssq *SyncStatusQuery) querySpec() *sqlgraph.QuerySpec { 368 _spec := sqlgraph.NewQuerySpec(syncstatus.Table, syncstatus.Columns, sqlgraph.NewFieldSpec(syncstatus.FieldID, field.TypeInt)) 369 _spec.From = ssq.sql 370 if unique := ssq.ctx.Unique; unique != nil { 371 _spec.Unique = *unique 372 } else if ssq.path != nil { 373 _spec.Unique = true 374 } 375 if fields := ssq.ctx.Fields; len(fields) > 0 { 376 _spec.Node.Columns = make([]string, 0, len(fields)) 377 _spec.Node.Columns = append(_spec.Node.Columns, syncstatus.FieldID) 378 for i := range fields { 379 if fields[i] != syncstatus.FieldID { 380 _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) 381 } 382 } 383 } 384 if ps := ssq.predicates; len(ps) > 0 { 385 _spec.Predicate = func(selector *sql.Selector) { 386 for i := range ps { 387 ps[i](selector) 388 } 389 } 390 } 391 if limit := ssq.ctx.Limit; limit != nil { 392 _spec.Limit = *limit 393 } 394 if offset := ssq.ctx.Offset; offset != nil { 395 _spec.Offset = *offset 396 } 397 if ps := ssq.order; len(ps) > 0 { 398 _spec.Order = func(selector *sql.Selector) { 399 for i := range ps { 400 ps[i](selector) 401 } 402 } 403 } 404 return _spec 405} 406 407func (ssq *SyncStatusQuery) sqlQuery(ctx context.Context) *sql.Selector { 408 builder := sql.Dialect(ssq.driver.Dialect()) 409 t1 := builder.Table(syncstatus.Table) 410 columns := ssq.ctx.Fields 411 if len(columns) == 0 { 412 columns = syncstatus.Columns 413 } 414 selector := builder.Select(t1.Columns(columns...)...).From(t1) 415 if ssq.sql != nil { 416 selector = ssq.sql 417 selector.Select(selector.Columns(columns...)...) 418 } 419 if ssq.ctx.Unique != nil && *ssq.ctx.Unique { 420 selector.Distinct() 421 } 422 for _, p := range ssq.predicates { 423 p(selector) 424 } 425 for _, p := range ssq.order { 426 p(selector) 427 } 428 if offset := ssq.ctx.Offset; offset != nil { 429 // limit is mandatory for offset clause. We start 430 // with default value, and override it below if needed. 431 selector.Offset(*offset).Limit(math.MaxInt32) 432 } 433 if limit := ssq.ctx.Limit; limit != nil { 434 selector.Limit(*limit) 435 } 436 return selector 437} 438 439// SyncStatusGroupBy is the group-by builder for SyncStatus entities. 440type SyncStatusGroupBy struct { 441 selector 442 build *SyncStatusQuery 443} 444 445// Aggregate adds the given aggregation functions to the group-by query. 446func (ssgb *SyncStatusGroupBy) Aggregate(fns ...AggregateFunc) *SyncStatusGroupBy { 447 ssgb.fns = append(ssgb.fns, fns...) 448 return ssgb 449} 450 451// Scan applies the selector query and scans the result into the given value. 452func (ssgb *SyncStatusGroupBy) Scan(ctx context.Context, v any) error { 453 ctx = setContextOp(ctx, ssgb.build.ctx, ent.OpQueryGroupBy) 454 if err := ssgb.build.prepareQuery(ctx); err != nil { 455 return err 456 } 457 return scanWithInterceptors[*SyncStatusQuery, *SyncStatusGroupBy](ctx, ssgb.build, ssgb, ssgb.build.inters, v) 458} 459 460func (ssgb *SyncStatusGroupBy) sqlScan(ctx context.Context, root *SyncStatusQuery, v any) error { 461 selector := root.sqlQuery(ctx).Select() 462 aggregation := make([]string, 0, len(ssgb.fns)) 463 for _, fn := range ssgb.fns { 464 aggregation = append(aggregation, fn(selector)) 465 } 466 if len(selector.SelectedColumns()) == 0 { 467 columns := make([]string, 0, len(*ssgb.flds)+len(ssgb.fns)) 468 for _, f := range *ssgb.flds { 469 columns = append(columns, selector.C(f)) 470 } 471 columns = append(columns, aggregation...) 472 selector.Select(columns...) 473 } 474 selector.GroupBy(selector.Columns(*ssgb.flds...)...) 475 if err := selector.Err(); err != nil { 476 return err 477 } 478 rows := &sql.Rows{} 479 query, args := selector.Query() 480 if err := ssgb.build.driver.Query(ctx, query, args, rows); err != nil { 481 return err 482 } 483 defer rows.Close() 484 return sql.ScanSlice(rows, v) 485} 486 487// SyncStatusSelect is the builder for selecting fields of SyncStatus entities. 488type SyncStatusSelect struct { 489 *SyncStatusQuery 490 selector 491} 492 493// Aggregate adds the given aggregation functions to the selector query. 494func (sss *SyncStatusSelect) Aggregate(fns ...AggregateFunc) *SyncStatusSelect { 495 sss.fns = append(sss.fns, fns...) 496 return sss 497} 498 499// Scan applies the selector query and scans the result into the given value. 500func (sss *SyncStatusSelect) Scan(ctx context.Context, v any) error { 501 ctx = setContextOp(ctx, sss.ctx, ent.OpQuerySelect) 502 if err := sss.prepareQuery(ctx); err != nil { 503 return err 504 } 505 return scanWithInterceptors[*SyncStatusQuery, *SyncStatusSelect](ctx, sss.SyncStatusQuery, sss, sss.inters, v) 506} 507 508func (sss *SyncStatusSelect) sqlScan(ctx context.Context, root *SyncStatusQuery, v any) error { 509 selector := root.sqlQuery(ctx) 510 aggregation := make([]string, 0, len(sss.fns)) 511 for _, fn := range sss.fns { 512 aggregation = append(aggregation, fn(selector)) 513 } 514 switch n := len(*sss.selector.flds); { 515 case n == 0 && len(aggregation) > 0: 516 selector.Select(aggregation...) 517 case n != 0 && len(aggregation) > 0: 518 selector.AppendSelect(aggregation...) 519 } 520 rows := &sql.Rows{} 521 query, args := selector.Query() 522 if err := sss.driver.Query(ctx, query, args, rows); err != nil { 523 return err 524 } 525 defer rows.Close() 526 return sql.ScanSlice(rows, v) 527}