Merge pull request #1 from Vivino/add_f

Added the method.
This commit is contained in:
Raul-Mircea Crivineanu 2019-11-14 16:07:08 +02:00 committed by GitHub
commit 91763c8e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

27
table_and_query.go Normal file
View File

@ -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
}