forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_store_add.go
55 lines (48 loc) · 1.07 KB
/
db_store_add.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
package account
import "github.com/lib/pq"
func (s *DBStore) Add(a Account) error {
tx, err := s.DB.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
accountID := int64(0)
err = tx.Get(&accountID, `
INSERT INTO accounts (address)
VALUES ($1)
RETURNING id
`, a.Address)
if err != nil {
// 23505 is the PostgreSQL error for Unique Violation.
// See https://www.postgresql.org/docs/9.2/errcodes-appendix.html.
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
return ErrAlreadyExists
}
return err
}
for _, i := range a.Identities {
identityID := int64(0)
err = tx.Get(&identityID, `
INSERT INTO identities (account_id, role)
VALUES ($1, $2)
RETURNING id
`, accountID, i.Role)
if err != nil {
return err
}
for _, m := range i.AuthMethods {
_, err = tx.Exec(`
INSERT INTO auth_methods (account_id, identity_id, type_, value)
VALUES ($1, $2, $3, $4)
`, accountID, identityID, m.Type, m.Value)
if err != nil {
return err
}
}
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}