This commit is contained in:
jdl 2024-11-18 17:01:17 +01:00
parent 687206bb45
commit 3b23aeabbe
2 changed files with 50 additions and 26 deletions

View File

@ -5,6 +5,24 @@ import (
"iter" "iter"
) )
var (
ErrConstraint = errors.New("constraint violation")
ErrNotFound = errors.New("not found")
)
func translateError(err error) error {
if err == nil {
return nil
}
if e, ok := err.(sqlite3.Error); ok && e.Code == 19 {
return ErrConstraint
}
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
return err
}
type TX interface { type TX interface {
Exec(query string, args ...any) (sql.Result, error) Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error) Query(query string, args ...any) (*sql.Rows, error)
@ -36,7 +54,7 @@ func {{.Type}}_Insert(
} }
_, err = tx.Exec("{{.InsertQuery}}", {{.InsertArgs}}) _, err = tx.Exec("{{.InsertQuery}}", {{.InsertArgs}})
return err return translateError(err)
} }
{{- end}} {{/* if not .NoInsert */}} {{- end}} {{/* if not .NoInsert */}}
@ -48,27 +66,29 @@ func {{.Type}}_Insert(
func {{.Type}}_Update( func {{.Type}}_Update(
tx TX, tx TX,
row *{{.Type}}, row *{{.Type}},
) (found bool, err error) { ) (err error) {
{{.Type}}_Sanitize(row) {{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil { if err = {{.Type}}_Validate(row); err != nil {
return false, err return err
} }
result, err := tx.Exec("{{.UpdateQuery}}", {{.UpdateArgs}}) result, err := tx.Exec("{{.UpdateQuery}}", {{.UpdateArgs}})
if err != nil { if err != nil {
return false, err return translateError(err)
} }
n, err := result.RowsAffected() n, err := result.RowsAffected()
if err != nil { if err != nil {
panic(err) panic(err)
} }
switch n {
if n > 1 { case 0:
return ErrNotFound
case 1:
return nil
default:
panic("multiple rows updated") panic("multiple rows updated")
} }
return n != 0, nil
} }
{{- end}} {{- end}}
@ -77,27 +97,29 @@ func {{.Type}}_Update(
func {{.Type}}_UpdateFull( func {{.Type}}_UpdateFull(
tx TX, tx TX,
row *{{.Type}}, row *{{.Type}},
) (found bool, err error) { ) (err error) {
{{.Type}}_Sanitize(row) {{.Type}}_Sanitize(row)
if err = {{.Type}}_Validate(row); err != nil { if err = {{.Type}}_Validate(row); err != nil {
return false, err return err
} }
result, err := tx.Exec("{{.UpdateFullQuery}}", {{.UpdateFullArgs}}) result, err := tx.Exec("{{.UpdateFullQuery}}", {{.UpdateFullArgs}})
if err != nil { if err != nil {
return false, err return translateError(err)
} }
n, err := result.RowsAffected() n, err := result.RowsAffected()
if err != nil { if err != nil {
panic(err) panic(err)
} }
switch n {
if n > 1 { case 0:
return ErrNotFound
case 1:
return nil
default:
panic("multiple rows updated") panic("multiple rows updated")
} }
return n != 0, nil
} }
{{- end}} {{- end}}
@ -109,22 +131,24 @@ func {{.Type}}_UpdateFull(
func {{.Type}}_Delete( func {{.Type}}_Delete(
tx TX, tx TX,
{{.PKFunctionArgs -}} {{.PKFunctionArgs -}}
) (found bool, err error) { ) (err error) {
result, err := tx.Exec("{{.DeleteQuery}}", {{.DeleteArgs}}) result, err := tx.Exec("{{.DeleteQuery}}", {{.DeleteArgs}})
if err != nil { if err != nil {
return false, err return translateError(err)
} }
n, err := result.RowsAffected() n, err := result.RowsAffected()
if err != nil { if err != nil {
panic(err) panic(err)
} }
switch n {
if n > 1 { case 0:
return ErrNotFound
case 1:
return nil
default:
panic("multiple rows deleted") panic("multiple rows deleted")
} }
return n != 0, nil
} }
{{- end}} {{- end}}
@ -138,7 +162,7 @@ func {{.Type}}_Get(
) { ) {
row = &{{.Type}}{} row = &{{.Type}}{}
r := tx.QueryRow("{{.GetQuery}}", {{.DeleteArgs}}) r := tx.QueryRow("{{.GetQuery}}", {{.DeleteArgs}})
err = r.Scan({{.ScanArgs}}) err = translateError(r.Scan({{.ScanArgs}}))
return return
} }
@ -153,7 +177,7 @@ func {{.Type}}_GetWhere(
) { ) {
row = &{{.Type}}{} row = &{{.Type}}{}
r := tx.QueryRow(query, args...) r := tx.QueryRow(query, args...)
err = r.Scan({{.ScanArgs}}) err = translateError(r.Scan({{.ScanArgs}}))
return return
} }
@ -177,7 +201,7 @@ func {{.Type}}_Iterate(
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
row := &{{.Type}}{} row := &{{.Type}}{}
err := rows.Scan({{.ScanArgs}}) err := translateError(rows.Scan({{.ScanArgs}}))
if !yield(row, err) { if !yield(row, err) {
return return
} }

View File

@ -2,7 +2,7 @@ package sqliteutil
import "github.com/mattn/go-sqlite3" import "github.com/mattn/go-sqlite3"
func ErrIsDuplicate(err error) bool { func ErrIsConstraint(err error) bool {
e, ok := err.(sqlite3.Error) e, ok := err.(sqlite3.Error)
return ok && e.Code == 19 return ok && e.Code == 19
} }