Add Selects, Omits for statement

This commit is contained in:
Jinzhu 2020-02-16 13:45:27 +08:00
parent 18236fa3d7
commit 98ad29f2c2
7 changed files with 73 additions and 31 deletions

View File

@ -2,6 +2,7 @@ package gorm
import ( import (
"fmt" "fmt"
"strings"
"github.com/jinzhu/gorm/clause" "github.com/jinzhu/gorm/clause"
) )
@ -31,9 +32,7 @@ func (db *DB) Clauses(conds ...clause.Expression) (tx *DB) {
} }
if len(whereConds) > 0 { if len(whereConds) > 0 {
tx.Statement.AddClause(&clause.Where{ tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(whereConds[0], whereConds[1:]...)})
tx.Statement.BuildCondtion(whereConds[0], whereConds[1:]...),
})
} }
return return
} }
@ -48,38 +47,83 @@ func (db *DB) Table(name string) (tx *DB) {
// Select specify fields that you want when querying, creating, updating // Select specify fields that you want when querying, creating, updating
func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) { func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
switch v := query.(type) {
case []string:
tx.Statement.Selects = v
for _, arg := range args {
switch arg := arg.(type) {
case string:
tx.Statement.Selects = append(tx.Statement.Selects, arg)
case []string:
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
default:
tx.AddError(fmt.Errorf("unsupported select args %v %v", query, args))
return
}
}
case string:
fields := strings.FieldsFunc(v, isChar)
// normal field names
if len(fields) == 1 || (len(fields) == 3 && strings.ToUpper(fields[1]) == "AS") {
tx.Statement.Selects = fields
for _, arg := range args {
switch arg := arg.(type) {
case string:
tx.Statement.Selects = append(tx.Statement.Selects, arg)
case []string:
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
default:
tx.Statement.AddClause(clause.Select{
Expression: clause.Expr{SQL: v, Vars: args},
})
return
}
}
} else {
tx.Statement.AddClause(clause.Select{
Expression: clause.Expr{SQL: v, Vars: args},
})
}
default:
tx.AddError(fmt.Errorf("unsupported select args %v %v", query, args))
}
return return
} }
// Omit specify fields that you want to ignore when creating, updating and querying // Omit specify fields that you want to ignore when creating, updating and querying
func (db *DB) Omit(columns ...string) (tx *DB) { func (db *DB) Omit(columns ...string) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
if len(columns) == 1 && strings.Contains(columns[0], ",") {
tx.Statement.Omits = strings.FieldsFunc(columns[0], isChar)
} else {
tx.Statement.Omits = columns
}
return return
} }
func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB) { func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Statement.AddClause(&clause.Where{ tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(query, args...)})
tx.Statement.BuildCondtion(query, args...),
})
return return
} }
// Not add NOT condition // Not add NOT condition
func (db *DB) Not(query interface{}, args ...interface{}) (tx *DB) { func (db *DB) Not(query interface{}, args ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Statement.AddClause(&clause.Where{ tx.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.Not(tx.Statement.BuildCondtion(query, args...)...)}})
[]clause.Expression{clause.Not(tx.Statement.BuildCondtion(query, args...)...)},
})
return return
} }
// Or add OR conditions // Or add OR conditions
func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) { func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Statement.AddClause(&clause.Where{ tx.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.Or(tx.Statement.BuildCondtion(query, args...)...)}})
[]clause.Expression{clause.Or(tx.Statement.BuildCondtion(query, args...)...)},
})
return return
} }
@ -110,11 +154,11 @@ func (db *DB) Order(value interface{}) (tx *DB) {
switch v := value.(type) { switch v := value.(type) {
case clause.OrderByColumn: case clause.OrderByColumn:
db.Statement.AddClause(clause.OrderBy{ tx.Statement.AddClause(clause.OrderBy{
Columns: []clause.OrderByColumn{v}, Columns: []clause.OrderByColumn{v},
}) })
default: default:
db.Statement.AddClause(clause.OrderBy{ tx.Statement.AddClause(clause.OrderBy{
Columns: []clause.OrderByColumn{{ Columns: []clause.OrderByColumn{{
Column: clause.Column{Name: fmt.Sprint(value), Raw: true}, Column: clause.Column{Name: fmt.Sprint(value), Raw: true},
}}, }},

View File

@ -2,8 +2,8 @@ package clause
// Select select attrs when querying, updating, creating // Select select attrs when querying, updating, creating
type Select struct { type Select struct {
Columns []Column Columns []Column
Omits []Column Expression Expression
} }
func (s Select) Name() string { func (s Select) Name() string {
@ -24,9 +24,9 @@ func (s Select) Build(builder Builder) {
} }
func (s Select) MergeClause(clause *Clause) { func (s Select) MergeClause(clause *Clause) {
if v, ok := clause.Expression.(Select); ok { if s.Expression != nil {
s.Columns = append(v.Columns, s.Columns...) clause.Expression = s.Expression
s.Omits = append(v.Omits, s.Omits...) } else {
clause.Expression = s
} }
clause.Expression = s
} }

View File

@ -29,7 +29,7 @@ func TestSelect(t *testing.T) {
}, clause.Select{ }, clause.Select{
Columns: []clause.Column{{Name: "name"}}, Columns: []clause.Column{{Name: "name"}},
}, clause.From{}}, }, clause.From{}},
"SELECT `users`.`id`,`name` FROM `users`", nil, "SELECT `name` FROM `users`", nil,
}, },
} }

View File

@ -1,7 +0,0 @@
module github.com/jinzhu/gorm/dialects/mysql
go 1.13
require (
github.com/go-sql-driver/mysql v1.5.0
)

4
go.mod
View File

@ -3,8 +3,6 @@ module github.com/jinzhu/gorm
go 1.13 go 1.13
require ( require (
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/inflection v1.0.0
github.com/lib/pq v1.3.0 // indirect github.com/jinzhu/now v1.1.1
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
) )

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"errors" "errors"
"time" "time"
"unicode"
) )
var ( var (
@ -27,3 +28,7 @@ type Model struct {
UpdatedAt time.Time UpdatedAt time.Time
DeletedAt *time.Time `gorm:"index"` DeletedAt *time.Time `gorm:"index"`
} }
func isChar(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}

View File

@ -43,6 +43,8 @@ type Statement struct {
Model interface{} Model interface{}
Dest interface{} Dest interface{}
Clauses map[string]clause.Clause Clauses map[string]clause.Clause
Selects []string // selected columns
Omits []string // omit columns
Settings sync.Map Settings sync.Map
DB *DB DB *DB
Schema *schema.Schema Schema *schema.Schema