identification of user function
(1) By anonymous on 2020-09-27 15:48:44 [link] [source]
Given the following code: sqlite3_create_function(db,"daysinmonth",3,cf_flags,0,days_in_month,0,0); sqlite3_create_function(db,"dim",3,cf_flags,0,days_in_month,0,0); Is there a way for the function days_in_month() to identify which form was used in the SQL - DaysInMonth() or DIM() ?
(2) By Richard Hipp (drh) on 2020-09-27 16:05:46 in reply to 1 [source]
Pass a different value into sqlite3_create_function() for the pApp parameter, and then inside of dates_in_month(), check the result of sqlite3_user_data() to figure out which form of the function was invoked.
(3.1) By Keith Medcalf (kmedcalf) on 2020-09-27 21:34:20 edited from 3.0 in reply to 1 [link] [source]
I do this in my extensions all the time by passing a parameter in the pApp and checking its value in the function using the sqlite3_user_data API as Richard suggests -- this is why that functionality exists.
sqlite3_create_function(db,"daysinmonth",3,cf_flags,(void*)0,days_in_month,0,0);
sqlite3_create_function(db,"dim",3,cf_flags,(void*)1,days_in_month,0,0);
then inside days_in_month you simply use the passed value:
if ((intptr_t)sqlite3_user_data(context) == 0)
{
... user called daysinmonth" ...
}
if ((intptr_t)sqlite3_user_data(context) == 1)
{
... user called "dim" ...
}
The same can be used for aggregate and window functions as well. In fact, I have some functions that use the same C function to implement dozens of different API functions (ie, the SQL functions all implement the same thing just slightly differently differently -- for example a Hash function which can use any of a dozen different algorothms).
NB: Edited the typo (void^)1 to the correct (void*)1
(4) By anonymous on 2020-09-27 19:48:18 in reply to 2 [link] [source]
Thanks! That was what I was looking for.
(5) By anonymous on 2020-09-27 19:49:43 in reply to 3.0 [link] [source]
And that was the next part I was working on - how to pass integers. Thanks!