Rename expr type to make it public. (#2604)

This commit is contained in:
macklin-10x 2019-10-17 08:44:34 -07:00 committed by Jinzhu
parent a8a530db5a
commit 5b3e40ac12
4 changed files with 10 additions and 10 deletions

View File

@ -209,8 +209,8 @@ func (s *DB) NewScope(value interface{}) *Scope {
return scope return scope
} }
// QueryExpr returns the query as expr object // QueryExpr returns the query as SqlExpr object
func (s *DB) QueryExpr() *expr { func (s *DB) QueryExpr() *SqlExpr {
scope := s.NewScope(s.Value) scope := s.NewScope(s.Value)
scope.InstanceSet("skip_bindvar", true) scope.InstanceSet("skip_bindvar", true)
scope.prepareQuerySQL() scope.prepareQuerySQL()
@ -219,7 +219,7 @@ func (s *DB) QueryExpr() *expr {
} }
// SubQuery returns the query as sub query // SubQuery returns the query as sub query
func (s *DB) SubQuery() *expr { func (s *DB) SubQuery() *SqlExpr {
scope := s.NewScope(s.Value) scope := s.NewScope(s.Value)
scope.InstanceSet("skip_bindvar", true) scope.InstanceSet("skip_bindvar", true)
scope.prepareQuerySQL() scope.prepareQuerySQL()

View File

@ -257,7 +257,7 @@ func (scope *Scope) CallMethod(methodName string) {
func (scope *Scope) AddToVars(value interface{}) string { func (scope *Scope) AddToVars(value interface{}) string {
_, skipBindVar := scope.InstanceGet("skip_bindvar") _, skipBindVar := scope.InstanceGet("skip_bindvar")
if expr, ok := value.(*expr); ok { if expr, ok := value.(*SqlExpr); ok {
exp := expr.expr exp := expr.expr
for _, arg := range expr.args { for _, arg := range expr.args {
if skipBindVar { if skipBindVar {
@ -785,7 +785,7 @@ func (scope *Scope) orderSQL() string {
for _, order := range scope.Search.orders { for _, order := range scope.Search.orders {
if str, ok := order.(string); ok { if str, ok := order.(string); ok {
orders = append(orders, scope.quoteIfPossible(str)) orders = append(orders, scope.quoteIfPossible(str))
} else if expr, ok := order.(*expr); ok { } else if expr, ok := order.(*SqlExpr); ok {
exp := expr.expr exp := expr.expr
for _, arg := range expr.args { for _, arg := range expr.args {
exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1) exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
@ -912,7 +912,7 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
for key, value := range convertInterfaceToMap(value, true, scope.db) { for key, value := range convertInterfaceToMap(value, true, scope.db) {
if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) { if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
if _, ok := value.(*expr); ok { if _, ok := value.(*SqlExpr); ok {
hasUpdate = true hasUpdate = true
results[field.DBName] = value results[field.DBName] = value
} else { } else {

View File

@ -98,7 +98,7 @@ func (s *search) Group(query string) *search {
} }
func (s *search) Having(query interface{}, values ...interface{}) *search { func (s *search) Having(query interface{}, values ...interface{}) *search {
if val, ok := query.(*expr); ok { if val, ok := query.(*SqlExpr); ok {
s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": val.expr, "args": val.args}) s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": val.expr, "args": val.args})
} else { } else {
s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": query, "args": values}) s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": query, "args": values})

View File

@ -58,15 +58,15 @@ func newSafeMap() *safeMap {
} }
// SQL expression // SQL expression
type expr struct { type SqlExpr struct {
expr string expr string
args []interface{} args []interface{}
} }
// Expr generate raw SQL expression, for example: // Expr generate raw SQL expression, for example:
// DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100)) // DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100))
func Expr(expression string, args ...interface{}) *expr { func Expr(expression string, args ...interface{}) *SqlExpr {
return &expr{expr: expression, args: args} return &SqlExpr{expr: expression, args: args}
} }
func indirect(reflectValue reflect.Value) reflect.Value { func indirect(reflectValue reflect.Value) reflect.Value {