nalgeon / sqlean

The ultimate set of SQLite extensions
MIT License
3.65k stars 115 forks source link

problem with a function defined with the define extension that runs on Windows but not on Linux (wsl2) #91

Closed phrrngtn closed 1 year ago

phrrngtn commented 1 year ago

I encountered an odd problem with different behavior of sqlean on Windows vs Linux (wsl2). The code in question is a simple function in the define extension. The code seems to be fine when run outside of define but causes a "Runtime error: bad parameter or other API misuse (21)" when run as a define function (both explicitly as below and as part of a trigger, which is where I found the problem for the first place).

sqlean> select * FROM sqlean_define where name = 'time_t_ms';
time_t_ms|scalar|format("%d.%d", strftime("%s","now"),substr(strftime("%f","now"),4))
sqlean> select format("%d.%d", strftime("%s","now"),substr(strftime("%f","now"),4));
1692412460.849
sqlean> select time_t_ms();
Runtime error: bad parameter or other API misuse (21)

sqlean> .version
SQLite 3.42.0 2023-05-16 12:36:15 831d0fb2836b71c9bc51067c49fee4b8f18047814f2ff22d817d25195cf350b0
zlib version 1.2.11
gcc-9.4.0
phrrngtn commented 1 year ago

for comparison, here is the same thing on Windows. The main obvious difference is the version of gcc.

C:\packages\cmake>\tools\sqlean.exe c:\data\soc.db3
SQLite version 3.42.0 2023-05-16 12:36:15
Enter ".help" for usage hints.
sqlean> select time_t_ms();
1692413338.489
sqlean> .version
SQLite 3.42.0 2023-05-16 12:36:15 831d0fb2836b71c9bc51067c49fee4b8f18047814f2ff22d817d25195cf350b0
gcc-8.1.0
sqlean> select * FROM sqlean_define where name = 'time_t_ms';
time_t_ms|scalar|format("%d.%d", strftime("%s","now"),substr(strftime("%f","now"),4))
phrrngtn commented 1 year ago

The problem appears to lie elsewhere: I created the function in another database on Linux and it ran fine. There is a large database initialization script which has two-level nested .read commands; uses parameters, triggers, extensions, templating and a bunch more! (I am trying an experiment to see how much I can do with the sqlean shell as the programming environment). I presume there is some statement handle active or something similar. I am really curious as to why the problem appears to be OS-specific. I have never encountered any differences in behavior before.

phrrngtn commented 1 year ago

Since a lot of the schema is templated, I changed the definition of my temporal backlog maintenance triggers to use regular SQLite functions rather than those created by define and everything is running correctly. I will avoid using define via triggers

format("%d.%d", strftime("%s","now"),substr(strftime("%f","now"),4))