Skip to content

Commit

Permalink
add the comment for the sql statement
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Aug 30, 2024
1 parent 1d42cc9 commit 6afe80c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dml_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewDeleteBuilder(tables ...string) *DeleteBuilder {
// DeleteBuilder is used to build the DELETE statement.
type DeleteBuilder struct {
db *DB
comment string
dtables []string
ftables []sqlTable
jtables []joinTable
Expand Down Expand Up @@ -124,6 +125,12 @@ func (b *DeleteBuilder) WhereNamedArgs(andArgs ...sql.NamedArg) *DeleteBuilder {
return b
}

// Comment set the comment, which will be appended to the end of the built SQL statement.
func (b *DeleteBuilder) Comment(comment string) *DeleteBuilder {
b.comment = comment
return b
}

// Where sets the "WHERE" conditions.
func (b *DeleteBuilder) Where(andConditions ...op.Condition) *DeleteBuilder {
b.wheres = appendWheres(b.wheres, andConditions...)
Expand Down Expand Up @@ -202,6 +209,12 @@ func (b *DeleteBuilder) Build() (sql string, args *ArgsBuilder) {
buf.WriteString(BuildOper(args, op.And(b.wheres...)))
}

if b.comment != "" {
buf.WriteString("/* ")
buf.WriteString(b.comment)
buf.WriteString(" */")
}

sql = buf.String()
putBuffer(buf)
return
Expand Down
13 changes: 13 additions & 0 deletions dml_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type InsertBuilder struct {

verb string
table string
comment string
columns []string
values [][]any
}
Expand Down Expand Up @@ -68,6 +69,12 @@ func (b *InsertBuilder) ReplaceInto(table string) *InsertBuilder {
return b
}

// Comment set the comment, which will be appended to the end of the built SQL statement.
func (b *InsertBuilder) Comment(comment string) *InsertBuilder {
b.comment = comment
return b
}

// Columns sets the inserted columns.
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
b.columns = columns
Expand Down Expand Up @@ -304,6 +311,12 @@ func (b *InsertBuilder) Build() (sql string, args *ArgsBuilder) {
}
}

if b.comment != "" {
buf.WriteString("/* ")
buf.WriteString(b.comment)
buf.WriteString(" */")
}

sql = buf.String()
putBuffer(buf)
return
Expand Down
13 changes: 13 additions & 0 deletions dml_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type SelectBuilder struct {
havings []string
groupbys []string
orderbys []orderby
comment string
offset int64
limit int64
page op.Paginator
Expand Down Expand Up @@ -472,6 +473,12 @@ func (b *SelectBuilder) Paginator(page op.Paginator) *SelectBuilder {
return b
}

// Comment set the comment, which will be appended to the end of the built SQL statement.
func (b *SelectBuilder) Comment(comment string) *SelectBuilder {
b.comment = comment
return b
}

// SetDB sets the db.
func (b *SelectBuilder) SetDB(db *DB) *SelectBuilder {
b.db = db
Expand Down Expand Up @@ -594,6 +601,12 @@ func (b *SelectBuilder) Build() (sql string, args *ArgsBuilder) {
buf.WriteString(BuildOper(args, b.page))
}

if b.comment != "" {
buf.WriteString("/* ")
buf.WriteString(b.comment)
buf.WriteString(" */")
}

sql = buf.String()
putBuffer(buf)
return
Expand Down
13 changes: 13 additions & 0 deletions dml_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewUpdateBuilder(table ...string) *UpdateBuilder {
// UpdateBuilder is used to build the UPDATE statement.
type UpdateBuilder struct {
db *DB
comment string
utables []sqlTable
ftables []sqlTable
jtables []joinTable
Expand Down Expand Up @@ -143,6 +144,12 @@ func (b *UpdateBuilder) SetNamedArg(args ...sql.NamedArg) *UpdateBuilder {
return b
}

// Comment set the comment, which will be appended to the end of the built SQL statement.
func (b *UpdateBuilder) Comment(comment string) *UpdateBuilder {
b.comment = comment
return b
}

// WhereNamedArgs is the same as Where, but uses the NamedArg as the EQUAL condition.
func (b *UpdateBuilder) WhereNamedArgs(andArgs ...sql.NamedArg) *UpdateBuilder {
if b.wheres == nil {
Expand Down Expand Up @@ -250,6 +257,12 @@ func (b *UpdateBuilder) Build() (sql string, args *ArgsBuilder) {
buf.WriteString(BuildOper(args, op.And(b.wheres...)))
}

if b.comment != "" {
buf.WriteString("/* ")
buf.WriteString(b.comment)
buf.WriteString(" */")
}

sql = buf.String()
putBuffer(buf)
return
Expand Down

0 comments on commit 6afe80c

Please sign in to comment.