Skip to content

Commit

Permalink
Fix required C++ standard
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Nov 21, 2024
1 parent ceeddf1 commit 9cafd37
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/test/pq/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ foreach(testsourcefile ${testsources})
add_executable(${exename} ${testsourcefile})
target_link_libraries(${exename} PRIVATE taocpp::taopq)
set_target_properties(${exename} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
Expand Down
72 changes: 72 additions & 0 deletions src/test/pq/single_row_mode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2016-2024 Daniel Frey and Dr. Colin Hirsch
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include "../getenv.hpp"
#include "../macros.hpp"

#include <exception>
#include <iostream>
#include <optional>
#include <tuple>
#include <utility>

#include <tao/pq.hpp>

namespace
{
void run()
{
// overwrite the default with an environment variable if needed
const auto connection_string = tao::pq::internal::getenv( "TAOPQ_TEST_DATABASE", "dbname=template1" );

// open a connection to the database
const auto conn = tao::pq::connection::create( connection_string );

conn->execute( "DROP TABLE IF EXISTS tao_single_row_mode" );
conn->execute( "CREATE TABLE tao_single_row_mode ( name TEXT PRIMARY KEY, age INTEGER NOT NULL )" );

conn->prepare( "insert_user", "INSERT INTO tao_single_row_mode ( name, age ) VALUES ( $1, $2 )" );
conn->execute( "insert_user", "Daniel", 42 );
conn->execute( "insert_user", "Tom", 41 );
conn->execute( "insert_user", "Jerry", 29 );

const auto tr = conn->transaction();
tr->send( "SELECT name, age FROM tao_single_row_mode" );
tr->set_single_row_mode();

while( true ) {
// in single row mode, each result contains either
// a single row or no row when the end is reached.
const auto result = tr->get_result();
if( result.empty() ) {
break;
}

// the loop is unnecessary for single row mode,
// but in chunked mode multiple rows per result are possible.
for( const auto& row : result ) {
std::cout << row[ "name" ].as< std::string >() << " is "
<< row[ "age" ].as< unsigned >() << " years old.\n";
}
}
}

} // namespace

auto main() -> int // NOLINT(bugprone-exception-escape)
{
try {
run();
}
// LCOV_EXCL_START
catch( const std::exception& e ) {
std::cerr << "exception: " << e.what() << '\n';
throw;
}
catch( ... ) {
std::cerr << "unknown exception\n";
throw;
}
// LCOV_EXCL_STOP
}

0 comments on commit 9cafd37

Please sign in to comment.