21 feb 2016, Igor Tandetnik:

> 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

Hello Oliver, I have a tool that is purely within SQLite. I once named  
it sqlite_mprint. It is far from the requirement but may be a start.

sqlite> .load sqlite_mprint
sqlite> select mprint (datetime('now')); --set time
2016-02-22 14:30:03
sqlite> select mprint() as t0; --get old time
2016-02-22 14:30:03

The code is temporarily at  
http://h1972688.stratoserver.net/sqlite_mprint/160222

Best regards, E. Pasma


Reply via email to