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

Dynamic methods of select, insert and co return *this now.

Not sure if chaining makes much sense, but it is possible now
This commit is contained in:
Roland Bock 2013-10-03 20:53:55 +02:00
parent bd337954fa
commit 96c6e5d435
9 changed files with 65 additions and 26 deletions

View File

@ -44,6 +44,11 @@ namespace sqlpp
using _value_type = boolean; using _value_type = boolean;
bool_operand(bool t): _t(t) {} 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<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
@ -53,7 +58,7 @@ namespace sqlpp
bool _is_trivial() const { return _t == false; } bool _is_trivial() const { return _t == false; }
const bool _t; bool _t;
}; };
template<typename T> template<typename T>
@ -63,6 +68,11 @@ namespace sqlpp
using _value_type = numeric; using _value_type = numeric;
numeric_operand(T t): _t(t) {} 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<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
@ -72,7 +82,7 @@ namespace sqlpp
bool _is_trivial() const { return _t == 0; } bool _is_trivial() const { return _t == 0; }
const T _t; T _t;
}; };
template<typename T> template<typename T>
@ -82,6 +92,11 @@ namespace sqlpp
using _value_type = text; using _value_type = text;
text_operand(const T& t): _t(t) {} 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<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
@ -91,7 +106,7 @@ namespace sqlpp
bool _is_trivial() const { return _t.empty(); } bool _is_trivial() const { return _t.empty(); }
const std::string _t; std::string _t;
}; };
template<typename T, typename Enable = void> template<typename T, typename Enable = void>

View File

@ -80,11 +80,13 @@ namespace sqlpp
} }
template<typename Assignment> template<typename Assignment>
void add_set(Assignment&& assignment) insert_t add_set(Assignment&& assignment)
{ {
static_assert(is_dynamic_t<InsertList>::value, "cannot call add_set() in a non-dynamic set"); static_assert(is_dynamic_t<InsertList>::value, "cannot call add_set() in a non-dynamic set");
_insert_list.add(std::forward<Assignment>(assignment)); _insert_list.add(std::forward<Assignment>(assignment));
return *this;
} }
template<typename Db> template<typename Db>

View File

@ -73,23 +73,25 @@ namespace sqlpp
}; };
} }
auto dynamic_using_() auto dynamic_using_()
-> set_using_t<dynamic_using_t<Database>> -> set_using_t<dynamic_using_t<Database>>
{ {
static_assert(std::is_same<Using, noop>::value, "cannot call using() twice"); static_assert(std::is_same<Using, noop>::value, "cannot call using() twice");
static_assert(std::is_same<Where, noop>::value, "cannot call using() after where()"); static_assert(std::is_same<Where, noop>::value, "cannot call using() after where()");
return { return {
_table, _table,
{{}}, {{}},
_where _where
}; };
} }
template<typename Tab> template<typename Tab>
void add_using_(Tab&& table) remove_t& add_using_(Tab&& table)
{ {
static_assert(is_dynamic_t<Using>::value, "cannot call add_using() in a non-dynamic using"); static_assert(is_dynamic_t<Using>::value, "cannot call add_using() in a non-dynamic using");
_using.add(std::forward<Tab>(table)); _using.add(std::forward<Tab>(table));
return *this;
} }
template<typename Expr> template<typename Expr>
@ -116,11 +118,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_where(Expr&& expr) remove_t& add_where(Expr&& expr)
{ {
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where"); static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where");
_where.add(std::forward<Expr>(expr)); _where.add(std::forward<Expr>(expr));
return *this;
} }

View File

@ -206,12 +206,14 @@ namespace sqlpp
} }
template<typename Table> template<typename Table>
void add_from(Table&& table) select_t& add_from(Table&& table)
{ {
static_assert(not is_noop<ExpressionList>::value, "cannot call add_from() without having selected anything"); static_assert(not is_noop<ExpressionList>::value, "cannot call add_from() without having selected anything");
static_assert(is_dynamic_t<From>::value, "cannot call add_from() in a non-dynamic from"); static_assert(is_dynamic_t<From>::value, "cannot call add_from() in a non-dynamic from");
_from.add(std::forward<Table>(table)); _from.add(std::forward<Table>(table));
return *this;
} }
template<typename Expr> template<typename Expr>
@ -252,11 +254,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_where(Expr&& expr) select_t& add_where(Expr&& expr)
{ {
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where"); static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where");
_where.add(std::forward<Expr>(expr)); _where.add(std::forward<Expr>(expr));
return *this;
} }
template<typename... Col> template<typename... Col>
@ -297,11 +301,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_group_by(Expr&& expr) select_t& add_group_by(Expr&& expr)
{ {
static_assert(is_dynamic_t<GroupBy>::value, "cannot call add_group_by() in a non-dynamic group_by"); static_assert(is_dynamic_t<GroupBy>::value, "cannot call add_group_by() in a non-dynamic group_by");
_group_by.add(std::forward<Expr>(expr)); _group_by.add(std::forward<Expr>(expr));
return *this;
} }
template<typename Expr> template<typename Expr>
@ -342,11 +348,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_having(Expr&& expr) select_t& add_having(Expr&& expr)
{ {
static_assert(is_dynamic_t<Having>::value, "cannot call add_having() in a non-dynamic having"); static_assert(is_dynamic_t<Having>::value, "cannot call add_having() in a non-dynamic having");
_having.add(std::forward<Expr>(expr)); _having.add(std::forward<Expr>(expr));
return *this;
} }
template<typename... OrderExpr> template<typename... OrderExpr>
@ -387,11 +395,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_order_by(Expr&& expr) select_t& add_order_by(Expr&& expr)
{ {
static_assert(is_dynamic_t<OrderBy>::value, "cannot call add_order_by() in a non-dynamic order_by"); static_assert(is_dynamic_t<OrderBy>::value, "cannot call add_order_by() in a non-dynamic order_by");
_order_by.add(std::forward<Expr>(expr)); _order_by.add(std::forward<Expr>(expr));
return *this;
} }
auto limit(std::size_t limit) 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<Limit>::value, "cannot call set_limit() in a non-dynamic limit"); static_assert(is_dynamic_t<Limit>::value, "cannot call set_limit() in a non-dynamic limit");
_limit.set(limit); _limit.set(limit);
return *this;
} }
auto offset(std::size_t offset) 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<Offset>::value, "cannot call set_offset() in a non-dynamic limit"); static_assert(is_dynamic_t<Offset>::value, "cannot call set_offset() in a non-dynamic limit");
_offset.set(offset); _offset.set(offset);
return *this;
} }
template<typename AliasProvider> template<typename AliasProvider>

View File

@ -85,11 +85,13 @@ namespace sqlpp
} }
template<typename Assignment> template<typename Assignment>
void add_set(Assignment&& assignment) update_t& add_set(Assignment&& assignment)
{ {
static_assert(is_dynamic_t<Assignments>::value, "cannot call add_set() in a non-dynamic set"); static_assert(is_dynamic_t<Assignments>::value, "cannot call add_set() in a non-dynamic set");
_assignments.add(std::forward<Assignment>(assignment)); _assignments.add(std::forward<Assignment>(assignment));
return *this;
} }
template<typename Expr> template<typename Expr>
@ -118,11 +120,13 @@ namespace sqlpp
} }
template<typename Expr> template<typename Expr>
void add_where(Expr&& expr) update_t& add_where(Expr&& expr)
{ {
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where"); static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where");
_where.add(std::forward<Expr>(expr)); _where.add(std::forward<Expr>(expr));
return *this;
} }
template<typename Db> template<typename Db>

View File

@ -51,7 +51,7 @@ int main()
insert_into(t).serialize(std::cerr, db); std::cerr << "\n"; insert_into(t).serialize(std::cerr, db); std::cerr << "\n";
insert_into(t).set(t.beta = "kirschauflauf").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(); 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"; 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).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"; //insert_into(t).columns(t.alpha, t.beta).values(25, "drei").serialize(std::cerr, db); std::cerr << "\n";

View File

@ -49,8 +49,8 @@ int main()
remove_from(t).where(t.beta != "transparent").serialize(std::cerr, db); std::cerr << "\n"; 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"; remove_from(t).using_(t).serialize(std::cerr, db); std::cerr << "\n";
auto r = dynamic_remove_from(db, t).dynamic_using_().dynamic_where(); auto r = dynamic_remove_from(db, t).dynamic_using_().dynamic_where();
r.add_using_(t); r = r.add_using_(t);
r.add_where(t.beta != "transparent"); r = r.add_where(t.beta != "transparent");
r.serialize(std::cerr, db); std::cerr << "\n"; 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).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"; //insert_into(t).columns(t.alpha, t.beta).values(25, "drei").serialize(std::cerr, db); std::cerr << "\n";

View File

@ -264,10 +264,10 @@ int main()
{ {
auto s = dynamic_select(db, t).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset(); auto s = dynamic_select(db, t).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset();
s.add_from(t); s = s.add_from(t);
s.add_where(t.alpha > 7); s = s.add_where(t.alpha > 7);
s.set_limit(30); s = s.set_limit(30);
s.set_limit(3); s = s.set_limit(3);
std::cerr << "------------------------\n"; std::cerr << "------------------------\n";
s.serialize(std::cerr, db); s.serialize(std::cerr, db);
std::cerr << "------------------------\n"; std::cerr << "------------------------\n";

View File

@ -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).serialize(std::cerr, db); std::cerr << "\n";
update(t).set(t.gamma = false).where(t.beta != "transparent").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(); 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"; u.serialize(std::cerr, db); std::cerr << "\n";
//insert_into(t).values(7, "wurstwaren", true).serialize(std::cerr, db); std::cerr << "\n"; //insert_into(t).values(7, "wurstwaren", true).serialize(std::cerr, db); std::cerr << "\n";