Files
sqlite3/func.go

176 lines
5.5 KiB
Go
Raw Normal View History

2023-06-28 17:32:55 +01:00
package sqlite3
import (
"context"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/tetratelabs/wazero/api"
)
2023-07-04 11:16:29 +01:00
// AnyCollationNeeded registers a fake collating function
// for any unknown collating sequence.
// The fake collating function works like BINARY.
//
2023-09-01 02:26:30 +01:00
// This can be used to load schemas that contain
2023-07-04 11:16:29 +01:00
// one or more unknown collating sequences.
func (c *Conn) AnyCollationNeeded() {
c.call(c.api.anyCollation, uint64(c.handle), 0, 0)
}
2023-06-30 02:49:21 +01:00
// CreateCollation defines a new collating sequence.
//
// https://www.sqlite.org/c3ref/create_collation.html
func (c *Conn) CreateCollation(name string, fn func(a, b []byte) int) error {
namePtr := c.arena.string(name)
funcPtr := util.AddHandle(c.ctx, fn)
r := c.call(c.api.createCollation,
uint64(c.handle), uint64(namePtr), uint64(funcPtr))
if err := c.error(r); err != nil {
util.DelHandle(c.ctx, funcPtr)
return err
}
return nil
}
2023-07-03 15:45:16 +01:00
// CreateFunction defines a new scalar SQL function.
2023-07-01 00:15:28 +01:00
//
// https://www.sqlite.org/c3ref/create_function.html
func (c *Conn) CreateFunction(name string, nArg int, flag FunctionFlag, fn func(ctx Context, arg ...Value)) error {
namePtr := c.arena.string(name)
funcPtr := util.AddHandle(c.ctx, fn)
r := c.call(c.api.createFunction,
uint64(c.handle), uint64(namePtr), uint64(nArg),
uint64(flag), uint64(funcPtr))
return c.error(r)
}
2023-07-03 15:45:16 +01:00
// CreateWindowFunction defines a new aggregate or aggregate window SQL function.
// If fn returns a [WindowFunction], then an aggregate window function is created.
2023-10-25 12:56:52 +01:00
// If fn returns an [io.Closer], it will be called to free resources.
2023-07-01 15:14:45 +01:00
//
// https://www.sqlite.org/c3ref/create_function.html
2023-07-03 15:45:16 +01:00
func (c *Conn) CreateWindowFunction(name string, nArg int, flag FunctionFlag, fn func() AggregateFunction) error {
2023-07-01 15:14:45 +01:00
call := c.api.createAggregate
namePtr := c.arena.string(name)
funcPtr := util.AddHandle(c.ctx, fn)
2023-07-03 15:45:16 +01:00
if _, ok := fn().(WindowFunction); ok {
2023-07-01 15:14:45 +01:00
call = c.api.createWindow
}
r := c.call(call,
uint64(c.handle), uint64(namePtr), uint64(nArg),
uint64(flag), uint64(funcPtr))
return c.error(r)
}
// AggregateFunction is the interface an aggregate function should implement.
//
// https://www.sqlite.org/appfunc.html
type AggregateFunction interface {
2023-07-03 17:08:16 +01:00
// Step is invoked to add a row to the current window.
// The function arguments, if any, corresponding to the row being added are passed to Step.
2023-07-01 15:14:45 +01:00
Step(ctx Context, arg ...Value)
2023-07-03 17:08:16 +01:00
2023-10-25 12:56:52 +01:00
// Value is invoked to return the current (or final) value of the aggregate.
2023-07-03 17:08:16 +01:00
Value(ctx Context)
2023-07-01 15:14:45 +01:00
}
// WindowFunction is the interface an aggregate window function should implement.
//
// https://www.sqlite.org/windowfunctions.html
type WindowFunction interface {
AggregateFunction
2023-07-03 17:08:16 +01:00
// Inverse is invoked to remove the oldest presently aggregated result of Step from the current window.
// The function arguments, if any, are those passed to Step for the row being removed.
2023-07-01 15:14:45 +01:00
Inverse(ctx Context, arg ...Value)
}
func callbackDestroy(ctx context.Context, mod api.Module, pApp uint32) {
2023-07-01 00:15:28 +01:00
util.DelHandle(ctx, pApp)
2023-06-30 02:49:21 +01:00
}
2023-06-28 17:32:55 +01:00
2023-07-01 15:14:45 +01:00
func callbackCompare(ctx context.Context, mod api.Module, pApp, nKey1, pKey1, nKey2, pKey2 uint32) uint32 {
2023-07-01 00:15:28 +01:00
fn := util.GetHandle(ctx, pApp).(func(a, b []byte) int)
2023-06-30 02:49:21 +01:00
return uint32(fn(util.View(mod, pKey1, uint64(nKey1)), util.View(mod, pKey2, uint64(nKey2))))
2023-06-28 17:32:55 +01:00
}
2023-07-01 15:14:45 +01:00
func callbackFunc(ctx context.Context, mod api.Module, pCtx, nArg, pArg uint32) {
2023-11-06 18:11:47 +00:00
db := ctx.Value(connKey{}).(*Conn)
fn := callbackHandle(db, pCtx).(func(ctx Context, arg ...Value))
fn(Context{db, pCtx}, callbackArgs(db, nArg, pArg)...)
2023-07-01 15:14:45 +01:00
}
func callbackStep(ctx context.Context, mod api.Module, pCtx, nArg, pArg uint32) {
2023-11-06 18:11:47 +00:00
db := ctx.Value(connKey{}).(*Conn)
fn := callbackAggregate(db, pCtx, nil).(AggregateFunction)
fn.Step(Context{db, pCtx}, callbackArgs(db, nArg, pArg)...)
2023-07-01 00:15:28 +01:00
}
2023-06-28 17:32:55 +01:00
2023-07-01 15:14:45 +01:00
func callbackFinal(ctx context.Context, mod api.Module, pCtx uint32) {
2023-07-03 15:45:16 +01:00
var handle uint32
2023-11-06 18:11:47 +00:00
db := ctx.Value(connKey{}).(*Conn)
fn := callbackAggregate(db, pCtx, &handle).(AggregateFunction)
fn.Value(Context{db, pCtx})
2023-07-03 17:08:16 +01:00
if err := util.DelHandle(ctx, handle); err != nil {
2023-11-06 18:11:47 +00:00
Context{db, pCtx}.ResultError(err)
2023-07-03 17:08:16 +01:00
}
2023-07-01 15:14:45 +01:00
}
2023-06-28 17:32:55 +01:00
2023-07-01 15:14:45 +01:00
func callbackValue(ctx context.Context, mod api.Module, pCtx uint32) {
2023-11-06 18:11:47 +00:00
db := ctx.Value(connKey{}).(*Conn)
fn := callbackAggregate(db, pCtx, nil).(AggregateFunction)
fn.Value(Context{db, pCtx})
2023-07-01 15:14:45 +01:00
}
2023-06-28 17:32:55 +01:00
2023-07-01 15:14:45 +01:00
func callbackInverse(ctx context.Context, mod api.Module, pCtx, nArg, pArg uint32) {
2023-11-06 18:11:47 +00:00
db := ctx.Value(connKey{}).(*Conn)
fn := callbackAggregate(db, pCtx, nil).(WindowFunction)
fn.Inverse(Context{db, pCtx}, callbackArgs(db, nArg, pArg)...)
2023-07-01 15:14:45 +01:00
}
2023-06-28 17:32:55 +01:00
2023-11-06 18:11:47 +00:00
func callbackHandle(db *Conn, pCtx uint32) any {
pApp := uint32(db.call(db.api.userData, uint64(pCtx)))
return util.GetHandle(db.ctx, pApp)
2023-07-03 15:45:16 +01:00
}
2023-11-06 18:11:47 +00:00
func callbackAggregate(db *Conn, pCtx uint32, close *uint32) any {
2023-07-03 17:08:16 +01:00
// On close, we're getting rid of the handle.
// Don't allocate space to store it.
2023-07-03 15:45:16 +01:00
var size uint64
2023-07-03 17:08:16 +01:00
if close == nil {
2023-07-03 15:45:16 +01:00
size = ptrlen
}
2023-11-06 18:11:47 +00:00
ptr := uint32(db.call(db.api.aggregateCtx, uint64(pCtx), size))
2023-07-03 15:45:16 +01:00
2023-07-03 17:08:16 +01:00
// Try loading the handle, if we already have one, or want a new one.
if ptr != 0 || size != 0 {
2023-11-06 18:11:47 +00:00
if handle := util.ReadUint32(db.mod, ptr); handle != 0 {
fn := util.GetHandle(db.ctx, handle)
2023-07-03 17:08:16 +01:00
if close != nil {
*close = handle
2023-07-03 15:45:16 +01:00
}
if fn != nil {
return fn
}
}
}
2023-07-03 17:08:16 +01:00
// Create a new aggregate and store the handle.
2023-11-06 18:11:47 +00:00
fn := callbackHandle(db, pCtx).(func() AggregateFunction)()
2023-07-03 15:45:16 +01:00
if ptr != 0 {
2023-11-06 18:11:47 +00:00
util.WriteUint32(db.mod, ptr, util.AddHandle(db.ctx, fn))
2023-07-03 15:45:16 +01:00
}
return fn
}
2023-11-06 18:11:47 +00:00
func callbackArgs(db *Conn, nArg, pArg uint32) []Value {
2023-07-01 15:14:45 +01:00
args := make([]Value, nArg)
for i := range args {
args[i] = Value{
2023-11-06 18:11:47 +00:00
sqlite: db.sqlite,
handle: util.ReadUint32(db.mod, pArg+ptrlen*uint32(i)),
2023-07-01 15:14:45 +01:00
}
}
return args
}