mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Add VariadicBind.h from PR #85 to CMake for Visual Studio
+ fix style issues (cpplint)
This commit is contained in:
parent
a28283f1b8
commit
4e7c12f629
@ -104,12 +104,13 @@ source_group(src FILES ${SQLITECPP_SRC})
|
|||||||
set(SQLITECPP_INC
|
set(SQLITECPP_INC
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/SQLiteCpp.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/SQLiteCpp.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Assertion.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/Column.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Database.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Database.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.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})
|
source_group(inc FILES ${SQLITECPP_INC})
|
||||||
|
|
||||||
|
@ -82,35 +82,6 @@ private:
|
|||||||
SQLite::Statement mQuery; ///< Database prepared SQL query
|
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 ()
|
int main ()
|
||||||
{
|
{
|
||||||
std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl;
|
std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl;
|
||||||
@ -452,14 +423,37 @@ int main ()
|
|||||||
}
|
}
|
||||||
remove("out.png");
|
remove("out.png");
|
||||||
|
|
||||||
//example with variadic bind (requires c++14)
|
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
|
||||||
#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) )
|
// example with C++14 variadic bind
|
||||||
try {
|
try
|
||||||
demonstrateVariadicBind();
|
{
|
||||||
} catch (std::exception& e) {
|
// Open a database file in create/write mode
|
||||||
std::cout << "SQLite exception: " << e.what() << std::endl;
|
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||||
return EXIT_FAILURE; // unexpected error : exit the example program
|
|
||||||
}
|
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
|
#endif
|
||||||
|
|
||||||
std::cout << "everything ok, quitting\n";
|
std::cout << "everything ok, quitting\n";
|
||||||
|
@ -10,13 +10,10 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
|
||||||
|
|
||||||
#include <SQLiteCpp/Statement.h>
|
#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
|
/// @cond
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
@ -26,19 +23,20 @@ namespace SQLite
|
|||||||
|
|
||||||
/// implementation detail for variadic bind.
|
/// implementation detail for variadic bind.
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<class F,class ...Args, std::size_t ... I>
|
template<class F, class ...Args, std::size_t ... I>
|
||||||
inline void invoke_with_index(F&& f, std::integer_sequence<std::size_t, I...>,
|
inline void invoke_with_index(F&& f, std::integer_sequence<std::size_t, I...>, const Args& ...args)
|
||||||
const Args& ...args) {
|
{
|
||||||
std::initializer_list<int> { (f(I+1,args),0)... };
|
std::initializer_list<int> { (f(I+1, args), 0)... };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// implementation detail for variadic bind.
|
/// implementation detail for variadic bind.
|
||||||
template<class F,class ...Args>
|
template<class F, class ...Args>
|
||||||
inline void invoke_with_index(F&&f, const Args& ... args) {
|
inline void invoke_with_index(F&& f, const Args& ... args)
|
||||||
invoke_with_index(std::forward<F>(f),std::index_sequence_for<Args...>(), args...);
|
{
|
||||||
|
invoke_with_index(std::forward<F>(f), std::index_sequence_for<Args...>(), args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namespace detail
|
} // namespace detail
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,19 +58,18 @@ inline void invoke_with_index(F&&f, const Args& ... args) {
|
|||||||
* @param args one or more args to bind.
|
* @param args one or more args to bind.
|
||||||
*/
|
*/
|
||||||
template<class ...Args>
|
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)
|
||||||
|
{
|
||||||
auto f=[&s](std::size_t index, const auto& value) {
|
s.bind(index, value);
|
||||||
s.bind(index,value);
|
};
|
||||||
};
|
detail::invoke_with_index(f, args...);
|
||||||
detail::invoke_with_index(f, args...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace SQLite
|
} // namespace SQLite
|
||||||
|
|
||||||
#else
|
|
||||||
//not supported in older c++. provide a fallback?
|
|
||||||
#endif // c++14
|
#endif // c++14
|
||||||
|
|
||||||
|
@ -17,9 +17,7 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// this requires c++14. visual studio 2015 is working
|
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
|
||||||
#if ( __cplusplus>= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) )
|
|
||||||
|
|
||||||
TEST(VariadicBind, invalid) {
|
TEST(VariadicBind, invalid) {
|
||||||
// Create a new database
|
// Create a new database
|
||||||
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user