forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_sql.go
96 lines (80 loc) · 2.61 KB
/
build_sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gorm
import (
"errors"
"fmt"
"reflect"
)
type SQL struct {
SQL string
SQLVars []interface{}
}
// First find first record that match given conditions, order by primary key
func (s *DB) FirstSQL(sql *SQL, out interface{}, where ...interface{}) *DB {
newScope := s.clone().NewScope(out)
newScope.Search.Limit(1)
return newScope.Set("gorm:order_by_primary_key", "ASC").
inlineCondition(where...).buildQuerySQL(sql).db
}
// Last find last record that match given conditions, order by primary key
func (s *DB) LastSQL(sql *SQL, out interface{}, where ...interface{}) *DB {
newScope := s.clone().NewScope(out)
newScope.Search.Limit(1)
return newScope.Set("gorm:order_by_primary_key", "DESC").
inlineCondition(where...).buildQuerySQL(sql).db
}
// Find find records that match given conditions
func (s *DB) FindSQL(sql *SQL, out interface{}, where ...interface{}) *DB {
return s.clone().NewScope(out).inlineCondition(where...).buildQuerySQL(sql).db
}
func (s *DB) CountSQL(sql *SQL, value interface{}) *DB {
scope := s.NewScope(s.Value)
if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) {
scope.Search.Select("count(*)")
}
scope.Search.ignoreOrderQuery = true
result := &RowQueryResult{}
scope.InstanceSet("row_query_result", result)
return scope.buildRowSQL(sql).db
}
func (scope *Scope) buildQuerySQL(sql *SQL) *Scope {
var (
resultType reflect.Type
results = scope.IndirectValue()
)
if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
if primaryField := scope.PrimaryField(); primaryField != nil {
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), scope.Quote(primaryField.DBName), orderBy))
}
}
if value, ok := scope.Get("gorm:query_destination"); ok {
results = indirect(reflect.ValueOf(value))
}
if kind := results.Kind(); kind == reflect.Slice {
resultType = results.Type().Elem()
results.Set(reflect.MakeSlice(results.Type(), 0, 0))
if resultType.Kind() == reflect.Ptr {
resultType = resultType.Elem()
}
} else if kind != reflect.Struct {
scope.Err(errors.New("unsupported destination, should be slice or struct"))
return scope
}
scope.prepareQuerySQL()
if !scope.HasError() {
scope.db.RowsAffected = 0
if str, ok := scope.Get("gorm:query_option"); ok {
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
}
sql.SQL = scope.SQL
sql.SQLVars = scope.SQLVars
}
return scope
}
func (scope *Scope) buildRowSQL(sql *SQL) *Scope {
if _, ok := scope.InstanceGet("row_query_result"); ok {
scope.prepareQuerySQL()
sql.SQL = scope.SQL
sql.SQLVars = scope.SQLVars
}
return scope
}