-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduckdb-api.lisp
1001 lines (790 loc) · 30.1 KB
/
duckdb-api.lisp
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
;;;; duckdb-api.lisp
(in-package #:duckdb-api)
(define-foreign-library duckdb-lib
(:darwin "libduckdb.dylib")
(:unix "libduckdb.so")
(:windows "duckdb.dll")
(t (:default "libduckdb")))
(use-foreign-library duckdb-lib)
(defun get-message (p-string)
(foreign-string-to-lisp (mem-ref p-string '(:pointer :string))))
(defctype duckdb-database (:pointer :void))
(defctype duckdb-connection (:pointer :void))
(defctype duckdb-prepared-statement (:pointer :void))
(defctype duckdb-appender (:pointer :void))
(defctype duckdb-config (:pointer :void))
(defctype duckdb-arrow (:pointer :void))
(defctype duckdb-arrow-schema (:pointer :void))
(defctype duckdb-arrow-array (:pointer :void))
(defctype duckdb-logical-type (:pointer :void))
(defctype duckdb-data-chunk (:pointer :void))
(defctype duckdb-vector (:pointer :void))
(defctype duckdb-value (:pointer :void))
(defctype duckdb-table-function (:pointer :void))
(defctype duckdb-bind-info (:pointer :void))
(defctype duckdb-init-info (:pointer :void))
(defctype duckdb-function-info (:pointer :void))
(defctype duckdb-replacement-scan-info (:pointer :void))
(defcenum duckdb-state
(:duckdb-success 0)
(:duckdb-error 1))
(defctype idx :uint64)
(defctype p-validity (:pointer :uint64))
(defvar *inline-length* 12)
(defcstruct (duckdb-string :class duckdb-string-type)
(length :uint32)
(data :uint8 :count 12))
(defun get-base-pointer (length data)
(if (<= length *inline-length*)
data
(mem-ref data '(:pointer :uint8) 4)))
(defmethod translate-from-foreign (value (type duckdb-string-type))
(with-foreign-slots ((length data) value (:struct duckdb-string))
(foreign-string-to-lisp (get-base-pointer length data) :count length)))
(defcstruct (duckdb-hugeint :class duckdb-hugeint-type)
(lower :uint64)
(upper :int64))
(defmethod translate-from-foreign (value (type duckdb-hugeint-type))
(with-foreign-slots ((lower upper) value (:struct duckdb-hugeint))
(logior (ash upper 64) lower)))
(defmethod translate-into-foreign-memory
(value (type duckdb-hugeint-type) ptr)
(let ((multiplier (if (minusp value) -1 1)))
(with-foreign-slots ((lower upper) ptr (:struct duckdb-hugeint))
(setf lower (ldb (byte 64 0) value)
upper (* multiplier (ldb (byte 64 64) value))))))
(defcstruct (duckdb-uhugeint :class duckdb-uhugeint-type)
(lower :uint64)
(upper :uint64))
(defmethod translate-from-foreign (value (type duckdb-uhugeint-type))
(with-foreign-slots ((lower upper) value (:struct duckdb-uhugeint))
(logior (ash upper 64) lower)))
(defmethod translate-into-foreign-memory
(value (type duckdb-uhugeint-type) ptr)
(with-foreign-slots ((lower upper) ptr (:struct duckdb-uhugeint))
(setf lower (ldb (byte 64 0) value)
upper (ldb (byte 64 64) value))))
(defconstant +varint-header-size+ 3)
(defcstruct (duckdb-varint :class duckdb-varint-type)
(length :uint32)
(data :uint8 :count 12))
(defmethod translate-from-foreign (value (type duckdb-varint-type))
(with-foreign-slots ((length data) value (:struct duckdb-varint))
(let* ((p-base (get-base-pointer length data))
(is-negative (zerop (logand (mem-ref p-base :uint8) #x80))))
(loop :with result := 0
:for i :from (1- length) :downto +varint-header-size+
:for j :from 0
:for k := (mem-ref p-base :uint8 i)
:do (setf result (logior result (ash (if is-negative
(logxor k #xFF)
k)
(* j 8))))
:finally (return (if is-negative (- result) result))))))
(defcstruct (duckdb-uuid :class duckdb-uuid-type)
(lower :uint64)
(upper :uint64))
(defmethod translate-from-foreign (value (type duckdb-uuid-type))
(with-foreign-slots ((lower upper) value (:struct duckdb-uuid))
(let ((upper (logxor upper (ash 1 63))))
(make-instance 'fuuid:uuid
:time-low (ldb (byte 32 32) upper)
:time-mid (ldb (byte 16 16) upper)
:time-hi-and-version (ldb (byte 16 0) upper)
:clock-seq-hi-and-res (ldb (byte 8 56) lower)
:clock-seq-low (ldb (byte 8 48) lower)
:node (ldb (byte 48 0) lower)))))
(defmethod translate-into-foreign-memory
(value (type duckdb-uuid-type) ptr)
(with-foreign-slots ((lower upper) ptr (:struct duckdb-uuid))
(setf (ldb (byte 32 32) upper) (fuuid:time-low value)
(ldb (byte 16 16) upper) (fuuid:time-mid value)
(ldb (byte 16 0) upper) (fuuid:time-hi-and-version value)
(ldb (byte 8 56) lower) (fuuid:clock-seq-hi-and-res value)
(ldb (byte 8 48) lower) (fuuid:clock-seq-low value)
(ldb (byte 48 0) lower) (fuuid:node value)
upper (logxor upper (ash 1 63)))))
(defcstruct (duckdb-blob :class duckdb-blob-type)
(length :uint32)
(data :uint8 :count 12))
(defmethod translate-from-foreign (value (type duckdb-blob-type))
(with-foreign-slots ((length data) value (:struct duckdb-blob))
(let ((p-base (get-base-pointer length data)))
(loop :with result := (make-array length :element-type '(unsigned-byte 8))
:for i :below length
:do (setf (aref result i) (mem-ref p-base :uint8 i))
:finally (return result)))))
(defcstruct (duckdb-date :class duckdb-date-type)
(days :int32))
(defmethod translate-from-foreign (value (type duckdb-date-type))
(with-foreign-slots ((days) value (:struct duckdb-date))
(local-time:make-timestamp :day (- days 11017))))
(defmethod translate-into-foreign-memory
(value (type duckdb-date-type) ptr)
(with-foreign-slots ((days) ptr (:struct duckdb-date))
(setf days (+ (local-time:day-of value) 11017))))
(defcstruct (duckdb-time :class duckdb-time-type)
(micros :int64))
(defmethod translate-from-foreign (value (type duckdb-time-type))
(with-foreign-slots ((micros) value (:struct duckdb-time))
(local-time-duration:duration :nsec (* micros 1000))))
(defmethod translate-into-foreign-memory
(value (type duckdb-time-type) ptr)
(with-foreign-slots ((micros) ptr (:struct duckdb-time))
(setf micros (round (local-time-duration:duration-as value :nsec)
1000))))
(defcstruct (duckdb-timestamp :class duckdb-timestamp-type)
(micros :int64))
(defmethod translate-from-foreign (value (type duckdb-timestamp-type))
(with-foreign-slots ((micros) value (:struct duckdb-timestamp))
(multiple-value-bind (secs remainder) (floor micros 1000000)
(local-time:unix-to-timestamp secs :nsec (* remainder 1000)))))
(defmethod translate-into-foreign-memory
(value (type duckdb-timestamp-type) ptr)
(with-foreign-slots ((micros) ptr (:struct duckdb-timestamp))
(setf micros (+ (* 1000000 (+ (* (+ (local-time:day-of value) 11017)
24 60 60)
(local-time:sec-of value)))
(round (local-time:nsec-of value) 1000)))))
(defcstruct (duckdb-interval :class duckdb-interval-type)
(months :int32)
(days :int32)
(micros :int64))
(defmethod translate-from-foreign (value (type duckdb-interval-type))
(with-foreign-slots ((months days micros) value (:struct duckdb-interval))
(let+ (((&values years months) (floor months 12))
((&values milliseconds microseconds) (floor micros 1000))
((&values seconds milliseconds) (floor milliseconds 1000))
((&values minutes seconds) (floor seconds 60))
((&values hours minutes) (floor minutes 60)))
(periods:duration :years years
:months months
:days days
:hours hours
:minutes minutes
:seconds seconds
:milliseconds milliseconds
:microseconds microseconds))))
(defcstruct (duckdb-list :class duckdb-list-type)
(offset idx)
(length idx))
(defmethod translate-from-foreign (value (type duckdb-list-type))
(with-foreign-slots ((offset length) value (:struct duckdb-list))
(list offset length)))
(defcenum duckdb-type
(:duckdb-invalid-type 0)
(:duckdb-boolean 1)
(:duckdb-tinyint 2)
(:duckdb-smallint 3)
(:duckdb-integer 4)
(:duckdb-bigint 5)
(:duckdb-utinyint 6)
(:duckdb-usmallint 7)
(:duckdb-uinteger 8)
(:duckdb-ubigint 9)
(:duckdb-float 10)
(:duckdb-double 11)
(:duckdb-timestamp 12)
(:duckdb-date 13)
(:duckdb-time 14)
(:duckdb-interval 15)
(:duckdb-hugeint 16)
(:duckdb-uhugeint 32)
(:duckdb-varchar 17)
(:duckdb-blob 18)
(:duckdb-decimal 19)
(:duckdb-timestamp-s 20)
(:duckdb-timestamp-ms 21)
(:duckdb-timestamp-ns 22)
(:duckdb-enum 23)
(:duckdb-list 24)
(:duckdb-struct 25)
(:duckdb-map 26)
(:duckdb-uuid 27)
(:duckdb-union 28)
(:duckdb-bit 29)
(:duckdb-time-tz 30)
(:duckdb-timestamp-tz 31)
(:duckdb-any 34)
(:duckdb-varint 35)
(:duckdb-sqlnull 36))
(defun get-ffi-type (duckdb-type)
(ecase duckdb-type
(:duckdb-boolean :bool)
(:duckdb-tinyint :int8)
(:duckdb-smallint :int16)
(:duckdb-integer :int32)
(:duckdb-bigint :int64)
(:duckdb-utinyint :uint8)
(:duckdb-usmallint :uint16)
(:duckdb-uinteger :uint32)
(:duckdb-ubigint :uint64)
(:duckdb-float :float)
(:duckdb-double :double)
(:duckdb-varchar '(:struct duckdb-string))
(:duckdb-hugeint '(:struct duckdb-hugeint))
(:duckdb-uhugeint '(:struct duckdb-uhugeint))
(:duckdb-varint '(:struct duckdb-varint))
(:duckdb-blob '(:struct duckdb-blob))
(:duckdb-date '(:struct duckdb-date))
(:duckdb-time '(:struct duckdb-time))
(:duckdb-timestamp '(:struct duckdb-timestamp))
(:duckdb-timestamp-s '(:struct duckdb-timestamp))
(:duckdb-timestamp-ms '(:struct duckdb-timestamp))
(:duckdb-timestamp-ns '(:struct duckdb-timestamp))
(:duckdb-interval '(:struct duckdb-interval))
(:duckdb-uuid '(:struct duckdb-uuid))
(:duckdb-list '(:struct duckdb-list))
(:duckdb-struct :void)
(:duckdb-map '(:struct duckdb-list))
(:duckdb-union :void)
(:duckdb-bit '(:struct duckdb-blob))
(:duckdb-timestamp-tz '(:struct duckdb-timestamp))))
(defcstruct duckdb-column)
(defctype p-duckdb-column
(:pointer (:struct duckdb-column)))
(defcstruct (duckdb-result)
(__deprecated-column-count idx)
(__deprecated-row-count idx)
(__deprecated-rows-changed idx)
(__deprecated-deprecated-columns p-duckdb-column)
(__deprecated-error-message (:pointer :string))
(internal-data (:pointer :void)))
(defctype p-duckdb-result
(:pointer (:struct duckdb-result)))
(defcfun duckdb-open duckdb-state
(path :string)
(out-database (:pointer duckdb-database)))
(defcfun duckdb-open-ext duckdb-state
(path :string)
(out-database (:pointer duckdb-database))
(config duckdb-config)
(out-error (:pointer :string)))
(defcfun duckdb-close :void
(database (:pointer duckdb-database)))
(defcfun duckdb-connect duckdb-state
(database duckdb-database)
(out-connection (:pointer duckdb-connection)))
(defcfun duckdb-disconnect :void
(connection (:pointer duckdb-connection)))
(defcfun duckdb-query duckdb-state
(connection duckdb-connection)
(query :string)
(out-result (:pointer (:struct duckdb-result))))
(defcfun duckdb-destroy-result :void
(result p-duckdb-result))
(defcfun duckdb-column-count idx
(result p-duckdb-result))
(defcfun duckdb-row-count idx
(result p-duckdb-result))
(defcfun duckdb-rows-changed idx
(result p-duckdb-result))
(defcfun duckdb-result-error :string
(result p-duckdb-result))
(defcfun duckdb-column-name :string
(result p-duckdb-result)
(col idx))
(defcfun duckdb-column-type duckdb-type
(result p-duckdb-result)
(col idx))
(defcfun duckdb-column-data (:pointer :void)
(result p-duckdb-result)
(col idx))
(defcfun duckdb-nullmask-data (:pointer :bool)
(result p-duckdb-result)
(col idx))
(defcfun duckdb-fetch-chunk (duckdb-data-chunk)
(result (:struct duckdb-result)))
(defcfun duckdb-destroy-data-chunk :void
(chunk (:pointer duckdb-data-chunk)))
(defmacro with-data-chunk ((chunk-var result) &body body)
`(let ((,chunk-var (duckdb-fetch-chunk
(mem-ref ,result '(:struct duckdb-result)))))
(unwind-protect (progn ,@body)
(unless (null-pointer-p ,chunk-var)
(with-foreign-object (p-chunk '(:pointer duckdb-data-chunk))
(setf (mem-ref p-chunk 'duckdb-data-chunk) ,chunk-var)
(duckdb-destroy-data-chunk p-chunk))))))
(defcfun duckdb-data-chunk-get-column-count (idx)
(chunk duckdb-data-chunk))
(defcfun duckdb-data-chunk-get-vector (duckdb-vector)
(chunk duckdb-data-chunk)
(col-idx idx))
(defcfun duckdb-data-chunk-get-size (idx)
(chunk duckdb-data-chunk))
(defcfun duckdb-data-chunk-set-size :void
(chunk duckdb-data-chunk)
(size idx))
(defcfun duckdb-vector-size idx)
(defcfun duckdb-vector-get-column-type (duckdb-logical-type)
(vector duckdb-vector))
(defcfun duckdb-get-type-id (duckdb-type)
(type duckdb-logical-type))
(defcfun duckdb-decimal-internal-type duckdb-type
(type duckdb-logical-type))
(defcfun duckdb-decimal-width :uint8
(type duckdb-logical-type))
(defcfun duckdb-decimal-scale :uint8
(type duckdb-logical-type))
(defcfun duckdb-enum-internal-type duckdb-type
(type duckdb-logical-type))
(defcfun duckdb-enum-dictionary-size :uint32
(type duckdb-logical-type))
(defcfun duckdb-enum-dictionary-value (:pointer :char)
(type duckdb-logical-type)
(index idx))
(defcfun duckdb-free :void
(ptr (:pointer :void)))
(defmacro with-free ((var alloc-form) &body body)
`(let ((,var ,alloc-form))
(unwind-protect (progn ,@body)
(duckdb-free ,var))))
(defun get-enum-alist (logical-type)
(let ((enum-size (duckdb-enum-dictionary-size logical-type)))
(loop :for index :below enum-size
:for ptr := (duckdb-enum-dictionary-value logical-type index)
:collect (unwind-protect `(,index . ,(foreign-string-to-lisp ptr))
(duckdb-free ptr)))))
(defmacro with-logical-type ((logical-type-var alloc-form) &body body)
`(let ((,logical-type-var ,alloc-form))
(unwind-protect (progn ,@body)
(with-foreign-object (p-type '(:pointer duckdb-logical-type))
(setf (mem-ref p-type 'duckdb-logical-type) ,logical-type-var)
(duckdb-destroy-logical-type p-type)))))
(defun resolve-logical-type (logical-type)
(let ((type (duckdb-get-type-id logical-type)))
(case type
(:duckdb-decimal (list type
(duckdb-decimal-internal-type logical-type)
(duckdb-decimal-scale logical-type)))
(:duckdb-enum (list type
(duckdb-enum-internal-type logical-type)
(get-enum-alist logical-type)))
(:duckdb-list
(with-logical-type (child-type (duckdb-list-type-child-type logical-type))
(list type
nil
(resolve-logical-type child-type))))
(:duckdb-struct
(loop :for i :below (duckdb-struct-type-child-count logical-type)
:collect
(with-logical-type (field-type (duckdb-struct-type-child-type logical-type i))
(with-free (p-field-name (duckdb-struct-type-child-name logical-type i))
(cons (foreign-string-to-lisp p-field-name)
(resolve-logical-type field-type))))
:into fields
:finally (return (list type nil fields))))
(:duckdb-union
(loop :for i :below (duckdb-union-type-member-count logical-type)
:collect
(with-logical-type (field-type (duckdb-union-type-member-type logical-type i))
(with-free (p-field-name (duckdb-union-type-member-name logical-type i))
(cons (foreign-string-to-lisp p-field-name)
(resolve-logical-type field-type))))
:into fields
:finally (return (list type nil fields))))
(:duckdb-map
(list type
nil
(with-logical-type (key-type (duckdb-map-type-key-type logical-type))
(with-logical-type (value-type (duckdb-map-type-value-type logical-type))
(list (resolve-logical-type key-type)
(resolve-logical-type value-type))))))
(t (list type nil nil)))))
(defun get-vector-type (vector)
(with-logical-type (logical-type (duckdb-vector-get-column-type vector))
(resolve-logical-type logical-type)))
(defcfun duckdb-vector-get-data (:pointer :void)
(vector duckdb-vector))
(defcfun duckdb-vector-get-validity p-validity
(vector duckdb-vector))
(defcfun duckdb-vector-ensure-validity-writable :void
(vector duckdb-vector))
(defcfun duckdb-vector-assign-string-element :void
(vector duckdb-vector)
(index idx)
(str :string))
(defcfun duckdb-validity-row-is-valid :bool
(validity p-validity)
(row idx))
(defcfun duckdb-prepare-error :string
(statement duckdb-prepared-statement))
(defcfun duckdb-prepare duckdb-state
(connection duckdb-connection)
(query :string)
(out-prepared-statement duckdb-prepared-statement))
(defcfun duckdb-destroy-prepare :void
(prepared-statement (:pointer duckdb-prepared-statement)))
(defcfun duckdb-execute-prepared duckdb-state
(prepared-statement duckdb-prepared-statement)
(out-result p-duckdb-result))
(defcfun duckdb-nparams idx
(prepared-statement duckdb-prepared-statement))
(defcfun duckdb-param-type duckdb-type
(prepared-statement duckdb-prepared-statement)
(param-idx idx))
(defcfun duckdb-clear-bindings duckdb-state
(prepared-statement duckdb-prepared-statement))
(defcfun duckdb-bind-null duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx))
(defcfun duckdb-bind-boolean duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :bool))
(defcfun duckdb-bind-varchar duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :string))
(defcfun duckdb-bind-blob duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(data (:pointer :void))
(length idx))
(defcfun duckdb-bind-float duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :float))
(defcfun duckdb-bind-double duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :double))
(defcfun duckdb-bind-uint8 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :uint8))
(defcfun duckdb-bind-uint16 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :uint16))
(defcfun duckdb-bind-uint32 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :uint32))
(defcfun duckdb-bind-uint64 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :uint64))
(defcfun duckdb-bind-int8 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :int8))
(defcfun duckdb-bind-int16 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :int16))
(defcfun duckdb-bind-int32 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :int32))
(defcfun duckdb-bind-int64 duckdb-state
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val :int64))
(defcfun duckdb-bind-hugeint :void ; TODO: duckdb-state doesn't work
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val (:struct duckdb-hugeint)))
(defcfun duckdb-bind-uhugeint :void
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val (:struct duckdb-uhugeint)))
(defcfun duckdb-bind-date :void
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val (:struct duckdb-date)))
(defcfun duckdb-bind-timestamp :void
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val (:struct duckdb-timestamp)))
(defcfun duckdb-bind-time :void
(prepared-statement duckdb-prepared-statement)
(param-idx idx)
(val (:struct duckdb-time)))
(defcfun duckdb-appender-create duckdb-state
(connection duckdb-connection)
(schema :string)
(table :string)
(out-appender duckdb-appender))
(defcfun duckdb-appender-error :string
(appender duckdb-appender))
(defcfun duckdb-appender-flush duckdb-state
(appender duckdb-appender))
(defcfun duckdb-appender-close duckdb-state
(appender duckdb-appender))
(defcfun duckdb-appender-destroy duckdb-state
(appender (:pointer duckdb-appender)))
(defcfun duckdb-appender-begin-row duckdb-state
(appender duckdb-appender))
(defcfun duckdb-appender-end-row duckdb-state
(appender duckdb-appender))
(defcfun duckdb-append-null duckdb-state
(appender duckdb-appender))
(defcfun duckdb-append-bool duckdb-state
(appender duckdb-appender)
(val :bool))
(defcfun duckdb-append-varchar duckdb-state
(appender duckdb-appender)
(val :string))
(defcfun duckdb-append-blob duckdb-state
(appender duckdb-appender)
(data (:pointer :void))
(length idx))
(defcfun duckdb-append-float duckdb-state
(appender duckdb-appender)
(val :float))
(defcfun duckdb-append-double duckdb-state
(appender duckdb-appender)
(val :double))
(defcfun duckdb-append-uint8 duckdb-state
(appender duckdb-appender)
(val :uint8))
(defcfun duckdb-append-uint16 duckdb-state
(appender duckdb-appender)
(val :uint16))
(defcfun duckdb-append-uint32 duckdb-state
(appender duckdb-appender)
(val :uint32))
(defcfun duckdb-append-uint64 duckdb-state
(appender duckdb-appender)
(val :uint64))
(defcfun duckdb-append-int8 duckdb-state
(appender duckdb-appender)
(val :int8))
(defcfun duckdb-append-int16 duckdb-state
(appender duckdb-appender)
(val :int16))
(defcfun duckdb-append-int32 duckdb-state
(appender duckdb-appender)
(val :int32))
(defcfun duckdb-append-int64 duckdb-state
(appender duckdb-appender)
(val :int64))
(defcfun duckdb-append-hugeint :void ; TODO: duckdb-state doesn't work
(appender duckdb-appender)
(val (:struct duckdb-hugeint)))
(defcfun duckdb-append-uhugeint :void
(appender duckdb-appender)
(val (:struct duckdb-uhugeint)))
(defcfun duckdb-append-date :void
(appender duckdb-appender)
(val (:struct duckdb-date)))
(defcfun duckdb-append-timestamp :void
(appender duckdb-appender)
(val (:struct duckdb-timestamp)))
(defcfun duckdb-append-time :void
(appender duckdb-appender)
(val (:struct duckdb-time)))
(defcfun duckdb-create-logical-type duckdb-logical-type
(type duckdb-type))
(defcfun duckdb-destroy-logical-type duckdb-logical-type
(type (:pointer duckdb-type)))
(defcfun duckdb-list-type-child-type duckdb-logical-type
(type duckdb-logical-type))
(defcfun duckdb-list-vector-get-child duckdb-vector
(vector duckdb-vector))
(defcfun duckdb-map-type-key-type duckdb-logical-type
(type duckdb-logical-type))
(defcfun duckdb-map-type-value-type duckdb-logical-type
(type duckdb-logical-type))
(defcfun duckdb-struct-type-child-count idx
(type duckdb-logical-type))
(defcfun duckdb-struct-type-child-name (:pointer :char)
(type duckdb-logical-type)
(index idx))
(defcfun duckdb-struct-type-child-type duckdb-logical-type
(type duckdb-logical-type)
(index idx))
(defcfun duckdb-union-type-member-count idx
(type duckdb-logical-type))
(defcfun duckdb-union-type-member-name (:pointer :char)
(type duckdb-logical-type)
(index idx))
(defcfun duckdb-union-type-member-type duckdb-logical-type
(type duckdb-logical-type)
(index idx))
(defcfun duckdb-struct-vector-get-child duckdb-vector
(vector duckdb-vector)
(index idx))
(defcfun duckdb-get-varchar :string
(value duckdb-value))
(defcfun duckdb-create-varchar duckdb-value
(value :string))
(defcfun duckdb-destroy-value :void
(value (:pointer duckdb-value)))
(defcfun duckdb-create-table-function duckdb-table-function)
(defcfun duckdb-destroy-table-function :void
(table-function (:pointer duckdb-table-function)))
(defcfun duckdb-table-function-set-name :void
(table-function duckdb-table-function)
(name :string))
(defcfun duckdb-table-function-add-parameter :void
(table-function duckdb-table-function)
(type duckdb-logical-type))
(defcfun duckdb-table-function-set-extra-info :void
(table-function duckdb-table-function)
(extra-info (:pointer :void))
(destroy :pointer))
(defcfun duckdb-table-function-set-bind :void
(table-function duckdb-table-function)
(bind :pointer))
(defcfun duckdb-table-function-set-init :void
(table-function duckdb-table-function)
(init :pointer))
(defcfun duckdb-table-function-set-local-init :void
(table-function duckdb-table-function)
(init :pointer))
(defcfun duckdb-table-function-set-function :void
(table-function duckdb-table-function)
(function :pointer))
(defcfun duckdb-table-function-supports-projection-pushdown :void
(table-function duckdb-table-function)
(pushdown :bool))
(defcfun duckdb-register-table-function duckdb-state
(connection duckdb-connection)
(function duckdb-table-function))
(defcfun duckdb-bind-add-result-column :void
(info duckdb-bind-info)
(name :string)
(type duckdb-logical-type))
(defcfun duckdb-bind-get-parameter-count idx
(info duckdb-bind-info))
(defcfun duckdb-bind-get-parameter duckdb-value
(info duckdb-bind-info)
(index idx))
(defcfun duckdb-bind-set-bind-data :void
(info duckdb-bind-info)
(bind-data (:pointer :void))
(destroy :pointer))
(defcfun duckdb-bind-set-cardinality :void
(info duckdb-bind-info)
(cardinality idx)
(is-exact :bool))
(defcfun duckdb-bind-set-error :void
(info duckdb-bind-info)
(error :string))
(defcfun duckdb-init-get-extra-info (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-init-get-bind-data (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-init-set-init-data (:pointer :void)
(info duckdb-bind-info)
(init-data (:pointer :void))
(destroy :pointer))
(defcfun duckdb-init-get-column-count idx
(info duckdb-bind-info))
(defcfun duckdb-init-get-column-index idx
(info duckdb-bind-info)
(column-index idx))
(defcfun duckdb-init-set-max-threads :void
(info duckdb-bind-info)
(max-threads idx))
(defcfun duckdb-init-set-error :void
(info duckdb-bind-info)
(error :string))
(defcfun duckdb-function-get-extra-info (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-function-get-bind-data (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-function-get-init-data (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-function-get-local-init-data (:pointer :void)
(info duckdb-bind-info))
(defcfun duckdb-function-set-error :void
(info duckdb-bind-info)
(error :string))
(defmacro with-duckdb-value ((value-var alloc-form) &body body)
`(let ((,value-var ,alloc-form))
(unwind-protect
(progn ,@body)
(with-foreign-object (p-value '(:pointer duckdb-value))
(setf (mem-ref p-value 'duckdb-value) ,value-var)
(duckdb-destroy-value p-value)))))
(defmacro with-table-function ((function-var) &body body)
`(let ((,function-var (duckdb-create-table-function)))
(unwind-protect
(progn ,@body)
(with-foreign-object (p-function '(:pointer duckdb-table-function))
(setf (mem-ref p-function 'duckdb-table-function) ,function-var)
(duckdb-destroy-table-function p-function)))))
(defcfun duckdb-add-replacement-scan :void
(db duckdb-database)
(replacement :pointer)
(extra-data (:pointer :void))
(delete-callback :pointer))
(defcfun duckdb-replacement-scan-set-function-name :void
(info duckdb-replacement-scan-info)
(function-name :string))
(defcfun duckdb-replacement-scan-add-parameter :void
(info duckdb-replacement-scan-info)
(parameter duckdb-value))
(defcfun duckdb-replacement-scan-set-error :void
(info duckdb-replacement-scan-info)
(error :string))
;;; Configuration
(defcfun duckdb-create-config duckdb-state
(out-config (:pointer duckdb-config)))
(defcfun duckdb-set-config duckdb-state
(config duckdb-config)
(name :string)
(option :string))
(defcfun duckdb-destroy-config :void
(config (:pointer duckdb-config)))
(defun create-config ()
(with-foreign-object (p-config 'duckdb-config)
(duckdb-create-config p-config)
(mem-ref p-config 'duckdb-config)))
(defun destroy-config (config)
(with-foreign-object (p-config 'duckdb-config)
(setf (mem-ref p-config 'duckdb-config) config)
(duckdb-destroy-config p-config)))
(defmacro with-config ((config-var config-plist) &body body)
(alexandria:with-gensyms (name option)
`(let ((,config-var (create-config)))
(unwind-protect
(progn
(alexandria:doplist (,name ,option ,config-plist)
(duckdb-set-config ,config-var
,name
(format nil "~a" ,option)))
,@body)
(destroy-config ,config-var)))))
;;; Threads
(defctype duckdb-task-state (:pointer :void))
(defcfun duckdb-execute-tasks :void
(database duckdb-database)
(max-tasks idx))
(defcfun duckdb-create-task-state duckdb-task-state
(database duckdb-database))
(defcfun duckdb-execute-tasks-state :void
(state duckdb-task-state))
(defcfun duckdb-execute-n-tasks-state idx
(state duckdb-task-state)
(max-tasks idx))
(defcfun duckdb-finish-execution :void
(state duckdb-task-state))
(defcfun duckdb-task-state-is-finished :bool
(state duckdb-task-state))
(defcfun duckdb-destroy-task-state :void
(state duckdb-task-state))
(defclass worker-pool ()
((task-state :initarg :task-state :accessor task-state)
(worker-threads :initarg :worker-threads :accessor worker-threads)))
(defun start-worker-pool (database n)
(let* ((task-state (duckdb-create-task-state database))
(worker-threads
(loop :for i :below n
:collect (bt2:make-thread
(lambda ()
(duckdb-execute-tasks-state task-state))
:name (format nil "DuckDB worker thread #~a" i)))))
(make-instance 'worker-pool :task-state task-state :worker-threads worker-threads)))
(defun stop-worker-pool (pool)
(when pool
(let ((task-state (task-state pool))
(worker-threads (worker-threads pool)))
(duckdb-finish-execution task-state)
(loop :for worker-thread :in worker-threads
:when (bt2:thread-alive-p worker-thread)
:do (bt2:join-thread worker-thread)
:finally (duckdb-destroy-task-state task-state)))))