-
Notifications
You must be signed in to change notification settings - Fork 0
/
upsert.go
132 lines (112 loc) · 3.66 KB
/
upsert.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
package basicdam
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
func (dam *BasicDAM) Insert(obj interface{}) (string, error) {
counter := 1
var strKeys, strParams string
values := make([]interface{}, 0)
fieldValues := parseObjectData(obj)
for _, fData := range fieldValues {
if fData.PrimaryKey || fData.PGName == "-" {
continue
}
strKeys = strKeys + fData.PGName + ","
values = append(values, fData.Value)
strParams = strParams + "$" + strconv.Itoa(counter) + ","
counter++
}
query := " insert into " + dam.TableName + "(" + TrimSuffix(strKeys, ",") + ") values(" + TrimSuffix(strParams, ",") + ") RETURNING id;"
log.Debugf("insert query: %s, %+v\n", query, values)
var id string
err := dam.DB.QueryRow(query, values...).Scan(&id)
return id, err
}
func (dam *BasicDAM) ValidateUpdate(obj interface{}, json string) error {
fields := parseObjectData(obj)
for _, fData := range fields {
inpVal := gjson.Get(json, fData.JsonName)
inpEmpty := inpVal.Raw == ""
props := fData.FieldType.Tag.Get("props")
if !inpEmpty {
//something is provided
if !strings.Contains(props, "editable") {
return errors.Wrap(ErrInvalid, fmt.Sprintf("field: %s is not editable", fData.JsonName))
}
} else {
//provided data is Null
if strings.Contains(props, "editable") && strings.Contains(props, "notNull") {
return errors.Wrap(ErrInvalid, fmt.Sprintf("field: %s can not be empty/zero", fData.JsonName))
}
} //end of field logic
}
return nil
}
func (dam *BasicDAM) Update(id string, obj interface{}) error {
updateQuery := ""
counter := 1
values := make([]interface{}, 0)
fields := parseObjectData(obj)
for _, fData := range fields {
props := fData.FieldType.Tag.Get("props")
if fData.PGName == "-" || !strings.Contains(props, "editable") {
continue
}
updateQuery = updateQuery + fData.PGName + "=" + "$" + strconv.Itoa(counter) + ","
values = append(values, fData.Value)
counter++
}
values = append(values, id)
strQ := " update " + dam.TableName + " set " + TrimSuffix(updateQuery, ",")
strQ += " where id=" + "$" + strconv.Itoa(counter) + ";"
// log.Infof("update query is...%s, %+v", strQ, values)
_, err := dam.DB.Exec(strQ, values...)
return err
}
func (dam *BasicDAM) ValidatePatch(obj interface{}, json string) error {
fields := parseObjectData(obj)
for _, fData := range fields {
inpVal := gjson.Get(json, fData.JsonName)
inpEmpty := inpVal.Raw == ""
// log.Printf("checking field: %s, provided value: %s, empty?:%t ", typeField.Name, inpVal, inpEmpty)
props := fData.FieldType.Tag.Get("props")
if !inpEmpty {
//something is provided
if !strings.Contains(props, "editable") {
return errors.Wrap(ErrInvalid, fmt.Sprintf("field: %s is not editable", fData.JsonName))
}
}
}
return nil
}
func (dam *BasicDAM) Patch(id string, obj interface{}, json string) error {
updateQuery := ""
counter := 1
values := make([]interface{}, 0)
fields := parseObjectData(obj)
for _, fData := range fields {
log.Debugf("check fdata, %s, %s\n", fData.PGName, fData.JsonName)
if fData.PGName == "-" {
continue
}
inpVal := gjson.Get(json, fData.JsonName)
if inpVal.Raw == "" {
//filed is not provded to get updated
continue
}
updateQuery = updateQuery + fData.PGName + "=" + "$" + strconv.Itoa(counter) + ","
values = append(values, fData.Value)
counter++
}
values = append(values, id)
strQ := " update " + dam.TableName + " set " + TrimSuffix(updateQuery, ",")
strQ += " where id=" + "$" + strconv.Itoa(counter) + ";"
log.Debugf("patch query is...%s, %+v\n", strQ, values)
_, err := dam.DB.Exec(strQ, values...)
return err
}