From 41fb72d8d048d38b049e34c1879f1b6b52c7e9ae Mon Sep 17 00:00:00 2001 From: hanyin Date: Thu, 4 Jul 2019 12:05:40 +0800 Subject: [PATCH] To support sql comment. Some dbproxy middleware use sql comment to make make a query on master node. Example: db.Table("test").Comment("/*m:master*/").Get( ptrObj ) The sql builder will output: /*m:master*/SELECT * from test ....; --- session.go | 7 +++++++ session_query.go | 1 + 2 files changed, 8 insertions(+) diff --git a/session.go b/session.go index b33955fdc..91ec7ecc2 100644 --- a/session.go +++ b/session.go @@ -57,6 +57,7 @@ type Session struct { //beforeSQLExec func(string, ...interface{}) lastSQL string lastSQLArgs []interface{} + comment string ctx context.Context sessionType sessionType @@ -157,6 +158,12 @@ func (session *Session) Table(tableNameOrBean interface{}) *Session { return session } +// Set comment for select statement +func (session *Session) Comment(comment string) *Session { + session.comment = comment + return session +} + // Alias set the table alias func (session *Session) Alias(alias string) *Session { session.statement.Alias(alias) diff --git a/session_query.go b/session_query.go index 21c00b8d7..3652ddd5d 100644 --- a/session_query.go +++ b/session_query.go @@ -20,6 +20,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa return convertSQLOrArgs(sqlOrArgs...) } + sqlStr = session.comment + sqlStr if session.statement.RawSQL != "" { return session.statement.RawSQL, session.statement.RawParams, nil }