iwongu / sqlite3pp

SQLite3++ - C++ wrapper of SQLite3 API
MIT License
609 stars 177 forks source link

added the possibility to assign an existent database handler #45

Closed fradav closed 7 years ago

fradav commented 7 years ago

this is required by the loadext doc: see there When we load an extension, the database handler is already there before we can call any sqlite3pp::database constructor, hence the assignement overload with sqlite3*.

There is still some boilerplate to manage to make the extension loadable:

sqlite3pp::database dbe;

extern "C" {
int sqlite3_extension_init(/* <== Change this name, maybe */
                           sqlite3 *db,
                           char **pzErrMsg,
                           const sqlite3_api_routines *pApi)
{
    int rc = SQLITE_OK;
    SQLITE_EXTENSION_INIT2(pApi);
    dbe = db;
    sqlite3pp::ext::aggregate aggr(dbe);
    aggr.create<strcnt, string>("aggr2");
    aggr.create<plussum, int, int>("aggr3");
    return rc;
}
}
iwongu commented 7 years ago

Check https://github.com/iwongu/sqlite3pp/commit/2285e1ba3e6437ed382a0945e39d6b0b34820e1e out. You can use like the following code.

// sqlite3* pdb;
sqlite3pp::database db(sqlite3pp::ext::borrow(pdb);

I'm trying to keep the construction using sqlite* from the basic sqlite3pp.h. In most cases, you might need sqlite3ppext.h to use function or aggregate to build extensions anyway.

fradav commented 7 years ago

It's a start but I need to if-elsify the import of sqlite3.h in order to compile the extension, see my PR.

iwongu commented 7 years ago

Check https://github.com/iwongu/sqlite3pp/commit/fd0ed12d436cc4d833f6510f7024b7f09312f3c9 out.

iwongu commented 7 years ago

https://github.com/iwongu/sqlite3pp/pull/46 is merged into master branch to support loadable extension. Thanks for great feature suggestion!