Move statement out
This commit is contained in:
		
							parent
							
								
									94e06eb2f8
								
							
						
					
					
						commit
						24ed796198
					
				
							
								
								
									
										30
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								api.go
									
									
									
									
									
								
							@ -1,9 +1,5 @@
 | 
			
		||||
package gorm
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/jinzhu/gorm/builder"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Where add condition
 | 
			
		||||
func (s *DB) Where(query interface{}, args ...interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
@ -14,21 +10,21 @@ func (s *DB) Where(query interface{}, args ...interface{}) *DB {
 | 
			
		||||
// Not add NOT condition
 | 
			
		||||
func (s *DB) Not(query interface{}, args ...interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.AddConditions(builder.Not(tx.Statement.BuildCondition(query, args...)))
 | 
			
		||||
	tx.Statement.AddConditions(Not(tx.Statement.BuildCondition(query, args...)))
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// And add AND conditions
 | 
			
		||||
func (s *DB) And(conds ...builder.Condition) *DB {
 | 
			
		||||
func (s *DB) And(conds ...ConditionInterface) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.AddConditions(builder.And(conds))
 | 
			
		||||
	tx.Statement.AddConditions(And(conds))
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Or add OR conditions
 | 
			
		||||
func (s *DB) Or(conds ...builder.Condition) *DB {
 | 
			
		||||
func (s *DB) Or(conds ...ConditionInterface) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.AddConditions(builder.Or(conds))
 | 
			
		||||
	tx.Statement.AddConditions(Or(conds))
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -37,7 +33,7 @@ func (s *DB) Or(conds ...builder.Condition) *DB {
 | 
			
		||||
func (s *DB) Joins(query string, args ...interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	// FIXME
 | 
			
		||||
	tx.Statement.Joins = append(tx.Statement.Joins, builder.Join{Conditions: []builder.Condition{tx.Statement.BuildCondition(query, args...)}})
 | 
			
		||||
	tx.Statement.Joins = append(tx.Statement.Joins, Join{Conditions: []ConditionInterface{tx.Statement.BuildCondition(query, args...)}})
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -67,7 +63,7 @@ func (s *DB) Order(value interface{}) *DB {
 | 
			
		||||
// Reorder works like Order, but will overwrite current order information
 | 
			
		||||
func (s *DB) Reorder(value interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.OrderBy = []builder.OrderCondition{value}
 | 
			
		||||
	tx.Statement.OrderBy = []OrderCondition{value}
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -118,7 +114,7 @@ func (s *DB) Omit(columns ...string) *DB {
 | 
			
		||||
 | 
			
		||||
// First find first record that match given conditions, order by primary key
 | 
			
		||||
func (s *DB) First(out interface{}, where ...interface{}) *DB {
 | 
			
		||||
	conds := []interface{}{builder.Limit{Limit: &one}, builder.Settings{"gorm:order_by_primary_key": "ASC"}}
 | 
			
		||||
	conds := []interface{}{Limit{Limit: &one}, Settings{"gorm:order_by_primary_key": "ASC"}}
 | 
			
		||||
	if len(where) > 0 {
 | 
			
		||||
		conds = append(conds, s.Statement.BuildCondition(where[0], where[1:]...))
 | 
			
		||||
	}
 | 
			
		||||
@ -127,7 +123,7 @@ func (s *DB) First(out interface{}, where ...interface{}) *DB {
 | 
			
		||||
 | 
			
		||||
// Take return a record that match given conditions, the order will depend on the database implementation
 | 
			
		||||
func (s *DB) Take(out interface{}, where ...interface{}) *DB {
 | 
			
		||||
	conds := []interface{}{builder.Limit{Limit: &one}}
 | 
			
		||||
	conds := []interface{}{Limit{Limit: &one}}
 | 
			
		||||
	if len(where) > 0 {
 | 
			
		||||
		conds = append(conds, s.Statement.BuildCondition(where[0], where[1:]...))
 | 
			
		||||
	}
 | 
			
		||||
@ -136,7 +132,7 @@ func (s *DB) Take(out interface{}, where ...interface{}) *DB {
 | 
			
		||||
 | 
			
		||||
// Last find last record that match given conditions, order by primary key
 | 
			
		||||
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
 | 
			
		||||
	conds := []interface{}{builder.Limit{Limit: &one}, builder.Settings{"gorm:order_by_primary_key": "DESC"}}
 | 
			
		||||
	conds := []interface{}{Limit{Limit: &one}, Settings{"gorm:order_by_primary_key": "DESC"}}
 | 
			
		||||
	if len(where) > 0 {
 | 
			
		||||
		conds = append(conds, s.Statement.BuildCondition(where[0], where[1:]...))
 | 
			
		||||
	}
 | 
			
		||||
@ -196,7 +192,7 @@ func (s *DB) Save(value interface{}) *DB {
 | 
			
		||||
// Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
			
		||||
func (s *DB) Update(column string, value interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.Assignments = append(tx.Statement.Assignments, builder.Assignment{Column: column, Value: value})
 | 
			
		||||
	tx.Statement.Assignments = append(tx.Statement.Assignments, Assignment{Column: column, Value: value})
 | 
			
		||||
	tx.AddError(tx.Dialect().Update(tx))
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
@ -204,7 +200,7 @@ func (s *DB) Update(column string, value interface{}) *DB {
 | 
			
		||||
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
 | 
			
		||||
func (s *DB) Updates(values interface{}) *DB {
 | 
			
		||||
	tx := s.init()
 | 
			
		||||
	tx.Statement.Assignments = append(tx.Statement.Assignments, builder.Assignment{Value: values})
 | 
			
		||||
	tx.Statement.Assignments = append(tx.Statement.Assignments, Assignment{Value: values})
 | 
			
		||||
	tx.AddError(tx.Dialect().Update(tx))
 | 
			
		||||
	return tx
 | 
			
		||||
}
 | 
			
		||||
@ -305,7 +301,7 @@ func (s *DB) init() *DB {
 | 
			
		||||
	if s.Statement == nil {
 | 
			
		||||
		return &DB{
 | 
			
		||||
			TxDialect: s.TxDialect,
 | 
			
		||||
			Statement: &builder.Statement{},
 | 
			
		||||
			Statement: &Statement{},
 | 
			
		||||
			Config:    s.Config,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -1,60 +0,0 @@
 | 
			
		||||
package builder
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// Comparison Operators
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
// Raw raw sql
 | 
			
		||||
type Raw struct {
 | 
			
		||||
	Value string
 | 
			
		||||
	Args  []interface{} // TODO NamedArg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Eq equal to
 | 
			
		||||
type Eq struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Neq not equal to
 | 
			
		||||
type Neq struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gt greater than
 | 
			
		||||
type Gt struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gte greater than or equal to
 | 
			
		||||
type Gte struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lt less than
 | 
			
		||||
type Lt struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lte less than or equal to
 | 
			
		||||
type Lte struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// Logical Operators
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
// And TRUE if all the conditions is TRUE
 | 
			
		||||
type And []Condition
 | 
			
		||||
 | 
			
		||||
// Not TRUE if condition is false
 | 
			
		||||
type Not Condition
 | 
			
		||||
 | 
			
		||||
// Or TRUE if any of the conditions is TRUE
 | 
			
		||||
type Or []Condition
 | 
			
		||||
@ -1,95 +0,0 @@
 | 
			
		||||
package builder
 | 
			
		||||
 | 
			
		||||
import "sync"
 | 
			
		||||
 | 
			
		||||
// Column column type
 | 
			
		||||
type Column = string
 | 
			
		||||
 | 
			
		||||
// Statement GORM statement
 | 
			
		||||
type Statement struct {
 | 
			
		||||
	Dest        interface{}  // Insert, Select, Update, Delete
 | 
			
		||||
	Table       interface{}  // Insert, Select, Update, Delete
 | 
			
		||||
	Select      SelectColumn // Insert, Select, Update
 | 
			
		||||
	Omit        []Column     // Insert, Select, Update
 | 
			
		||||
	Joins       []Join       // Select
 | 
			
		||||
	GroupBy     GroupBy      // Select
 | 
			
		||||
	OrderBy     OrderBy      // Select
 | 
			
		||||
	Preload     []Column     // Select
 | 
			
		||||
	Limit       Limit        // Select, Update
 | 
			
		||||
	Conditions  []Condition  // Select, Update, Delete
 | 
			
		||||
	Assignments []Assignment // Insert, Update
 | 
			
		||||
	Returnnings []Column     // Insert, Update, Delete
 | 
			
		||||
	Settings    sync.Map
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Condition query condition statement interface
 | 
			
		||||
type Condition interface {
 | 
			
		||||
	// ToSQL()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Settings settings
 | 
			
		||||
type Settings map[string]interface{}
 | 
			
		||||
 | 
			
		||||
// DefaultValue default value type
 | 
			
		||||
type DefaultValue string
 | 
			
		||||
 | 
			
		||||
// SelectColumn select columns
 | 
			
		||||
type SelectColumn struct {
 | 
			
		||||
	Columns []string
 | 
			
		||||
	Args    []interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Join join statement
 | 
			
		||||
type Join struct {
 | 
			
		||||
	Table        string
 | 
			
		||||
	LocalField   string
 | 
			
		||||
	ForeignField string
 | 
			
		||||
	Conditions   []Condition
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GroupBy group by statement
 | 
			
		||||
type GroupBy struct {
 | 
			
		||||
	GroupByColumns []string
 | 
			
		||||
	Having         []Condition
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// OrderCondition order condition, could be string or sql expr
 | 
			
		||||
type OrderCondition interface{}
 | 
			
		||||
 | 
			
		||||
// OrderBy order by statement
 | 
			
		||||
type OrderBy []OrderCondition
 | 
			
		||||
 | 
			
		||||
// OrderByColumn column used for order
 | 
			
		||||
type OrderByColumn struct {
 | 
			
		||||
	Name string
 | 
			
		||||
	Asc  bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Limit limit statement
 | 
			
		||||
type Limit struct {
 | 
			
		||||
	Limit  *int64
 | 
			
		||||
	Offset *int64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Assignment assign statement
 | 
			
		||||
type Assignment struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Clone clone current statement
 | 
			
		||||
func (stmt *Statement) Clone() *Statement {
 | 
			
		||||
	newStatement := *stmt
 | 
			
		||||
	// FIXME fix settings
 | 
			
		||||
	return &newStatement
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BuildCondition build condition
 | 
			
		||||
func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) Condition {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AddConditions add conditions
 | 
			
		||||
func (stmt *Statement) AddConditions(conds ...Condition) {
 | 
			
		||||
	stmt.Conditions = append(stmt.Conditions, conds...)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								gorm.go
									
									
									
									
									
								
							@ -3,7 +3,6 @@ package gorm
 | 
			
		||||
import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/jinzhu/gorm/builder"
 | 
			
		||||
	"github.com/jinzhu/gorm/logger"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@ -29,7 +28,7 @@ type Config struct {
 | 
			
		||||
// DB GORM DB definition
 | 
			
		||||
type DB struct {
 | 
			
		||||
	TxDialect Dialect
 | 
			
		||||
	Statement *builder.Statement
 | 
			
		||||
	Statement *Statement
 | 
			
		||||
 | 
			
		||||
	// Global config
 | 
			
		||||
	Config *Config
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										159
									
								
								statement.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								statement.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,159 @@
 | 
			
		||||
package gorm
 | 
			
		||||
 | 
			
		||||
import "sync"
 | 
			
		||||
 | 
			
		||||
// Column column type
 | 
			
		||||
type Column = string
 | 
			
		||||
 | 
			
		||||
// Statement GORM statement
 | 
			
		||||
type Statement struct {
 | 
			
		||||
	Dest        interface{}          // Insert, Select, Update, Delete
 | 
			
		||||
	Table       interface{}          // Insert, Select, Update, Delete
 | 
			
		||||
	Select      SelectColumn         // Insert, Select, Update
 | 
			
		||||
	Omit        []Column             // Insert, Select, Update
 | 
			
		||||
	Joins       []Join               // Select
 | 
			
		||||
	GroupBy     GroupBy              // Select
 | 
			
		||||
	OrderBy     OrderBy              // Select
 | 
			
		||||
	Preload     []Column             // Select
 | 
			
		||||
	Limit       Limit                // Select, Update
 | 
			
		||||
	Conditions  []ConditionInterface // Select, Update, Delete
 | 
			
		||||
	Assignments []Assignment         // Insert, Update
 | 
			
		||||
	Returnnings []Column             // Insert, Update, Delete
 | 
			
		||||
	Settings    sync.Map
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ConditionInterface query condition statement interface
 | 
			
		||||
type ConditionInterface interface{}
 | 
			
		||||
 | 
			
		||||
// Settings settings
 | 
			
		||||
type Settings map[string]interface{}
 | 
			
		||||
 | 
			
		||||
// DefaultValue default value type
 | 
			
		||||
type DefaultValue string
 | 
			
		||||
 | 
			
		||||
// SelectColumn select columns
 | 
			
		||||
type SelectColumn struct {
 | 
			
		||||
	Columns []string
 | 
			
		||||
	Args    []interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Join join statement
 | 
			
		||||
type Join struct {
 | 
			
		||||
	Table        string
 | 
			
		||||
	LocalField   string
 | 
			
		||||
	ForeignField string
 | 
			
		||||
	Conditions   []ConditionInterface
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GroupBy group by statement
 | 
			
		||||
type GroupBy struct {
 | 
			
		||||
	GroupByColumns []string
 | 
			
		||||
	Having         []ConditionInterface
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// OrderCondition order condition, could be string or sql expr
 | 
			
		||||
type OrderCondition interface{}
 | 
			
		||||
 | 
			
		||||
// OrderBy order by statement
 | 
			
		||||
type OrderBy []OrderCondition
 | 
			
		||||
 | 
			
		||||
// OrderByColumn column used for order
 | 
			
		||||
type OrderByColumn struct {
 | 
			
		||||
	Name string
 | 
			
		||||
	Asc  bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Limit limit statement
 | 
			
		||||
type Limit struct {
 | 
			
		||||
	Limit  *int64
 | 
			
		||||
	Offset *int64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Assignment assign statement
 | 
			
		||||
type Assignment struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Clone clone current statement
 | 
			
		||||
func (stmt *Statement) Clone() *Statement {
 | 
			
		||||
	newStatement := *stmt
 | 
			
		||||
	return &newStatement
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BuildCondition build condition
 | 
			
		||||
func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) ConditionInterface {
 | 
			
		||||
	if sql, ok := query.(string); ok {
 | 
			
		||||
		return Raw{Value: sql, Args: args}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	andConds := And([]ConditionInterface{ConditionInterface(query)})
 | 
			
		||||
	for _, arg := range args {
 | 
			
		||||
		andConds = append(andConds, ConditionInterface(arg))
 | 
			
		||||
	}
 | 
			
		||||
	return andConds
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AddConditions add conditions
 | 
			
		||||
func (stmt *Statement) AddConditions(conds ...ConditionInterface) {
 | 
			
		||||
	stmt.Conditions = append(stmt.Conditions, conds...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// Comparison Operators
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
// Raw raw sql
 | 
			
		||||
type Raw struct {
 | 
			
		||||
	Value string
 | 
			
		||||
	Args  []interface{} // TODO NamedArg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Eq equal to
 | 
			
		||||
type Eq struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Neq not equal to
 | 
			
		||||
type Neq struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gt greater than
 | 
			
		||||
type Gt struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gte greater than or equal to
 | 
			
		||||
type Gte struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lt less than
 | 
			
		||||
type Lt struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lte less than or equal to
 | 
			
		||||
type Lte struct {
 | 
			
		||||
	Column Column
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// Logical Operators
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
// And TRUE if all the conditions is TRUE
 | 
			
		||||
type And []ConditionInterface
 | 
			
		||||
 | 
			
		||||
// Not TRUE if condition is false
 | 
			
		||||
type Not ConditionInterface
 | 
			
		||||
 | 
			
		||||
// Or TRUE if any of the conditions is TRUE
 | 
			
		||||
type Or []ConditionInterface
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user