mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Update VariadicBind.h for C++11 instead of C++14 (#196)
* Update VariadicBind.h * replace c++14 by c++11 * activate tests for c++11 * replace string_literals for c++11 support
This commit is contained in:
parent
bb8c0ef0f2
commit
81913790f2
@ -5,13 +5,14 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2016 Paul Dreik (github@pauldreik.se)
|
* Copyright (c) 2016 Paul Dreik (github@pauldreik.se)
|
||||||
* Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
* Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
||||||
|
* Copyright (c) 2019 Maximilian Bachmann (github@maxbachmann)
|
||||||
*
|
*
|
||||||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||||
* or copy at http://opensource.org/licenses/MIT)
|
* or copy at http://opensource.org/licenses/MIT)
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
|
#if (__cplusplus >= 201103L) || ( defined(_MSC_VER) && (_MSC_VER >= 1800) ) // c++11: Visual Studio 2013
|
||||||
|
|
||||||
#include <SQLiteCpp/Statement.h>
|
#include <SQLiteCpp/Statement.h>
|
||||||
|
|
||||||
@ -21,23 +22,6 @@
|
|||||||
|
|
||||||
namespace SQLite
|
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)... });
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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...);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,7 +29,7 @@ inline void invoke_with_index(F&& f, const Args& ... args)
|
|||||||
*
|
*
|
||||||
* This takes care of incrementing the index between each calls to bind.
|
* This takes care of incrementing the index between each calls to bind.
|
||||||
*
|
*
|
||||||
* This feature requires a c++14 capable compiler.
|
* This feature requires a c++11 capable compiler.
|
||||||
*
|
*
|
||||||
* \code{.cpp}
|
* \code{.cpp}
|
||||||
* SQLite::Statement stm("SELECT * FROM MyTable WHERE colA>? && colB=? && colC<?");
|
* SQLite::Statement stm("SELECT * FROM MyTable WHERE colA>? && colB=? && colC<?");
|
||||||
@ -61,16 +45,13 @@ inline void invoke_with_index(F&& f, const Args& ... args)
|
|||||||
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");
|
int pos = 0;
|
||||||
|
(void)std::initializer_list<int>{
|
||||||
auto f=[&s](std::size_t index, const auto& value)
|
((void)s.bind(++pos, std::forward<decltype(args)>(args)), 0)...
|
||||||
{
|
|
||||||
s.bind(index, value);
|
|
||||||
};
|
};
|
||||||
detail::invoke_with_index(f, args...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace SQLite
|
} // namespace SQLite
|
||||||
|
|
||||||
#endif // c++14
|
#endif // c++11
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2016 Paul Dreik (github@pauldreik.se)
|
* Copyright (c) 2016 Paul Dreik (github@pauldreik.se)
|
||||||
* Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
* Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
||||||
|
* Copyright (c) 2019 Maximilian Bachmann (github@maxbachmann)
|
||||||
*
|
*
|
||||||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||||
* or copy at http://opensource.org/licenses/MIT)
|
* or copy at http://opensource.org/licenses/MIT)
|
||||||
@ -18,7 +19,7 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015
|
#if (__cplusplus >= 201103L) || ( defined(_MSC_VER) && (_MSC_VER >= 1800) ) // c++11: Visual Studio 2013
|
||||||
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);
|
||||||
@ -53,9 +54,7 @@ TEST(VariadicBind, invalid) {
|
|||||||
|
|
||||||
// make sure the content is as expected
|
// make sure the content is as expected
|
||||||
{
|
{
|
||||||
using namespace std::string_literals;
|
SQLite::Statement query(db, std::string{"SELECT id, value FROM test ORDER BY id"});
|
||||||
|
|
||||||
SQLite::Statement query(db, "SELECT id, value FROM test ORDER BY id"s);
|
|
||||||
std::vector<std::pair<int, std::string> > results;
|
std::vector<std::pair<int, std::string> > results;
|
||||||
while (query.executeStep()) {
|
while (query.executeStep()) {
|
||||||
const int id = query.getColumn(0);
|
const int id = query.getColumn(0);
|
||||||
@ -64,9 +63,9 @@ TEST(VariadicBind, invalid) {
|
|||||||
}
|
}
|
||||||
EXPECT_EQ(std::size_t(3), results.size());
|
EXPECT_EQ(std::size_t(3), results.size());
|
||||||
|
|
||||||
EXPECT_EQ(std::make_pair(1,""s), results.at(0));
|
EXPECT_EQ(std::make_pair(1,std::string{""}), results.at(0));
|
||||||
EXPECT_EQ(std::make_pair(2,"two"s), results.at(1));
|
EXPECT_EQ(std::make_pair(2,std::string{"two"}), results.at(1));
|
||||||
EXPECT_EQ(std::make_pair(3,"three"s), results.at(2));
|
EXPECT_EQ(std::make_pair(3,std::string{"three"}), results.at(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // c++14
|
#endif // c++11
|
||||||
|
Loading…
x
Reference in New Issue
Block a user