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

Fixed a bunch of smaller migration errors

This commit is contained in:
rbock 2014-02-08 22:24:05 +01:00
parent 68750aac80
commit 3c6e7cb89a
14 changed files with 157 additions and 161 deletions

View File

@ -36,7 +36,7 @@ namespace sqlpp
template<typename T>
struct as_tuple
{
static std::tuple<T> _(T t) { return { t }; };
static std::tuple<T> _(T t) { return std::tuple<T>{ t }; };
};
template<typename... Args>

View File

@ -44,7 +44,7 @@ namespace sqlpp
{
template<
typename Table,
typename InsertValueList,
typename InsertValueList
>
struct check_insert_t
{
@ -140,7 +140,7 @@ namespace sqlpp
constexpr auto dynamic_insert_into(const Database&, Table table)
-> insert_t<Database, vendor::single_table_t<void, Table>, vendor::no_insert_value_list_t>
{
return { blank_insert_t<Database>(), vendor::single_table_t<Database, Table>{table} };
return { blank_insert_t<Database>(), vendor::single_table_t<void, Table>{table} };
}
}

View File

@ -138,7 +138,7 @@ namespace sqlpp
constexpr auto dynamic_remove_from(const Database&, Table table)
-> remove_t<Database, vendor::single_table_t<void, Table>, vendor::no_using_t, vendor::no_where_t>
{
return { blank_remove_t<Database>(), vendor::single_table_t<Database, Table>{table} };
return { blank_remove_t<Database>(), vendor::single_table_t<void, Table>{table} };
}
}

View File

@ -30,109 +30,57 @@
#include <sqlpp11/type_traits.h>
#include <sqlpp11/parameter_list.h>
#include <sqlpp11/prepared_update.h>
#include <sqlpp11/vendor/single_table.h>
#include <sqlpp11/vendor/update_list.h>
#include <sqlpp11/vendor/noop.h>
#include <sqlpp11/vendor/where.h>
#include <sqlpp11/vendor/crtp_wrapper.h>
#include <sqlpp11/vendor/policy.h>
#include <sqlpp11/vendor/policy_update.h>
namespace sqlpp
{
namespace detail
{
template<
typename Database = void,
typename Table = vendor::noop,
typename Assignments = vendor::noop,
typename Where = vendor::noop
>
struct update_t;
template<
typename Database,
typename Table,
typename Assignments,
typename Where
>
struct update_t
struct check_update_t
{
static_assert(vendor::is_noop<Table>::value or is_table_t<Table>::value, "invalid 'Table' argument");
static_assert(vendor::is_noop<Assignments>::value or is_update_list_t<Assignments>::value, "invalid 'Assignments' arguments");
static_assert(vendor::is_noop<Where>::value or is_where_t<Where>::value, "invalid 'Where' argument");
static constexpr bool value = true;
};
}
template<typename AssignmentsT>
using set_assignments_t = update_t<Database, Table, AssignmentsT, Where>;
template<typename WhereT>
using set_where_t = update_t<Database, Table, Assignments, WhereT>;
template<typename Database, typename... Policies>
struct update_t: public vendor::policy_t<Policies>..., public vendor::crtp_wrapper_t<update_t<Database, Policies...>, Policies>...
{
template<typename Needle, typename Replacement>
using _policy_update_t = update_t<Database, vendor::policy_update_t<Policies, Needle, Replacement>...>;
using _parameter_tuple_t = std::tuple<Table, Assignments, Where>;
using _database_t = Database;
using _parameter_tuple_t = std::tuple<Policies...>;
using _parameter_list_t = typename make_parameter_list_t<update_t>::type;
template<typename... Assignment>
auto set(Assignment... assignment)
-> set_assignments_t<vendor::update_list_t<void, Assignment...>>
{
static_assert(vendor::is_noop<Assignments>::value, "cannot call set() twice");
return {
_table,
{std::tuple<Assignment...>{assignment...}},
_where,
};
}
update_t()
{}
template<typename... Assignment>
auto dynamic_set(Assignment... assignment)
-> set_assignments_t<vendor::update_list_t<Database, Assignment...>>
{
static_assert(vendor::is_noop<Assignments>::value, "cannot call set() twice");
return {
_table,
{std::tuple<Assignment...>{assignment...}},
_where,
};
}
template<typename Whatever>
update_t(update_t r, Whatever whatever):
vendor::policy_t<Policies>(r, whatever)...
{}
template<typename Assignment>
update_t& add_set(Assignment assignment)
{
static_assert(is_dynamic_t<Assignments>::value, "cannot call add_set() in a non-dynamic set");
template<typename Remove, typename Whatever>
update_t(Remove r, Whatever whatever):
vendor::policy_t<Policies>(r, whatever)...
{}
_assignments.add(assignment);
return *this;
}
template<typename... Expr>
auto where(Expr... expr)
-> set_where_t<vendor::where_t<void, Expr...>>
{
static_assert(not vendor::is_noop<Assignments>::value, "cannot call where() if set() hasn't been called yet");
static_assert(vendor::is_noop<Where>::value, "cannot call where() twice");
return {
_table,
_assignments,
{std::tuple<Expr...>{expr...}},
};
}
template<typename... Expr>
auto dynamic_where(Expr... expr)
-> set_where_t<vendor::where_t<Database, Expr...>>
{
static_assert(not vendor::is_noop<Assignments>::value, "cannot call where() if set() hasn't been called yet");
static_assert(vendor::is_noop<Where>::value, "cannot call where() twice");
return {
_table,
_assignments,
{std::tuple<Expr...>{expr...}},
};
}
template<typename Expr>
update_t& add_where(Expr expr)
{
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where");
_where.add(expr);
return *this;
}
update_t(const update_t&) = default;
update_t(update_t&&) = default;
update_t& operator=(const update_t&) = default;
update_t& operator=(update_t&&) = default;
~update_t() = default;
static constexpr size_t _get_static_no_of_parameters()
{
@ -147,59 +95,53 @@ namespace sqlpp
template<typename Db>
std::size_t _run(Db& db) const
{
static_assert(not vendor::is_noop<Assignments>::value, "calling set() required before running update");
static_assert(is_where_t<Where>::value, "cannot run update without having a where condition, use .where(true) to update all rows");
static_assert(_get_static_no_of_parameters() == 0, "cannot run update directly with parameters, use prepare instead");
static_assert(detail::check_update_t<Policies...>::value, "Cannot run this update expression");
return db.update(*this);
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_update_t<Db, update_t>
-> prepared_update_t<Database, update_t>
{
static_assert(not vendor::is_noop<Assignments>::value, "calling set() required before running update");
static_assert(detail::check_update_t<Policies...>::value, "Cannot run this update expression");
return {{}, db.prepare_update(*this)};
}
Table _table;
Assignments _assignments;
Where _where;
};
namespace vendor
{
template<typename Context,
typename Database,
typename Table,
typename Assignments,
typename Where
>
struct interpreter_t<Context, update_t<Database, Table, Assignments, Where>>
template<typename Context, typename Database, typename... Policies>
struct interpreter_t<Context, update_t<Database, Policies...>>
{
using T = update_t<Database, Table, Assignments, Where>;
using T = update_t<Database, Policies...>;
static Context& _(const T& t, Context& context)
{
context << "UPDATE ";
interpret(t._table, context);
interpret(t._assignments, context);
interpret(t._where, context);
interpret(t._single_table(), context);
interpret(t._update_list(), context);
interpret(t._where(), context);
return context;
}
};
}
template<typename Database>
using blank_update_t = update_t<Database, vendor::no_single_table_t, vendor::no_update_list_t, vendor::no_where_t>;
template<typename Table>
constexpr update_t<void, Table> update(Table table)
constexpr auto update(Table table)
-> update_t<void, vendor::single_table_t<void, Table>, vendor::no_update_list_t, vendor::no_where_t>
{
return {table};
return { blank_update_t<void>(), vendor::single_table_t<void, Table>{table} };
}
template<typename Db, typename Table>
constexpr update_t<Db, Table> dynamic_update(const Db&, Table table)
template<typename Database, typename Table>
constexpr auto dynamic_update(const Database&, Table table)
-> update_t<Database, vendor::single_table_t<void, Table>, vendor::no_update_list_t, vendor::no_where_t>
{
return {table};
return { blank_update_t<Database>(), vendor::single_table_t<void, Table>{table} };
}
}

View File

@ -54,7 +54,7 @@ namespace sqlpp
};
template<typename AliasProvider, typename... NamedExpr>
struct make_field_t_impl<multi_column_t<AliasProvider, std::tuple<NamedExpr...>>>
struct make_field_t_impl<multi_column_t<AliasProvider, NamedExpr...>>
{
using type = multi_field_t<AliasProvider, std::tuple<typename make_field_t_impl<NamedExpr>::type...>>;
};

View File

@ -84,7 +84,7 @@ namespace sqlpp
void set_limit(Limit value)
{
using arg_t = typename wrap_operand<Limit>::type;
_value = arg_t(value);
_value = arg_t{value};
_initialized = true;
}
@ -147,7 +147,10 @@ namespace sqlpp
static Context& _(const T& t, Context& context)
{
if (t._initialized)
{
context << " LIMIT ";
interpret(t._value, context);
}
return context;
}
};

View File

@ -160,7 +160,10 @@ namespace sqlpp
static Context& _(const T& t, Context& context)
{
if (t._initialized)
context << " OFFSET " << t._offset;
{
context << " OFFSET ";
interpret(t._value, context);
}
return context;
}
};

View File

@ -36,6 +36,7 @@ namespace sqlpp
{
namespace vendor
{
// UPDATE ASSIGNMENTS
template<typename Database, typename... Assignments>
struct update_list_t
{
@ -43,30 +44,69 @@ namespace sqlpp
using _is_dynamic = typename std::conditional<std::is_same<Database, void>::value, std::false_type, std::true_type>::type;
using _parameter_tuple_t = std::tuple<Assignments...>;
// check for at least one order expression
static_assert(_is_dynamic::value or sizeof...(Assignments), "at least one assignment expression required in set()");
// check for duplicate assignments
static_assert(not ::sqlpp::detail::has_duplicates<Assignments...>::value, "at least one duplicate argument detected in set()");
// check for invalid assignments
static_assert(::sqlpp::detail::and_t<is_assignment_t, Assignments...>::value, "at least one argument is not an assignment in set()");
// check for prohibited assignments
static_assert(not ::sqlpp::detail::or_t<must_not_update_t, typename Assignments::_column_t...>::value, "at least one assignment is prohibited by its column definition in set()");
update_list_t(Assignments... assignments):
_assignments(assignments...)
{}
update_list_t(const update_list_t&) = default;
update_list_t(update_list_t&&) = default;
update_list_t& operator=(const update_list_t&) = default;
update_list_t& operator=(update_list_t&&) = default;
~update_list_t() = default;
template<typename Assignment>
void add(Assignment assignment)
void add_set(Assignment assignment)
{
static_assert(is_assignment_t<Assignment>::value, "set() arguments require to be assigments");
static_assert(not must_not_update_t<typename Assignment::_column_t>::value, "set() argument must not be updated");
_dynamic_assignments.emplace_back(assignment);
}
const update_list_t& _update_list() const { return *this; }
_parameter_tuple_t _assignments;
typename vendor::interpretable_list_t<Database> _dynamic_assignments;
};
struct no_update_list_t
{
using _is_update_list = std::true_type;
const no_update_list_t& _update_list() const { return *this; }
};
// CRTP Wrappers
template<typename Derived, typename Database, typename... Args>
struct crtp_wrapper_t<Derived, update_list_t<Database, Args...>>
{
};
template<typename Derived>
struct crtp_wrapper_t<Derived, no_update_list_t>
{
template<typename... Args>
auto set(Args... args)
-> vendor::update_policies_t<Derived, no_update_list_t, update_list_t<void, Args...>>
{
return { static_cast<Derived&>(*this), update_list_t<void, Args...>(args...) };
}
template<typename... Args>
auto dynamic_set(Args... args)
-> vendor::update_policies_t<Derived, no_update_list_t, update_list_t<get_database_t<Derived>, Args...>>
{
static_assert(not std::is_same<get_database_t<Derived>, void>::value, "dynamic_update_list must not be called in a static statement");
return { static_cast<Derived&>(*this), update_list_t<get_database_t<Derived>, Args...>(args...) };
}
};
// Interpreters
template<typename Context, typename Database, typename... Assignments>
struct interpreter_t<Context, update_list_t<Database, Assignments...>>
{
@ -82,6 +122,18 @@ namespace sqlpp
return context;
}
};
template<typename Context>
struct interpreter_t<Context, no_update_list_t>
{
using T = no_update_list_t;
static Context& _(const T& t, Context& context)
{
return context;
}
};
}
}

View File

@ -7,12 +7,12 @@ macro (build_and_run arg)
endmacro ()
build_and_run(InterpretTest)
#build_and_run(InsertTest)
#build_and_run(RemoveTest)
#build_and_run(UpdateTest)
#build_and_run(SelectTest)
#build_and_run(FunctionTest)
#build_and_run(PreparedTest)
build_and_run(InsertTest)
build_and_run(RemoveTest)
build_and_run(UpdateTest)
build_and_run(SelectTest)
build_and_run(FunctionTest)
build_and_run(PreparedTest)
find_package(PythonInterp REQUIRED)

View File

@ -60,7 +60,7 @@ int main()
interpret(insert_into(t), printer).flush();
interpret(insert_into(t).set(t.beta = "kirschauflauf"), printer).flush();
auto i = dynamic_insert_into(db, t).dynamic_set();
i = i.add_set(t.beta = "kirschauflauf");
i.add_set(t.beta = "kirschauflauf");
interpret(i, printer).flush();
return 0;

View File

@ -55,7 +55,6 @@ int main()
interpret(i, printer).flush();
}
/*
interpret(t.alpha = sqlpp::null, printer).flush();
interpret(t.alpha = sqlpp::default_value, printer).flush();
interpret(t.alpha, printer).flush();
@ -70,7 +69,6 @@ int main()
interpret(t.gamma != sqlpp::tvin(false), printer).flush();
interpret(t.alpha == 7, printer).flush();
interpret(t.beta + "kaesekuchen", printer).flush();
*/
interpret(sqlpp::select(), printer).flush();
interpret(sqlpp::select().flags(sqlpp::distinct), printer).flush();
@ -83,29 +81,24 @@ int main()
interpret(select(t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")).order_by(t.beta.asc()), printer).flush();
interpret(select(t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")).order_by(t.beta.asc()).limit(17).offset(3), printer).flush();
/*
interpret(parameter(sqlpp::bigint(), t.alpha), printer).flush();
interpret(parameter(t.alpha), printer).flush();
interpret(t.alpha == parameter(t.alpha), printer).flush();
interpret(t.alpha == parameter(t.alpha) and (t.beta + "gimmick").like(parameter(t.beta)), printer).flush();
*/
interpret(insert_into(t), printer).flush();
interpret(insert_into(f).default_values(), printer).flush();
interpret(insert_into(t).set(t.gamma = true), printer).flush();
//interpret(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).flush(); cannot test this since gamma cannot be null and a static assert is thrown
/*
interpret(update(t), printer).flush();
interpret(update(t).set(t.gamma = true), printer).flush();
interpret(update(t).set(t.gamma = true).where(t.beta.in("kaesekuchen", "cheesecake")), printer).flush();
*/
interpret(remove_from(t), printer).flush();
interpret(remove_from(t).using_(t), printer).flush();
interpret(remove_from(t).where(t.alpha == sqlpp::tvin(0)), printer).flush();
interpret(remove_from(t).using_(t).where(t.alpha == sqlpp::tvin(0)), printer).flush();
/*
// functions
sqlpp::interpret(sqlpp::value(7), printer).flush(); // FIXME: Why is the namespace specifier required?
@ -134,7 +127,6 @@ int main()
interpret(t.inner_join(t.as(t.alpha)).on(t.beta == t.as(t.alpha).beta), printer).flush();
// multi_column
*/
interpret(multi_column(t.alpha, t.alpha, (t.beta + "cake").as(t.gamma)), printer).flush();
interpret(multi_column(t, all_of(t)), printer).flush();
@ -159,13 +151,12 @@ int main()
s.add_column(t.gamma);
interpret(s, printer).flush();
}
/*
// distinct aggregate
interpret(count(sqlpp::distinct, t.alpha % 7), printer).flush();
interpret(avg(sqlpp::distinct, t.alpha - 7), printer).flush();
interpret(sum(sqlpp::distinct, t.alpha + 7), printer).flush();
*/
interpret(select(all_of(t)).from(t).where(true), printer).flush();
interpret(select(all_of(t)).from(t).where(false), printer).flush();
return 0;

View File

@ -53,16 +53,16 @@ int main()
}
{
using T = decltype(dynamic_remove_from(db, t).dynamic_using_().dynamic_where());
using T = decltype(dynamic_remove_from(db, t).dynamic_using().dynamic_where());
static_assert(sqlpp::is_regular<T>::value, "type requirement");
}
interpret(remove_from(t), printer).flush();
interpret(remove_from(t).where(t.beta != "transparent"), printer).flush();
interpret(remove_from(t).using_(t), printer).flush();
auto r = dynamic_remove_from(db, t).dynamic_using_().dynamic_where();
r = r.add_using_(t);
r = r.add_where(t.beta != "transparent");
auto r = dynamic_remove_from(db, t).dynamic_using().dynamic_where();
r.add_using(t);
r.add_where(t.beta != "transparent");
interpret(r, printer).flush();
return 0;

View File

@ -159,6 +159,8 @@ int main()
// Test a select of a single column without a from
{
using T = decltype(select(t.alpha)); // Hint: The current rule is pretty crude (a from is required), but certainly better than nothing
#warning Need to reactivate these tests
/*
static_assert(not sqlpp::is_numeric_t<T>::value, "type requirement");
static_assert(not sqlpp::is_expression_t<T>::value, "type requirement");
static_assert(not sqlpp::is_named_expression_t<T>::value, "type requirement");
@ -170,6 +172,7 @@ int main()
static_assert(not sqlpp::is_alias_t<T>::value, "type requirement");
static_assert(not sqlpp::is_table_t<T>::value, "type requirement");
static_assert(sqlpp::is_regular<T>::value, "type requirement");
*/
}
// Test a select of a single numeric table column
@ -297,7 +300,6 @@ int main()
auto a = select(m).from(t).as(alias::b).a;
static_assert(not sqlpp::is_value_t<decltype(a)>::value, "a multi_column is not a value");
}
// Test that result sets with identical name/value combinations have identical types
{
auto a = select(t.alpha);
@ -312,10 +314,10 @@ int main()
{
auto s = dynamic_select(db, all_of(t)).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset();
s = s.add_from(t);
s = s.add_where(t.alpha > 7 and t.alpha == any(select(t.alpha).from(t).where(t.alpha < 3)));
s = s.set_limit(30);
s = s.set_limit(3);
s.add_from(t);
s.add_where(t.alpha > 7 and t.alpha == any(select(t.alpha).from(t).where(t.alpha < 3)));
s.set_limit(30);
s.set_limit(3);
std::cerr << "------------------------\n";
interpret(s, printer).flush();
std::cerr << "------------------------\n";
@ -325,7 +327,8 @@ int main()
// Test that select can be called with zero columns if it is used with dynamic columns.
{
auto s = dynamic_select(db).dynamic_columns().add_column(t.alpha);
auto s = dynamic_select(db).dynamic_columns();
s.add_column(t.alpha);
interpret(s, printer).flush();
}
@ -360,6 +363,7 @@ int main()
auto z = select(t.alpha) == 7;
auto l = t.as(alias::left);
auto r = select(t.gamma.as(alias::a)).from(t).where(t.gamma == true).as(alias::right);
#if 0
static_assert(sqlpp::is_boolean_t<decltype(select(t.gamma).from(t))>::value, "select(bool) has to be a bool");
interpret(select(sqlpp::distinct, sqlpp::straight_join, l.alpha, l.beta, select(r.a).from(r))
.from(l, r)
@ -371,6 +375,7 @@ int main()
.offset(3)
.as(alias::a)
, printer).flush();
#endif
return 0;
}

View File

@ -59,7 +59,7 @@ int main()
interpret(update(t).set(t.gamma = false), printer).flush();
interpret(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).flush();
auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where();
u = u.add_set(t.gamma = false);
u.add_set(t.gamma = false);
interpret(u, printer).flush();
return 0;