-
Notifications
You must be signed in to change notification settings - Fork 113
/
udf_utils.go
55 lines (43 loc) · 986 Bytes
/
udf_utils.go
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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"runtime"
"runtime/cgo"
"unsafe"
)
// Helpers for passing values to C and back.
type pinnedValue[T any] struct {
pinner *runtime.Pinner
value T
}
type unpinner interface {
unpin()
}
func (v pinnedValue[T]) unpin() {
v.pinner.Unpin()
}
func getPinned[T any](handle unsafe.Pointer) T {
h := *(*cgo.Handle)(handle)
return h.Value().(pinnedValue[T]).value
}
// Set error helpers.
func setBindError(info C.duckdb_bind_info, msg string) {
err := C.CString(msg)
defer C.duckdb_free(unsafe.Pointer(err))
C.duckdb_bind_set_error(info, err)
}
func setFuncError(function_info C.duckdb_function_info, msg string) {
err := C.CString(msg)
defer C.duckdb_free(unsafe.Pointer(err))
C.duckdb_scalar_function_set_error(function_info, err)
}
// Data deletion handlers.
//export udf_delete_callback
func udf_delete_callback(info unsafe.Pointer) {
h := (*cgo.Handle)(info)
h.Value().(unpinner).unpin()
h.Delete()
}