Skip to content

Commit b5be2e3

Browse files
committed
Simplify registerfunc logic.
1 parent b635c51 commit b5be2e3

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

src/UDF.jl

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,34 @@
1-
function registerfunc(db, name, nargs, func, step, final, isdeterm)
2-
#=
3-
Create a function in the database connection db.
4-
=#
5-
if func != C_NULL
6-
if !(step == final == C_NULL)
7-
msg = "step and final can not be defined for scalar functions"
8-
throw(SQLiteException(msg))
9-
end
10-
else
11-
if step == C_NULL || final == C_NULL
12-
msg = "both step and final must be defined for aggregate functions"
13-
throw(SQLiteException(msg))
14-
end
15-
if func != C_NULL
16-
msg = "func must not be defined for aggregate functions"
17-
throw(SQLiteException(msg))
18-
end
19-
end
20-
1+
# scalar functions
2+
function registerfunc(db::SQLiteDB, nargs::Integer, func::Function, isdeterm::Bool=true; name="")
213
@assert (-1 <= nargs <= 127) "nargs must follow the inequality -1 <= nargs <= 127"
224

23-
cfunc = func == C_NULL ? C_NULL : cfunction(
24-
func, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})
25-
)
26-
cstep = step == C_NULL ? C_NULL : cfunction(
27-
step, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})
28-
)
29-
cfinal = final == C_NULL ? C_NULL : cfunction(
30-
step, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})
31-
)
5+
name = isempty(name) ? string(func) : name::String
6+
cfunc = cfunction(func, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}}))
327

338
# TODO: allow the other encodings
349
enc = SQLITE_UTF8
3510
enc = isdeterm ? enc | SQLITE_DETERMINISTIC : enc
3611

3712
@CHECK db sqlite3_create_function_v2(
38-
db.handle, name, nargs, enc, C_NULL, cfunc, cstep, cfinal, C_NULL
13+
db.handle, name, nargs, enc, C_NULL, cfunc, C_NULL, C_NULL, C_NULL
3914
)
4015
end
4116

42-
# scalar functions
43-
function registerfunc(db::SQLiteDB, nargs::Integer, func::Function,
44-
isdeterm::Bool=true; name="")
45-
name = isempty(name) ? string(func) : name::String
46-
registerfunc(db, name, nargs, func, C_NULL, C_NULL, isdeterm)
47-
end
48-
4917
# aggregate functions
50-
function registerfunc(db::SQLiteDB, nargs::Integer, step::Function,
51-
final::Function, isdeterm::Bool=true; name="")
18+
function registerfunc(db::SQLiteDB, nargs::Integer, step::Function, final::Function, isdeterm::Bool=true; name="")
19+
@assert (-1 <= nargs <= 127) "nargs must follow the inequality -1 <= nargs <= 127"
20+
5221
name = isempty(name) ? string(step) : name::String
53-
registerfunc(db, name, nargs, C_NULL, step, final, isdeterm)
22+
cstep = cfunction(step, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}}))
23+
cfinal = cfunction(final, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}}))
24+
25+
# TODO: allow the other encodings
26+
enc = SQLITE_UTF8
27+
enc = isdeterm ? enc | SQLITE_DETERMINISTIC : enc
28+
29+
@CHECK db sqlite3_create_function_v2(
30+
db.handle, name, nargs, enc, C_NULL, C_NULL, cstep, cfinal, C_NULL
31+
)
5432
end
5533

5634
function sqlvalue(values, i)

0 commit comments

Comments
 (0)