Files
sqlite3/ext/stats/boolean.go

47 lines
751 B
Go
Raw Permalink Normal View History

2024-06-06 16:41:20 +01:00
package stats
import "github.com/ncruces/go-sqlite3"
const (
every = iota
some
)
2025-03-08 14:07:43 +00:00
func newBoolean(kind int) sqlite3.AggregateConstructor {
2024-06-06 16:41:20 +01:00
return func() sqlite3.AggregateFunction { return &boolean{kind: kind} }
}
type boolean struct {
count int
total int
kind int
}
func (b *boolean) Value(ctx sqlite3.Context) {
if b.kind == every {
ctx.ResultBool(b.count == b.total)
} else {
ctx.ResultBool(b.count > 0)
}
}
func (b *boolean) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {
2024-10-17 08:15:44 +01:00
a := arg[0]
if a.Bool() {
2024-06-06 16:41:20 +01:00
b.count++
}
2024-10-17 08:15:44 +01:00
if a.Type() != sqlite3.NULL {
b.total++
}
2024-06-06 16:41:20 +01:00
}
func (b *boolean) Inverse(ctx sqlite3.Context, arg ...sqlite3.Value) {
2024-10-17 08:15:44 +01:00
a := arg[0]
if a.Bool() {
2024-06-06 16:41:20 +01:00
b.count--
}
2024-10-17 08:15:44 +01:00
if a.Type() != sqlite3.NULL {
b.total--
}
2024-06-06 16:41:20 +01:00
}