Commit aae6b3f4 authored by Pavel Vainerman's avatar Pavel Vainerman

supported c++17 (require c++17)

parent 5fad4d4c
...@@ -23,7 +23,8 @@ with open source third-party libraries. UniSet provides the consistent interface ...@@ -23,7 +23,8 @@ with open source third-party libraries. UniSet provides the consistent interface
add-on components and third-party libraries. Python wrapper helps in using the library add-on components and third-party libraries. Python wrapper helps in using the library
in python scripts. in python scripts.
libuniset requires minimum C++11 libuniset requires minimum C++11 (without pqxx support)
libuniset requires C++17 for pqxx support
More information: More information:
* [RU] https://habr.com/post/278535/ * [RU] https://habr.com/post/278535/
......
...@@ -63,7 +63,7 @@ BuildRequires: libsqlite3-devel ...@@ -63,7 +63,7 @@ BuildRequires: libsqlite3-devel
%endif %endif
%if_enabled pgsql %if_enabled pgsql
BuildRequires: libpqxx-devel BuildRequires: libpqxx-devel >= 7.6.0
%endif %endif
%if_enabled rrd %if_enabled rrd
......
...@@ -485,7 +485,7 @@ CXX_EXTRA_FLAGS="-Wnon-virtual-dtor -Woverloaded-virtual -Woverflow -D_GLIBCXX_U ...@@ -485,7 +485,7 @@ CXX_EXTRA_FLAGS="-Wnon-virtual-dtor -Woverloaded-virtual -Woverflow -D_GLIBCXX_U
# export # export
LDFLAGS="$LDFLAGS ${OMNI_LIBS} ${XML_LIBS} ${SIGC_LIBS} ${COV_LIBS} ${POCO_LIBS} ${EV_LIBS}" LDFLAGS="$LDFLAGS ${OMNI_LIBS} ${XML_LIBS} ${SIGC_LIBS} ${COV_LIBS} ${POCO_LIBS} ${EV_LIBS}"
# all developer liked options add to autogen.sh, please # all developer liked options add to autogen.sh, please
CXXFLAGS="-I\$(top_builddir)/include $CXXFLAGS -funsigned-char -g -DCATCH_VERSION_MAJOR=${CATCH_VERSION_MAJOR} -D_GNU_SOURCE ${REST_API_CFLAGS} ${COMPORT_485F_CFLAGS} ${OMNI_CFLAGS} ${XML_CFLAGS} ${SIGC_CFLAGS} ${COV_CFLAGS} ${POCO_CFLAGS} ${EV_CFLAGS} $CXX_EXTRA_FLAGS" CXXFLAGS="-std=c++17 -I\$(top_builddir)/include $CXXFLAGS -funsigned-char -g -DCATCH_VERSION_MAJOR=${CATCH_VERSION_MAJOR} -D_GNU_SOURCE ${REST_API_CFLAGS} ${COMPORT_485F_CFLAGS} ${OMNI_CFLAGS} ${XML_CFLAGS} ${SIGC_CFLAGS} ${COV_CFLAGS} ${POCO_CFLAGS} ${EV_CFLAGS} $CXX_EXTRA_FLAGS"
AC_SUBST(LDFLAGS) AC_SUBST(LDFLAGS)
AC_SUBST(CXXFLAGS) AC_SUBST(CXXFLAGS)
......
...@@ -271,7 +271,7 @@ void DBServer_PostgreSQL::addRecord( const PostgreSQLInterface::Record&& rec ) ...@@ -271,7 +271,7 @@ void DBServer_PostgreSQL::addRecord( const PostgreSQLInterface::Record&& rec )
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
bool DBServer_PostgreSQL::writeInsertBufferToDB( const std::string& tableName bool DBServer_PostgreSQL::writeInsertBufferToDB( const std::string& tableName
, const std::vector<std::string>& colNames , const std::initializer_list<std::string_view> colNames
, const InsertBuffer& wbuf ) , const InsertBuffer& wbuf )
{ {
return db->copy(tableName, colNames, wbuf); return db->copy(tableName, colNames, wbuf);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#ifndef DBServer_PostgreSQL_H_ #ifndef DBServer_PostgreSQL_H_
#define DBServer_PostgreSQL_H_ #define DBServer_PostgreSQL_H_
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include <string_view>
#include <unordered_map> #include <unordered_map>
#include <queue> #include <queue>
#include "UniSetTypes.h" #include "UniSetTypes.h"
...@@ -113,13 +114,13 @@ namespace uniset ...@@ -113,13 +114,13 @@ namespace uniset
void flushBuffer(); void flushBuffer();
// writeBuffer // writeBuffer
const std::vector<std::string> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" }; const std::initializer_list<std::string_view> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" };
typedef std::vector<PostgreSQLInterface::Record> InsertBuffer; typedef std::vector<PostgreSQLInterface::Record> InsertBuffer;
void flushInsertBuffer(); void flushInsertBuffer();
virtual void addRecord( const PostgreSQLInterface::Record&& rec ); virtual void addRecord( const PostgreSQLInterface::Record&& rec );
virtual bool writeInsertBufferToDB( const std::string& table virtual bool writeInsertBufferToDB( const std::string& table
, const std::vector<std::string>& colname , const std::initializer_list<std::string_view> colname
, const InsertBuffer& ibuf ); , const InsertBuffer& ibuf );
private: private:
......
if ENABLE_PGSQL if ENABLE_PGSQL
UPGSQL_VER=@LIBVER@ UPGSQL_VER=@LIBVER@
lib_LTLIBRARIES = libUniSet2-pgsql.la lib_LTLIBRARIES = libUniSet2-pgsql.la
libUniSet2_pgsql_la_LDFLAGS = -version-info $(UPGSQL_VER) libUniSet2_pgsql_la_LDFLAGS = -version-info $(UPGSQL_VER)
libUniSet2_pgsql_la_SOURCES = PostgreSQLInterface.cc DBServer_PostgreSQL.cc libUniSet2_pgsql_la_SOURCES = PostgreSQLInterface.cc DBServer_PostgreSQL.cc
libUniSet2_pgsql_la_LIBADD = $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS) libUniSet2_pgsql_la_LIBADD = $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS)
libUniSet2_pgsql_la_CXXFLAGS = -std=c++11 $(PGSQL_CFLAGS) libUniSet2_pgsql_la_CXXFLAGS = $(PGSQL_CFLAGS)
bin_PROGRAMS = @PACKAGE@-pgsql-dbserver bin_PROGRAMS = @PACKAGE@-pgsql-dbserver
@PACKAGE@_pgsql_dbserver_LDADD = libUniSet2-pgsql.la $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS) @PACKAGE@_pgsql_dbserver_LDADD = libUniSet2-pgsql.la $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS)
@PACKAGE@_pgsql_dbserver_CXXFLAGS = -std=c++11 $(PGSQL_CFLAGS) @PACKAGE@_pgsql_dbserver_CXXFLAGS = $(PGSQL_CFLAGS)
@PACKAGE@_pgsql_dbserver_SOURCES = main.cc @PACKAGE@_pgsql_dbserver_SOURCES = main.cc
noinst_PROGRAMS = pgsql-test noinst_PROGRAMS = pgsql-test
pgsql_test_LDADD = libUniSet2-pgsql.la $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS) pgsql_test_LDADD = libUniSet2-pgsql.la $(top_builddir)/lib/libUniSet2.la $(PGSQL_LIBS)
pgsql_test_CXXFLAGS = -std=c++11 $(PGSQL_CFLAGS) pgsql_test_CXXFLAGS = $(PGSQL_CFLAGS)
pgsql_test_SOURCES = test.cc pgsql_test_SOURCES = test.cc
# install # install
devel_include_HEADERS = *.h devel_include_HEADERS = *.h
......
...@@ -54,7 +54,7 @@ bool PostgreSQLInterface::ping() const ...@@ -54,7 +54,7 @@ bool PostgreSQLInterface::ping() const
try try
{ {
// pqxx doesn't work with unique_ptr ( // pqxx doesn't work with unique_ptr (
nontransaction n(*(db.get())); nontransaction n{*(db.get())};
n.exec("select 1;"); n.exec("select 1;");
return true; return true;
} }
...@@ -105,14 +105,14 @@ bool PostgreSQLInterface::close() ...@@ -105,14 +105,14 @@ bool PostgreSQLInterface::close()
{ {
if( db ) if( db )
{ {
db->disconnect(); db->close();
db.reset(); db.reset();
} }
return true; return true;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool PostgreSQLInterface::copy( const std::string& tblname, const std::vector<std::string>& cols, bool PostgreSQLInterface::copy( const std::string& tblname, const std::initializer_list<std::string_view>& cols,
const PostgreSQLInterface::Data& data ) const PostgreSQLInterface::Data& data )
{ {
if( !db ) if( !db )
...@@ -123,17 +123,14 @@ bool PostgreSQLInterface::copy( const std::string& tblname, const std::vector<st ...@@ -123,17 +123,14 @@ bool PostgreSQLInterface::copy( const std::string& tblname, const std::vector<st
try try
{ {
// pqxx doesn't work with unique_ptr pqxx::work tx{ *(db.get()) };
work w( *(db.get()) ); auto t{pqxx::stream_to::table(tx, {tblname}, cols)};
tablewriter t(w, tblname, cols.begin(), cols.end());
t.reserve(data.size());
for( const auto& d : data ) for( const auto& d : data )
t.push_back(d.begin(), d.end()); t << d;
t.complete(); t.complete();
w.commit(); tx.commit();
return true; return true;
} }
catch( const std::exception& e ) catch( const std::exception& e )
...@@ -208,7 +205,7 @@ DBResult PostgreSQLInterface::query( const string& q ) ...@@ -208,7 +205,7 @@ DBResult PostgreSQLInterface::query( const string& q )
try try
{ {
// pqxx doesn't work with unique_ptr // pqxx doesn't work with unique_ptr
nontransaction n(*(db.get())); nontransaction n{*(db.get())};
lastQ = q; lastQ = q;
/* Execute SQL query */ /* Execute SQL query */
result res( n.exec(q) ); result res( n.exec(q) );
...@@ -261,11 +258,11 @@ DBResult PostgreSQLInterface::makeResult( const pqxx::result& res ) ...@@ -261,11 +258,11 @@ DBResult PostgreSQLInterface::makeResult( const pqxx::result& res )
{ {
DBResult result; DBResult result;
for( result::const_iterator c = res.begin(); c != res.end(); ++c ) for( const auto& c : res )
{ {
DBResult::COL col; DBResult::COL col;
for( pqxx::result::tuple::const_iterator i = c.begin(); i != c.end(); i++ ) for( const auto& i : c )
{ {
if( i.is_null() ) if( i.is_null() )
col.push_back(""); col.push_back("");
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#define PostgreSQLInterface_H_ #define PostgreSQLInterface_H_
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
#include <string> #include <string>
#include <string_view>
#include <list> #include <list>
#include <vector> #include <vector>
#include <queue> #include <queue>
...@@ -58,7 +59,7 @@ namespace uniset ...@@ -58,7 +59,7 @@ namespace uniset
typedef std::vector<Record> Data; typedef std::vector<Record> Data;
// fast insert: Use COPY..from SDTIN.. // fast insert: Use COPY..from SDTIN..
bool copy( const std::string& tblname, const std::vector<std::string>& cols, const Data& data ); bool copy( const std::string& tblname, const std::initializer_list<std::string_view>& cols, const Data& data );
virtual const std::string error() override; virtual const std::string error() override;
......
...@@ -8,4 +8,4 @@ Description: Support library for libUniSet2PostgreSQL ...@@ -8,4 +8,4 @@ Description: Support library for libUniSet2PostgreSQL
Requires: libUniSet2 libpqxx Requires: libUniSet2 libpqxx
Version: @VERSION@ Version: @VERSION@
Libs: -L${libdir} -lUniSet2-pgsql Libs: -L${libdir} -lUniSet2-pgsql
Cflags: -std=c++11 -I${includedir}/@PACKAGE@ -I${includedir}/@PACKAGE@/extensions/pgsql Cflags: -I${includedir}/@PACKAGE@ -I${includedir}/@PACKAGE@/extensions/pgsql
...@@ -64,7 +64,7 @@ int main(int argc, char** argv) ...@@ -64,7 +64,7 @@ int main(int argc, char** argv)
if( ver == 3 ) if( ver == 3 )
{ {
std::vector<std::string> cols = { "date", "time", "time_usec", "sensor_id", "value", "node" }; std::initializer_list<std::string_view> cols{ "date", "time", "time_usec", "sensor_id", "value", "node" };
PostgreSQLInterface::Data data; PostgreSQLInterface::Data data;
for( size_t i = 0; i < num; i++ ) for( size_t i = 0; i < num; i++ )
......
version: '3.2'
services:
pghost:
image: postgres:12.0-alpine
volumes:
- ./pg_data:/var/lib/postgresql/data/pg_data
- ./entrypoint-initdb.d:/docker-entrypoint-initdb.d
cap_add:
- ALL
environment:
- "PGDATA=/var/lib/postgresql/data/pg_data"
- "POSTGRES_PASSWORD=dbadmin"
- "POSTGRES_USER=dbadmin"
- "POSTGRES_DB=test-db"
ports:
- 5432:5432
tty: true
CREATE TABLE main_history (
id BIGSERIAL PRIMARY KEY NOT NULL,
date date NOT NULL,
time time NOT NULL,
time_usec int NOT NULL CHECK (time_usec >= 0),
sensor_id int NOT NULL,
value double precision NOT NULL,
node int NOT NULL,
confirm int DEFAULT NULL
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment