Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed May 29, 2024
1 parent a365b5a commit 09c8491
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions dialect/mysql/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa
var queryMods mods.QueryMods[*dialect.SelectQuery]

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
alias = settings.Alias
if settings.Alias == "" {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
Expand Down
10 changes: 5 additions & 5 deletions dialect/mysql/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func (v *View[T, Tslice]) Prepare(ctx context.Context, exec bob.Preparer, queryM

// Prepare a statement from an existing query that will be mapped to the view's type
func (v *View[T, Tslice]) PrepareQuery(ctx context.Context, exec bob.Preparer, q bob.Query) (bob.QueryStmt[T, Tslice], error) {
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, v.scanner, v.afterSelect(ctx, exec))
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, v.scanner, v.afterSelect(exec))
}

func (v *View[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
func (v *View[T, Ts]) afterSelect(exec bob.Executor) bob.ExecOption[T] {
return func(es *bob.ExecSettings[T]) {
es.AfterSelect = func(ctx context.Context, retrieved []T) error {
_, err := v.AfterSelectHooks.Do(ctx, exec, retrieved)
Expand Down Expand Up @@ -154,23 +154,23 @@ func (v *ViewQuery[T, Tslice]) One() (T, error) {
if err := v.hook(); err != nil {
return *new(T), err
}
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// All matching rows
func (v *ViewQuery[T, Tslice]) All() (Tslice, error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Cursor to scan through the results
func (v *ViewQuery[T, Tslice]) Cursor() (scan.ICursor[T], error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Count the number of matching rows
Expand Down
6 changes: 3 additions & 3 deletions dialect/psql/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa
var queryMods mods.QueryMods[*dialect.SelectQuery]

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
alias = settings.Alias
if settings.Alias == "" {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
Expand Down
8 changes: 4 additions & 4 deletions dialect/psql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (t *TableQuery[Q, T, Ts]) addReturning() {
}
}

func (t *TableQuery[Q, T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
func (t *TableQuery[Q, T, Ts]) afterSelect(exec bob.Executor) bob.ExecOption[T] {
return func(es *bob.ExecSettings[T]) {
es.AfterSelect = func(ctx context.Context, retrieved []T) error {
_, err := t.view.AfterSelectHooks.Do(ctx, exec, retrieved)
Expand Down Expand Up @@ -390,7 +390,7 @@ func (t *TableQuery[Q, T, Tslice]) One() (T, error) {
if err := t.hook(); err != nil {
return *new(T), err
}
return bob.One(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.One(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}

// All matching rows
Expand All @@ -399,7 +399,7 @@ func (t *TableQuery[Q, T, Tslice]) All() (Tslice, error) {
if err := t.hook(); err != nil {
return nil, err
}
return bob.Allx[T, Tslice](t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.Allx[T, Tslice](t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}

// Cursor to scan through the results
Expand All @@ -408,5 +408,5 @@ func (t *TableQuery[Q, T, Tslice]) Cursor() (scan.ICursor[T], error) {
if err := t.hook(); err != nil {
return nil, err
}
return bob.Cursor(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.Cursor(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}
10 changes: 5 additions & 5 deletions dialect/psql/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func (v *View[T, Tslice]) Prepare(ctx context.Context, exec bob.Preparer, queryM

// Prepare a statement from an existing query that will be mapped to the view's type
func (v *View[T, Tslice]) PrepareQuery(ctx context.Context, exec bob.Preparer, q bob.Query) (bob.QueryStmt[T, Tslice], error) {
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, v.scanner, v.afterSelect(ctx, exec))
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, v.scanner, v.afterSelect(exec))
}

func (v *View[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
func (v *View[T, Ts]) afterSelect(exec bob.Executor) bob.ExecOption[T] {
return func(es *bob.ExecSettings[T]) {
es.AfterSelect = func(ctx context.Context, retrieved []T) error {
_, err := v.AfterSelectHooks.Do(ctx, exec, retrieved)
Expand Down Expand Up @@ -173,23 +173,23 @@ func (v *ViewQuery[T, Tslice]) One() (T, error) {
if err := v.hook(); err != nil {
return *new(T), err
}
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// All matching rows
func (v *ViewQuery[T, Tslice]) All() (Tslice, error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Cursor to scan through the results
func (v *ViewQuery[T, Tslice]) Cursor() (scan.ICursor[T], error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Count the number of matching rows
Expand Down
6 changes: 3 additions & 3 deletions dialect/sqlite/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa
var queryMods mods.QueryMods[*dialect.SelectQuery]

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
alias = settings.Alias
if settings.Alias == "" {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
Expand Down
8 changes: 4 additions & 4 deletions dialect/sqlite/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (t *TQuery[Q, T, Ts]) addReturning() {
}
}

func (t *TQuery[Q, T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
func (t *TQuery[Q, T, Ts]) afterSelect(exec bob.Executor) bob.ExecOption[T] {
return func(es *bob.ExecSettings[T]) {
es.AfterSelect = func(ctx context.Context, retrieved []T) error {
_, err := t.view.AfterSelectHooks.Do(ctx, exec, retrieved)
Expand Down Expand Up @@ -393,7 +393,7 @@ func (t *TQuery[Q, T, Tslice]) One() (T, error) {
if err := t.hook(); err != nil {
return *new(T), err
}
return bob.One(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.One(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}

// All matching rows
Expand All @@ -402,7 +402,7 @@ func (t *TQuery[Q, T, Tslice]) All() (Tslice, error) {
if err := t.hook(); err != nil {
return nil, err
}
return bob.Allx[T, Tslice](t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.Allx[T, Tslice](t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}

// Cursor to scan through the results
Expand All @@ -411,5 +411,5 @@ func (t *TQuery[Q, T, Tslice]) Cursor() (scan.ICursor[T], error) {
if err := t.hook(); err != nil {
return nil, err
}
return bob.Cursor(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.ctx, t.exec))
return bob.Cursor(t.ctx, t.exec, t, t.view.scanner, t.afterSelect(t.exec))
}
10 changes: 5 additions & 5 deletions dialect/sqlite/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func (v *View[T, Tslice]) Prepare(ctx context.Context, exec bob.Preparer, queryM

// Prepare a statement from an existing query that will be mapped to the view's type
func (v *View[T, Tslice]) PrepareQuery(ctx context.Context, exec bob.Preparer, q bob.Query) (bob.QueryStmt[T, Tslice], error) {
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, scan.StructMapper[T](), v.afterSelect(ctx, exec))
return bob.PrepareQueryx[T, Tslice](ctx, exec, q, scan.StructMapper[T](), v.afterSelect(exec))
}

func (v *View[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
func (v *View[T, Ts]) afterSelect(exec bob.Executor) bob.ExecOption[T] {
return func(es *bob.ExecSettings[T]) {
es.AfterSelect = func(ctx context.Context, retrieved []T) error {
_, err := v.AfterSelectHooks.Do(ctx, exec, retrieved)
Expand Down Expand Up @@ -173,23 +173,23 @@ func (v *ViewQuery[T, Tslice]) One() (T, error) {
if err := v.hook(); err != nil {
return *new(T), err
}
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.One(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// All matching rows
func (v *ViewQuery[T, Tslice]) All() (Tslice, error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Allx[T, Tslice](v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Cursor to scan through the results
func (v *ViewQuery[T, Tslice]) Cursor() (scan.ICursor[T], error) {
if err := v.hook(); err != nil {
return nil, err
}
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.ctx, v.exec))
return bob.Cursor(v.ctx, v.exec, v, v.view.scanner, v.view.afterSelect(v.exec))
}

// Count the number of matching rows
Expand Down

0 comments on commit 09c8491

Please sign in to comment.