From ab5a04fe8b57a6b1b27050b06ff8588220ba49d8 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Fri, 28 Feb 2020 16:07:41 +0200 Subject: [PATCH] Allow sqlite to copy only temporaries Signed-off-by: Anthony Fieroni --- src/claimtrie/sqlite.h | 16 ++++++++ .../hdr/sqlite_modern_cpp/type_wrapper.h | 39 +++++++++++++++++-- src/txdb.h | 8 ++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/claimtrie/sqlite.h b/src/claimtrie/sqlite.h index bb9be35c4..ff1798b5a 100644 --- a/src/claimtrie/sqlite.h +++ b/src/claimtrie/sqlite.h @@ -14,15 +14,31 @@ return sqlite3_bind_blob(stmt, inx, val.begin(), int(val.size()), SQLITE_STATIC); } + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, uint160&& val) { + return sqlite3_bind_blob(stmt, inx, val.begin(), int(val.size()), SQLITE_TRANSIENT); + } + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const uint256& val) { return sqlite3_bind_blob(stmt, inx, val.begin(), int(val.size()), SQLITE_STATIC); } + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, uint256&& val) { + return sqlite3_bind_blob(stmt, inx, val.begin(), int(val.size()), SQLITE_TRANSIENT); + } + inline void store_result_in_db(sqlite3_context* db, const uint160& val) { + sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_STATIC); + } + + inline void store_result_in_db(sqlite3_context* db, uint160&& val) { sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_TRANSIENT); } inline void store_result_in_db(sqlite3_context* db, const uint256& val) { + sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_STATIC); + } + + inline void store_result_in_db(sqlite3_context* db, uint256&& val) { sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_TRANSIENT); } } diff --git a/src/claimtrie/sqlite/hdr/sqlite_modern_cpp/type_wrapper.h b/src/claimtrie/sqlite/hdr/sqlite_modern_cpp/type_wrapper.h index b915ef83e..3538343e8 100644 --- a/src/claimtrie/sqlite/hdr/sqlite_modern_cpp/type_wrapper.h +++ b/src/claimtrie/sqlite/hdr/sqlite_modern_cpp/type_wrapper.h @@ -32,14 +32,18 @@ #include namespace sqlite { - typedef const std::string_view str_ref; - typedef const std::u16string_view u16str_ref; + typedef const std::string_view& str_ref; + typedef std::string_view&& str_uni_ref; + typedef const std::u16string_view& u16str_ref; + typedef std::u16string_view&& u16str_uni_ref; } #else namespace sqlite { typedef const std::string& str_ref; + typedef std::string&& str_uni_ref; typedef const std::u16string& u16str_ref; + typedef std::u16string&& u16str_uni_ref; } #endif #include "../../sqlite3.h" @@ -182,12 +186,17 @@ namespace sqlite { template<> struct has_sqlite_type : std::true_type {}; // inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, str_ref val) { + return sqlite3_bind_blob(stmt, inx, val.data(), val.length(), SQLITE_STATIC); + } + + // str_uni_ref + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, str_uni_ref val) { return sqlite3_bind_blob(stmt, inx, val.data(), val.length(), SQLITE_TRANSIENT); } // Convert char* to string_view to trigger op<<(..., const str_ref ) template inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char(&STR)[N]) { - return sqlite3_bind_blob(stmt, inx, &STR[0], N-1, SQLITE_TRANSIENT); + return sqlite3_bind_blob(stmt, inx, &STR[0], N-1, SQLITE_STATIC); } inline std::string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type) { @@ -220,18 +229,26 @@ namespace sqlite { } inline void store_result_in_db(sqlite3_context* db, str_ref val) { + sqlite3_result_blob(db, val.data(), val.length(), SQLITE_STATIC); + } + + inline void store_result_in_db(sqlite3_context* db, str_uni_ref val) { sqlite3_result_blob(db, val.data(), val.length(), SQLITE_TRANSIENT); } // u16str_ref template<> struct has_sqlite_type : std::true_type {}; inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, u16str_ref val) { + return sqlite3_bind_text16(stmt, inx, val.data(), sizeof(char16_t) * val.length(), SQLITE_STATIC); + } + + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, u16str_uni_ref val) { return sqlite3_bind_text16(stmt, inx, val.data(), sizeof(char16_t) * val.length(), SQLITE_TRANSIENT); } // Convert char* to string_view to trigger op<<(..., const str_ref ) template inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char16_t(&STR)[N]) { - return sqlite3_bind_text16(stmt, inx, &STR[0], sizeof(char16_t) * (N-1), SQLITE_TRANSIENT); + return sqlite3_bind_text16(stmt, inx, &STR[0], sizeof(char16_t) * (N-1), SQLITE_STATIC); } inline std::u16string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type) { @@ -244,6 +261,10 @@ namespace sqlite { } inline void store_result_in_db(sqlite3_context* db, u16str_ref val) { + sqlite3_result_text16(db, val.data(), sizeof(char16_t) * val.length(), SQLITE_STATIC); + } + + inline void store_result_in_db(sqlite3_context* db, u16str_uni_ref val) { sqlite3_result_text16(db, val.data(), sizeof(char16_t) * val.length(), SQLITE_TRANSIENT); } @@ -273,11 +294,21 @@ namespace sqlite { struct has_sqlite_type, SQLITE_BLOB, void> : std::true_type {}; template inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const std::vector& vec) { + void const* buf = reinterpret_cast(vec.data()); + int bytes = vec.size() * sizeof(T); + return sqlite3_bind_blob(stmt, inx, buf, bytes, SQLITE_STATIC); + } + template inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, std::vector&& vec) { void const* buf = reinterpret_cast(vec.data()); int bytes = vec.size() * sizeof(T); return sqlite3_bind_blob(stmt, inx, buf, bytes, SQLITE_TRANSIENT); } template inline void store_result_in_db(sqlite3_context* db, const std::vector& vec) { + void const* buf = reinterpret_cast(vec.data()); + int bytes = vec.size() * sizeof(T); + sqlite3_result_blob(db, buf, bytes, SQLITE_STATIC); + } + template inline void store_result_in_db(sqlite3_context* db, std::vector&& vec) { void const* buf = reinterpret_cast(vec.data()); int bytes = vec.size() * sizeof(T); sqlite3_result_blob(db, buf, bytes, SQLITE_TRANSIENT); diff --git a/src/txdb.h b/src/txdb.h index 11c3cdf2e..51dbcf9f6 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -23,7 +23,15 @@ namespace sqlite { return sqlite3_bind_blob(stmt, inx, val.data(), int(val.size()), SQLITE_STATIC); } + inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, CScript&& val) { + return sqlite3_bind_blob(stmt, inx, val.data(), int(val.size()), SQLITE_TRANSIENT); + } + inline void store_result_in_db(sqlite3_context* db, const CScript& val) { + sqlite3_result_blob(db, val.data(), int(val.size()), SQLITE_STATIC); + } + + inline void store_result_in_db(sqlite3_context* db, CScript&& val) { sqlite3_result_blob(db, val.data(), int(val.size()), SQLITE_TRANSIENT); } }