Skip to content

Commit 6ed1a1c

Browse files
committed
Merge branch 'Pilatuz/master' into dev
2 parents 63a4abc + 0c33b74 commit 6ed1a1c

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

queries/qm/query_mods.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,22 @@ func For(clause string) QueryMod {
554554
}
555555
}
556556

557+
type commentQueryMod struct {
558+
comment string
559+
}
560+
561+
// Apply implements QueryMod.Apply.
562+
func (qm commentQueryMod) Apply(q *queries.Query) {
563+
queries.SetComment(q, qm.comment)
564+
}
565+
566+
// Comment inserts a custom comment at the begin of your query
567+
func Comment(comment string) QueryMod {
568+
return commentQueryMod{
569+
comment: comment,
570+
}
571+
}
572+
557573
// Rels is an alias for strings.Join to make it easier to use relationship name
558574
// constants in Load.
559575
func Rels(r ...string) string {

queries/query.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Query struct {
4444
offset int
4545
forlock string
4646
distinct string
47+
comment string
4748
}
4849

4950
// Applicator exists only to allow
@@ -270,6 +271,11 @@ func SetFor(q *Query, clause string) {
270271
q.forlock = clause
271272
}
272273

274+
// SetComment on the query.
275+
func SetComment(q *Query, comment string) {
276+
q.comment = comment
277+
}
278+
273279
// SetUpdate on the query.
274280
func SetUpdate(q *Query, cols map[string]interface{}) {
275281
q.update = cols

queries/query_builders.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
4848
buf := strmangle.GetBuffer()
4949
var args []interface{}
5050

51+
writeComment(q, buf)
5152
writeCTEs(q, buf, &args)
5253

5354
buf.WriteString("SELECT ")
@@ -138,6 +139,7 @@ func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
138139
var args []interface{}
139140
buf := strmangle.GetBuffer()
140141

142+
writeComment(q, buf)
141143
writeCTEs(q, buf, &args)
142144

143145
buf.WriteString("DELETE FROM ")
@@ -160,6 +162,7 @@ func buildUpdateQuery(q *Query) (*bytes.Buffer, []interface{}) {
160162
buf := strmangle.GetBuffer()
161163
var args []interface{}
162164

165+
writeComment(q, buf)
163166
writeCTEs(q, buf, &args)
164167

165168
buf.WriteString("UPDATE ")
@@ -586,6 +589,19 @@ func parseFromClause(toks []string) (alias, name string, ok bool) {
586589
return alias, name, ok
587590
}
588591

592+
func writeComment(q *Query, buf *bytes.Buffer) {
593+
if len(q.comment) == 0 {
594+
return
595+
}
596+
597+
lines := strings.Split(q.comment, "\n")
598+
for _, line := range lines {
599+
buf.WriteString("-- ")
600+
buf.WriteString(line)
601+
buf.WriteByte('\n')
602+
}
603+
}
604+
589605
func writeCTEs(q *Query, buf *bytes.Buffer, args *[]interface{}) {
590606
if len(q.withs) == 0 {
591607
return

queries/query_builders_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,36 @@ func TestWriteAsStatements(t *testing.T) {
629629
}
630630
}
631631
}
632+
633+
func TestWriteComment(t *testing.T) {
634+
t.Parallel()
635+
636+
var buf bytes.Buffer
637+
query := Query{
638+
dialect: &drivers.Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
639+
}
640+
641+
// empty comment
642+
buf.Reset()
643+
query.comment = ""
644+
writeComment(&query, &buf)
645+
if got := buf.String(); got != "" {
646+
t.Errorf(`bad empty comment, got: %s`, got)
647+
}
648+
649+
// one line comment
650+
buf.Reset()
651+
query.comment = "comment"
652+
writeComment(&query, &buf)
653+
if got := buf.String(); got != "-- comment\n" {
654+
t.Errorf(`bad one line comment, got: %s`, got)
655+
}
656+
657+
// two lines comment
658+
buf.Reset()
659+
query.comment = "first\nsecond"
660+
writeComment(&query, &buf)
661+
if got := buf.String(); got != "-- first\n-- second\n" {
662+
t.Errorf(`bad two lines comment, got: %s`, got)
663+
}
664+
}

queries/query_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,14 @@ func TestAppendWith(t *testing.T) {
640640
t.Errorf("Got invalid with on string: %#v", q.withs)
641641
}
642642
}
643+
644+
func TestSetComment(t *testing.T) {
645+
t.Parallel()
646+
647+
q := &Query{}
648+
SetComment(q, "my comment")
649+
650+
if q.comment != "my comment" {
651+
t.Errorf("Got invalid comment: %s", q.comment)
652+
}
653+
}

0 commit comments

Comments
 (0)