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