-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_updater.go
150 lines (131 loc) · 2.98 KB
/
sql_updater.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
package dbx
import (
"database/sql"
"fmt"
"reflect"
)
// SQLUpdater
type SQLUpdater struct {
table *Table
db *sql.DB
tx *sql.Tx
err error
columns []string
filter sqlFilter
}
// Set sets columns to be updated
func (this *SQLUpdater) Set(cols ...string) *SQLUpdater {
this.columns = cols
return this
}
// Values updates given values to table
func (this *SQLUpdater) Values(values ...interface{}) (sql.Result, error) {
if this.err != nil {
return nil, this.err
}
// if not given columns, all columns will be updated
size := len(this.columns)
if size < 1 {
return nil, fmt.Errorf("please specify columns to update")
}
if len(values) != size {
return nil, fmt.Errorf("the specified columns and values are not equal")
}
cols := ""
for _, n := range this.columns {
if n != "" {
cols += n + "=?,"
}
}
if cols == "" {
return nil, fmt.Errorf("no specified columns to update")
}
cols = cols[:len(cols)-1]
q := "UPDATE " + this.table.Name + " SET " + cols
if this.filter.where != "" {
q += " WHERE " + this.filter.where
values = append(values, this.filter.args...)
}
if dbLogger != nil {
dbLogger(q)
}
if this.tx != nil {
return this.tx.Exec(q, values...)
} else {
return this.db.Exec(q, values...)
}
}
func (this *SQLUpdater) ValueMap(valMap map[string]interface{}) (
sql.Result, error) {
if this.err != nil {
return nil, this.err
}
n := len(valMap)
if n < 1 {
return nil, fmt.Errorf("please specify columns to update")
}
i := 0
cols := ""
vals := make([]interface{}, n)
for col, val := range valMap {
cols += col + "=?,"
vals[i] = val
i += 1
}
cols = cols[:len(cols)-1]
q := "UPDATE " + this.table.Name + " SET " + cols
if this.filter.where != "" {
q += " WHERE " + this.filter.where
vals = append(vals, this.filter.args...)
}
if dbLogger != nil {
dbLogger(q)
}
if this.tx != nil {
return this.tx.Exec(q, vals...)
} else {
return this.db.Exec(q, vals...)
}
}
// Value updates given row to table
func (this *SQLUpdater) Value(row interface{}) (sql.Result, error) {
if this.err != nil {
return nil, this.err
}
// if not given columns, all columns will be updated
size := len(this.columns)
if size < 1 {
this.columns = this.table.ColumnNames()
size = len(this.columns)
}
cols := ""
vals := []interface{}{}
rowVal := reflect.ValueOf(row).Elem()
for _, n := range this.columns {
col, ok := this.table.Columns[n]
if !ok {
return nil, fmt.Errorf("column %s is not found", n)
}
if !col.IsAutoIncrement {
cols += n + "=?,"
vals = append(vals, rowVal.Field(col.Index).Interface())
}
}
if cols == "" {
return nil, fmt.Errorf("no specified columns to update")
}
cols = cols[:len(cols)-1]
q := "UPDATE " + this.table.Name + " SET " + cols
if this.filter.where != "" {
q += " WHERE " + this.filter.where
vals = append(vals, this.filter.args...)
}
if dbLogger != nil {
dbLogger(q)
}
if this.tx != nil {
return this.tx.Exec(q, vals...)
} else {
return this.db.Exec(q, vals...)
}
}