From 235d3d8cf6460bc97eef6eb3955e6b45ae7d6faa Mon Sep 17 00:00:00 2001 From: Raul-Mircea Date: Thu, 14 Nov 2019 16:05:40 +0200 Subject: [PATCH] Add --- table_and_query.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 table_and_query.go diff --git a/table_and_query.go b/table_and_query.go new file mode 100644 index 00000000..8a5add28 --- /dev/null +++ b/table_and_query.go @@ -0,0 +1,27 @@ +package gorm + +import ( + "strings" + "time" +) + +// TableAndQuery returns the table name and the query already formatted as a string +func (s *DB) TableAndQuery() (string, string) { + scope := s.NewScope(s.Value) + scope.InstanceSet("skip_bindvar", true) + scope.prepareQuerySQL() + + qs := LogFormatter("sql", "q", time.Duration(1), scope.SQL, scope.SQLVars, int64(1)) + t, q := scope.TableName(), qs[3].(string) + if t == "" { + qsplit := strings.Fields(strings.ToLower(q)) + for i := range qsplit { + if qsplit[i] == "from" && i < len(qsplit)-2 { + t = qsplit[i+1] + break + } + } + } + + return t, q +}