0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Use serializable_list in update and insert

This commit is contained in:
Roland Bock 2013-11-15 09:59:44 +01:00
parent 16e3851847
commit 3c5682f3ae
2 changed files with 14 additions and 66 deletions

View File

@ -27,29 +27,13 @@
#ifndef SQLPP_ASSIGNMENT_LIST_H #ifndef SQLPP_ASSIGNMENT_LIST_H
#define SQLPP_ASSIGNMENT_LIST_H #define SQLPP_ASSIGNMENT_LIST_H
#include <vector>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/detail/set.h> #include <sqlpp11/detail/set.h>
#include <sqlpp11/detail/serialize_tuple.h> #include <sqlpp11/detail/serialize_tuple.h>
#include <sqlpp11/detail/serializable.h> #include <sqlpp11/detail/serializable_list.h>
namespace sqlpp namespace sqlpp
{ {
namespace detail
{
template<typename Db>
struct dynamic_assignment_list
{
using type = std::vector<detail::serializable_t<Db>>;
};
template<>
struct dynamic_assignment_list<void>
{
using type = std::vector<noop>;
};
};
template<typename Database, template<typename> class ProhibitPredicate, typename... Assignments> template<typename Database, template<typename> class ProhibitPredicate, typename... Assignments>
struct assignment_list_t struct assignment_list_t
{ {
@ -75,7 +59,7 @@ namespace sqlpp
{ {
static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments"); static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments");
static_assert(not ProhibitPredicate<typename std::decay<Assignment>::type::column_type>::value, "set() argument must not be updated"); static_assert(not ProhibitPredicate<typename std::decay<Assignment>::type::column_type>::value, "set() argument must not be updated");
_dynamic_assignments.push_back(std::forward<Assignment>(assignment)); _dynamic_assignments.emplace_back(std::forward<Assignment>(assignment));
} }
template<typename Db> template<typename Db>
@ -83,18 +67,11 @@ namespace sqlpp
{ {
os << " SET "; os << " SET ";
detail::serialize_tuple(os, db, _assignments, ','); detail::serialize_tuple(os, db, _assignments, ',');
bool first = sizeof...(Assignments) == 0; _dynamic_assignments.serialize(os, db, sizeof...(Assignments) == 0);
for (const auto& assignment : _dynamic_assignments)
{
if (not first)
os << ',';
assignment.serialize(os, db);
first = false;
}
} }
std::tuple<Assignments...> _assignments; std::tuple<typename std::decay<Assignments>::type...> _assignments;
typename detail::dynamic_assignment_list<Database>::type _dynamic_assignments; typename detail::serializable_list<Database> _dynamic_assignments;
}; };
} }

View File

@ -27,11 +27,10 @@
#ifndef SQLPP_INSERT_LIST_H #ifndef SQLPP_INSERT_LIST_H
#define SQLPP_INSERT_LIST_H #define SQLPP_INSERT_LIST_H
#include <vector>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/detail/set.h> #include <sqlpp11/detail/set.h>
#include <sqlpp11/detail/serialize_tuple.h> #include <sqlpp11/detail/serialize_tuple.h>
#include <sqlpp11/detail/serializable.h> #include <sqlpp11/detail/serializable_list.h>
namespace sqlpp namespace sqlpp
{ {
@ -47,18 +46,6 @@ namespace sqlpp
os << " DEFAULT VALUES"; os << " DEFAULT VALUES";
} }
template<typename Db>
struct dynamic_column_list
{
using type = std::vector<detail::serializable_t<Db>>;
};
template<>
struct dynamic_column_list<void>
{
using type = std::vector<noop>;
};
template<typename Column> template<typename Column>
struct insert_column struct insert_column
{ {
@ -107,8 +94,8 @@ namespace sqlpp
{ {
static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments"); static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments");
static_assert(not ProhibitPredicate<typename std::decay<Assignment>::type>::value, "set() argument must not be used in insert"); static_assert(not ProhibitPredicate<typename std::decay<Assignment>::type>::value, "set() argument must not be used in insert");
_dynamic_columns.push_back(std::forward<typename Assignment::column_type>(assignment._lhs)); _dynamic_columns.emplace_back(detail::insert_column<typename Assignment::column_type>{std::forward<typename Assignment::column_type>(assignment._lhs)});
_dynamic_values.push_back(std::forward<typename Assignment::value_type>(assignment._rhs)); _dynamic_values.emplace_back(std::forward<typename Assignment::value_type>(assignment._rhs));
} }
template<typename Db> template<typename Db>
@ -120,38 +107,22 @@ namespace sqlpp
} }
else else
{ {
constexpr bool first = sizeof...(Assignments) == 0;
os << " ("; os << " (";
detail::serialize_tuple(os, db, _columns, ','); detail::serialize_tuple(os, db, _columns, ',');
{ _dynamic_columns.serialize(os, db, first);
bool first = sizeof...(Assignments) == 0;
for (const auto column : _dynamic_columns)
{
if (not first)
os << ',';
column.serialize(os, db);
first = false;
}
}
os << ") VALUES ("; os << ") VALUES (";
detail::serialize_tuple(os, db, _values, ','); detail::serialize_tuple(os, db, _values, ',');
{ _dynamic_values.serialize(os, db, first);
bool first = sizeof...(Assignments) == 0;
for (const auto column : _dynamic_values)
{
if (not first)
os << ',';
column.serialize(os, db);
first = false;
}
}
os << ")"; os << ")";
} }
} }
std::tuple<detail::insert_column<typename Assignments::column_type>...> _columns; std::tuple<detail::insert_column<typename Assignments::column_type>...> _columns;
std::tuple<typename Assignments::value_type...> _values; std::tuple<typename Assignments::value_type...> _values;
typename detail::dynamic_column_list<Database>::type _dynamic_columns; typename detail::serializable_list<Database> _dynamic_columns;
typename detail::dynamic_column_list<Database>::type _dynamic_values; typename detail::serializable_list<Database> _dynamic_values;
}; };
} }