Does someone know a wrapper which would allow SQLite to load its data from an std::iostream?
To be more explicit:
std::fstream dataStream("database.sqlite");
...
sqlite3_open(...something using dataStream...);
I want to use streams because of their modularity: being able to load a file while it is still being downloaded from the network, decompressing data on-the-fly, etc.
It should be possible by using sqlite3_open_v2 after registering your own VFS. After one hour of implementing (badly-documented) functions, my first attempts got me a weird “out of memory” error from SQLite, and I thought I would ask here if someone knows an existing implementation before spending hours debugging mine.
Since nobody seems to know an existing implementation, I coded one myself 🙂
How does it work:
sqlite3_open_v2with the name of the custom file system as the last parameterstd::shared_ptr<std::iostream>sqlite3_open_v2is the lexical_cast‘ing of astd::shared_ptr<std::iostream>*; the open function of the file system will make a copy of thisshared_ptrYou may wonder why I used lexical_cast while I could have used
reinterpret_cast<const char*>(&stream). This is because SQLite callsstrcpyon the file name, and this could cause buffer overflow or data truncation issues. Also SQLite takes our file name and appends “-journal” or “-wal” in order to generate temporary file names. This would cause other problems.The code is really far from being perfect (some things are not implemented) but it’s working for some of the basic operations I tried (creating a table, inserting some entries and selecting).
EDIT: temporary tables and commit/rollbacks are working too