On 2/21/2016 12:09 PM, Olivier Mascia wrote:
> Is it possible to implement a SQL function 
> (https://www.sqlite.org/c3ref/create_function.html), which implementation 
> would be able to return the same value for the duration of the current 
> transaction?
>
> In other words, assume I'd like to implement some function which I could name 
> 'now()' which would return the current_timestamp as julian day, but would 
> return exactly the same value for any call made during the same transaction?

When I needed a stable version of now(), this is what I did. I had a 
global (well, actually, per-connection) time_t variable. I passed its 
address to sqlite3_create_function, to be retrieved within the custom 
function's implementation by means of sqlite3_user_data.

The custom function would check this variable. If it's zero, it would 
obtain the current timestamp save it in that variable, and return it as 
its result; otherwise, it would return the previously-cached value.

Whenever I started a new transaction, I reset the variable to zero. 
Luckily, I had a single spot issuing BEGIN statements (a C++ class 
wrapping SQLite connection handle; the time_t variable was also part of 
that class).
-- 
Igor Tandetnik

Reply via email to