From 36d1ebb68359d7faa194a1f62c17011b462a3a1f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 4 Feb 2019 16:56:59 +0000 Subject: Switch to string_view --- libpqpp/pq-binarycolumn.cpp | 2 +- libpqpp/pq-column.cpp | 7 +++---- libpqpp/pq-column.h | 2 +- libpqpp/pq-command.cpp | 7 ++++++- libpqpp/pq-command.h | 3 ++- libpqpp/unittests/testpq.cpp | 4 ++-- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libpqpp/pq-binarycolumn.cpp b/libpqpp/pq-binarycolumn.cpp index ff49b26..1c6f9e2 100644 --- a/libpqpp/pq-binarycolumn.cpp +++ b/libpqpp/pq-binarycolumn.cpp @@ -19,7 +19,7 @@ PQ::BinaryColumn::apply(DB::HandleField & h) const case 1043: //VARCHAROID: case 25: //TEXTOID: case 142: //XMLOID: - h.string(value(), length()); + h.string({ value(), length() }); break; case 16: //BOOLOID: h.boolean(valueAs()); diff --git a/libpqpp/pq-column.cpp b/libpqpp/pq-column.cpp index 6dbbfd2..5e02015 100644 --- a/libpqpp/pq-column.cpp +++ b/libpqpp/pq-column.cpp @@ -25,7 +25,7 @@ PQ::Column::isNull() const return PQgetisnull(sc->execRes, sc->tuple, colNo); } -int +std::size_t PQ::Column::length() const { return PQgetlength(sc->execRes, sc->tuple, colNo); @@ -49,7 +49,8 @@ PQ::Column::apply(DB::HandleField & h) const case 1043: //VARCHAROID: case 25: //TEXTOID: case 142: //XMLOID: - h.string(value(), length()); + default: + h.string({ value(), length() }); break; case 16: //BOOLOID: h.boolean(value()[0] == 't'); @@ -97,8 +98,6 @@ PQ::Column::apply(DB::HandleField & h) const h.blob(DB::Blob(buf, len)); break; } - default: - h.string(value(), length()); } } diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index f6050ea..5c71d86 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -20,7 +20,7 @@ namespace PQ { template inline T * valueAsPtr() const { return (T*)(value()); } const char * value() const; - int length() const; + std::size_t length() const; const SelectBase * sc; const Oid oid; diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 58b5c49..97f649c 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -103,7 +103,7 @@ PQ::Command::paramSet(unsigned int n, const char * fmt, const T & ... v) } void -PQ::Command::paramSet(unsigned int n, const std::string & b) +PQ::Command::paramSet(unsigned int n, const std::string_view & b) { paramsAtLeast(n); bufs[n] = new std::string(b); @@ -158,6 +158,11 @@ PQ::Command::bindParamF(unsigned int n, float v) } void PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s) +{ + paramSet(n, s.raw()); +} +void +PQ::Command::bindParamS(unsigned int n, const std::string_view & s) { paramSet(n, s); } diff --git a/libpqpp/pq-command.h b/libpqpp/pq-command.h index c2674eb..a26bae8 100644 --- a/libpqpp/pq-command.h +++ b/libpqpp/pq-command.h @@ -43,6 +43,7 @@ namespace PQ { void bindParamF(unsigned int, float) override; void bindParamS(unsigned int, const Glib::ustring&) override; + void bindParamS(unsigned int, const std::string_view&) override; void bindParamT(unsigned int, const boost::posix_time::time_duration &) override; void bindParamT(unsigned int, const boost::posix_time::ptime &) override; @@ -59,7 +60,7 @@ namespace PQ { void paramsAtLeast(unsigned int); template void paramSet(unsigned int, const char * fmt, const T & ... t); - void paramSet(unsigned int, const std::string &); + void paramSet(unsigned int, const std::string_view &); std::vector values; std::vector lengths; std::vector formats; diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index e5d86ef..0a13c1e 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) mod->execute(); mod->bindParamI(0, (unsigned int)(testInt + 10)); mod->bindParamF(1, (float)(testDouble + 10)); - mod->bindParamS(2, testString + " something"); + mod->bindParamS(2, std::string(testString) + " something"); mod->bindParamB(3, true); mod->bindParamT(4, testDateTime); mod->bindParamT(5, testInterval); @@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE( bindAndSelectOther ) while (select->fetch()) { assertColumnValueHelper(*select, 0, 4); assertColumnValueHelper(*select, 1, 123.45); - assertColumnValueHelper(*select, 2, std::string("some text with a ; in it and a ' too")); + assertColumnValueHelper(*select, 2, std::string_view("some text with a ; in it and a ' too")); assertColumnValueHelper(*select, 3, true); assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12)); -- cgit v1.2.3