resolveLabel resolves a book label conflict by repeatedly appending an increasing integer to the label until it finds a unique label. It returns the first non-conflicting label.
(tx *database.DB, label string)
| 248 | // resolveLabel resolves a book label conflict by repeatedly appending an increasing integer |
| 249 | // to the label until it finds a unique label. It returns the first non-conflicting label. |
| 250 | func resolveLabel(tx *database.DB, label string) (string, error) { |
| 251 | var ret string |
| 252 | |
| 253 | for i := 2; ; i++ { |
| 254 | ret = fmt.Sprintf("%s_%d", label, i) |
| 255 | |
| 256 | var cnt int |
| 257 | if err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", ret).Scan(&cnt); err != nil { |
| 258 | return "", errors.Wrapf(err, "checking availability of label %s", ret) |
| 259 | } |
| 260 | |
| 261 | if cnt == 0 { |
| 262 | break |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return ret, nil |
| 267 | } |
| 268 | |
| 269 | // mergeBook inserts or updates the given book in the local database. |
| 270 | // If a book with a duplicate label exists locally, it renames the duplicate by appending a number. |