diff --git a/dml_delete.go b/dml_delete.go index 893f0bc..9d5ecf9 100644 --- a/dml_delete.go +++ b/dml_delete.go @@ -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 @@ -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...) @@ -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 diff --git a/dml_insert.go b/dml_insert.go index 77e855a..11c4bd3 100644 --- a/dml_insert.go +++ b/dml_insert.go @@ -41,6 +41,7 @@ type InsertBuilder struct { verb string table string + comment string columns []string values [][]any } @@ -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 @@ -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 diff --git a/dml_select.go b/dml_select.go index 4bf8cbb..89f2cca 100644 --- a/dml_select.go +++ b/dml_select.go @@ -122,6 +122,7 @@ type SelectBuilder struct { havings []string groupbys []string orderbys []orderby + comment string offset int64 limit int64 page op.Paginator @@ -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 @@ -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 diff --git a/dml_update.go b/dml_update.go index d75762c..36f944e 100644 --- a/dml_update.go +++ b/dml_update.go @@ -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 @@ -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 { @@ -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