-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdivision.go
49 lines (43 loc) · 1.14 KB
/
subdivision.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
package db
import (
"context"
"github.com/cockroachdb/errors"
"github.com/jmoiron/sqlx"
"github.com/shihanng/country-codes/extract"
)
type SubdivisionTable struct {
db *sqlx.DB
}
func NewSubdivisionTable(db *sqlx.DB) *SubdivisionTable {
return &SubdivisionTable{db: db}
}
func (t *SubdivisionTable) UpsertSubdivision(ctx context.Context, subdivision extract.Subdivision) error {
_, err := t.db.NamedExecContext(ctx, `
INSERT INTO subdivisions (
country_code
, code_31662
, name
, local_variant
, language_code
, romanization_system
, parent_subdivision
) VALUES (
:country_code
, :code_31662
, :name
, :local_variant
, :language_code
, :romanization_system
, :parent_subdivision
) ON CONFLICT (country_code, code_31662) DO UPDATE SET
name = excluded.name
, local_variant = excluded.local_variant
, language_code = excluded.language_code
, romanization_system = excluded.romanization_system
, parent_subdivision = excluded.parent_subdivision
;`, subdivision)
if err != nil {
return errors.Wrap(err, "db: upsert subdivision")
}
return nil
}