# # # patch "database.cc" # from [471ca42ba84aaf3d978e3a52ce1d2ea18b7c6eed] # to [c1f3a79f013df8595779a3f1f65e20a7f4e0b2ea] # # patch "schema_migration.cc" # from [902ae090438daba762cc6b8af3aedc5dfa6586e3] # to [8ad58d8be2a5a3507f26923363b94a4996e08718] # ============================================================ --- database.cc 471ca42ba84aaf3d978e3a52ce1d2ea18b7c6eed +++ database.cc c1f3a79f013df8595779a3f1f65e20a7f4e0b2ea @@ -393,6 +393,36 @@ private: query & q); }; +#if SQLITE_VERSION_NUMBER < 3003013 +// sqlite before version 3.3.13 didn't have the hex() function. + +void +sqlite3_hex_fn(sqlite3_context *f, int nargs, sqlite3_value **args) +{ + if (nargs != 1) + { + sqlite3_result_error(f, "need exactly 1 arg to hex()", -1); + return; + } + string decoded; + + // This operation may throw informative_failure. We must intercept that + // and turn it into a call to sqlite3_result_error, or rollback will fail. + try + { + decoded = encode_hexenc(reinterpret_cast( + sqlite3_value_text(args[0]))); + } + catch (informative_failure & e) + { + sqlite3_result_error(f, e.what(), -1); + return; + } + + sqlite3_result_blob(f, decoded.data(), decoded.size(), SQLITE_TRANSIENT); +} +#endif + database_impl::database_impl(system_path const & f) : filename(f), __sql(NULL), @@ -2889,6 +2919,13 @@ database_impl::install_functions() void database_impl::install_functions() { +#if SQLITE_VERSION_NUMBER < 3003013 + I(sqlite3_create_function(sql(), "hex", -1, + SQLITE_UTF8, NULL, + &sqlite3_hex_fn, + NULL, NULL) == 0); +#endif + // register any functions we're going to use I(sqlite3_create_function(sql(), "gunzip", -1, SQLITE_UTF8, NULL, ============================================================ --- schema_migration.cc 902ae090438daba762cc6b8af3aedc5dfa6586e3 +++ schema_migration.cc 8ad58d8be2a5a3507f26923363b94a4996e08718 @@ -1000,6 +1000,11 @@ check_sql_schema(sqlite3 * db, system_pa % filename % ui.prog_name); } +// import the hex function for old sqlite libraries +#if SQLITE_VERSION_NUMBER < 3003013 +void sqlite3_hex_fn(sqlite3_context *f, int nargs, sqlite3_value **args); +#endif + void migrate_sql_schema(sqlite3 * db, key_store & keys, system_path const & filename) @@ -1036,6 +1041,9 @@ migrate_sql_schema(sqlite3 * db, key_sto return; } +#if SQLITE_VERSION_NUMBER < 3003013 + sql::create_function(db, "hex", sqlite3_hex_fn); +#endif sql::create_function(db, "sha1", sqlite_sha1_fn); sql::create_function(db, "unbase64", sqlite3_unbase64_fn); sql::create_function(db, "unhex", sqlite3_unhex_fn); @@ -1107,6 +1115,10 @@ test_migration_step(sqlite3 * db, key_st string const & schema) { I(db != NULL); + +#if SQLITE_VERSION_NUMBER < 3003013 + sql::create_function(db, "hex", sqlite3_hex_fn); +#endif sql::create_function(db, "sha1", sqlite_sha1_fn); sql::create_function(db, "unbase64", sqlite3_unbase64_fn); sql::create_function(db, "unhex", sqlite3_unhex_fn);