Replace FullTable with TableExpr

This commit is contained in:
Jinzhu 2020-07-17 21:19:11 +08:00
parent e77156980c
commit de764d9e3d
2 changed files with 4 additions and 5 deletions

View File

@ -47,7 +47,7 @@ var tableRegexp = regexp.MustCompile(`(?i).+ AS (\w+)\s*$`)
func (db *DB) Table(name string) (tx *DB) {
tx = db.getInstance()
if strings.Contains(name, " ") {
tx.Statement.FullTable = name
tx.Statement.TableExpr = &clause.Expr{SQL: name}
if results := tableRegexp.FindStringSubmatch(name); len(results) == 2 {
tx.Statement.Table = results[1]
return

View File

@ -19,7 +19,7 @@ import (
// Statement statement
type Statement struct {
*DB
FullTable string
TableExpr *clause.Expr
Table string
Model interface{}
Unscoped bool
@ -69,8 +69,8 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
switch v := field.(type) {
case clause.Table:
if v.Name == clause.CurrentTable {
if stmt.FullTable != "" {
writer.WriteString(stmt.FullTable)
if stmt.TableExpr != nil {
stmt.TableExpr.Build(stmt)
} else {
stmt.DB.Dialector.QuoteTo(writer, stmt.Table)
}
@ -378,7 +378,6 @@ func (stmt *Statement) Build(clauses ...string) {
func (stmt *Statement) Parse(value interface{}) (err error) {
if stmt.Schema, err = schema.Parse(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy); err == nil && stmt.Table == "" {
stmt.Table = stmt.Schema.Table
stmt.FullTable = stmt.Schema.Table
}
return err
}