2022-06-18 09:45:45 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-12-04 10:22:49 +00:00
|
|
|
"log/slog"
|
2022-06-18 09:45:45 +00:00
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/grammar"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/validation"
|
2022-06-18 09:45:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ validation.FromCall[*sql.Rows] = (*RowsValidator)(nil)
|
|
|
|
|
|
|
|
type RowsValidator struct {
|
|
|
|
Want int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RowsValidator) Validate(in *sql.Rows) error {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Debug("Validate rows count",
|
|
|
|
slog.Int("expected_row_count", r.Want),
|
|
|
|
)
|
|
|
|
|
2022-06-18 09:45:45 +00:00
|
|
|
readRows := 0
|
|
|
|
for in.Next() {
|
|
|
|
readRows++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := in.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if readRows != r.Want {
|
|
|
|
return fmt.Errorf("expected %d rows but got %d", r.Want, readRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RowsValidator) UnmarshalCall(c grammar.Call) error {
|
|
|
|
if err := grammar.ValidateParameterCount(c.Params, 1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r.Want, err = c.Params[0].AsInt()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|