Dan Bishop wrote:
> It's convenient to be able to define new functions in C.  But sometimes,
> it would be *more* convenient to be able to define new functions in
> SQL.  This could be done by registering a CREATE_FUNCTION() function;
> then you could write something like:
> 
> SELECT CREATE_FUNCTION('LEFT', 2, 'SUBSTR(?1, 1, ?2)');
> 
> My first idea for implementing CREATE_FUNCTION is:
> 
> 1. Create (if not exists) a table with columns for the function name,
> number of arguments, and SQL expression.
> 2. Add the new function to the table.
> 3. Call sqlite3_create_function to register the new function.
> 
> C doesn't have the ability to create functions at runtime, so the xFunc
> parameter would refer to a common global function, which would:
> 
> 1. Look up the SQL expression corresponding to the SQL function name.
> 2. Evaluate the expression.
> 
> But how do I get the SQL function name from within the xFunc function?
> Can I get it from the sqlite3_context object, or do I have to use
> sqlite3_user_data()?

You have to use sqlite3_user_data. In fact, it's not clear why you would want 
to store anything in the table. Just allocate some structure describing your 
new "function" (containing the same information that you planned to store in 
that table), and pass its address to sqlite3_create_function, to be picked up 
with sqlite3_user_data.
-- 
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to