diff options
Diffstat (limited to 'tools/importer/main.go')
-rw-r--r-- | tools/importer/main.go | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/tools/importer/main.go b/tools/importer/main.go index f0c0856..9caf287 100644 --- a/tools/importer/main.go +++ b/tools/importer/main.go | |||
@@ -81,8 +81,7 @@ func main() { | |||
81 | for rows.Next() { | 81 | for rows.Next() { |
82 | var id int | 82 | var id int |
83 | var name string | 83 | var name string |
84 | rows.Scan(&id) | 84 | rows.Scan(&id, &name) |
85 | rows.Scan(&name) | ||
86 | taglist[name] = id | 85 | taglist[name] = id |
87 | } | 86 | } |
88 | idmap := make(map[int]int) | 87 | idmap := make(map[int]int) |
@@ -90,8 +89,7 @@ func main() { | |||
90 | for rows.Next() { | 89 | for rows.Next() { |
91 | var sourceID int | 90 | var sourceID int |
92 | var grilID int | 91 | var grilID int |
93 | rows.Scan(&sourceID) | 92 | rows.Scan(&sourceID, &grilID) |
94 | rows.Scan(&grilID) | ||
95 | idmap[sourceID] = grilID | 93 | idmap[sourceID] = grilID |
96 | } | 94 | } |
97 | filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | 95 | filepath.Walk(path, func(path string, info os.FileInfo, err error) error { |
@@ -99,28 +97,16 @@ func main() { | |||
99 | return nil | 97 | return nil |
100 | } | 98 | } |
101 | g := r.Read(path) | 99 | g := r.Read(path) |
102 | for _, v := range g.Gril.Tags { | 100 | var dbID int |
103 | if _, ok := taglist[v]; ok { | 101 | var ok bool |
104 | continue | 102 | if dbID, ok = idmap[g.Gril.ID]; !ok { |
105 | } | ||
106 | var id int | ||
107 | row := db.QueryRow("INSERT INTO grilist.tags (name) VALUES ($1) RETURNING id;", v) | ||
108 | row.Scan(&id) | ||
109 | taglist[v] = id | ||
110 | } | ||
111 | if _, ok := idmap[g.Gril.ID]; !ok { | ||
112 | row := db.QueryRow("INSERT INTO grilist.grils (age) VALUES (NULL) RETURNING id;") | 103 | row := db.QueryRow("INSERT INTO grilist.grils (age) VALUES (NULL) RETURNING id;") |
113 | var dbID int | ||
114 | row.Scan(&dbID) | 104 | row.Scan(&dbID) |
115 | fmt.Printf( | 105 | fmt.Printf( |
116 | "Assigned %d from %s to %d\n", | 106 | "Assigned %d from %s to %d\n", |
117 | g.Gril.ID, | 107 | g.Gril.ID, |
118 | sourceType, | 108 | sourceType, |
119 | dbID) | 109 | dbID) |
120 | for _, v := range g.Gril.Tags { | ||
121 | tagID := taglist[v] | ||
122 | db.Exec("INSERT INTO grilist.grils_tags (gril_id, tag_id) VALUES ($1, $2);", dbID, tagID) | ||
123 | } | ||
124 | db.Exec(`INSERT INTO grilist.grils_id_mappings (gril_id, | 110 | db.Exec(`INSERT INTO grilist.grils_id_mappings (gril_id, |
125 | source, source_id) VALUES ($1, $2, $3);`, | 111 | source, source_id) VALUES ($1, $2, $3);`, |
126 | dbID, r.ID(), g.Gril.ID) | 112 | dbID, r.ID(), g.Gril.ID) |
@@ -164,6 +150,24 @@ func main() { | |||
164 | } | 150 | } |
165 | fmt.Printf("Inserted %s\n", g.Gril.RomajiName) | 151 | fmt.Printf("Inserted %s\n", g.Gril.RomajiName) |
166 | } | 152 | } |
153 | for _, v := range g.Gril.Tags { | ||
154 | if _, ok := taglist[v]; ok { | ||
155 | continue | ||
156 | } | ||
157 | var id int | ||
158 | row := db.QueryRow("INSERT INTO grilist.tags (name) VALUES ($1) RETURNING id;", v) | ||
159 | row.Scan(&id) | ||
160 | taglist[v] = id | ||
161 | } | ||
162 | _, err = db.Exec(`DELETE FROM grilist.grils_tags WHERE gril_id | ||
163 | = $1`, dbID) | ||
164 | LogErr(err) | ||
165 | for _, v := range g.Gril.Tags { | ||
166 | tagID := taglist[v] | ||
167 | _, err := db.Exec(`INSERT INTO grilist.grils_tags | ||
168 | (gril_id, tag_id) VALUES ($1, $2);`, dbID, tagID) | ||
169 | LogErr(err) | ||
170 | } | ||
167 | return nil | 171 | return nil |
168 | }) | 172 | }) |
169 | } | 173 | } |