-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsqlite.go
More file actions
1130 lines (1015 loc) · 34.2 KB
/
Copy pathsqlite.go
File metadata and controls
1130 lines (1015 loc) · 34.2 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run generator.go -full-path-comments
package sqlite // import "modernc.org/sqlite"
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"math/bits"
"net/url"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"modernc.org/libc"
"modernc.org/libc/sys/types"
sqlite3 "modernc.org/sqlite/lib"
)
var (
_ driver.Conn = (*conn)(nil)
_ driver.Driver = (*Driver)(nil)
//lint:ignore SA1019 TODO implement ExecerContext
_ driver.Execer = (*conn)(nil)
//lint:ignore SA1019 TODO implement QueryerContext
_ driver.Queryer = (*conn)(nil)
_ driver.Result = (*result)(nil)
_ driver.Rows = (*rows)(nil)
_ driver.RowsColumnTypeDatabaseTypeName = (*rows)(nil)
_ driver.RowsColumnTypeLength = (*rows)(nil)
_ driver.RowsColumnTypeNullable = (*rows)(nil)
_ driver.RowsColumnTypePrecisionScale = (*rows)(nil)
_ driver.RowsColumnTypeScanType = (*rows)(nil)
_ driver.Stmt = (*stmt)(nil)
_ driver.Tx = (*tx)(nil)
_ error = (*Error)(nil)
)
const (
driverName = "sqlite"
ptrSize = unsafe.Sizeof(uintptr(0))
sqliteLockedSharedcache = sqlite3.SQLITE_LOCKED | (1 << 8)
)
func init() {
sql.Register(driverName, newDriver())
sqlite3.PatchIssue199() // https://gitlab.com/cznic/sqlite/-/issues/199
}
// Inspired by mattn/go-sqlite3: https://github.com/mattn/go-sqlite3/blob/ab91e934/sqlite3.go#L210-L226
//
// These time.Parse formats handle formats 1 through 7 listed at https://www.sqlite.org/lang_datefunc.html.
var parseTimeFormats = []string{
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02T15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
}
// interruptOnDone sets up a goroutine to interrupt the provided db when the
// context is canceled, and returns a function the caller must defer so it
// doesn't interrupt after the caller finishes.
func interruptOnDone(
ctx context.Context,
c *conn,
done *int32,
) func() {
if done == nil {
var d int32
done = &d
}
// donemu prevents a TOCTOU logical race between checking the done flag and
// calling interrupt in the select statement below.
var donemu sync.Mutex
donech := make(chan struct{})
go func() {
select {
case <-ctx.Done():
// don't call interrupt if we were already done: it indicates that this
// call to exec is no longer running and we would be interrupting
// nothing, or even possibly an unrelated later call to exec.
donemu.Lock()
if atomic.CompareAndSwapInt32(done, 0, 1) {
c.interrupt(c.db)
}
donemu.Unlock()
case <-donech:
}
}()
// the caller is expected to defer this function
return func() {
// set the done flag so that a context cancellation right after the caller
// returns doesn't trigger a call to interrupt for some other statement.
donemu.Lock()
atomic.StoreInt32(done, 1)
donemu.Unlock()
close(donech)
}
}
func getVFSName(query string) (r string, err error) {
q, err := url.ParseQuery(query)
if err != nil {
return "", err
}
for _, v := range q["vfs"] {
if r != "" && r != v {
return "", fmt.Errorf("conflicting vfs query parameters: %v", q["vfs"])
}
r = v
}
return r, nil
}
// applyDQSConfig consults the _dqs DSN query parameter and, when set to a
// false value, disables SQLite's double-quoted string literal compatibility
// quirk on the connection by calling sqlite3_db_config with both
// SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML. Absence or a true
// value leaves SQLite's default (DQS enabled) untouched.
//
// Called from newConn after sqlite3_open_v2 and before applyQueryParams.
// The DBCONFIG_DQS_* flags are required to be set before any statement is
// prepared on the connection, and applyQueryParams runs user-supplied
// PRAGMA statements, so this must come first.
//
// See https://www.sqlite.org/quirks.html#dblquote and
// https://gitlab.com/cznic/sqlite/-/issues/61.
func applyDQSConfig(c *conn, query string) error {
q, err := url.ParseQuery(query)
if err != nil {
return err
}
v := q.Get("_dqs")
if v == "" {
return nil
}
on, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("invalid _dqs value %q: %w", v, err)
}
if on {
// _dqs=1 is the SQLite default; nothing to do.
return nil
}
for _, op := range []int32{
sqlite3.SQLITE_DBCONFIG_DQS_DDL,
sqlite3.SQLITE_DBCONFIG_DQS_DML,
} {
if rc := c.dbConfigBool(op, false); rc != sqlite3.SQLITE_OK {
return fmt.Errorf("sqlite3_db_config(op=%d, off) returned %d", op, rc)
}
}
return nil
}
// getErrorRcMode reads the _error_rc DSN query parameter and returns
// the parsed boolean. Called from newConn before sqlite3_open_v2 so
// open-time failures get the conditional errmsg treatment too: the
// temporary db handle that openV2 may leave behind on failure carries
// a stale errmsg from earlier initialisation, and the legacy
// "errstr: errmsg" form surfaces that as a misleading message.
//
// Absent parameter or false value preserves the SQLite-default error
// reporting byte-for-byte (legacy behavior). A true value switches
// the connection into the conditional mode described on
// errstrForDB. An unparseable value is reported as a descriptive
// error.
//
// See #230.
func getErrorRcMode(query string) (bool, error) {
q, err := url.ParseQuery(query)
if err != nil {
return false, err
}
v := q.Get("_error_rc")
if v == "" {
return false, nil
}
on, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("invalid _error_rc value %q: %w", v, err)
}
return on, nil
}
func applyQueryParams(c *conn, query string) error {
q, err := url.ParseQuery(query)
if err != nil {
return err
}
var a []string
for _, v := range q["_pragma"] {
a = append(a, v)
}
// Push 'busy_timeout' first, the rest in lexicographic order, case insenstive.
// See https://gitlab.com/cznic/sqlite/-/issues/198#note_2233423463 for
// discussion.
sort.Slice(a, func(i, j int) bool {
x, y := strings.TrimSpace(strings.ToLower(a[i])), strings.TrimSpace(strings.ToLower(a[j]))
if strings.HasPrefix(x, "busy_timeout") {
return true
}
if strings.HasPrefix(y, "busy_timeout") {
return false
}
return x < y
})
for _, v := range a {
cmd := "pragma " + v
_, err := c.exec(context.Background(), cmd, nil)
if err != nil {
return err
}
}
if v := q.Get("_time_format"); v != "" {
f, ok := writeTimeFormats[v]
if !ok {
return fmt.Errorf("unknown _time_format %q", v)
}
c.writeTimeFormat = f
}
if v := q.Get("_time_integer_format"); v != "" {
switch v {
case "unix":
case "unix_milli":
case "unix_micro":
case "unix_nano":
default:
return fmt.Errorf("unknown _time_integer_format %q", v)
}
c.integerTimeFormat = v
}
if v := q.Get("_timezone"); v != "" {
loc, err := time.LoadLocation(v)
if err != nil {
return fmt.Errorf("unknown _timezone %q: %w", v, err)
}
c.loc = loc
}
if v := q.Get("_txlock"); v != "" {
lower := strings.ToLower(v)
if lower != "deferred" && lower != "immediate" && lower != "exclusive" {
return fmt.Errorf("unknown _txlock %q", v)
}
c.beginMode = v
}
if v := q.Get("_inttotime"); v != "" {
onoff, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("unknown _inttotime %q, must be 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False",
v)
}
c.intToTime = onoff
}
if v := q.Get("_texttotime"); v != "" {
onoff, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("unknown _texttotime %q, must be 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False",
v)
}
c.textToTime = onoff
}
return nil
}
func unlockNotify(t *libc.TLS, ppArg uintptr, nArg int32) {
for i := int32(0); i < nArg; i++ {
mu := *(*uintptr)(unsafe.Pointer(ppArg))
(*mutex)(unsafe.Pointer(mu)).Unlock()
ppArg += ptrSize
}
}
// FunctionImpl describes an [application-defined SQL function]. If Scalar is
// set, it is treated as a scalar function; otherwise, it is treated as an
// aggregate function using MakeAggregate.
//
// [application-defined SQL function]: https://sqlite.org/appfunc.html
type FunctionImpl struct {
// NArgs is the required number of arguments that the function accepts.
// If NArgs is negative, then the function is variadic.
NArgs int32
// If Deterministic is true, the function must always give the same
// output when the input parameters are the same. This enables functions
// to be used in additional contexts like the WHERE clause of partial
// indexes and enables additional optimizations.
//
// See https://sqlite.org/c3ref/c_deterministic.html#sqlitedeterministic
// for more details.
Deterministic bool
// Scalar is called when a scalar function is invoked in SQL. The
// argument Values are not valid past the return of the function.
Scalar func(ctx *FunctionContext, args []driver.Value) (driver.Value, error)
// MakeAggregate is called at the beginning of each evaluation of an
// aggregate function.
MakeAggregate func(ctx FunctionContext) (AggregateFunction, error)
// VolatileArgs is an opt-in performance flag that eliminates the per-call
// allocation of string and []byte argument bodies. When true, the driver
// passes argument strings and byte slices as zero-copy views into
// SQLite-owned memory instead of Go-allocated copies.
//
// Setting this is unsafe unless the user-provided Scalar / Step /
// WindowInverse callbacks treat string and []byte arguments as strictly
// transient: they must not be retained past the return of the call,
// neither directly (storing the slice or string in a struct field, map,
// channel, or outer-scope variable) nor indirectly (passing them to
// something that captures them, including most concurrency primitives).
//
// Retaining a volatile argument produces silent data corruption: SQLite
// reuses the underlying buffer for the next row, so on a later read every
// retained value will appear to hold the contents of the most recent row.
// The Go race detector cannot catch this because UDF execution is
// sequential on a single goroutine; the corruption is deterministic and
// invisible to -race.
//
// As a guard against accidental capture, callbacks that must retain
// values across rows should copy:
//
// saved := append([]byte(nil), args[0].([]byte)...) // []byte
// saved := string(append([]byte(nil), args[0].(string)...)) // string, no aliasing
//
// When in doubt, leave VolatileArgs at its default (false) — the driver
// already pools the argument-slice header (issue #226), so the per-row
// overhead with VolatileArgs=false is one make([]byte) per BLOB column
// and one libc.GoString per TEXT column, not a fresh slice header.
//
// Similarly, do not re-enter SQLite on the same connection while a
// volatile argument is in scope. A nested Query/Exec on the same conn
// can cause SQLite to reuse the underlying value buffers, so a volatile
// string or []byte read before the nested call may alias different
// bytes after it returns.
//
// VolatileArgs has no effect on integer, float, time, or NULL arguments.
VolatileArgs bool
}
// An AggregateFunction is an invocation of an aggregate or window function. See
// the documentation for [aggregate function callbacks] and [application-defined
// window functions] for an overview.
//
// [aggregate function callbacks]: https://www.sqlite.org/appfunc.html#the_aggregate_function_callbacks
// [application-defined window functions]: https://www.sqlite.org/windowfunctions.html#user_defined_aggregate_window_functions
type AggregateFunction interface {
// Step is called for each row of an aggregate function's SQL
// invocation. The argument Values are not valid past the return of the
// function. When the aggregate was registered with
// [FunctionImpl.VolatileArgs] set to true, string and []byte arguments
// in rowArgs are zero-copy views into SQLite-owned memory and retaining
// them produces silent data corruption — see [FunctionImpl.VolatileArgs]
// for the full safety contract.
Step(ctx *FunctionContext, rowArgs []driver.Value) error
// WindowInverse is called to remove the oldest presently aggregated
// result of Step from the current window. The arguments are those
// passed to Step for the row being removed. The argument Values are not
// valid past the return of the function. The same
// [FunctionImpl.VolatileArgs] caveat applies as for Step.
WindowInverse(ctx *FunctionContext, rowArgs []driver.Value) error
// WindowValue is called to get the current value of an aggregate
// function. This is used to return the final value of the function,
// whether it is used as a window function or not.
WindowValue(ctx *FunctionContext) (driver.Value, error)
// Final is called after all of the aggregate function's input rows have
// been stepped through. No other methods will be called on the
// AggregateFunction after calling Final. WindowValue returns the value
// from the function.
Final(ctx *FunctionContext)
}
type collation struct {
zName uintptr
pApp uintptr
enc int32
}
// RegisterCollationUtf8 makes a Go function available as a collation named zName.
// impl receives two UTF-8 strings: left and right.
// The result needs to be:
//
// - 0 if left == right
// - 1 if left < right
// - +1 if left > right
//
// impl must always return the same result given the same inputs.
// Additionally, it must have the following properties for all strings A, B and C:
// - if A==B, then B==A
// - if A==B and B==C, then A==C
// - if A<B, then B>A
// - if A<B and B<C, then A<C.
//
// The new collation will be available to all new connections opened after
// executing RegisterCollationUtf8.
func RegisterCollationUtf8(
zName string,
impl func(left, right string) int,
) error {
return registerCollation(zName, impl, sqlite3.SQLITE_UTF8)
}
// MustRegisterCollationUtf8 is like RegisterCollationUtf8 but panics on error.
func MustRegisterCollationUtf8(
zName string,
impl func(left, right string) int,
) {
if err := RegisterCollationUtf8(zName, impl); err != nil {
panic(err)
}
}
func registerCollation(
zName string,
impl func(left, right string) int,
enc int32,
) error {
if _, ok := d.collations[zName]; ok {
return fmt.Errorf("a collation %q is already registered", zName)
}
// dont free, collations registered on the driver live as long as the program
name, err := libc.CString(zName)
if err != nil {
return err
}
xCollations.mu.Lock()
id := xCollations.ids.next()
xCollations.m[id] = impl
xCollations.mu.Unlock()
d.collations[zName] = &collation{
zName: name,
pApp: id,
enc: enc,
}
return nil
}
type ExecQuerierContext interface {
driver.ExecerContext
driver.QueryerContext
}
type HookRegisterer interface {
RegisterPreUpdateHook(PreUpdateHookFn)
RegisterCommitHook(CommitHookFn)
RegisterRollbackHook(RollbackHookFn)
}
// ConnectionHookFn function type for a connection hook on the Driver. Connection
// hooks are called after the connection has been set up.
type ConnectionHookFn func(
conn ExecQuerierContext,
dsn string,
) error
// FunctionContext represents the context user defined functions execute in.
// Fields and/or methods of this type may get addedd in the future.
type FunctionContext struct {
tls *libc.TLS
ctx uintptr
}
const sqliteValPtrSize = unsafe.Sizeof(&sqlite3.Sqlite3_value{})
// RegisterFunction registers a function named zFuncName with nArg arguments.
// Passing -1 for nArg indicates the function is variadic. The FunctionImpl
// determines whether the function is deterministic or not, and whether it is a
// scalar function (when Scalar is defined) or an aggregate function (when
// Scalar is not defined and MakeAggregate is defined).
//
// The new function will be available to all new connections opened after
// executing RegisterFunction.
func RegisterFunction(
zFuncName string,
impl *FunctionImpl,
) error {
return registerFunction(zFuncName, impl)
}
// MustRegisterFunction is like RegisterFunction but panics on error.
func MustRegisterFunction(
zFuncName string,
impl *FunctionImpl,
) {
if err := RegisterFunction(zFuncName, impl); err != nil {
panic(err)
}
}
// RegisterScalarFunction registers a scalar function named zFuncName with nArg
// arguments. Passing -1 for nArg indicates the function is variadic.
//
// The new function will be available to all new connections opened after
// executing RegisterScalarFunction.
func RegisterScalarFunction(
zFuncName string,
nArg int32,
xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
) (err error) {
if dmesgs {
defer func() {
dmesg("zFuncName %q, nArg %v, xFunc %p: err %v", zFuncName, nArg, xFunc, err)
}()
}
return registerFunction(zFuncName, &FunctionImpl{NArgs: nArg, Scalar: xFunc, Deterministic: false})
}
// MustRegisterScalarFunction is like RegisterScalarFunction but panics on
// error.
func MustRegisterScalarFunction(
zFuncName string,
nArg int32,
xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
) {
if dmesgs {
dmesg("zFuncName %q, nArg %v, xFunc %p", zFuncName, nArg, xFunc)
}
if err := RegisterScalarFunction(zFuncName, nArg, xFunc); err != nil {
panic(err)
}
}
// MustRegisterDeterministicScalarFunction is like
// RegisterDeterministicScalarFunction but panics on error.
func MustRegisterDeterministicScalarFunction(
zFuncName string,
nArg int32,
xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
) {
if dmesgs {
dmesg("zFuncName %q, nArg %v, xFunc %p", zFuncName, nArg, xFunc)
}
if err := RegisterDeterministicScalarFunction(zFuncName, nArg, xFunc); err != nil {
panic(err)
}
}
// RegisterDeterministicScalarFunction registers a deterministic scalar
// function named zFuncName with nArg arguments. Passing -1 for nArg indicates
// the function is variadic. A deterministic function means that the function
// always gives the same output when the input parameters are the same.
//
// The new function will be available to all new connections opened after
// executing RegisterDeterministicScalarFunction.
func RegisterDeterministicScalarFunction(
zFuncName string,
nArg int32,
xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
) (err error) {
if dmesgs {
defer func() {
dmesg("zFuncName %q, nArg %v, xFunc %p: err %v", zFuncName, nArg, xFunc, err)
}()
}
return registerFunction(zFuncName, &FunctionImpl{NArgs: nArg, Scalar: xFunc, Deterministic: true})
}
func registerFunction(
zFuncName string,
impl *FunctionImpl,
) error {
if _, ok := d.udfs[zFuncName]; ok {
return fmt.Errorf("a function named %q is already registered", zFuncName)
}
// dont free, functions registered on the driver live as long as the program
name, err := libc.CString(zFuncName)
if err != nil {
return err
}
var textrep int32 = sqlite3.SQLITE_UTF8
if impl.Deterministic {
textrep |= sqlite3.SQLITE_DETERMINISTIC
}
udf := &userDefinedFunction{
zFuncName: name,
nArg: impl.NArgs,
eTextRep: textrep,
}
if impl.Scalar != nil {
xFuncs.mu.Lock()
id := xFuncs.ids.next()
xFuncs.m[id] = xFuncEntry{fn: impl.Scalar, volatile: impl.VolatileArgs}
xFuncs.mu.Unlock()
udf.scalar = true
udf.pApp = id
} else {
xAggregateFactories.mu.Lock()
id := xAggregateFactories.ids.next()
xAggregateFactories.m[id] = xAggregateFactoryEntry{factory: impl.MakeAggregate, volatile: impl.VolatileArgs}
xAggregateFactories.mu.Unlock()
udf.pApp = id
}
d.udfs[zFuncName] = udf
return nil
}
// RegisterConnectionHook registers a function to be called after each connection
// is opened. This is called after all the connection has been set up.
func RegisterConnectionHook(fn ConnectionHookFn) {
d.RegisterConnectionHook(fn)
}
func origin(skip int) string {
pc, fn, fl, _ := runtime.Caller(skip)
f := runtime.FuncForPC(pc)
var fns string
if f != nil {
fns = f.Name()
if x := strings.LastIndex(fns, "."); x > 0 {
fns = fns[x+1:]
}
}
return fmt.Sprintf("%s:%d:%s", fn, fl, fns)
}
func errorResultFunction(tls *libc.TLS, ctx uintptr) func(error) {
return func(res error) {
errmsg, cerr := libc.CString(res.Error())
if cerr != nil {
panic(cerr)
}
defer libc.Xfree(tls, errmsg)
sqlite3.Xsqlite3_result_error(tls, ctx, errmsg, -1)
sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR)
}
}
// udfArgsPool reuses []driver.Value slices passed to user-defined functions.
// The driver's contract (documented on FunctionImpl.Scalar and
// AggregateFunction.Step/WindowInverse) states that the args values are not
// valid past the return of the user function, which makes the slice itself
// safe to reuse. See https://gitlab.com/cznic/sqlite/-/issues/226.
var udfArgsPool = sync.Pool{
New: func() any {
s := make([]driver.Value, 0, 8)
return &s
},
}
// acquireUDFArgs returns a pooled *[]driver.Value with len == n. The caller
// must invoke releaseUDFArgs after the user function has returned.
func acquireUDFArgs(n int) *[]driver.Value {
sp := udfArgsPool.Get().(*[]driver.Value)
if cap(*sp) < n {
*sp = make([]driver.Value, n)
} else {
*sp = (*sp)[:n]
}
return sp
}
// releaseUDFArgs returns the slice to the pool after clearing each entry so
// any heap references held in the previous invocation can be reclaimed.
func releaseUDFArgs(sp *[]driver.Value) {
s := *sp
for i := range s {
s[i] = nil
}
*sp = s[:0]
udfArgsPool.Put(sp)
}
// functionArgs prepares a []driver.Value for one user-function invocation.
// The returned slice is owned by the driver and must be released via
// releaseUDFArgs once the user function returns.
//
// When volatile is true, SQLITE_TEXT and SQLITE_BLOB arguments are returned as
// zero-copy views into SQLite-owned memory (see [FunctionImpl.VolatileArgs]
// for the user-facing safety contract). When false (the default for all
// existing call sites), text and blob payloads are copied into Go-owned
// memory and stay valid for the lifetime of the slice.
func functionArgs(tls *libc.TLS, argc int32, argv uintptr, volatile bool) *[]driver.Value {
sp := acquireUDFArgs(int(argc))
args := *sp
for i := int32(0); i < argc; i++ {
valPtr := *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*sqliteValPtrSize))
switch valType := sqlite3.Xsqlite3_value_type(tls, valPtr); valType {
case sqlite3.SQLITE_TEXT:
if volatile {
p := sqlite3.Xsqlite3_value_text(tls, valPtr)
n := sqlite3.Xsqlite3_value_bytes(tls, valPtr)
if p == 0 || n == 0 {
args[i] = ""
} else {
args[i] = unsafe.String((*byte)(unsafe.Pointer(p)), int(n))
}
} else {
args[i] = libc.GoString(sqlite3.Xsqlite3_value_text(tls, valPtr))
}
case sqlite3.SQLITE_INTEGER:
args[i] = sqlite3.Xsqlite3_value_int64(tls, valPtr)
case sqlite3.SQLITE_FLOAT:
args[i] = sqlite3.Xsqlite3_value_double(tls, valPtr)
case sqlite3.SQLITE_NULL:
args[i] = nil
case sqlite3.SQLITE_BLOB:
size := sqlite3.Xsqlite3_value_bytes(tls, valPtr)
blobPtr := sqlite3.Xsqlite3_value_blob(tls, valPtr)
if volatile {
if blobPtr == 0 || size == 0 {
args[i] = make([]byte, 0)
} else {
args[i] = unsafe.Slice((*byte)(unsafe.Pointer(blobPtr)), int(size))
}
} else {
v := make([]byte, size)
if size != 0 {
copy(v, (*libc.RawMem)(unsafe.Pointer(blobPtr))[:size:size])
}
args[i] = v
}
default:
panic(fmt.Sprintf("unexpected argument type %q passed by sqlite", valType))
}
}
return sp
}
func functionReturnValue(tls *libc.TLS, ctx uintptr, res driver.Value) error {
switch resTyped := res.(type) {
case nil:
sqlite3.Xsqlite3_result_null(tls, ctx)
case int64:
sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped)
case float64:
sqlite3.Xsqlite3_result_double(tls, ctx, resTyped)
case bool:
sqlite3.Xsqlite3_result_int(tls, ctx, libc.Bool32(resTyped))
case time.Time:
sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped.Unix())
case string:
size := int32(len(resTyped))
cstr, err := libc.CString(resTyped)
if err != nil {
panic(err)
}
defer libc.Xfree(tls, cstr)
sqlite3.Xsqlite3_result_text(tls, ctx, cstr, size, sqlite3.SQLITE_TRANSIENT)
case []byte:
size := int32(len(resTyped))
if size == 0 {
sqlite3.Xsqlite3_result_zeroblob(tls, ctx, 0)
return nil
}
p := libc.Xmalloc(tls, types.Size_t(size))
if p == 0 {
panic(fmt.Sprintf("unable to allocate space for blob: %d", size))
}
defer libc.Xfree(tls, p)
copy((*libc.RawMem)(unsafe.Pointer(p))[:size:size], resTyped)
sqlite3.Xsqlite3_result_blob(tls, ctx, p, size, sqlite3.SQLITE_TRANSIENT)
default:
return fmt.Errorf("function did not return a valid driver.Value: %T", resTyped)
}
return nil
}
// The below is all taken from zombiezen.com/go/sqlite. Aggregate functions need
// to maintain state (for instance, the count of values seen so far). We give
// each aggregate function an ID, generated by idGen, and put that in the pApp
// argument to sqlite3_create_function. We track this on the Go side in
// xAggregateFactories.
//
// When (if) the function is called is called by a query, we call the
// MakeAggregate factory function to set it up, and track that in
// xAggregateContext, retrieving it via sqlite3_aggregate_context.
//
// We also need to ensure that, for both aggregate and scalar functions, the
// function pointer we pass to SQLite meets certain rules on the Go side, so
// that the pointer remains valid.
var (
xFuncs = struct {
mu sync.RWMutex
m map[uintptr]xFuncEntry
ids idGen
}{
m: make(map[uintptr]xFuncEntry),
}
xAggregateFactories = struct {
mu sync.RWMutex
m map[uintptr]xAggregateFactoryEntry
ids idGen
}{
m: make(map[uintptr]xAggregateFactoryEntry),
}
xAggregateContext = struct {
mu sync.RWMutex
m map[uintptr]xAggregateContextEntry
ids idGen
}{
m: make(map[uintptr]xAggregateContextEntry),
}
xCollations = struct {
mu sync.RWMutex
m map[uintptr]func(string, string) int
ids idGen
}{
m: make(map[uintptr]func(string, string) int),
}
)
// xFuncEntry pairs a registered scalar function with the VolatileArgs flag
// captured at registration time. Bundled so trampolines can decide whether to
// pass zero-copy or copied argument values without a second map lookup.
type xFuncEntry struct {
fn func(*FunctionContext, []driver.Value) (driver.Value, error)
volatile bool
}
// xAggregateFactoryEntry pairs a registered aggregate factory with its
// VolatileArgs flag, for the same reason as xFuncEntry.
type xAggregateFactoryEntry struct {
factory func(FunctionContext) (AggregateFunction, error)
volatile bool
}
// xAggregateContextEntry holds the AggregateFunction instance for one
// in-flight aggregate evaluation together with the VolatileArgs flag inherited
// from its factory registration. Caching the flag here avoids a second
// xAggregateFactories lookup in the Step / WindowInverse trampolines.
type xAggregateContextEntry struct {
fn AggregateFunction
volatile bool
}
type idGen struct {
bitset []uint64
}
func (gen *idGen) next() uintptr {
base := uintptr(1)
for i := 0; i < len(gen.bitset); i, base = i+1, base+64 {
b := gen.bitset[i]
if b != 1<<64-1 {
n := uintptr(bits.TrailingZeros64(^b))
gen.bitset[i] |= 1 << n
return base + n
}
}
gen.bitset = append(gen.bitset, 1)
return base
}
func (gen *idGen) reclaim(id uintptr) {
bit := id - 1
gen.bitset[bit/64] &^= 1 << (bit % 64)
}
func makeAggregate(tls *libc.TLS, ctx uintptr) (AggregateFunction, bool, uintptr) {
goCtx := FunctionContext{tls: tls, ctx: ctx}
aggCtx := (*uintptr)(unsafe.Pointer(sqlite3.Xsqlite3_aggregate_context(tls, ctx, int32(ptrSize))))
setErrorResult := errorResultFunction(tls, ctx)
if aggCtx == nil {
setErrorResult(errors.New("insufficient memory for aggregate"))
return nil, false, 0
}
if *aggCtx != 0 {
// Already created.
xAggregateContext.mu.RLock()
entry := xAggregateContext.m[*aggCtx]
xAggregateContext.mu.RUnlock()
return entry.fn, entry.volatile, *aggCtx
}
factoryID := sqlite3.Xsqlite3_user_data(tls, ctx)
xAggregateFactories.mu.RLock()
factoryEntry := xAggregateFactories.m[factoryID]
xAggregateFactories.mu.RUnlock()
f, err := factoryEntry.factory(goCtx)
if err != nil {
setErrorResult(err)
return nil, false, 0
}
if f == nil {
setErrorResult(errors.New("MakeAggregate function returned nil"))
return nil, false, 0
}
xAggregateContext.mu.Lock()
*aggCtx = xAggregateContext.ids.next()
xAggregateContext.m[*aggCtx] = xAggregateContextEntry{fn: f, volatile: factoryEntry.volatile}
xAggregateContext.mu.Unlock()
return f, factoryEntry.volatile, *aggCtx
}
// cFuncPointer converts a function defined by a function declaration to a C pointer.
// The result of using cFuncPointer on closures is undefined.
func cFuncPointer[T any](f T) uintptr {
// This assumes the memory representation described in https://golang.org/s/go11func.
//
// cFuncPointer does its conversion by doing the following in order:
// 1) Create a Go struct containing a pointer to a pointer to
// the function. It is assumed that the pointer to the function will be
// stored in the read-only data section and thus will not move.
// 2) Convert the pointer to the Go struct to a pointer to uintptr through
// unsafe.Pointer. This is permitted via Rule #1 of unsafe.Pointer.
// 3) Dereference the pointer to uintptr to obtain the function value as a
// uintptr. This is safe as long as function values are passed as pointers.
return *(*uintptr)(unsafe.Pointer(&struct{ f T }{f}))
}
func funcTrampoline(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
id := sqlite3.Xsqlite3_user_data(tls, ctx)
xFuncs.mu.RLock()
entry := xFuncs.m[id]
xFuncs.mu.RUnlock()
setErrorResult := errorResultFunction(tls, ctx)
sp := functionArgs(tls, argc, argv, entry.volatile)
defer releaseUDFArgs(sp)
res, err := entry.fn(&FunctionContext{}, *sp)
if err != nil {
setErrorResult(err)
return
}
err = functionReturnValue(tls, ctx, res)
if err != nil {
setErrorResult(err)
}
}
// sqlite3AllocCString allocates a NUL-terminated copy of s using SQLite's
// memory allocator (sqlite3_malloc). The caller must arrange for SQLite to
// free the returned pointer via sqlite3_free.
func sqlite3AllocCString(tls *libc.TLS, s string) uintptr {
n := len(s) + 1
p := sqlite3.Xsqlite3_malloc(tls, int32(n))
if p == 0 {
return 0
}
mem := (*libc.RawMem)(unsafe.Pointer(p))[:n:n]
copy(mem, []byte(s))
mem[n-1] = 0
return p
}
func stepTrampoline(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
impl, volatile, _ := makeAggregate(tls, ctx)
if impl == nil {
return
}
setErrorResult := errorResultFunction(tls, ctx)
sp := functionArgs(tls, argc, argv, volatile)