2024-10-03 07:32:48 +02:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-12-29 09:32:49 +01:00
|
|
|
"errors"
|
2024-10-03 07:32:48 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalID struct {
|
2025-01-13 09:13:48 +01:00
|
|
|
tx *storage.Tx
|
2024-10-03 07:32:48 +02:00
|
|
|
}
|
|
|
|
|
2024-12-29 09:32:49 +01:00
|
|
|
func (l *LocalID) FindOne(lid int) (string, error) {
|
|
|
|
var id string
|
2025-01-13 09:13:48 +01:00
|
|
|
err := l.tx.QueryRow(`
|
2024-12-29 09:32:49 +01:00
|
|
|
SELECT id
|
|
|
|
FROM localids
|
|
|
|
WHERE local_id = ?
|
|
|
|
`, lid).Scan(&id)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return "", storage.ErrNotFound
|
|
|
|
case err != nil:
|
|
|
|
return "", fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2024-10-03 07:32:48 +02:00
|
|
|
func (l *LocalID) FindAll() (map[string]int, error) {
|
2025-01-13 09:13:48 +01:00
|
|
|
rows, err := l.tx.Query(`
|
2024-10-03 07:32:48 +02:00
|
|
|
SELECT id, local_id
|
|
|
|
FROM localids
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
result := make(map[string]int)
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var id string
|
|
|
|
var localID int
|
|
|
|
if err := rows.Scan(&id, &localID); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
result[id] = localID
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
func (l *LocalID) FindOrNext(id string) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2024-10-03 07:32:48 +02:00
|
|
|
func (l *LocalID) Next() (int, error) {
|
|
|
|
idMap, err := l.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
cur := make([]int, 0, len(idMap))
|
|
|
|
for _, localID := range idMap {
|
|
|
|
cur = append(cur, localID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage.NextLocalID(cur), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LocalID) Store(id string, localID int) error {
|
2025-01-13 09:13:48 +01:00
|
|
|
if _, err := l.tx.Exec(`
|
2024-10-03 07:32:48 +02:00
|
|
|
INSERT INTO localids
|
|
|
|
(id, local_id)
|
|
|
|
VALUES
|
|
|
|
(? ,?)`, id, localID); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LocalID) Delete(id string) error {
|
2025-01-13 09:13:48 +01:00
|
|
|
result, err := l.tx.Exec(`
|
2024-10-03 07:32:48 +02:00
|
|
|
DELETE FROM localids
|
|
|
|
WHERE id = ?`, id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return storage.ErrNotFound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|