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

interpreter for from()

This commit is contained in:
rbock 2014-01-12 15:45:50 +01:00
parent d957e8c0ae
commit 1d3ea8516f
8 changed files with 95 additions and 51 deletions

View File

@ -30,6 +30,8 @@
#include <vector>
#include <sqlpp11/detail/serializable.h>
// FIXME: Needs to leave detail namespace
// FIXME: serializable is not a very good name any more...
namespace sqlpp
{
namespace detail
@ -55,29 +57,6 @@ namespace sqlpp
_serializables.emplace_back(std::forward<Expr>(expr));
}
void serialize(std::ostream& os, Db& db, bool first) const
{
for (const auto entry : _serializables)
{
if (not first)
os << ',';
entry.serialize(os, db);
first = false;
}
}
template<typename Separator>
void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const
{
for (const auto entry : _serializables)
{
if (not first)
os << separator;
entry.serialize(os, db);
first = false;
}
}
};
template<>
@ -86,29 +65,61 @@ namespace sqlpp
template<typename T>
void emplace_back(const T&) {}
constexpr std::size_t size() const
static constexpr std::size_t size()
{
return 0;
}
constexpr bool empty() const
static constexpr bool empty()
{
return true;
}
template<typename Db>
void serialize(std::ostream&, Db&, bool) const
{
}
template<typename Db, typename Separator>
void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const
{
}
};
}
template<typename Context, typename List>
struct serializable_list_interpreter_t
{
using T = List;
template<typename Separator>
static Context& _(const T& t, const Separator& separator, Context& context)
{
bool first = true;
for (const auto entry : t._serializables)
{
if (not first)
{
context << separator;
first = false;
}
interpret(t, context);
}
return context;
}
};
template<typename Context>
struct serializable_list_interpreter_t<Context, detail::serializable_list<void>>
{
using T = detail::serializable_list<void>;
template<typename Separator>
static Context& _(const T& t, const Separator& separator, Context& context)
{
return context;
}
};
template<typename T, typename Separator, typename Context>
auto interpret_serializable_list(const T& t, const Separator& separator, Context& context)
-> decltype(serializable_list_interpreter_t<typename std::decay<Context>::type, typename std::decay<T>::type>::_(t, separator, context))
{
return serializable_list_interpreter_t<typename std::decay<Context>::type, typename std::decay<T>::type>::_(t, separator, context);
}
}
#endif

View File

@ -74,6 +74,25 @@ namespace sqlpp
std::tuple<TableOrJoin...> _tables;
detail::serializable_list<Database> _dynamic_tables;
};
template<typename Context, typename Database, typename... TableOrJoin>
struct interpreter_t<Context, from_t<Database, TableOrJoin...>>
{
using T = from_t<Database, TableOrJoin...>;
static Context& _(const T& t, Context& context)
{
if (sizeof...(TableOrJoin) == 0 and t._dynamic_tables.empty())
return context;
context << " FROM ";
interpret_tuple(t._tables, ',', context);
if (sizeof...(TableOrJoin) and not t._dynamic_tables.empty())
context << ',';
interpret_serializable_list(t._dynamic_tables, ',', context);
return context;
}
};
}
#endif

View File

@ -32,7 +32,7 @@
namespace sqlpp
{
template<typename Context, typename T>
template<typename Context, typename T, typename Enable = void>
struct interpreter_t
{
static void _(const T& t, Context& context)

View File

@ -34,7 +34,7 @@
#include <sqlpp11/no_value.h>
#include <sqlpp11/field.h>
#include <sqlpp11/result_row.h>
#include <sqlpp11/table_base.h>
#include <sqlpp11/table.h>
#include <sqlpp11/select_pseudo_table.h>
#include <sqlpp11/detail/named_serializable.h>
#include <sqlpp11/detail/serialize_tuple.h>
@ -185,7 +185,7 @@ namespace sqlpp
static Context& _(const T& t, Context& context)
{
interpret_tuple(t._expressions, ',', context);
if (not t._dynamic_expressions.empty())
if (sizeof...(NamedExpr) and not t._dynamic_expressions.empty())
context << ',';
interpret(t._dynamic_expressions, context);
return context;

View File

@ -44,7 +44,7 @@ namespace sqlpp
typename Select,
typename... NamedExpr
>
struct select_pseudo_table_t: public sqlpp::table_base_t<select_pseudo_table_t<
struct select_pseudo_table_t: public sqlpp::table_t<select_pseudo_table_t<
Select,
NamedExpr...>, select_column_spec_t<NamedExpr>...>
{

View File

@ -24,8 +24,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_TABLE_BASE_H
#define SQLPP_TABLE_BASE_H
#ifndef SQLPP_TABLE_H
#define SQLPP_TABLE_H
#include <ostream>
#include <sqlpp11/alias.h>
@ -37,8 +37,10 @@
namespace sqlpp
{
struct table_base_t {};
template<typename Table, typename... ColumnSpec>
struct table_base_t: public ColumnSpec::_name_t::template _member_t<column_t<Table, ColumnSpec>>...
struct table_t: public table_base_t, public ColumnSpec::_name_t::template _member_t<column_t<Table, ColumnSpec>>...
{
using _table_set = detail::set<Table>; // Hint need a set here to be similar to a join (which always represents more than one table)
using _all_columns = typename detail::make_set<column_t<Table, ColumnSpec>...>::type;
@ -117,12 +119,10 @@ namespace sqlpp
return {*static_cast<const Table*>(this)};
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
static_cast<const Table*>(this)->serialize_impl(os, db);
}
const Table& ref() const
{
return *static_cast<const Table*>(this);
}
};
template<typename Table>
@ -131,6 +131,19 @@ namespace sqlpp
return {};
}
template<typename Context, typename X>
struct interpreter_t<Context, X, typename std::enable_if<std::is_base_of<table_base_t, X>::value, void>::type>
{
using T = X;
static Context& _(const T& t, Context& context)
{
context << "table";
return context;
}
};
}
#endif

View File

@ -43,6 +43,7 @@ int main()
interpret(t.beta + "kaesekuchen", printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush();
return 0;
}

View File

@ -27,7 +27,7 @@
#ifndef SQLPP_TAB_SAMPLE_H
#define SQLPP_TAB_SAMPLE_H
#include <sqlpp11/table_base.h>
#include <sqlpp11/table.h>
#include <sqlpp11/column_types.h>
@ -72,7 +72,7 @@ namespace TabFoo_
};
}
struct TabFoo: sqlpp::table_base_t<
struct TabFoo: sqlpp::table_t<
TabFoo,
TabFoo_::Epsilon,
TabFoo_::Omega
@ -166,7 +166,7 @@ namespace TabSample_
};
}
struct TabSample: sqlpp::table_base_t<
struct TabSample: sqlpp::table_t<
TabSample,
TabSample_::Alpha,
TabSample_::Beta,