Exemplo n.º 1
0
int CustomQuery(int, char* [])
{
  const auto foo = test::TabFoo{};
  const auto bar = test::TabBar{};
  auto db = MockDb{};

  // Unconditionally
  compare(__LINE__, custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo),
                                 sqlpp::unconditionally()),
          "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo ");

  // A full select statement made individual clauses
  compare(__LINE__,
          custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega),
                       from(foo.join(bar).on(foo.omega == bar.alpha)), where(bar.alpha > 17), group_by(foo.omega),
                       having(avg(bar.alpha) > 19), order_by(foo.omega.asc()), sqlpp::limit(10), sqlpp::offset(100)),
          "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha)  WHERE "
          "(tab_bar.alpha>17)  GROUP BY tab_foo.omega  HAVING (AVG(tab_bar.alpha)>19)  ORDER BY tab_foo.omega ASC  "
          "LIMIT 10  OFFSET 100");

  // A full select statement made individual clauses
  compare(
      __LINE__,
      custom_query(sqlpp::select(), dynamic_select_flags(db, sqlpp::distinct), dynamic_select_columns(db, foo.omega),
                   dynamic_from(db, foo.join(bar).on(foo.omega == bar.alpha)), dynamic_where(db, bar.alpha > 17),
                   dynamic_group_by(db, foo.omega), dynamic_having(db, avg(bar.alpha) > 19),
                   dynamic_order_by(db, foo.omega.asc()), sqlpp::dynamic_limit(db), sqlpp::dynamic_offset(db)),
      "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha)  WHERE "
      "(tab_bar.alpha>17)  GROUP BY tab_foo.omega  HAVING (AVG(tab_bar.alpha)>19)  ORDER BY tab_foo.omega ASC  ");

  return 0;
}
Exemplo n.º 2
0
int CustomQuery(int, char*[])
{
  const auto foo = test::TabFoo{};
  const auto bar = test::TabBar{};
  auto db = MockDb{};

  // Unconditionally
  compare(__LINE__,
          custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo),
                       sqlpp::unconditionally()),
          "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo ");

  // A full select statement made individual clauses
  compare(__LINE__,
          custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega),
                       from(foo.join(bar).on(foo.omega == bar.alpha)), where(bar.alpha > 17), group_by(foo.omega),
                       having(avg(bar.alpha) > 19), order_by(foo.omega.asc()), sqlpp::limit(10u), sqlpp::offset(100u)),
          "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha)  WHERE "
          "(tab_bar.alpha>17)  GROUP BY tab_foo.omega  HAVING (AVG(tab_bar.alpha)>19)  ORDER BY tab_foo.omega ASC  "
          "LIMIT 10  OFFSET 100");

  // A full select statement made individual clauses
  compare(
      __LINE__,
      custom_query(sqlpp::select(), dynamic_select_flags(db, sqlpp::distinct), dynamic_select_columns(db, foo.omega),
                   dynamic_from(db, foo.join(bar).on(foo.omega == bar.alpha)), dynamic_where(db, bar.alpha > 17),
                   dynamic_group_by(db, foo.omega), dynamic_having(db, avg(bar.alpha) > 19),
                   dynamic_order_by(db, foo.omega.asc(), foo.psi.order(sqlpp::sort_type::desc)),
                   sqlpp::dynamic_limit(db), sqlpp::dynamic_offset(db)),
      "SELECT  DISTINCT  tab_foo.omega  FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha)  WHERE "
      "(tab_bar.alpha>17)  GROUP BY tab_foo.omega  HAVING (AVG(tab_bar.alpha)>19)  ORDER BY tab_foo.omega "
      "ASC,tab_foo.psi DESC  ");

  // A pragma query for sqlite
  compare(__LINE__,
          custom_query(sqlpp::verbatim("PRAGMA user_version")).with_result_type_of(select(sqlpp::value(1).as(pragma))),
          " PRAGMA user_version");

  // An insert from select for postgresql
  const auto x = 17;
  compare(__LINE__,
          custom_query(insert_into(foo).columns(foo.omega),
                       select(sqlpp::value(x).as(foo.omega))
                           .from(foo)
                           .where(not exists(select(foo.omega).from(foo).where(foo.omega == x)))),
          "INSERT INTO tab_foo (omega) "
          "SELECT 17 AS omega FROM tab_foo "
          "WHERE (NOT EXISTS(SELECT tab_foo.omega FROM tab_foo WHERE (tab_foo.omega=17)))");

  return 0;
}