Restructured expression types and braces to handle ANY and SOME

This commit is contained in:
Roland Bock
2013-08-21 23:25:38 +02:00
parent cf1dae396a
commit 09226497a4
5 changed files with 73 additions and 33 deletions

View File

@@ -124,13 +124,13 @@ namespace sqlpp
struct operators: public basic_operators<Base, _constraint>
{
template<typename T>
nary_expression_t<Base, and_, typename _constraint<T>::type> operator and(T&& t) const
binary_expression_t<Base, and_, typename _constraint<T>::type> operator and(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, or_, typename _constraint<T>::type> operator or(T&& t) const
binary_expression_t<Base, or_, typename _constraint<T>::type> operator or(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}

View File

@@ -104,25 +104,25 @@ namespace sqlpp
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, lt_, typename Constraint<T>::type> operator<(T&& t) const
binary_expression_t<Base, lt_, typename Constraint<T>::type> operator<(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, le_, typename Constraint<T>::type> operator<=(T&& t) const
binary_expression_t<Base, le_, typename Constraint<T>::type> operator<=(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, ge_, typename Constraint<T>::type> operator>=(T&& t) const
binary_expression_t<Base, ge_, typename Constraint<T>::type> operator>=(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, gt_, typename Constraint<T>::type> operator>(T&& t) const
binary_expression_t<Base, gt_, typename Constraint<T>::type> operator>(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
@@ -149,14 +149,14 @@ namespace sqlpp
// Hint: use value_list wrapper for containers...
template<typename... T>
nary_expression_t<Base, in_, typename Constraint<T>::type...> in(T&&... t) const
nary_member_function_t<Base, in_, typename Constraint<T>::type...> in(T&&... t) const
{
static_assert(sizeof...(T) > 0, "in() requires at least one argument");
return { *static_cast<const Base*>(this), std::forward<T>(t)... };
}
template<typename... T>
nary_expression_t<Base, not_in_, typename Constraint<T>::type...> not_in(T&&... t) const
nary_member_function_t<Base, not_in_, typename Constraint<T>::type...> not_in(T&&... t) const
{
static_assert(sizeof...(T) > 0, "not_in() requires at least one argument");
return { *static_cast<const Base*>(this), std::forward<T>(t)... };

View File

@@ -50,9 +50,8 @@ namespace sqlpp
}
else
{
os << "=(";
os << "=";
_rhs.serialize(os, db);
os << ")";
}
}
@@ -80,6 +79,7 @@ namespace sqlpp
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
{
@@ -87,10 +87,10 @@ namespace sqlpp
}
else
{
os << "=(";
os << "=";
_rhs.serialize(os, db);
os << ")";
}
os << ")";
}
private:
@@ -118,6 +118,7 @@ namespace sqlpp
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
{
@@ -125,10 +126,10 @@ namespace sqlpp
}
else
{
os << "!=(";
os << "!=";
_rhs.serialize(os, db);
os << ")";
}
os << ")";
}
private:
@@ -155,6 +156,7 @@ namespace sqlpp
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
if (trivial_value_is_null_t<Lhs>::value and _lhs._is_trivial())
{
_lhs.serialize(os, db);
@@ -162,10 +164,10 @@ namespace sqlpp
}
else
{
os << "NOT(";
os << "NOT";
_lhs.serialize(os, db);
os << ")";
}
os << ")";
}
private:
@@ -196,45 +198,83 @@ namespace sqlpp
{
os << "(";
_lhs.serialize(os, db);
os << ")";
os << " ";
os << O::_name;
os << ")";
}
private:
Lhs _lhs;
};
template<typename Lhs, typename O, typename... Rhs>
struct nary_expression_t: public O::_value_type::template operators<nary_expression_t<Lhs, O, Rhs...>>
template<typename Lhs, typename O, typename Rhs>
struct binary_expression_t: public O::_value_type::template operators<binary_expression_t<Lhs, O, Rhs>>
{
using _value_type = typename O::_value_type;
nary_expression_t(Lhs&& l, Rhs&&... r):
binary_expression_t(Lhs&& l, Rhs&& r):
_lhs(std::move(l)),
_rhs(std::move(r)...)
_rhs(std::move(r))
{}
nary_expression_t(const Lhs& l, const Rhs&... r):
binary_expression_t(const Lhs& l, const Rhs& r):
_lhs(l),
_rhs(r...)
_rhs(r)
{}
nary_expression_t(const nary_expression_t&) = default;
nary_expression_t(nary_expression_t&&) = default;
nary_expression_t& operator=(const nary_expression_t&) = default;
nary_expression_t& operator=(nary_expression_t&&) = default;
~nary_expression_t() = default;
binary_expression_t(const binary_expression_t&) = default;
binary_expression_t(binary_expression_t&&) = default;
binary_expression_t& operator=(const binary_expression_t&) = default;
binary_expression_t& operator=(binary_expression_t&&) = default;
~binary_expression_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
os << O::_name;
_rhs.serialize(os, db);
os << ")";
}
private:
Lhs _lhs;
Rhs _rhs;
};
template<typename Lhs, typename O, typename... Rhs>
struct nary_member_function_t: public O::_value_type::template operators<nary_member_function_t<Lhs, O, Rhs...>>
{
using _value_type = typename O::_value_type;
nary_member_function_t(Lhs&& l, Rhs&&... r):
_lhs(std::move(l)),
_rhs(std::move(r)...)
{}
nary_member_function_t(const Lhs& l, const Rhs&... r):
_lhs(l),
_rhs(r...)
{}
nary_member_function_t(const nary_member_function_t&) = default;
nary_member_function_t(nary_member_function_t&&) = default;
nary_member_function_t& operator=(const nary_member_function_t&) = default;
nary_member_function_t& operator=(nary_member_function_t&&) = default;
~nary_member_function_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
os << " ";
os << O::_name;
os << "(";
detail::serialize_tuple(os, db, _rhs, ',');
os << ")";
os << ")";
}
private:

View File

@@ -125,25 +125,25 @@ namespace sqlpp
struct operators: public basic_operators<Base, _constraint>
{
template<typename T>
nary_expression_t<Base, plus_, typename _constraint<T>::type> operator +(T&& t) const
binary_expression_t<Base, plus_, typename _constraint<T>::type> operator +(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, minus_, typename _constraint<T>::type> operator -(T&& t) const
binary_expression_t<Base, minus_, typename _constraint<T>::type> operator -(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, multiplies_, typename _constraint<T>::type> operator *(T&& t) const
binary_expression_t<Base, multiplies_, typename _constraint<T>::type> operator *(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_expression_t<Base, divides_, typename _constraint<T>::type> operator /(T&& t) const
binary_expression_t<Base, divides_, typename _constraint<T>::type> operator /(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}

View File

@@ -128,7 +128,7 @@ namespace sqlpp
}
template<typename T>
nary_expression_t<Base, like_, typename _constraint<T>::type> like(T&& t) const
nary_member_function_t<Base, like_, typename _constraint<T>::type> like(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}