forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_issuer.go
39 lines (31 loc) · 1.09 KB
/
queries_issuer.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
package tickerdb
import (
"strings"
"github.com/aiblocks/go/services/ticker/internal/utils"
)
// InsertOrUpdateIssuer inserts an Issuer on the database (if new),
// or updates an existing one
func (s *TickerSession) InsertOrUpdateIssuer(issuer *Issuer, preserveFields []string) (id int32, err error) {
dbFields := getDBFieldTags(*issuer, true)
dbFieldsString := strings.Join(dbFields, ", ")
dbValues := getDBFieldValues(*issuer, true)
cleanPreservedFields := sanitizeFieldNames(preserveFields)
toUpdateFields := utils.SliceDiff(dbFields, cleanPreservedFields)
qs := "INSERT INTO issuers (" + dbFieldsString + ")"
qs += " VALUES (" + generatePlaceholders(dbValues) + ")"
qs += " " + createOnConflictFragment("public_key_unique", toUpdateFields)
qs += " RETURNING id;"
rows, err := s.QueryRaw(qs, dbValues...)
if err != nil {
return
}
for rows.Next() {
err = rows.Scan(&id)
}
return
}
// GetAllIssuers returns a slice with all issuers in the database
func (s *TickerSession) GetAllIssuers() (issuers []Issuer, err error) {
err = s.SelectRaw(&issuers, "SELECT * FROM issuers")
return
}