Skip to content

Commit

Permalink
Merge pull request #11 from funvit/feat/iter-with-order
Browse files Browse the repository at this point in the history
feat: iterWithOrder
  • Loading branch information
funvit authored Oct 11, 2021
2 parents 59eae4d + 322d5fb commit b16721b
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/goqu-crud-gen/get_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,70 @@ func (s *{{ .Repo.Name }}) iter(
return nil
}
// iterWithOrder iterates other select with specified filter(s).
//
// Can be used in your custom query methods.
func (s *{{ .Repo.Name }}) iterWithOrder(
ctx context.Context,
filter goqu.Expression,
f func(m {{ .Model.Name }}, stop func()),
order exp.OrderedExpression,
opt ...Option,
) error {
tx, err := s.txFromContext(ctx)
if err != nil {
return err
}
ds := s.dialect.From(s.t).Prepared(true)
if filter != nil {
ds = ds.Where(filter)
}
if order != nil {
ds = ds.Order(order)
}
for _, o := range opt {
o(ds)
}
q, args, err := ds.ToSQL()
if err != nil {
return fmt.Errorf("query builder error: %w", err)
}
sigCtx, sigCtxCancel := context.WithCancel(ctx)
defer sigCtxCancel()
rows, err := tx.QueryxContext(ctx, q, args...)
if err != nil {
return fmt.Errorf("select query error: %w", err)
}
defer func() {
_ = rows.Close()
}()
for rows.Next() {
var m {{ .Model.Name }}
select {
case <-sigCtx.Done():
break
default:
}
err = rows.StructScan(&m)
if err != nil {
return fmt.Errorf("row scan error: %w", err)
}
f(m, func() { sigCtxCancel() })
}
return nil
}
// iterPrimaryKeys iterates other select with specified filter(s).
//
// Can be used in your custom query methods.
Expand Down
64 changes: 64 additions & 0 deletions examples/example1/model/user_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions examples/example2/user_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions examples/example3/adapters/mysql/account_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions examples/example3/adapters/mysql/user_public_fields_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b16721b

Please sign in to comment.