-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselect.go
More file actions
137 lines (116 loc) · 3.11 KB
/
Copy pathselect.go
File metadata and controls
137 lines (116 loc) · 3.11 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package magicsql
import (
"fmt"
"reflect"
"strings"
)
// Select defines the table, where clause, and potentially other elements of an
// in-progress SELECT statement
type Select struct {
ot *OperationTable
where string
whereArgs []interface{}
order string
limit uint64
offset uint64
sqlfunc func(Select) string
}
func selectSQL(s Select) string {
var sql = fmt.Sprintf("SELECT %s FROM %s", strings.Join(s.ot.t.FieldNames(), ","), s.ot.t.Name)
if s.where != "" {
sql += fmt.Sprintf(" WHERE %s", s.where)
}
if s.order != "" {
sql += fmt.Sprintf(" ORDER BY %s", s.order)
}
if s.limit > 0 {
sql += fmt.Sprintf(" LIMIT %d", s.limit)
}
if s.offset > 0 {
sql += fmt.Sprintf(" OFFSET %d", s.offset)
}
return sql
}
// NewSelect creates a new Select instance tied to the given OperationTable
func NewSelect(ot *OperationTable) Select {
var s = Select{ot: ot}
s.sqlfunc = selectSQL
return s
}
// Where sets (or overwrites) the where clause information
func (s Select) Where(w string, args ...interface{}) Select {
s.where = w
s.whereArgs = args
return s
}
// Limit sets (or overwrites) the limit value
func (s Select) Limit(l uint64) Select {
s.limit = l
return s
}
// Offset sets (or overwrites) the offset value
func (s Select) Offset(o uint64) Select {
s.offset = o
return s
}
// Order sets (or overwrites) the order clause
func (s Select) Order(o string) Select {
s.order = o
return s
}
// SQL returns the raw query this Select represents
func (s Select) SQL() string {
return s.sqlfunc(s)
}
// Query builds the SQL statement, executes it through the parent
// OperationTable, and returns the resulting rows
func (s Select) Query() *Rows {
if s.ot.op.Err() != nil {
return &Rows{nil, s.ot.op}
}
return s.ot.op.Query(s.SQL(), s.whereArgs...)
}
// EachRow wraps Query, yielding a Scannable per row to the callback instead of
// returning a *Rows object
func (s Select) EachRow(cb func(Scannable)) {
var r = s.Query()
defer r.Close()
for r.Next() {
cb(r)
}
}
// First builds the SQL statement, executes it through the parent
// OperationTable, and returns the first object into dest. If there are no
// rows, ok is false.
func (s Select) First(dest interface{}) (ok bool) {
var r = s.Query()
defer r.Close()
if !r.Next() {
return false
}
r.Scan(s.ot.t.ScanStruct(dest)...)
return true
}
// AllObjects builds the SQL statement, executes it through the parent
// OperationTable, and returns the resulting objects into ptr, which must be a
// pointer to a slice of the type tied to this Select.
func (s Select) AllObjects(ptr interface{}) {
var rows = s.Query()
defer rows.Close()
var slice = reflect.ValueOf(ptr).Elem()
for rows.Next() {
var obj = reflect.New(s.ot.t.RType).Interface()
rows.Scan(s.ot.t.ScanStruct(obj)...)
slice.Set(reflect.Append(slice, reflect.ValueOf(obj)))
}
}
// EachObject mimics AllObjects, but yields each item to the callback instead
// of requiring a slice in which to put all of them at once
func (s Select) EachObject(dest interface{}, cb func()) {
var r = s.Query()
defer r.Close()
for r.Next() {
r.Scan(s.ot.t.ScanStruct(dest)...)
cb()
}
}