Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for header comments in ORM-generated queries #2011

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions orm/composite_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (q *CreateCompositeQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *CreateCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/composite_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (q *DropCompositeQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *DropCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (q *DeleteQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *DeleteQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (q *InsertQuery) AppendTemplate(b []byte) ([]byte, error) {
var _ QueryAppender = (*InsertQuery)(nil)

func (q *InsertQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
7 changes: 7 additions & 0 deletions orm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Query struct {

onConflict *SafeQueryAppender
returning []*SafeQueryAppender
comment string
}

func NewQuery(db DB, model ...interface{}) *Query {
Expand Down Expand Up @@ -779,6 +780,12 @@ func (q *Query) Apply(fn func(*Query) (*Query, error)) *Query {
return qq
}

// Comment adds a comment to the query, wrapped by /* ... */.
func (q *Query) Comment(c string) *Query {
q.comment = c
return q
}

// Count returns number of rows matching the query using count aggregate function.
func (q *Query) Count() (int, error) {
if q.stickyErr != nil {
Expand Down
1 change: 1 addition & 0 deletions orm/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (q *SelectQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *SelectQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) { //nolint:gocyclo
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/table_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (q *CreateTableQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *CreateTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/table_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (q *DropTableQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *DropTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
1 change: 1 addition & 0 deletions orm/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (q *UpdateQuery) AppendTemplate(b []byte) ([]byte, error) {
}

func (q *UpdateQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = appendComment(b, q.q.comment)
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
Expand Down
12 changes: 12 additions & 0 deletions orm/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package orm

import (
"fmt"
"reflect"
"strings"

"github.com/go-pg/pg/v10/types"
)
Expand Down Expand Up @@ -149,3 +151,13 @@ func appendColumns(b []byte, table types.Safe, fields []*Field) []byte {
}
return b
}

// appendComment adds comment in the header of the query into buffer
func appendComment(b []byte, name string) []byte {
if name == "" {
return b
}
name = strings.ReplaceAll(name, `/*`, `/\*`)
name = strings.ReplaceAll(name, `*/`, `*\/`)
return append(b, fmt.Sprintf("/* %s */ ", name)...)
}
30 changes: 30 additions & 0 deletions orm/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package orm

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Comment - escape comment symbols", func() {
It("only open sequence", func() {
var res []byte
c := "/* comment"

s := appendComment(res, c)
Expect(s).To(Equal([]byte("/* /\\* comment */ ")))
})
It("only close sequence", func() {
var res []byte
c := "comment */"

s := appendComment(res, c)
Expect(s).To(Equal([]byte("/* comment *\\/ */ ")))
})
It("open and close sequences", func() {
var res []byte
c := "/* comment */"

s := appendComment(res, c)
Expect(s).To(Equal([]byte("/* /\\* comment *\\/ */ ")))
})
})
Loading