1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Tell libpqtypes to not make copies of strings it receives.

The wrapper now copies boost::string_ref into std::string so
it's always safe to pass a string ref, even when it's not
null-terminated.
This commit is contained in:
King_DuckZ 2016-01-11 12:47:17 +00:00
parent fa12bb2d74
commit 7e737f56a2
2 changed files with 9 additions and 5 deletions

View file

@ -24,6 +24,7 @@
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <cassert>
struct pg_param; struct pg_param;
typedef pg_param PGparam; typedef pg_param PGparam;
@ -49,9 +50,12 @@ namespace pq {
}; };
template <> template <>
struct get_pqlib_c_type_struct<boost::string_ref> { struct get_pqlib_c_type_struct<boost::string_ref> {
private:
std::string m_str;
public:
using type = const char*; using type = const char*;
explicit get_pqlib_c_type_struct ( const Connection& ) { } explicit get_pqlib_c_type_struct ( const Connection& ) { }
static type conv ( const boost::string_ref& parParam ) { return parParam.data(); } type conv ( boost::string_ref parParam ) { m_str = std::string(parParam.data(), parParam.size()); return m_str.c_str(); }
}; };
template <> template <>
struct get_pqlib_c_type_struct<bool> { struct get_pqlib_c_type_struct<bool> {
@ -99,11 +103,11 @@ namespace pq {
template <> template <>
struct type_to_pqtypes_name<std::string> { struct type_to_pqtypes_name<std::string> {
constexpr static const char* name ( void ) { return "text"; } constexpr static const char* name ( void ) { return "text*"; }
}; };
template <> template <>
struct type_to_pqtypes_name<boost::string_ref> { struct type_to_pqtypes_name<boost::string_ref> {
constexpr static const char* name ( void ) { return "text"; } constexpr static const char* name ( void ) { return "text*"; }
}; };
template <> template <>
struct type_to_pqtypes_name<bool> { struct type_to_pqtypes_name<bool> {

View file

@ -133,8 +133,8 @@ namespace din {
system_clock::from_time_t(itm.mtime), system_clock::from_time_t(itm.mtime),
itm.hash_valid, itm.hash_valid,
itm.unreadable, itm.unreadable,
std::string(itm.mime_type.data(), itm.mime_type.size()), itm.mime_type,
std::string(itm.mime_charset.data(), itm.mime_charset.size()) itm.mime_charset
); );
} }
conn.query("COMMIT;"); conn.query("COMMIT;");