44
44
sqlreturn (context, val:: Bool ) = sqlreturn (context, Int (val))
45
45
sqlreturn (context, val) = sqlreturn (context, sqlserialize (val))
46
46
47
+ mutable struct ScalarUDFData
48
+ func:: Function
49
+ end
50
+
51
+ mutable struct AggregateUDFData
52
+ init:: Any
53
+ step:: Function
54
+ final:: Function
55
+ end
56
+
47
57
function wrap_scalarfunc (
48
- func,
49
58
context:: Ptr{Cvoid} ,
50
59
nargs:: Cint ,
51
60
values:: Ptr{Ptr{Cvoid}} ,
52
61
)
62
+ udf_data =
63
+ unsafe_pointer_to_objref (C. sqlite3_user_data (context)):: ScalarUDFData
64
+ func = udf_data. func
65
+
53
66
args = [sqlvalue (values, i) for i in 1 : nargs]
54
67
ret = func (args... )
55
68
sqlreturn (context, ret)
@@ -70,12 +83,15 @@ function bytestoint(ptr::Ptr{UInt8}, start::Int, len::Int)
70
83
end
71
84
72
85
function wrap_stepfunc (
73
- init,
74
- func,
75
86
context:: Ptr{Cvoid} ,
76
87
nargs:: Cint ,
77
88
values:: Ptr{Ptr{Cvoid}} ,
78
89
)
90
+ udf_data =
91
+ unsafe_pointer_to_objref (C. sqlite3_user_data (context)):: AggregateUDFData
92
+ init = udf_data. init
93
+ func = udf_data. step
94
+
79
95
args = [sqlvalue (values, i) for i in 1 : nargs]
80
96
81
97
intsize = sizeof (Int)
@@ -143,12 +159,15 @@ function wrap_stepfunc(
143
159
end
144
160
145
161
function wrap_finalfunc (
146
- init,
147
- func,
148
162
context:: Ptr{Cvoid} ,
149
163
nargs:: Cint ,
150
164
values:: Ptr{Ptr{Cvoid}} ,
151
165
)
166
+ udf_data =
167
+ unsafe_pointer_to_objref (C. sqlite3_user_data (context)):: AggregateUDFData
168
+ init = udf_data. init
169
+ func = udf_data. final
170
+
152
171
acptr = convert (Ptr{UInt8}, C. sqlite3_aggregate_context (context, 0 ))
153
172
154
173
# step function wasn't run
@@ -201,7 +220,7 @@ Register a scalar (first method) or aggregate (second method) function
201
220
with a [`SQLite.DB`](@ref).
202
221
"""
203
222
function register (
204
- db,
223
+ db:: DB ,
205
224
func:: Function ;
206
225
nargs:: Int = - 1 ,
207
226
name:: AbstractString = string (func),
@@ -212,11 +231,12 @@ function register(
212
231
nargs < - 1 && (nargs = - 1 )
213
232
@assert sizeof (name) <= 255 " size of function name must be <= 255"
214
233
215
- f =
216
- (context, nargs, values) ->
217
- wrap_scalarfunc (func, context, nargs, values)
218
- cfunc = @cfunction ($ f, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
219
- push! (db. registered_UDFs, cfunc)
234
+ udf_data = ScalarUDFData (func)
235
+ push! (db. registered_UDF_data, udf_data)
236
+ udf_data_ptr = pointer_from_objref (udf_data)
237
+
238
+ cfunc =
239
+ @cfunction (wrap_scalarfunc, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
220
240
221
241
# TODO : allow the other encodings
222
242
enc = C. SQLITE_UTF8
@@ -227,7 +247,7 @@ function register(
227
247
name,
228
248
nargs,
229
249
enc,
230
- C_NULL ,
250
+ udf_data_ptr ,
231
251
cfunc,
232
252
C_NULL ,
233
253
C_NULL ,
237
257
238
258
# as above but for aggregate functions
239
259
function register (
240
- db,
260
+ db:: DB ,
241
261
init,
242
262
step:: Function ,
243
263
final:: Function = identity;
@@ -249,16 +269,12 @@ function register(
249
269
nargs < - 1 && (nargs = - 1 )
250
270
@assert sizeof (name) <= 255 " size of function name must be <= 255 chars"
251
271
252
- s =
253
- (context, nargs, values) ->
254
- wrap_stepfunc (init, step, context, nargs, values)
255
- cs = @cfunction ($ s, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
256
- f =
257
- (context, nargs, values) ->
258
- wrap_finalfunc (init, final, context, nargs, values)
259
- cf = @cfunction ($ f, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
260
- push! (db. registered_UDFs, cs)
261
- push! (db. registered_UDFs, cf)
272
+ udf_data = AggregateUDFData (init, step, final)
273
+ push! (db. registered_UDF_data, udf_data)
274
+ udf_data_ptr = pointer_from_objref (udf_data)
275
+
276
+ cs = @cfunction (wrap_stepfunc, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
277
+ cf = @cfunction (wrap_finalfunc, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Ptr{Cvoid}}))
262
278
263
279
enc = C. SQLITE_UTF8
264
280
enc = isdeterm ? enc | C. SQLITE_DETERMINISTIC : enc
@@ -268,7 +284,7 @@ function register(
268
284
name,
269
285
nargs,
270
286
enc,
271
- C_NULL ,
287
+ udf_data_ptr ,
272
288
C_NULL ,
273
289
cs,
274
290
cf,
0 commit comments