Add VariadicBind.h from PR #85 to CMake for Visual Studio

+ fix style issues (cpplint)
This commit is contained in:
Sébastien Rombauts 2016-07-02 14:04:39 +02:00
parent a28283f1b8
commit 4e7c12f629
4 changed files with 53 additions and 63 deletions

View File

@ -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})

View File

@ -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,11 +423,34 @@ 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) {
#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
}

View File

@ -10,13 +10,10 @@
*/
#pragma once
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
#include <SQLiteCpp/Statement.h>
//this requires c++14. seems like visual studio 2015 should work (yet untested).
#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) )
/// @cond
#include <utility>
#include <initializer_list>
@ -26,19 +23,20 @@ namespace SQLite
/// implementation detail for variadic bind.
namespace detail {
template<class F,class ...Args, std::size_t ... I>
inline void invoke_with_index(F&& f, std::integer_sequence<std::size_t, I...>,
const Args& ...args) {
std::initializer_list<int> { (f(I+1,args),0)... };
template<class F, class ...Args, std::size_t ... I>
inline void invoke_with_index(F&& f, std::integer_sequence<std::size_t, I...>, const Args& ...args)
{
std::initializer_list<int> { (f(I+1, args), 0)... };
}
/// implementation detail for variadic bind.
template<class F,class ...Args>
inline void invoke_with_index(F&&f, const Args& ... args) {
invoke_with_index(std::forward<F>(f),std::index_sequence_for<Args...>(), args...);
template<class F, class ...Args>
inline void invoke_with_index(F&& f, const Args& ... args)
{
invoke_with_index(std::forward<F>(f), std::index_sequence_for<Args...>(), 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<class ...Args>
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);
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

View File

@ -17,9 +17,7 @@
#include <cstdio>
// 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);