diff --git a/CMakeLists.txt b/CMakeLists.txt index be46fde..365c99a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,12 +104,13 @@ source_group(src FILES ${SQLITECPP_SRC}) set(SQLITECPP_INC ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/SQLiteCpp.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Assertion.h + ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Column.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Database.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h - ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h + ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/VariadicBind.h ) source_group(inc FILES ${SQLITECPP_INC}) diff --git a/examples/example1/main.cpp b/examples/example1/main.cpp index 15abe75..227d06c 100644 --- a/examples/example1/main.cpp +++ b/examples/example1/main.cpp @@ -82,35 +82,6 @@ private: SQLite::Statement mQuery; ///< Database prepared SQL query }; -void demonstrateVariadicBind() { -#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) - // Open a database file in create/write mode - SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); - - db.exec("DROP TABLE IF EXISTS test"); - db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); - - { - SQLite::Statement query(db, "INSERT INTO test VALUES (?, ?)"); - - SQLite::bind(query, 42, "fortytwo"); - // Execute the one-step query to insert the blob - int nb = query.exec(); - std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb - << std::endl; - } - - SQLite::Statement query(db, "SELECT * FROM test"); - std::cout << "SELECT * FROM test :\n"; - if (query.executeStep()) { - std::cout << query.getColumn(0).getInt() << "\t\"" - << query.getColumn(1).getText() << "\"\n"; - } -#else - throw std::runtime_error("demonstrateVariadicBind(): sorry, no c++14 support in this build."); -#endif -} - int main () { std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl; @@ -452,14 +423,37 @@ int main () } remove("out.png"); - //example with variadic bind (requires c++14) -#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) - try { - demonstrateVariadicBind(); - } catch (std::exception& e) { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } +#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015 + // example with C++14 variadic bind + try + { + // Open a database file in create/write mode + SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + + db.exec("DROP TABLE IF EXISTS test"); + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + + { + SQLite::Statement query(db, "INSERT INTO test VALUES (?, ?)"); + + SQLite::bind(query, 42, "fortytwo"); + // Execute the one-step query to insert the blob + int nb = query.exec(); + std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl; + } + + SQLite::Statement query(db, "SELECT * FROM test"); + std::cout << "SELECT * FROM test :\n"; + if (query.executeStep()) + { + std::cout << query.getColumn(0).getInt() << "\t\"" << query.getColumn(1).getText() << "\"\n"; + } + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } #endif std::cout << "everything ok, quitting\n"; diff --git a/include/SQLiteCpp/VariadicBind.h b/include/SQLiteCpp/VariadicBind.h index 8cce653..a040f15 100644 --- a/include/SQLiteCpp/VariadicBind.h +++ b/include/SQLiteCpp/VariadicBind.h @@ -10,13 +10,10 @@ */ #pragma once +#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015 + #include - - - -//this requires c++14. seems like visual studio 2015 should work (yet untested). -#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) /// @cond #include #include @@ -26,19 +23,20 @@ namespace SQLite /// implementation detail for variadic bind. namespace detail { -template -inline void invoke_with_index(F&& f, std::integer_sequence, - const Args& ...args) { - std::initializer_list { (f(I+1,args),0)... }; +template +inline void invoke_with_index(F&& f, std::integer_sequence, const Args& ...args) +{ + std::initializer_list { (f(I+1, args), 0)... }; } /// implementation detail for variadic bind. -template -inline void invoke_with_index(F&&f, const Args& ... args) { - invoke_with_index(std::forward(f),std::index_sequence_for(), args...); +template +inline void invoke_with_index(F&& f, const Args& ... args) +{ + invoke_with_index(std::forward(f), std::index_sequence_for(), args...); } -} //namespace detail +} // namespace detail /// @endcond /** @@ -60,19 +58,18 @@ inline void invoke_with_index(F&&f, const Args& ... args) { * @param args one or more args to bind. */ template -void bind(SQLite::Statement& s,const Args& ... args) { +void bind(SQLite::Statement& s, const Args& ... args) +{ + static_assert(sizeof...(args) > 0, "please invoke bind with one or more args"); - static_assert(sizeof...(args)>0,"please invoke bind with one or more args"); - - auto f=[&s](std::size_t index, const auto& value) { - s.bind(index,value); - }; - detail::invoke_with_index(f, args...); + auto f=[&s](std::size_t index, const auto& value) + { + s.bind(index, value); + }; + detail::invoke_with_index(f, args...); } } // namespace SQLite -#else -//not supported in older c++. provide a fallback? #endif // c++14 diff --git a/tests/VariadicBind_test.cpp b/tests/VariadicBind_test.cpp index 3154b29..693b8ee 100644 --- a/tests/VariadicBind_test.cpp +++ b/tests/VariadicBind_test.cpp @@ -17,9 +17,7 @@ #include -// this requires c++14. visual studio 2015 is working -#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) - +#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015 TEST(VariadicBind, invalid) { // Create a new database SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);