@@ -23,18 +23,7 @@ using namespace std;
2323 * connection & tuning helpers
2424 **************************************************************************************************/
2525
26- std::string GenomicSQLiteVersion () {
27- // The newest SQLite feature currently required is "Generated Columns"
28- const int MIN_SQLITE_VERSION_NUMBER = 3031000 ;
29- const string MIN_SQLITE_VERSION = " 3.31.0" ;
30- if (sqlite3_libversion_number () < MIN_SQLITE_VERSION_NUMBER) {
31- string version_msg = " SQLite library version (" + string (sqlite3_libversion ()) +
32- " ) is older than required by the GenomicSQLite extension (" +
33- MIN_SQLITE_VERSION + " )" ;
34- throw std::runtime_error (version_msg);
35- }
36- return string (GIT_REVISION);
37- }
26+ std::string GenomicSQLiteVersion () { return string (GIT_REVISION); }
3827
3928// boilerplate for C bindings to C++ functions
4029#define C_WRAPPER (call ) \
@@ -268,50 +257,37 @@ static void sqlfn_genomicsqlite_tuning_sql(sqlite3_context *ctx, int argc, sqlit
268257 SQL_WRAPPER (GenomicSQLiteTuningSQL (config_json, schema))
269258}
270259
271- bool _ext_loaded = false ;
260+ static void ensure_ext_loaded () {
261+ // for C/C++ GenomicSQLiteOpen
262+ static bool ext_loaded = false ;
263+ if (!ext_loaded) {
264+ SQLite::Database db (" :memory:" , SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
265+ db.loadExtension (" libgenomicsqlite" , nullptr );
266+ ext_loaded = true ;
267+ }
268+ }
269+
272270int GenomicSQLiteOpen (const string &dbfile, sqlite3 **ppDb, string &errmsg_out, int flags,
273271 const string &config_json) noexcept {
274- // ensure extension is registered
275- int ret;
276- char *zErrmsg = nullptr ;
277- *ppDb = nullptr ;
278- if (!_ext_loaded) {
279- ret =
280- sqlite3_open_v2 (" :memory:" , ppDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr );
281- if (ret != SQLITE_OK) {
282- errmsg_out = sqlite3_errstr (ret);
283- return ret;
284- }
285- ret = sqlite3_load_extension (*ppDb, " libgenomicsqlite" , nullptr , &zErrmsg);
286- if (ret != SQLITE_OK) {
287- errmsg_out = " failed loading libgenomicsqlite shared library: " ;
288- if (zErrmsg) {
289- errmsg_out += zErrmsg;
290- sqlite3_free (zErrmsg);
291- } else {
292- errmsg_out += sqlite3_errmsg (*ppDb);
293- }
294- sqlite3_close_v2 (*ppDb);
295- *ppDb = nullptr ;
296- return ret;
297- }
298- ret = sqlite3_close_v2 (*ppDb);
299- *ppDb = nullptr ;
300- if (ret != SQLITE_OK) {
301- errmsg_out = sqlite3_errstr (ret);
302- return ret;
303- }
304- _ext_loaded = true ;
272+ try {
273+ ensure_ext_loaded ();
274+ } catch (SQLite::Exception &exn) {
275+ errmsg_out = " failed loading libgenomicsqlite shared library: " + string (exn.what ());
276+ return exn.getErrorCode ();
277+ } catch (std::exception &exn) {
278+ errmsg_out = " failed loading libgenomicsqlite shared library: " + string (exn.what ());
279+ return SQLITE_ERROR;
305280 }
306281
307282 // open as requested
308283 try {
309- ret = sqlite3_open_v2 (GenomicSQLiteURI (dbfile, config_json).c_str (), ppDb,
310- SQLITE_OPEN_URI | flags, nullptr );
284+ int ret = sqlite3_open_v2 (GenomicSQLiteURI (dbfile, config_json).c_str (), ppDb,
285+ SQLITE_OPEN_URI | flags, nullptr );
311286 if (ret != SQLITE_OK) {
312287 errmsg_out = sqlite3_errstr (ret);
313288 return ret;
314289 }
290+ char *zErrmsg = nullptr ;
315291 ret = sqlite3_exec (*ppDb, GenomicSQLiteTuningSQL (config_json).c_str (), nullptr , nullptr ,
316292 &zErrmsg);
317293 if (ret != SQLITE_OK) {
@@ -327,8 +303,10 @@ int GenomicSQLiteOpen(const string &dbfile, sqlite3 **ppDb, string &errmsg_out,
327303 }
328304 return SQLITE_OK;
329305 } catch (SQLite::Exception &exn) {
306+ errmsg_out = exn.what ();
330307 return exn.getErrorCode ();
331308 } catch (std::exception &exn) {
309+ errmsg_out = exn.what ();
332310 return SQLITE_ERROR;
333311 }
334312}
@@ -347,18 +325,9 @@ extern "C" int genomicsqlite_open(const char *filename, sqlite3 **ppDb, char **p
347325
348326unique_ptr<SQLite::Database> GenomicSQLiteOpen (const string &dbfile, int flags,
349327 const string &config_json) {
350- unique_ptr<SQLite::Database> db;
351- if (!_ext_loaded) {
352- db.reset (new SQLite::Database (" :memory:" , SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE));
353- try {
354- db->loadExtension (" libgenomicsqlite" , nullptr );
355- } catch (std::exception &exn) {
356- throw std::runtime_error (" failed loading libgenomicsqlite shared library: " +
357- string (exn.what ()));
358- }
359- _ext_loaded = true ;
360- }
361- db.reset (new SQLite::Database (GenomicSQLiteURI (dbfile, config_json), SQLITE_OPEN_URI | flags));
328+ ensure_ext_loaded ();
329+ unique_ptr<SQLite::Database> db (
330+ new SQLite::Database (GenomicSQLiteURI (dbfile, config_json), SQLITE_OPEN_URI | flags));
362331 db->exec (GenomicSQLiteTuningSQL (config_json));
363332 return db;
364333}
0 commit comments