2023-06-28 17:32:55 +01:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-01-10 16:38:31 +00:00
|
|
|
"sync"
|
2023-06-28 17:32:55 +01:00
|
|
|
|
|
|
|
|
"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() {
|
2023-11-30 17:52:35 +00:00
|
|
|
c.call("sqlite3_anycollseq_init", uint64(c.handle), 0, 0)
|
2023-07-04 11:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-30 02:49:21 +01:00
|
|
|
// CreateCollation defines a new collating sequence.
|
|
|
|
|
//
|
2023-11-09 16:35:45 +00:00
|
|
|
// https://sqlite.org/c3ref/create_collation.html
|
2023-06-30 02:49:21 +01:00
|
|
|
func (c *Conn) CreateCollation(name string, fn func(a, b []byte) int) error {
|
2023-11-29 10:38:03 +00:00
|
|
|
defer c.arena.mark()()
|
2023-06-30 02:49:21 +01:00
|
|
|
namePtr := c.arena.string(name)
|
|
|
|
|
funcPtr := util.AddHandle(c.ctx, fn)
|
2023-11-30 17:52:35 +00:00
|
|
|
r := c.call("sqlite3_create_collation_go",
|
2023-06-30 02:49:21 +01:00
|
|
|
uint64(c.handle), uint64(namePtr), uint64(funcPtr))
|
2023-11-14 13:56:27 +00:00
|
|
|
return c.error(r)
|
2023-06-30 02:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 15:45:16 +01:00
|
|
|
// CreateFunction defines a new scalar SQL function.
|
2023-07-01 00:15:28 +01:00
|
|
|
//
|
2023-11-09 16:35:45 +00:00
|
|
|
// https://sqlite.org/c3ref/create_function.html
|
2023-12-04 12:37:53 +00:00
|
|
|
func (c *Conn) CreateFunction(name string, nArg int, flag FunctionFlag, fn ScalarFunction) error {
|
2023-11-29 10:38:03 +00:00
|
|
|
defer c.arena.mark()()
|
2023-07-01 00:15:28 +01:00
|
|
|
namePtr := c.arena.string(name)
|
|
|
|
|
funcPtr := util.AddHandle(c.ctx, fn)
|
2023-11-30 17:52:35 +00:00
|
|
|
r := c.call("sqlite3_create_function_go",
|
2023-07-01 00:15:28 +01:00
|
|
|
uint64(c.handle), uint64(namePtr), uint64(nArg),
|
|
|
|
|
uint64(flag), uint64(funcPtr))
|
|
|
|
|
return c.error(r)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 12:37:53 +00:00
|
|
|
// ScalarFunction is the type of a scalar SQL function.
|
2024-01-10 16:38:31 +00:00
|
|
|
// Implementations must not retain arg.
|
2023-12-04 12:37:53 +00:00
|
|
|
type ScalarFunction func(ctx Context, arg ...Value)
|
|
|
|
|
|
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
|
|
|
//
|
2023-11-09 16:35:45 +00:00
|
|
|
// https://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-11-29 10:38:03 +00:00
|
|
|
defer c.arena.mark()()
|
2023-11-30 17:52:35 +00:00
|
|
|
call := "sqlite3_create_aggregate_function_go"
|
2023-07-01 15:14:45 +01:00
|
|
|
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-11-30 17:52:35 +00:00
|
|
|
call = "sqlite3_create_window_function_go"
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
//
|
2023-11-09 16:35:45 +00:00
|
|
|
// https://sqlite.org/appfunc.html
|
2023-07-01 15:14:45 +01:00
|
|
|
type AggregateFunction interface {
|
2023-07-03 17:08:16 +01:00
|
|
|
// Step is invoked to add a row to the current window.
|
2024-01-10 16:38:31 +00:00
|
|
|
// The function arguments, if any, corresponding to the row being added, are passed to Step.
|
|
|
|
|
// Implementations must not retain arg.
|
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.
|
|
|
|
|
//
|
2023-11-09 16:35:45 +00:00
|
|
|
// https://sqlite.org/windowfunctions.html
|
2023-07-01 15:14:45 +01:00
|
|
|
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.
|
2024-01-10 16:38:31 +00:00
|
|
|
// Implementations must not retain arg.
|
2023-07-01 15:14:45 +01:00
|
|
|
Inverse(ctx Context, arg ...Value)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-08 19:23:32 +00:00
|
|
|
// OverloadFunction overloads a function for a virtual table.
|
|
|
|
|
//
|
|
|
|
|
// https://sqlite.org/c3ref/overload_function.html
|
|
|
|
|
func (c *Conn) OverloadFunction(name string, nArg int) error {
|
|
|
|
|
defer c.arena.mark()()
|
|
|
|
|
namePtr := c.arena.string(name)
|
|
|
|
|
r := c.call("sqlite3_overload_function",
|
|
|
|
|
uint64(c.handle), uint64(namePtr), uint64(nArg))
|
|
|
|
|
return c.error(r)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 01:16:38 +00:00
|
|
|
func destroyCallback(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-11-16 01:16:38 +00:00
|
|
|
func compareCallback(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
|
|
|
}
|
|
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func funcCallback(ctx context.Context, mod api.Module, pCtx, pApp, nArg, pArg uint32) {
|
2024-01-10 16:38:31 +00:00
|
|
|
args := getFuncArgs()
|
|
|
|
|
defer putFuncArgs(args)
|
2023-11-06 18:11:47 +00:00
|
|
|
db := ctx.Value(connKey{}).(*Conn)
|
2024-01-11 02:18:12 +00:00
|
|
|
fn := util.GetHandle(db.ctx, pApp).(ScalarFunction)
|
2024-01-10 16:38:31 +00:00
|
|
|
callbackArgs(db, args[:nArg], pArg)
|
|
|
|
|
fn(Context{db, pCtx}, args[:nArg]...)
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func stepCallback(ctx context.Context, mod api.Module, pCtx, pAgg, pApp, nArg, pArg uint32) {
|
2024-01-10 16:38:31 +00:00
|
|
|
args := getFuncArgs()
|
|
|
|
|
defer putFuncArgs(args)
|
2023-11-06 18:11:47 +00:00
|
|
|
db := ctx.Value(connKey{}).(*Conn)
|
2024-01-10 16:38:31 +00:00
|
|
|
callbackArgs(db, args[:nArg], pArg)
|
2024-01-11 02:18:12 +00:00
|
|
|
fn, _ := callbackAggregate(db, pAgg, pApp)
|
2024-01-10 16:38:31 +00:00
|
|
|
fn.Step(Context{db, pCtx}, args[:nArg]...)
|
2023-07-01 00:15:28 +01:00
|
|
|
}
|
2023-06-28 17:32:55 +01:00
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func finalCallback(ctx context.Context, mod api.Module, pCtx, pAgg, pApp uint32) {
|
2023-11-06 18:11:47 +00:00
|
|
|
db := ctx.Value(connKey{}).(*Conn)
|
2024-01-11 02:18:12 +00:00
|
|
|
fn, handle := callbackAggregate(db, pAgg, pApp)
|
2023-11-06 18:11:47 +00:00
|
|
|
fn.Value(Context{db, pCtx})
|
2024-01-11 02:18:12 +00:00
|
|
|
util.DelHandle(ctx, handle)
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
2023-06-28 17:32:55 +01:00
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func valueCallback(ctx context.Context, mod api.Module, pCtx, pAgg uint32) {
|
2023-11-06 18:11:47 +00:00
|
|
|
db := ctx.Value(connKey{}).(*Conn)
|
2024-01-11 02:18:12 +00:00
|
|
|
fn := util.GetHandle(db.ctx, pAgg).(AggregateFunction)
|
2023-11-06 18:11:47 +00:00
|
|
|
fn.Value(Context{db, pCtx})
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
2023-06-28 17:32:55 +01:00
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func inverseCallback(ctx context.Context, mod api.Module, pCtx, pAgg, nArg, pArg uint32) {
|
2024-01-10 16:38:31 +00:00
|
|
|
args := getFuncArgs()
|
|
|
|
|
defer putFuncArgs(args)
|
2023-11-06 18:11:47 +00:00
|
|
|
db := ctx.Value(connKey{}).(*Conn)
|
2024-01-10 16:38:31 +00:00
|
|
|
callbackArgs(db, args[:nArg], pArg)
|
2024-01-11 02:18:12 +00:00
|
|
|
fn := util.GetHandle(db.ctx, pAgg).(WindowFunction)
|
2024-01-10 16:38:31 +00:00
|
|
|
fn.Inverse(Context{db, pCtx}, args[:nArg]...)
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
2023-06-28 17:32:55 +01:00
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
func callbackAggregate(db *Conn, pAgg, pApp uint32) (AggregateFunction, uint32) {
|
|
|
|
|
if pApp == 0 {
|
|
|
|
|
handle := util.ReadUint32(db.mod, pAgg)
|
|
|
|
|
return util.GetHandle(db.ctx, handle).(AggregateFunction), handle
|
2023-07-03 15:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 02:18:12 +00:00
|
|
|
// We need to create the aggregate.
|
|
|
|
|
fn := util.GetHandle(db.ctx, pApp).(func() AggregateFunction)()
|
|
|
|
|
handle := util.AddHandle(db.ctx, fn)
|
|
|
|
|
if pAgg != 0 {
|
|
|
|
|
util.WriteUint32(db.mod, pAgg, handle)
|
2023-07-03 15:45:16 +01:00
|
|
|
}
|
2024-01-11 02:18:12 +00:00
|
|
|
return fn, handle
|
2023-07-03 15:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-10 16:38:31 +00:00
|
|
|
func callbackArgs(db *Conn, arg []Value, pArg uint32) {
|
|
|
|
|
for i := range arg {
|
|
|
|
|
arg[i] = Value{
|
2024-01-08 19:23:32 +00:00
|
|
|
c: db,
|
2023-11-06 18:11:47 +00:00
|
|
|
handle: util.ReadUint32(db.mod, pArg+ptrlen*uint32(i)),
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 16:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var funcArgsPool sync.Pool
|
|
|
|
|
|
|
|
|
|
func putFuncArgs(p *[_MAX_FUNCTION_ARG]Value) {
|
|
|
|
|
funcArgsPool.Put(p)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getFuncArgs() *[_MAX_FUNCTION_ARG]Value {
|
|
|
|
|
if p := funcArgsPool.Get(); p == nil {
|
|
|
|
|
return new([_MAX_FUNCTION_ARG]Value)
|
|
|
|
|
} else {
|
|
|
|
|
return p.(*[_MAX_FUNCTION_ARG]Value)
|
|
|
|
|
}
|
2023-07-01 15:14:45 +01:00
|
|
|
}
|