|
1 |
| -# scalar functions |
2 |
| -function registerfunc(db::SQLiteDB, nargs::Integer, func::Function, isdeterm::Bool=true; name="") |
3 |
| - @assert nargs <= 127 "only varargs functions can have more than 127 arguments" |
4 |
| - # assume any negative number means a varargs function |
5 |
| - nargs < -1 && (nargs = -1) |
6 |
| - |
7 |
| - name = isempty(name) ? string(func) : name::AbstractString |
8 |
| - @assert sizeof(name) <= 255 "size of function name must be <= 255" |
9 |
| - |
10 |
| - cfunc = cfunction(func, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})) |
11 |
| - |
12 |
| - # TODO: allow the other encodings |
13 |
| - enc = SQLITE_UTF8 |
14 |
| - enc = isdeterm ? enc | SQLITE_DETERMINISTIC : enc |
15 |
| - |
16 |
| - @CHECK db sqlite3_create_function_v2( |
17 |
| - db.handle, name, nargs, enc, C_NULL, cfunc, C_NULL, C_NULL, C_NULL |
18 |
| - ) |
19 |
| -end |
20 |
| - |
21 |
| -# aggregate functions |
22 |
| -function registerfunc(db::SQLiteDB, nargs::Integer, step::Function, final::Function, isdeterm::Bool=true; name="") |
23 |
| - @assert nargs <= 127 "only varargs functions can have more than 127 arguments" |
24 |
| - # assume any negative number means a varargs function |
25 |
| - nargs < -1 && (nargs = -1) |
26 |
| - |
27 |
| - name = isempty(name) ? string(step) : name::AbstractString |
28 |
| - cstep = cfunction(step, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})) |
29 |
| - cfinal = cfunction(final, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})) |
30 |
| - |
31 |
| - # TODO: allow the other encodings |
32 |
| - enc = SQLITE_UTF8 |
33 |
| - enc = isdeterm ? enc | SQLITE_DETERMINISTIC : enc |
34 |
| - |
35 |
| - @CHECK db sqlite3_create_function_v2( |
36 |
| - db.handle, name, nargs, enc, C_NULL, C_NULL, cstep, cfinal, C_NULL |
37 |
| - ) |
38 |
| -end |
39 |
| - |
40 | 1 | function sqlvalue(values, i)
|
41 | 2 | temp_val_ptr = unsafe_load(values, i)
|
42 | 3 | valuetype = sqlite3_value_type(temp_val_ptr)
|
@@ -76,30 +37,55 @@ sqlreturn(context, val::Bool) = sqlreturn(context, int(val))
|
76 | 37 | sqludferror(context, msg::AbstractString) = sqlite3_result_error(context, msg)
|
77 | 38 | sqludferror(context, msg::UTF16String) = sqlite3_result_error16(context, msg)
|
78 | 39 |
|
79 |
| -function funcname(expr) |
80 |
| - if length(expr) == 2 |
81 |
| - func = expr[2] |
82 |
| - name = expr[1] |
83 |
| - else |
84 |
| - func = expr[1] |
85 |
| - name = func.args[1].args[1] |
86 |
| - end |
87 |
| - name, func |
88 |
| -end |
89 |
| - |
90 |
| -macro scalarfunc(args...) |
91 |
| - name, func = funcname(args) |
| 40 | +# Internal method for generating an SQLite scalar function from |
| 41 | +# a Julia function name |
| 42 | +function scalarfunc(func,fsym=symbol(string(func))) |
| 43 | + # check if name defined in Base so we don't clobber Base methods |
| 44 | + nm = isdefined(Base,fsym) ? :(Base.$fsym) : fsym |
92 | 45 | return quote
|
93 |
| - function $(esc(name))(context::Ptr{Void}, nargs::Cint, values::Ptr{Ptr{Void}}) |
94 |
| - args = [sqlvalue(values, i) for i in 1:nargs] |
| 46 | + #nm needs to be a symbol or expr, i.e. :sin or :(Base.sin) |
| 47 | + function $(nm)(context::Ptr{Void}, nargs::Cint, values::Ptr{Ptr{Void}}) |
| 48 | + args = [SQLite.sqlvalue(values, i) for i in 1:nargs] |
95 | 49 | ret = $(func)(args...)
|
96 |
| - sqlreturn(context, ret) |
| 50 | + SQLite.sqlreturn(context, ret) |
97 | 51 | nothing
|
98 | 52 | end
|
99 | 53 | end
|
100 | 54 | end
|
| 55 | +function scalarfunc(expr::Expr) |
| 56 | + f = eval(expr) |
| 57 | + return scalarfunc(f) |
| 58 | +end |
| 59 | +# User-facing macro for convenience in registering a simple function |
| 60 | +# with no configurations needed |
| 61 | +macro register(db, func) |
| 62 | + :(register($(esc(db)), $(esc(func)))) |
| 63 | +end |
| 64 | +# User-facing method with keyword arguments for registering a function |
| 65 | +# to be used within SQLite |
| 66 | +function register(db::SQLiteDB, func::Function; nargs::Int=-1, isdeterm::Bool=true, name::String=string(func)) |
| 67 | + register(db, func, nargs, isdeterm, name) |
| 68 | +end |
| 69 | +# User-facing method for registering a Julia function to be used within SQLite |
| 70 | +function register(db::SQLiteDB, func::Function, nargs::Int=-1, isdeterm::Bool=true, name::String=string(func)) |
| 71 | + @assert nargs <= 127 "use -1 if > 127 arguments are needed" |
| 72 | + # assume any negative number means a varargs function |
| 73 | + nargs < -1 && (nargs = -1) |
| 74 | + @assert sizeof(name) <= 255 "size of function name must be <= 255" |
| 75 | + |
| 76 | + f = eval(scalarfunc(func,symbol(name))) |
| 77 | + |
| 78 | + cfunc = cfunction(f, Nothing, (Ptr{Void}, Cint, Ptr{Ptr{Void}})) |
| 79 | + # TODO: allow the other encodings |
| 80 | + enc = SQLITE_UTF8 |
| 81 | + enc = isdeterm ? enc | SQLITE_DETERMINISTIC : enc |
| 82 | + |
| 83 | + @CHECK db sqlite3_create_function_v2( |
| 84 | + db.handle, name, nargs, enc, C_NULL, cfunc, C_NULL, C_NULL, C_NULL |
| 85 | + ) |
| 86 | +end |
101 | 87 |
|
102 | 88 | # annotate types because the MethodError makes more sense that way
|
103 |
| -@scalarfunc regexp(r::AbstractString, s::AbstractString) = ismatch(Regex(r), s) |
| 89 | +regexp(r::String, s::String) = ismatch(Regex(r), s) |
104 | 90 | # macro for preserving the special characters in a string
|
105 | 91 | macro sr_str(s) s end
|
0 commit comments