1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Package kenro registers kenro's spatial SQL functions — PostGIS-compatible
// ST_ functions, GeoPackage R-tree maintenance, CRS transform, H3, MVT — on
// the pure-Go SQLite driver modernc.org/sqlite.
//
// modernc.org/sqlite cannot load a native SQLite extension, so kenro's Rust
// core runs as a WebAssembly module inside [wazero] and is wired in as
// user-defined functions. Both halves are pure Go: a binary built with
// CGO_ENABLED=0 keeps working, which is the whole reason to be on
// modernc.org/sqlite in the first place.
//
// if err := kenro.Register(); err != nil { ... }
// db, err := sql.Open("sqlite", "parks.gpkg")
//
// Register affects connections opened afterwards — modernc.org/sqlite's
// user-defined-function registry is process-global — so call it once during
// start-up, before opening a database.
//
// [wazero]: https://wazero.io
package kenro
import (
"context"
"fmt"
"sync"
"github.com/reearth/kenro/go/internal/wasmbin"
)
// Option configures Register.
type Option func(*config)
type config struct
// UnionMode selects which ST_Union kenro registers.
//
// SQLite itself is happy to hold a scalar ST_Union(a, b) and an aggregate
// ST_Union(geom) under one name, because it keys functions by (name, arity).
// modernc.org/sqlite's registry keys them by name alone, so on this driver
// the two forms are mutually exclusive. Whichever form is not registered
// fails loudly, naming this option — it never silently does the wrong thing.
type UnionMode int
const (
// UnionAggregate registers ST_Union(geom) — the dissolve aggregate. This
// is the default. ST_Union(a, b) then returns an error.
UnionAggregate UnionMode = iota
// UnionScalar registers ST_Union(a, b) — the two-geometry overlay.
// ST_Union(geom) as an aggregate then returns an error.
UnionScalar
)
// WithModule replaces the embedded wasm module, e.g. with a smaller tier
// built by scripts/build-go-wasm.sh. Functions missing from the module
// register as stubs that name the absent cargo feature.
func WithModule(wasm []byte) Option
// WithUnionMode selects the ST_Union form to register. See [UnionMode].
func WithUnionMode(m UnionMode) Option
var (
registerOnce sync.Mutex
registered bool
)
// Register registers every kenro spatial function on the modernc.org/sqlite
// driver, for connections opened after it returns. It is safe to call
// concurrently but registers only once; later calls are no-ops.
func Register(opts ...Option) error
// RegisterContext is [Register] with a context governing wasm compilation.
func RegisterContext(ctx context.Context, opts ...Option) error
// Registered reports whether kenro's functions have been registered.
func Registered() bool
func errf(format string, a ...any) error
// defaultModule is the embedded wasm artifact, used when WithModule is not
// given.
func defaultModule() []byte