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

Started to use flags to indicate database traits

e.g. support for any or outer join, or how string concatenation is
implemented
This commit is contained in:
Roland Bock 2013-11-01 12:23:50 +01:00
parent a17a8ecfa6
commit e63a798a82
7 changed files with 54 additions and 11 deletions

View File

@ -72,6 +72,7 @@ namespace sqlpp
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
static_assert(Db::_supports_any, "any() not supported by current database");
os << "ANY("; os << "ANY(";
_select.serialize(os, db); _select.serialize(os, db);
os << ")"; os << ")";

View File

@ -73,11 +73,21 @@ namespace sqlpp
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{
static_assert(Db::_use_concat_operator or Db::_use_concat_function, "neither concat operator nor concat function supported by current database");
if (Db::_use_concat_operator)
{
os << "(";
detail::serialize_tuple(os, db, _args, "||");
os << ")";
}
else if (Db::_use_concat_function)
{ {
os << "CONCAT("; os << "CONCAT(";
detail::serialize_tuple(os, db, _args, ','); detail::serialize_tuple(os, db, _args, ',');
os << ")"; os << ")";
} }
}
private: private:
std::tuple<First, Args...> _args; std::tuple<First, Args...> _args;

View File

@ -38,8 +38,8 @@ namespace sqlpp
template<std::size_t begin, std::size_t index, std::size_t end> template<std::size_t begin, std::size_t index, std::size_t end>
struct tuple_serializer_impl struct tuple_serializer_impl
{ {
template<typename Db, typename Tuple> template<typename Db, typename Tuple, typename Separator>
static void serialize(std::ostream& os, Db& db, const Tuple& flags_and_columns, char separator) static void serialize(std::ostream& os, Db& db, const Tuple& flags_and_columns, const Separator& separator)
{ {
if (index > begin) if (index > begin)
os << separator; os << separator;
@ -56,14 +56,14 @@ namespace sqlpp
template<std::size_t begin, std::size_t end> template<std::size_t begin, std::size_t end>
struct tuple_serializer_impl<begin, end, end> struct tuple_serializer_impl<begin, end, end>
{ {
template<typename Db, typename Tuple> template<typename Db, typename Tuple, typename Separator>
static void serialize(std::ostream& os, Db& db, const Tuple& flags_and_columns, char separator) static void serialize(std::ostream& os, Db& db, const Tuple& flags_and_columns, const Separator& separator)
{ {
} }
}; };
template<typename Db, typename Tuple> template<typename Db, typename Tuple, typename Separator>
static void serialize_tuple(std::ostream& os, Db& db, const Tuple& flags_and_columns, char separator) static void serialize_tuple(std::ostream& os, Db& db, const Tuple& flags_and_columns, const Separator& separator)
{ {
tuple_serializer_impl<0, 0, std::tuple_size<Tuple>::value>::serialize(os, db, flags_and_columns, separator); tuple_serializer_impl<0, 0, std::tuple_size<Tuple>::value>::serialize(os, db, flags_and_columns, separator);
} }

View File

@ -101,7 +101,7 @@ namespace sqlpp
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
os << '"' << db.escape(_t) << '"'; os << '\'' << db.escape(_t) << '\'';
} }
bool _is_trivial() const { return _t.empty(); } bool _is_trivial() const { return _t.empty(); }

View File

@ -34,18 +34,42 @@ namespace sqlpp
{ {
struct inner_join_t struct inner_join_t
{ {
template<typename Db>
struct _is_supported
{
static constexpr bool value = Db::_supports_inner_join;
};
static constexpr const char* _name = " INNER "; static constexpr const char* _name = " INNER ";
}; };
struct outer_join_t struct outer_join_t
{ {
template<typename Db>
struct _is_supported
{
static constexpr bool value = Db::_supports_outer_join;
};
static constexpr const char* _name = " OUTER "; static constexpr const char* _name = " OUTER ";
}; };
struct left_outer_join_t struct left_outer_join_t
{ {
template<typename Db>
struct _is_supported
{
static constexpr bool value = Db::_supports_left_outer_join;
};
static constexpr const char* _name = " LEFT OUTER "; static constexpr const char* _name = " LEFT OUTER ";
}; };
struct right_outer_join_t struct right_outer_join_t
{ {
template<typename Db>
struct _is_supported
{
static constexpr bool value = Db::_supports_right_outer_join;
};
static constexpr const char* _name = " RIGHT OUTER "; static constexpr const char* _name = " RIGHT OUTER ";
}; };
@ -109,6 +133,7 @@ namespace sqlpp
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
// FIXME: Need to check if db supports the join type. e.g. sqlite does not support right outer or full outer join // FIXME: Need to check if db supports the join type. e.g. sqlite does not support right outer or full outer join
static_assert(JoinType::template _is_supported<Db>::value, "join type not supported by current database");
static_assert(not is_noop<On>::value, "joined tables require on()"); static_assert(not is_noop<On>::value, "joined tables require on()");
_lhs.serialize(os, db); _lhs.serialize(os, db);
os << JoinType::_name; os << JoinType::_name;

View File

@ -72,6 +72,7 @@ namespace sqlpp
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
static_assert(Db::_supports_some, "some() not supported by current database");
os << "SOME("; os << "SOME(";
_select.serialize(os, db); _select.serialize(os, db);
os << ")"; os << ")";

View File

@ -25,12 +25,18 @@
#include "TabSample.h" #include "TabSample.h"
#include <sqlpp11/select.h> #include <sqlpp11/select.h>
#include <sqlpp11/functions.h>
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <iostream> #include <iostream>
class DbMock: public sqlpp::connection class DbMock: public sqlpp::connection
{ {
public: public:
static constexpr bool _supports_any = true;
static constexpr bool _use_concat_function = true;
static constexpr bool _use_concat_operator = false;
const std::string& escape(const std::string& text) { return text; } const std::string& escape(const std::string& text) { return text; }
}; };
@ -266,7 +272,7 @@ int main()
{ {
auto s = dynamic_select(db, all_of(t)).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset(); auto s = dynamic_select(db, all_of(t)).dynamic_from().dynamic_where().dynamic_limit().dynamic_offset();
s = s.add_from(t); s = s.add_from(t);
s = s.add_where(t.alpha > 7); 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(30);
s = s.set_limit(3); s = s.set_limit(3);
std::cerr << "------------------------\n"; std::cerr << "------------------------\n";