lock_query.go 13 KB

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