Sergiusz Bazanski | 1fad2e5 | 2019-08-01 20:16:27 +0200 | [diff] [blame] | 1 | package model |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func (s *sqlModel) UpdatePGPKey(ctx context.Context, key *PeerPGPKey) error { |
| 10 | q := ` |
| 11 | INSERT INTO peer_pgp_keys |
| 12 | (peer_id, fingerprint, time_created) |
| 13 | SELECT |
| 14 | peers.id, :fingerprint, :time_created |
| 15 | FROM peers |
| 16 | WHERE peers.asn = :asn |
| 17 | ON CONFLICT (peer_id) |
| 18 | DO UPDATE SET |
| 19 | fingerprint = :fingerprint, |
| 20 | time_created = :time_created |
| 21 | ` |
| 22 | data := &sqlPeerPGPKey{ |
| 23 | Fingerprint: key.Fingerprint, |
| 24 | ASN: fmt.Sprintf("%d", key.PeerASN), |
| 25 | TimeCreated: time.Now().UnixNano(), |
| 26 | } |
| 27 | if _, err := s.db.NamedExecContext(ctx, q, data); err != nil { |
| 28 | return fmt.Errorf("INSERT peer_pgp_keys: %v", err) |
| 29 | } |
| 30 | return nil |
| 31 | } |