Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,32 +534,15 @@ namespace sqlite_orm::internal {
}
};

template<>
struct statement_serializer<window_ref_t, void> {
using statement_type = window_ref_t;

template<class Ctx>
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& statement,
const Ctx&) SQLITE_ORM_OR_CONST_CALLOP {
return statement.name;
}
};

template<class Tuple, class Ctx>
void serialize_over_arguments(std::stringstream& ss, const Tuple& arguments, const Ctx& context) {
using args_tuple = std::decay_t<Tuple>;
constexpr bool is_named_ref = std::tuple_size<args_tuple>::value == 1 &&
std::is_same<std::tuple_element_t<0, args_tuple>, window_ref_t>::value;
if constexpr (is_named_ref) {
if constexpr (std::tuple_size<Tuple>::value == 0) {
ss << " OVER ()";
} else if constexpr (std::tuple_size<Tuple>::value == 1 &&
std::is_same<typename std::tuple_element<0, Tuple>::type, window_ref_t>::value) {
ss << " OVER " << std::get<0>(arguments).name;
} else {
ss << " OVER (";
std::string separator;
iterate_tuple(arguments, [&ss, &context, &separator](auto& arg) {
ss << separator << serialize(arg, context);
separator = " ";
});
ss << ")";
ss << " OVER (" << streaming_actions_tuple(arguments, context) << ")";
}
}

Expand Down Expand Up @@ -599,13 +582,8 @@ namespace sqlite_orm::internal {
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& statement,
const Ctx& context) SQLITE_ORM_OR_CONST_CALLOP {
std::stringstream ss;
ss << "WINDOW " << statement.name << " AS (";
std::string separator;
iterate_tuple(statement.arguments, [&ss, &context, &separator](auto& arg) {
ss << separator << serialize(arg, context);
separator = " ";
});
ss << ")";
ss << "WINDOW " << statement.name << " AS (" << streaming_actions_tuple(statement.arguments, context)
<< ")";
return ss.str();
}
};
Expand Down
36 changes: 7 additions & 29 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22194,32 +22194,15 @@ namespace sqlite_orm::internal {
}
};

template<>
struct statement_serializer<window_ref_t, void> {
using statement_type = window_ref_t;

template<class Ctx>
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& statement,
const Ctx&) SQLITE_ORM_OR_CONST_CALLOP {
return statement.name;
}
};

template<class Tuple, class Ctx>
void serialize_over_arguments(std::stringstream& ss, const Tuple& arguments, const Ctx& context) {
using args_tuple = std::decay_t<Tuple>;
constexpr bool is_named_ref = std::tuple_size<args_tuple>::value == 1 &&
std::is_same<std::tuple_element_t<0, args_tuple>, window_ref_t>::value;
if constexpr (is_named_ref) {
if constexpr (std::tuple_size<Tuple>::value == 0) {
ss << " OVER ()";
} else if constexpr (std::tuple_size<Tuple>::value == 1 &&
std::is_same<typename std::tuple_element<0, Tuple>::type, window_ref_t>::value) {
ss << " OVER " << std::get<0>(arguments).name;
} else {
ss << " OVER (";
std::string separator;
iterate_tuple(arguments, [&ss, &context, &separator](auto& arg) {
ss << separator << serialize(arg, context);
separator = " ";
});
ss << ")";
ss << " OVER (" << streaming_actions_tuple(arguments, context) << ")";
}
}

Expand Down Expand Up @@ -22259,13 +22242,8 @@ namespace sqlite_orm::internal {
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& statement,
const Ctx& context) SQLITE_ORM_OR_CONST_CALLOP {
std::stringstream ss;
ss << "WINDOW " << statement.name << " AS (";
std::string separator;
iterate_tuple(statement.arguments, [&ss, &context, &separator](auto& arg) {
ss << separator << serialize(arg, context);
separator = " ";
});
ss << ")";
ss << "WINDOW " << statement.name << " AS (" << streaming_actions_tuple(statement.arguments, context)
<< ")";
return ss.str();
}
};
Expand Down
47 changes: 47 additions & 0 deletions tests/window_function_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,50 @@ TEST_CASE("window functions") {
REQUIRE(std::get<2>(rows[5]) == 2);
}
}

TEST_CASE("window functions - issue #475 count over with where order by limit") {
struct UserProfile {
int id = 0;
std::string firstName;
std::string lastName;
};

auto storage = make_storage("",
make_table("user_profile",
make_column("id", &UserProfile::id, primary_key().autoincrement()),
make_column("first_name", &UserProfile::firstName),
make_column("last_name", &UserProfile::lastName)));
storage.sync_schema();

storage.insert(UserProfile{0, "Alice", "Smith"});
storage.insert(UserProfile{0, "Bob", "Jones"});
storage.insert(UserProfile{0, "Charlie", "Brown"});
storage.insert(UserProfile{0, "Diana", "Davis"});
storage.insert(UserProfile{0, "Eve", "Wilson"});

int refId = 0;
int resultPerPage = 3;

// SQL: SELECT id, first_name, last_name, COUNT(id) OVER ()
// FROM user_profile WHERE id > ? ORDER BY id LIMIT ?
auto rows = storage.select(
columns(&UserProfile::id, &UserProfile::firstName, &UserProfile::lastName, count(&UserProfile::id).over()),
where(c(&UserProfile::id) > refId),
order_by(&UserProfile::id),
limit(resultPerPage));

REQUIRE(rows.size() == 3);
// Every row should have total_count=5 (total rows in table)
REQUIRE(std::get<0>(rows[0]) == 1);
REQUIRE(std::get<1>(rows[0]) == "Alice");
REQUIRE(std::get<2>(rows[0]) == "Smith");
REQUIRE(std::get<3>(rows[0]) == 5);

REQUIRE(std::get<0>(rows[1]) == 2);
REQUIRE(std::get<1>(rows[1]) == "Bob");
REQUIRE(std::get<3>(rows[1]) == 5);

REQUIRE(std::get<0>(rows[2]) == 3);
REQUIRE(std::get<1>(rows[2]) == "Charlie");
REQUIRE(std::get<3>(rows[2]) == 5);
}
Loading