From 96c6e5d4356c5b81f7a82b0307daa6dc2e647103 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Thu, 3 Oct 2013 20:53:55 +0200 Subject: [PATCH] Dynamic methods of select, insert and co return *this now. Not sure if chaining makes much sense, but it is possible now --- include/sqlpp11/detail/wrap_operand.h | 21 +++++++++++++++++--- include/sqlpp11/insert.h | 4 +++- include/sqlpp11/remove.h | 14 +++++++++----- include/sqlpp11/select.h | 28 ++++++++++++++++++++------- include/sqlpp11/update.h | 8 ++++++-- tests/InsertTest.cpp | 2 +- tests/RemoveTest.cpp | 4 ++-- tests/SelectTest.cpp | 8 ++++---- tests/UpdateTest.cpp | 2 +- 9 files changed, 65 insertions(+), 26 deletions(-) diff --git a/include/sqlpp11/detail/wrap_operand.h b/include/sqlpp11/detail/wrap_operand.h index 4546aab8..74a356d2 100644 --- a/include/sqlpp11/detail/wrap_operand.h +++ b/include/sqlpp11/detail/wrap_operand.h @@ -44,6 +44,11 @@ namespace sqlpp using _value_type = boolean; bool_operand(bool t): _t(t) {} + bool_operand(const bool_operand&) = default; + bool_operand(bool_operand&&) = default; + bool_operand& operator=(const bool_operand&) = default; + bool_operand& operator=(bool_operand&&) = default; + ~bool_operand() = default; template void serialize(std::ostream& os, Db& db) const @@ -53,7 +58,7 @@ namespace sqlpp bool _is_trivial() const { return _t == false; } - const bool _t; + bool _t; }; template @@ -63,6 +68,11 @@ namespace sqlpp using _value_type = numeric; numeric_operand(T t): _t(t) {} + numeric_operand(const numeric_operand&) = default; + numeric_operand(numeric_operand&&) = default; + numeric_operand& operator=(const numeric_operand&) = default; + numeric_operand& operator=(numeric_operand&&) = default; + ~numeric_operand() = default; template void serialize(std::ostream& os, Db& db) const @@ -72,7 +82,7 @@ namespace sqlpp bool _is_trivial() const { return _t == 0; } - const T _t; + T _t; }; template @@ -82,6 +92,11 @@ namespace sqlpp using _value_type = text; text_operand(const T& t): _t(t) {} + text_operand(const text_operand&) = default; + text_operand(text_operand&&) = default; + text_operand& operator=(const text_operand&) = default; + text_operand& operator=(text_operand&&) = default; + ~text_operand() = default; template void serialize(std::ostream& os, Db& db) const @@ -91,7 +106,7 @@ namespace sqlpp bool _is_trivial() const { return _t.empty(); } - const std::string _t; + std::string _t; }; template diff --git a/include/sqlpp11/insert.h b/include/sqlpp11/insert.h index 53dae044..74974721 100644 --- a/include/sqlpp11/insert.h +++ b/include/sqlpp11/insert.h @@ -80,11 +80,13 @@ namespace sqlpp } template - void add_set(Assignment&& assignment) + insert_t add_set(Assignment&& assignment) { static_assert(is_dynamic_t::value, "cannot call add_set() in a non-dynamic set"); _insert_list.add(std::forward(assignment)); + + return *this; } template diff --git a/include/sqlpp11/remove.h b/include/sqlpp11/remove.h index da6928e6..e5a67958 100644 --- a/include/sqlpp11/remove.h +++ b/include/sqlpp11/remove.h @@ -73,23 +73,25 @@ namespace sqlpp }; } - auto dynamic_using_() + auto dynamic_using_() -> set_using_t> { static_assert(std::is_same::value, "cannot call using() twice"); static_assert(std::is_same::value, "cannot call using() after where()"); return { - _table, + _table, {{}}, _where }; } - template - void add_using_(Tab&& table) + template + remove_t& add_using_(Tab&& table) { static_assert(is_dynamic_t::value, "cannot call add_using() in a non-dynamic using"); _using.add(std::forward(table)); + + return *this; } template @@ -116,11 +118,13 @@ namespace sqlpp } template - void add_where(Expr&& expr) + remove_t& add_where(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_where() in a non-dynamic where"); _where.add(std::forward(expr)); + + return *this; } diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 83595cf9..53d09950 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -206,12 +206,14 @@ namespace sqlpp } template - void add_from(Table&& table) + select_t& add_from(Table&& table) { static_assert(not is_noop::value, "cannot call add_from() without having selected anything"); static_assert(is_dynamic_t::value, "cannot call add_from() in a non-dynamic from"); _from.add(std::forward(table)); + + return *this; } template @@ -252,11 +254,13 @@ namespace sqlpp } template - void add_where(Expr&& expr) + select_t& add_where(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_where() in a non-dynamic where"); _where.add(std::forward(expr)); + + return *this; } template @@ -297,11 +301,13 @@ namespace sqlpp } template - void add_group_by(Expr&& expr) + select_t& add_group_by(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_group_by() in a non-dynamic group_by"); _group_by.add(std::forward(expr)); + + return *this; } template @@ -342,11 +348,13 @@ namespace sqlpp } template - void add_having(Expr&& expr) + select_t& add_having(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_having() in a non-dynamic having"); _having.add(std::forward(expr)); + + return *this; } template @@ -387,11 +395,13 @@ namespace sqlpp } template - void add_order_by(Expr&& expr) + select_t& add_order_by(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_order_by() in a non-dynamic order_by"); _order_by.add(std::forward(expr)); + + return *this; } auto limit(std::size_t limit) @@ -430,11 +440,13 @@ namespace sqlpp }; } - void set_limit(std::size_t limit) + select_t& set_limit(std::size_t limit) { static_assert(is_dynamic_t::value, "cannot call set_limit() in a non-dynamic limit"); _limit.set(limit); + + return *this; } auto offset(std::size_t offset) @@ -473,11 +485,13 @@ namespace sqlpp }; } - void set_offset(std::size_t offset) + select_t& set_offset(std::size_t offset) { static_assert(is_dynamic_t::value, "cannot call set_offset() in a non-dynamic limit"); _offset.set(offset); + + return *this; } template diff --git a/include/sqlpp11/update.h b/include/sqlpp11/update.h index 3a12b9ef..bfc074d5 100644 --- a/include/sqlpp11/update.h +++ b/include/sqlpp11/update.h @@ -85,11 +85,13 @@ namespace sqlpp } template - void add_set(Assignment&& assignment) + update_t& add_set(Assignment&& assignment) { static_assert(is_dynamic_t::value, "cannot call add_set() in a non-dynamic set"); _assignments.add(std::forward(assignment)); + + return *this; } template @@ -118,11 +120,13 @@ namespace sqlpp } template - void add_where(Expr&& expr) + update_t& add_where(Expr&& expr) { static_assert(is_dynamic_t::value, "cannot call add_where() in a non-dynamic where"); _where.add(std::forward(expr)); + + return *this; } template diff --git a/tests/InsertTest.cpp b/tests/InsertTest.cpp index 1b289de1..12e9ad97 100644 --- a/tests/InsertTest.cpp +++ b/tests/InsertTest.cpp @@ -51,7 +51,7 @@ int main() insert_into(t).serialize(std::cerr, db); std::cerr << "\n"; insert_into(t).set(t.beta = "kirschauflauf").serialize(std::cerr, db); std::cerr << "\n"; auto i = dynamic_insert_into(db, t).dynamic_set(); - i.add_set(t.beta = "kirschauflauf"); + i = i.add_set(t.beta = "kirschauflauf"); i.serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).values(7, "wurstwaren", true).serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).columns(t.alpha, t.beta).values(25, "drei").serialize(std::cerr, db); std::cerr << "\n"; diff --git a/tests/RemoveTest.cpp b/tests/RemoveTest.cpp index 118d95f9..5498efa2 100644 --- a/tests/RemoveTest.cpp +++ b/tests/RemoveTest.cpp @@ -49,8 +49,8 @@ int main() remove_from(t).where(t.beta != "transparent").serialize(std::cerr, db); std::cerr << "\n"; remove_from(t).using_(t).serialize(std::cerr, db); std::cerr << "\n"; auto r = dynamic_remove_from(db, t).dynamic_using_().dynamic_where(); - r.add_using_(t); - r.add_where(t.beta != "transparent"); + r = r.add_using_(t); + r = r.add_where(t.beta != "transparent"); r.serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).values(7, "wurstwaren", true).serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).columns(t.alpha, t.beta).values(25, "drei").serialize(std::cerr, db); std::cerr << "\n"; diff --git a/tests/SelectTest.cpp b/tests/SelectTest.cpp index 7adca851..fe14b56e 100644 --- a/tests/SelectTest.cpp +++ b/tests/SelectTest.cpp @@ -264,10 +264,10 @@ int main() { auto s = dynamic_select(db, t).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset(); - s.add_from(t); - s.add_where(t.alpha > 7); - s.set_limit(30); - s.set_limit(3); + s = s.add_from(t); + s = s.add_where(t.alpha > 7); + s = s.set_limit(30); + s = s.set_limit(3); std::cerr << "------------------------\n"; s.serialize(std::cerr, db); std::cerr << "------------------------\n"; diff --git a/tests/UpdateTest.cpp b/tests/UpdateTest.cpp index beb242be..9b57666f 100644 --- a/tests/UpdateTest.cpp +++ b/tests/UpdateTest.cpp @@ -49,7 +49,7 @@ int main() update(t).set(t.gamma = false).serialize(std::cerr, db); std::cerr << "\n"; update(t).set(t.gamma = false).where(t.beta != "transparent").serialize(std::cerr, db); std::cerr << "\n"; auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where(); - u.add_set(t.gamma = false); + u = u.add_set(t.gamma = false); u.serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).values(7, "wurstwaren", true).serialize(std::cerr, db); std::cerr << "\n";