mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Using assignments in add_values now
This makes mistakes much less likely
This commit is contained in:
parent
5ef5259988
commit
b0e7395f47
@ -114,13 +114,9 @@ namespace sqlpp
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Column>
|
||||
//using _insert_value_t = vendor::insert_value_t<typename vendor::wrap_operand<typename Column::_value_type::_cpp_value_type>::type>;
|
||||
using _insert_value_t = vendor::insert_value_t<typename Column::_value_type::_cpp_value_type>;
|
||||
|
||||
template<typename... Column>
|
||||
auto columns(Column... columns)
|
||||
-> set_column_value_list_t<vendor::column_list_t<Column...>, vendor::insert_value_list_t<_insert_value_t<Column>...>>
|
||||
-> set_column_value_list_t<vendor::column_list_t<Column...>, vendor::insert_value_list_t<vendor::insert_value_t<Column>...>>
|
||||
{
|
||||
static_assert(vendor::is_noop<ColumnList>::value, "cannot call columns() twice");
|
||||
static_assert(vendor::is_noop<InsertList>::value, "cannot call columns() after set() or dynamic_set()");
|
||||
@ -130,7 +126,7 @@ namespace sqlpp
|
||||
_table,
|
||||
_insert_list,
|
||||
{std::tuple<Column...>{columns...}},
|
||||
vendor::insert_value_list_t<_insert_value_t<Column>...>{},
|
||||
vendor::insert_value_list_t<vendor::insert_value_t<Column>...>{},
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ namespace sqlpp
|
||||
struct tvin_t
|
||||
{
|
||||
using _operand_t = typename vendor::wrap_operand<T>::type;
|
||||
static_assert(not std::is_same<_operand_t, T>::value, "tvin() used with invalid type (only string and primitive types allowed)");
|
||||
static_assert(std::is_same<_operand_t, vendor::text_operand>::value or not std::is_same<_operand_t, T>::value, "tvin() used with invalid type (only string and primitive types allowed)");
|
||||
using _value_type = typename _operand_t::_value_type;
|
||||
|
||||
tvin_t(T t):
|
||||
|
40
include/sqlpp11/vendor/insert_value.h
vendored
40
include/sqlpp11/vendor/insert_value.h
vendored
@ -37,31 +37,51 @@ namespace sqlpp
|
||||
{
|
||||
namespace vendor
|
||||
{
|
||||
template<typename ValueType>
|
||||
namespace detail
|
||||
{
|
||||
template<typename Type, bool>
|
||||
struct type_if
|
||||
{
|
||||
using type = Type;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
struct type_if<Type, false>
|
||||
{
|
||||
struct type
|
||||
{
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
template<typename Column>
|
||||
struct insert_value_t
|
||||
{
|
||||
using _is_insert_value = std::true_type;
|
||||
using _value_t = ValueType;
|
||||
using _pure_value_t = typename Column::_value_type::_cpp_value_type;
|
||||
using _wrapped_value_t = typename wrap_operand<_pure_value_t>::type;
|
||||
using _tvin_t = typename detail::type_if<tvin_t<_pure_value_t>, can_be_null_t<Column>::value>::type; // static asserts and SFINAE do not work together
|
||||
using _null_t = typename detail::type_if<null_t, can_be_null_t<Column>::value>::type; // static asserts and SFINAE do not work together
|
||||
|
||||
insert_value_t(_value_t value):
|
||||
insert_value_t(assignment_t<Column, _wrapped_value_t> assignment):
|
||||
_is_null(false),
|
||||
_is_default(false),
|
||||
_value({value})
|
||||
_value(assignment._rhs)
|
||||
{}
|
||||
|
||||
insert_value_t(tvin_t<_value_t> tvin):
|
||||
_is_null(tvin._is_trivial()),
|
||||
insert_value_t(assignment_t<Column, _tvin_t> assignment):
|
||||
_is_null(assignment._rhs._is_trivial()),
|
||||
_is_default(false),
|
||||
_value({tvin._value})
|
||||
_value(assignment._rhs._value)
|
||||
{}
|
||||
|
||||
insert_value_t(const ::sqlpp::null_t&):
|
||||
insert_value_t(const assignment_t<Column, _null_t>&):
|
||||
_is_null(true),
|
||||
_is_default(false),
|
||||
_value()
|
||||
{}
|
||||
|
||||
insert_value_t(const ::sqlpp::default_value_t&):
|
||||
insert_value_t(const assignment_t<Column, ::sqlpp::default_value_t>&):
|
||||
_is_null(false),
|
||||
_is_default(true),
|
||||
_value()
|
||||
@ -75,7 +95,7 @@ namespace sqlpp
|
||||
|
||||
bool _is_null;
|
||||
bool _is_default;
|
||||
typename wrap_operand<_value_t>::type _value;
|
||||
_wrapped_value_t _value;
|
||||
};
|
||||
|
||||
template<typename Context, typename ValueType>
|
||||
|
6
include/sqlpp11/vendor/select_flag_list.h
vendored
6
include/sqlpp11/vendor/select_flag_list.h
vendored
@ -41,7 +41,7 @@ namespace sqlpp
|
||||
template<typename Database, typename T>
|
||||
struct select_flag_list_t
|
||||
{
|
||||
static_assert(detail::wrong<T>::value, "invalid argument for select_flag_list");
|
||||
static_assert(::sqlpp::detail::wrong<T>::value, "invalid argument for select_flag_list");
|
||||
};
|
||||
|
||||
// select_flag_list_t
|
||||
@ -54,10 +54,10 @@ namespace sqlpp
|
||||
using size = std::tuple_size<_parameter_tuple_t>;
|
||||
|
||||
// check for duplicate order expressions
|
||||
static_assert(not detail::has_duplicates<Flag...>::value, "at least one duplicate argument detected in select flag list");
|
||||
static_assert(not ::sqlpp::detail::has_duplicates<Flag...>::value, "at least one duplicate argument detected in select flag list");
|
||||
|
||||
// check for invalid order expressions
|
||||
using _valid_flags = typename detail::make_set_if<is_select_flag_t, Flag...>::type;
|
||||
using _valid_flags = typename ::sqlpp::detail::make_set_if<is_select_flag_t, Flag...>::type;
|
||||
static_assert(_valid_flags::size::value == sizeof...(Flag), "at least one argument is not a select flag in select flag list");
|
||||
|
||||
template<typename E>
|
||||
|
@ -44,10 +44,16 @@ int main()
|
||||
TabFoo f;
|
||||
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta).add_values(true, "cheesecake"), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta).add_values(true, "cheesecake").add_values(false, sqlpp::tvin(std::string("coffee"))).add_values(false, sqlpp::tvin(std::string())), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta).add_values(sqlpp::default_value, sqlpp::null), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta).add_values(t.gamma = true, t.beta = "cheesecake"), printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta)
|
||||
.add_values(t.gamma = true, t.beta = "cheesecake")
|
||||
.add_values(t.gamma = false, t.beta = sqlpp::tvin(std::string("coffee"))) // FIXME: Want to use const char* const here, too
|
||||
.add_values(t.gamma = false, t.beta = sqlpp::tvin(std::string()))
|
||||
, printer).flush();
|
||||
interpret(insert_into(t).columns(t.gamma, t.beta)
|
||||
.add_values(t.gamma = sqlpp::default_value, t.beta = sqlpp::null)
|
||||
, printer).flush();
|
||||
|
||||
interpret(t.alpha = sqlpp::null, printer).flush();
|
||||
interpret(t.alpha = sqlpp::default_value, printer).flush();
|
||||
interpret(t.alpha, printer).flush();
|
||||
|
Loading…
Reference in New Issue
Block a user