-
Notifications
You must be signed in to change notification settings - Fork 17
/
advanced.go
181 lines (144 loc) · 4.11 KB
/
advanced.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package buildsqlx
import (
"database/sql"
"fmt"
"math"
"reflect"
"strconv"
)
// First getting the 1st row of query
func (r *DB) First(src any) error {
err := r.ScanStruct(src)
if err != nil {
return err
}
return nil
}
// Value gets the value of column in first query resulting row
func (r *DB) Value(src any, column string) error {
err := r.Select(column).ScanStruct(src)
if err != nil {
return err
}
return nil
}
// Find retrieves a single row by it's id column value
func (r *DB) Find(src any, id uint64) error {
return r.Where("id", "=", id).First(src)
}
// Pluck getting values of a particular column(s) of a struct and place them into slice
func (r *DB) Pluck(src any) ([]any, error) {
res, err := r.eachToStructRows(src, r.Builder.offset, r.Builder.limit)
if err != nil {
return nil, err
}
return res, nil
}
// PluckMap getting values of a particular key/value columns and place them into map
// values of the returning map is a structure passed as src and filled with data from DB
func (r *DB) PluckMap(src any, colKey, colValue string) (val []map[any]any, err error) {
resource := reflect.ValueOf(src).Elem()
if err = validateFields(resource, src, []string{colKey, colValue}); err != nil {
return nil, err
}
res, err := r.eachToStructRows(src, r.Builder.offset, r.Builder.limit)
if err != nil {
return nil, err
}
val = make([]map[any]any, len(res))
for k, m := range res {
val[k] = make(map[any]any)
fieldKeyData := getFieldValue(m, colKey)
val[k][fieldKeyData] = reflect.ValueOf(m).Interface()
}
return
}
// Exists checks whether conditional rows are existing (returns true) or not (returns false)
func (r *DB) Exists() (exists bool, err error) {
bldr := r.Builder
if bldr.table == "" {
return false, errTableCallBeforeOp
}
query := `SELECT EXISTS(SELECT 1 FROM "` + bldr.table + `" ` + bldr.buildClauses() + `)`
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&exists)
return
}
// DoesntExists an inverse of Exists
func (r *DB) DoesntExists() (bool, error) {
ex, err := r.Exists()
if err != nil {
return false, err
}
return !ex, nil
}
// Increment column on passed value
func (r *DB) Increment(column string, on uint64) (int64, error) {
return r.incrDecr(column, plusSign, on)
}
// Decrement column on passed value
func (r *DB) Decrement(column string, on uint64) (int64, error) {
return r.incrDecr(column, minusSign, on)
}
// increments or decrements depending on sign
func (r *DB) incrDecr(column, sign string, on uint64) (int64, error) {
bldr := r.Builder
if bldr.table == "" {
return 0, errTableCallBeforeOp
}
query := `UPDATE "` + r.Builder.table + `" SET ` + column + ` = ` + column + sign + strconv.FormatUint(on, 10)
res, err := r.Sql().Exec(query)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// Chunk run queries by chinks by passing user-land function with an ability to stop execution when needed
// by returning false and proceed to execute queries when return true
func (r *DB) Chunk(src any, amount int64, fn func(rows []any) bool) error {
cols := r.Builder.columns
cnt, err := r.Count()
if err != nil {
return err
}
r.Builder.columns = cols
if amount <= 0 {
return fmt.Errorf("chunk can't be <= 0, your chunk is: %d", amount)
}
if cnt < amount {
structRows, err := r.eachToStructRows(src, 0, 0)
if err != nil {
return err
}
fn(structRows) // execute all resulting records
return nil
}
// executing chunks amount < cnt
c := int64(math.Ceil(float64(cnt / amount)))
for i := int64(0); i < c; i++ {
structRows, err := r.eachToStructRows(src, i*amount, amount)
if err != nil {
return err
}
res := fn(structRows)
if !res { // stop an execution when false returned by user
break
}
}
return nil
}
func (r *DB) eachToStructRows(src any, offset, limit int64) ([]any, error) {
var structRows []any
if limit > 0 {
r.Offset(offset).Limit(limit)
}
err := r.EachToStruct(func(rows *sql.Rows) error {
err := r.Next(rows, src)
if err != nil {
return err
}
v := reflect.ValueOf(src).Elem().Interface()
structRows = append(structRows, v)
return nil
})
return structRows, err
}