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

Adjusted insert to use wrapped asserts as failure return

This commit is contained in:
rbock 2016-09-01 12:51:04 +02:00
parent cf83978118
commit 36afa04bcd
4 changed files with 48 additions and 48 deletions

View File

@ -426,6 +426,16 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_values_t, "insert values required, e.g. set(...) or default_values()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_columns_are_columns, "arguments for columns() must be table columns");
template <typename... Columns>
struct check_insert_columns
{
using type = static_combined_check_t<
static_check_t<logic::all_t<is_column_t<Columns>::value...>::value, assert_insert_columns_are_columns>>;
};
template <typename... Columns>
using check_insert_columns_t = typename check_insert_columns<Columns...>::type;
// NO INSERT COLUMNS/VALUES YET
struct no_insert_value_list_t
{
@ -478,32 +488,23 @@ namespace sqlpp
using _database_t = typename Policies::_database_t;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
// template <typename... T>
// using _column_check = logic::all_t<is_column_t<T>::value...>;
template <typename... T>
struct _column_check : logic::all_t<is_column_t<T>::value...>
{
};
template <typename Check, typename T>
using _new_statement_t = new_statement_t<Check::value, Policies, no_insert_value_list_t, T>;
using _new_statement_t = new_statement_t<Check, Policies, no_insert_value_list_t, T>;
using _consistency_check = assert_insert_values_t;
auto default_values() const -> _new_statement_t<std::true_type, insert_default_values_t>
auto default_values() const -> _new_statement_t<consistent_t, insert_default_values_t>
{
return {static_cast<const derived_statement_t<Policies>&>(*this), insert_default_values_data_t{}};
}
template <typename... Columns>
auto columns(Columns... cols) const -> _new_statement_t<_column_check<Columns...>, column_list_t<Columns...>>
auto columns(Columns... cols) const
-> _new_statement_t<check_insert_columns_t<Columns...>, column_list_t<Columns...>>
{
static_assert(logic::all_t<is_column_t<Columns>::value...>::value,
"at least one argument is not a column in columns()");
static_assert(sizeof...(Columns), "at least one column required in columns()");
return _columns_impl(_column_check<Columns...>{}, cols...);
return _columns_impl(check_insert_columns_t<Columns...>{}, cols...);
}
template <typename... Assignments>
@ -511,8 +512,6 @@ namespace sqlpp
-> _new_statement_t<check_insert_static_set_t<Assignments...>, insert_list_t<void, Assignments...>>
{
using Check = check_insert_static_set_t<Assignments...>;
Check{}._();
return _set_impl<void>(Check{}, assignments...);
}
@ -522,18 +521,16 @@ namespace sqlpp
insert_list_t<_database_t, Assignments...>>
{
using Check = check_insert_dynamic_set_t<_database_t, Assignments...>;
Check{}._();
return _set_impl<_database_t>(Check{}, assignments...);
}
private:
template <typename... Columns>
auto _columns_impl(const std::false_type&, Columns... cols) const -> bad_statement;
template <typename Check, typename... Columns>
auto _columns_impl(Check, Columns... cols) const -> Check;
template <typename... Columns>
auto _columns_impl(const std::true_type&, Columns... cols) const
-> _new_statement_t<std::true_type, column_list_t<Columns...>>
auto _columns_impl(consistent_t, Columns... cols) const
-> _new_statement_t<consistent_t, column_list_t<Columns...>>
{
static_assert(not detail::has_duplicates<Columns...>::value,
"at least one duplicate argument detected in columns()");
@ -548,12 +545,12 @@ namespace sqlpp
return {static_cast<const derived_statement_t<Policies>&>(*this), column_list_data_t<Columns...>{cols...}};
}
template <typename Database, typename... Assignments>
auto _set_impl(std::false_type, Assignments... assignments) const -> bad_statement;
template <typename Database, typename Check, typename... Assignments>
auto _set_impl(Check, Assignments... assignments) const -> Check;
template <typename Database, typename... Assignments>
auto _set_impl(std::true_type, Assignments... assignments) const
-> _new_statement_t<std::true_type, insert_list_t<Database, Assignments...>>
auto _set_impl(consistent_t, Assignments... assignments) const
-> _new_statement_t<consistent_t, insert_list_t<Database, Assignments...>>
{
return {static_cast<const derived_statement_t<Policies>&>(*this),
insert_list_data_t<Database, Assignments...>{assignments...}};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* Copyright (c) 2013-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -27,13 +27,13 @@
#ifndef SQLPP_INTO_H
#define SQLPP_INTO_H
#include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/no_value.h>
#include <sqlpp11/detail/type_set.h>
#include <sqlpp11/no_data.h>
#include <sqlpp11/prepared_insert.h>
#include <sqlpp11/serializer.h>
#include <sqlpp11/detail/type_set.h>
#include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h>
namespace sqlpp
{
@ -88,8 +88,7 @@ namespace sqlpp
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2091069
template <typename... Args>
_base_t(Args&&... args)
: into{std::forward<Args>(args)...}
_base_t(Args&&... args) : into{std::forward<Args>(args)...}
{
}
@ -115,6 +114,15 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(assert_into_t, "into() required");
SQLPP_PORTABLE_STATIC_ASSERT(assert_into_arg_is_table, "argument for into() must be a table");
template <typename T>
struct check_into
{
using type = static_combined_check_t<static_check_t<is_raw_table_t<T>::value, assert_into_arg_is_table>>;
};
template <typename T>
using check_into_t = typename check_into<wrap_operand_t<T>>::type;
// NO INTO YET
struct no_into_t
{
@ -155,28 +163,23 @@ namespace sqlpp
using _database_t = typename Policies::_database_t;
template <typename T>
using _check = logic::all_t<is_raw_table_t<T>::value>;
template <typename Check, typename T>
using _new_statement_t = new_statement_t<Check::value, Policies, no_into_t, T>;
using _new_statement_t = new_statement_t<Check, Policies, no_into_t, T>;
using _consistency_check = assert_into_t;
template <typename Table>
auto into(Table table) const -> _new_statement_t<_check<Table>, into_t<void, Table>>
auto into(Table table) const -> _new_statement_t<check_into_t<Table>, into_t<void, Table>>
{
static_assert(_check<Table>::value, "argument is not a raw table in into()");
return _into_impl<void>(_check<Table>{}, table);
return _into_impl<void>(check_into_t<Table>{}, table);
}
private:
template <typename Database, typename Table>
auto _into_impl(const std::false_type&, Table table) const -> bad_statement;
template <typename Database, typename Check, typename Table>
auto _into_impl(Check, Table table) const -> Check;
template <typename Database, typename Table>
auto _into_impl(const std::true_type&, Table table) const
-> _new_statement_t<std::true_type, into_t<Database, Table>>
auto _into_impl(consistent_t, Table table) const -> _new_statement_t<consistent_t, into_t<Database, Table>>
{
static_assert(required_tables_of<into_t<Database, Table>>::size::value == 0,
"argument depends on another table in into()");

View File

@ -34,10 +34,10 @@ set(test_names
#CustomQuery
#DateTime
#Interpret
#Insert
Insert
#Remove
#Update
Select
#Select
#SelectType
#Function
#Prepared

View File

@ -23,12 +23,12 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Sample.h"
#include "MockDb.h"
#include "Sample.h"
#include "is_regular.h"
#include <sqlpp11/insert.h>
#include <sqlpp11/functions.h>
#include <iostream>
#include <sqlpp11/functions.h>
#include <sqlpp11/insert.h>
int Insert(int, char* [])
{