Skip to content

Commit

Permalink
Add subquery parentheses to view queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Jul 18, 2023
1 parent 69acc07 commit 3db9c3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion dialect/mysql/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ func (v ViewQuery[T, Ts]) WriteSQL(w io.Writer, _ bob.Dialect, start int) ([]any

// it is necessary to override this method to be able to add columns if not set
func (v ViewQuery[T, Ts]) WriteQuery(w io.Writer, start int) ([]any, error) {
return v.WriteSQL(w, v.Dialect, start)
// Append the table columns
if len(v.BaseQuery.Expression.SelectList.Columns) == 0 {
v.BaseQuery.Expression.AppendSelect(v.view.Columns())
}

return v.BaseQuery.WriteQuery(w, start)
}

func (v *ViewQuery[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
Expand Down
9 changes: 7 additions & 2 deletions dialect/psql/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,17 @@ func (v ViewQuery[T, Ts]) WriteSQL(w io.Writer, _ bob.Dialect, start int) ([]any
v.BaseQuery.Expression.AppendSelect(v.view.Columns())
}

return v.Expression.WriteSQL(w, v.Dialect, start)
return v.BaseQuery.WriteSQL(w, v.Dialect, start)
}

// it is necessary to override this method to be able to add columns if not set
func (v ViewQuery[T, Ts]) WriteQuery(w io.Writer, start int) ([]any, error) {
return v.WriteSQL(w, v.Dialect, start)
// Append the table columns
if len(v.BaseQuery.Expression.SelectList.Columns) == 0 {
v.BaseQuery.Expression.AppendSelect(v.view.Columns())
}

return v.BaseQuery.WriteQuery(w, start)
}

func (v *ViewQuery[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
Expand Down
7 changes: 6 additions & 1 deletion dialect/sqlite/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ func (v ViewQuery[T, Ts]) WriteSQL(w io.Writer, _ bob.Dialect, start int) ([]any

// it is necessary to override this method to be able to add columns if not set
func (v ViewQuery[T, Ts]) WriteQuery(w io.Writer, start int) ([]any, error) {
return v.WriteSQL(w, v.Dialect, start)
// Append the table columns
if len(v.BaseQuery.Expression.SelectList.Columns) == 0 {
v.BaseQuery.Expression.AppendSelect(v.view.Columns())
}

return v.BaseQuery.WriteQuery(w, start)
}

func (v *ViewQuery[T, Ts]) afterSelect(ctx context.Context, exec bob.Executor) bob.ExecOption[T] {
Expand Down

0 comments on commit 3db9c3f

Please sign in to comment.