Allow sqlite to copy only temporaries

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2020-02-28 19:16:31 +02:00
parent 541b26e920
commit d6e230dcd2
5 changed files with 61 additions and 6 deletions

View file

@ -14,15 +14,31 @@
return sqlite3_bind_blob(stmt, inx, val.begin(), int(val.size()), SQLITE_STATIC); 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) { 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); 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) { 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); sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_TRANSIENT);
} }
inline void store_result_in_db(sqlite3_context* db, const uint256& val) { 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); sqlite3_result_blob(db, val.begin(), int(val.size()), SQLITE_TRANSIENT);
} }
} }

View file

@ -32,14 +32,18 @@
#include <string_view> #include <string_view>
namespace sqlite namespace sqlite
{ {
typedef const std::string_view str_ref; typedef const std::string_view& str_ref;
typedef const std::u16string_view u16str_ref; typedef std::string_view&& str_uni_ref;
typedef const std::u16string_view& u16str_ref;
typedef std::u16string_view&& u16str_uni_ref;
} }
#else #else
namespace sqlite namespace sqlite
{ {
typedef const std::string& str_ref; typedef const std::string& str_ref;
typedef std::string&& str_uni_ref;
typedef const std::u16string& u16str_ref; typedef const std::u16string& u16str_ref;
typedef std::u16string&& u16str_uni_ref;
} }
#endif #endif
#include "../../sqlite3.h" #include "../../sqlite3.h"
@ -182,12 +186,17 @@ namespace sqlite {
template<> template<>
struct has_sqlite_type<std::string, SQLITE_BLOB, void> : std::true_type {}; // struct has_sqlite_type<std::string, SQLITE_BLOB, void> : std::true_type {}; //
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, str_ref val) { 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); return sqlite3_bind_blob(stmt, inx, val.data(), val.length(), SQLITE_TRANSIENT);
} }
// Convert char* to string_view to trigger op<<(..., const str_ref ) // Convert char* to string_view to trigger op<<(..., const str_ref )
template<std::size_t N> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char(&STR)[N]) { template<std::size_t N> 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<std::string>) { inline std::string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::string>) {
@ -220,18 +229,26 @@ namespace sqlite {
} }
inline void store_result_in_db(sqlite3_context* db, str_ref val) { 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); sqlite3_result_blob(db, val.data(), val.length(), SQLITE_TRANSIENT);
} }
// u16str_ref // u16str_ref
template<> template<>
struct has_sqlite_type<std::u16string, SQLITE3_TEXT, void> : std::true_type {}; struct has_sqlite_type<std::u16string, SQLITE3_TEXT, void> : std::true_type {};
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, u16str_ref val) { 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); 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 ) // Convert char* to string_view to trigger op<<(..., const str_ref )
template<std::size_t N> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char16_t(&STR)[N]) { template<std::size_t N> 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<std::u16string>) { inline std::u16string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::u16string>) {
@ -244,6 +261,10 @@ namespace sqlite {
} }
inline void store_result_in_db(sqlite3_context* db, u16str_ref val) { 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); sqlite3_result_text16(db, val.data(), sizeof(char16_t) * val.length(), SQLITE_TRANSIENT);
} }
@ -273,11 +294,21 @@ namespace sqlite {
struct has_sqlite_type<std::vector<T, A>, SQLITE_BLOB, void> : std::true_type {}; struct has_sqlite_type<std::vector<T, A>, SQLITE_BLOB, void> : std::true_type {};
template<typename T, typename A> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const std::vector<T, A>& vec) { template<typename T, typename A> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const std::vector<T, A>& vec) {
void const* buf = reinterpret_cast<void const *>(vec.data());
int bytes = vec.size() * sizeof(T);
return sqlite3_bind_blob(stmt, inx, buf, bytes, SQLITE_STATIC);
}
template<typename T, typename A> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, std::vector<T, A>&& vec) {
void const* buf = reinterpret_cast<void const *>(vec.data()); void const* buf = reinterpret_cast<void const *>(vec.data());
int bytes = vec.size() * sizeof(T); int bytes = vec.size() * sizeof(T);
return sqlite3_bind_blob(stmt, inx, buf, bytes, SQLITE_TRANSIENT); return sqlite3_bind_blob(stmt, inx, buf, bytes, SQLITE_TRANSIENT);
} }
template<typename T, typename A> inline void store_result_in_db(sqlite3_context* db, const std::vector<T, A>& vec) { template<typename T, typename A> inline void store_result_in_db(sqlite3_context* db, const std::vector<T, A>& vec) {
void const* buf = reinterpret_cast<void const *>(vec.data());
int bytes = vec.size() * sizeof(T);
sqlite3_result_blob(db, buf, bytes, SQLITE_STATIC);
}
template<typename T, typename A> inline void store_result_in_db(sqlite3_context* db, std::vector<T, A>&& vec) {
void const* buf = reinterpret_cast<void const *>(vec.data()); void const* buf = reinterpret_cast<void const *>(vec.data());
int bytes = vec.size() * sizeof(T); int bytes = vec.size() * sizeof(T);
sqlite3_result_blob(db, buf, bytes, SQLITE_TRANSIENT); sqlite3_result_blob(db, buf, bytes, SQLITE_TRANSIENT);

View file

@ -65,7 +65,7 @@ class LockImpl : public Chain::Lock, public UniqueLock<CCriticalSection>
const Optional<int> height = getBlockHeight(hash); const Optional<int> height = getBlockHeight(hash);
return tip_height && height ? *tip_height - *height + 1 : 0; return tip_height && height ? *tip_height - *height + 1 : 0;
} }
uint256 getBlockHash(int height) override const uint256& getBlockHash(int height) override
{ {
LockAssertion lock(::cs_main); LockAssertion lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height]; CBlockIndex* block = ::ChainActive()[height];

View file

@ -81,7 +81,7 @@ public:
virtual int getBlockDepth(const uint256& hash) = 0; virtual int getBlockDepth(const uint256& hash) = 0;
//! Get block hash. Height must be valid or this function will abort. //! Get block hash. Height must be valid or this function will abort.
virtual uint256 getBlockHash(int height) = 0; virtual const uint256& getBlockHash(int height) = 0;
//! Get block time. Height must be valid or this function will abort. //! Get block time. Height must be valid or this function will abort.
virtual int64_t getBlockTime(int height) = 0; virtual int64_t getBlockTime(int height) = 0;

View file

@ -24,7 +24,15 @@ namespace sqlite {
return sqlite3_bind_blob(stmt, inx, val.data(), int(val.size()), SQLITE_STATIC); 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) { 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); sqlite3_result_blob(db, val.data(), int(val.size()), SQLITE_TRANSIENT);
} }
} }